From add8fe64d515199d5eb1f5776c3569665321a4c7 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Mon, 12 Nov 2018 09:24:34 +0100 Subject: [PATCH] [A/U/MP] Use another version of getPropertyVector. This version checks if the PropertyVector is assigned to the requested mesh item type and has the required number of components. --- .../generateMatPropsFromMatID.cpp | 36 +++++++++++-------- .../ModelPreparation/createNeumannBc.cpp | 29 ++++++++------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp index b8c312d408d..31b7c3162bd 100644 --- a/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp +++ b/Applications/Utils/FileConverter/generateMatPropsFromMatID.cpp @@ -52,31 +52,38 @@ int main (int argc, char* argv[]) cmd.parse( argc, argv ); // read mesh - std::unique_ptr<MeshLib::Mesh> mesh(MeshLib::IO::readMeshFromFile(mesh_arg.getValue())); - if (!mesh) { + std::unique_ptr<MeshLib::Mesh> mesh( + MeshLib::IO::readMeshFromFile(mesh_arg.getValue())); + + if (!mesh) + { INFO("Could not read mesh from file \"%s\".", mesh_arg.getValue().c_str()); return EXIT_FAILURE; } - if (!mesh->getProperties().existsPropertyVector<int>("MaterialIDs")) + + MeshLib::PropertyVector<int>* materialIds = nullptr; + try + { + materialIds = mesh->getProperties().getPropertyVector<int>( + "MaterialIDs", MeshLib::MeshItemType::Cell, 1); + } + catch (std::runtime_error const& e) { - ERR("Mesh contains no int-property vector named \"MaterialIds\"."); + WARN("%s", e.what()); return EXIT_FAILURE; } - auto materialIds = mesh->getProperties().getPropertyVector<int>("MaterialIDs"); std::size_t const n_properties(materialIds->size()); - if (n_properties != mesh->getNumberOfElements()) { - ERR("Size mismatch: number of elements (%u) != number of material " - "properties (%u).", mesh->getNumberOfElements(), n_properties); - return EXIT_FAILURE; - } - std::string const name = BaseLib::extractBaseNameWithoutExtension(mesh_arg.getValue()); + assert(n_properties != mesh->getNumberOfElements()); + + std::string const name = + BaseLib::extractBaseNameWithoutExtension(mesh_arg.getValue()); // create file std::string const new_matname(name + "_prop"); - std::ofstream out_prop( new_matname.c_str(), std::ios::out ); + std::ofstream out_prop(new_matname.c_str(), std::ios::out); if (out_prop.is_open()) { - for (std::size_t i=0; i<n_properties; ++i) + for (std::size_t i = 0; i < n_properties; ++i) out_prop << i << "\t" << (*materialIds)[i] << "\n"; out_prop.close(); } @@ -92,7 +99,8 @@ int main (int argc, char* argv[]) INFO("Writing mesh to file \"%s\".", new_mshname.c_str()); MeshLib::IO::writeMeshToFile(*mesh, new_mshname); - INFO("New files \"%s\" and \"%s\" written.", new_mshname.c_str(), new_matname.c_str()); + INFO("New files \"%s\" and \"%s\" written.", new_mshname.c_str(), + new_matname.c_str()); return EXIT_SUCCESS; } diff --git a/Applications/Utils/ModelPreparation/createNeumannBc.cpp b/Applications/Utils/ModelPreparation/createNeumannBc.cpp index 1a1f5078171..80c131ff60f 100644 --- a/Applications/Utils/ModelPreparation/createNeumannBc.cpp +++ b/Applications/Utils/ModelPreparation/createNeumannBc.cpp @@ -45,8 +45,8 @@ std::vector<double> getSurfaceIntegratedValuesForNodes( prop_name.c_str()); return std::vector<double>(); } - auto const* const elem_pv = - mesh.getProperties().getPropertyVector<double>(prop_name); + auto const* const elem_pv = mesh.getProperties().getPropertyVector<double>( + prop_name, MeshLib::MeshItemType::Cell, 1); std::vector<double> integrated_node_area_vec; double total_area(0); @@ -133,17 +133,21 @@ int main(int argc, char* argv[]) std::unique_ptr<MeshLib::Mesh> surface_mesh( MeshLib::IO::readMeshFromFile(in_mesh.getValue())); - std::string const prop_name("bulk_node_ids"); auto const* const node_id_pv = - surface_mesh->getProperties().getPropertyVector<std::size_t>(prop_name); + [&]() -> MeshLib::PropertyVector<std::size_t>* { + try + { + return surface_mesh->getProperties().getPropertyVector<std::size_t>( + "bulk_node_ids", MeshLib::MeshItemType::Node, 1); + } + catch (std::runtime_error const& e) + { + WARN("%s", e.what()); + return nullptr; + } + }(); if (!node_id_pv) - { - ERR( - "Need subsurface node ids, but the property \"%s\" is not " - "available.", - prop_name.c_str()); return EXIT_FAILURE; - } std::vector<double> integrated_values = getSurfaceIntegratedValuesForNodes( *surface_mesh, property_in_arg.getValue()); @@ -162,13 +166,14 @@ int main(int argc, char* argv[]) surface_mesh->getProperties().createNewPropertyVector<double>( property_out_arg.getValue(), MeshLib::MeshItemType::Node, 1); pv->resize(surface_mesh->getNodes().size()); - for (std::size_t k(0); k<surface_mesh->getNodes().size(); ++k) { + for (std::size_t k(0); k < surface_mesh->getNodes().size(); ++k) + { (*pv)[k] = direct_values[k].second; } MeshLib::IO::writeMeshToFile(*surface_mesh, result_file.getValue()); - std::ofstream result_out(result_file.getValue()+".txt"); + std::ofstream result_out(result_file.getValue() + ".txt"); result_out.precision(std::numeric_limits<double>::digits10); for (auto const& p : direct_values) result_out << p.first << " " << p.second << "\n"; -- GitLab