v0.16.3
Loading...
Searching...
No Matches
reading_med_file.cpp
Go to the documentation of this file.
1/** \file reading_med_file.cpp
2
3 \brief Testing interface for reading and writing med files
4
5*/
6
7#include <MoFEM.hpp>
8#include <filesystem>
9extern "C" {
10#include <med.h>
11}
12using namespace MoFEM;
13
14static char help[] = "...\n\n";
15
16namespace {
17
18// Both dimensions deliberately use the same gapped, non-monotone MED numbers.
19MoFEMErrorCode writeElementIdFixture(const char *file, bool numbered) {
21 const med_idt fid = MEDfileOpen(file, MED_ACC_CREAT);
22 if (fid < 0)
23 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
24 "Could not create MED element-ID fixture");
25 const char *name = "element_ids";
26 std::string axes(3 * MED_SNAME_SIZE, ' '), units(3 * MED_SNAME_SIZE, ' ');
27 for (int d = 0; d != 3; ++d)
28 axes[d * MED_SNAME_SIZE] = "xyz"[d];
29 CHKERR MEDmeshCr(fid, name, 3, 3, MED_UNSTRUCTURED_MESH, "Element ID test",
30 "", MED_SORT_DTIT, MED_CARTESIAN, axes.c_str(),
31 units.c_str());
32 CHKERR MEDfamilyCr(fid, name, "FAMILY_0", 0, 0, "");
33 const med_float coordinates[] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
34 2, 0, 0, 3, 0, 0, 2, 1, 0, 2, 0, 1};
35 const med_int families[8] = {};
36 CHKERR MEDmeshNodeWr(fid, name, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
37 MED_FULL_INTERLACE, 8, coordinates, MED_FALSE, nullptr,
38 MED_FALSE, nullptr, MED_TRUE, families);
39 const med_int tets[] = {1, 2, 3, 4, 5, 6, 7, 8};
40 const med_int triangles[] = {1, 2, 3, 5, 6, 7};
41 const med_int numbers[] = {41, 7};
42 for (const auto type : {MED_TRIA3, MED_TETRA4}) {
43 CHKERR MEDmeshElementWr(fid, name, MED_NO_DT, MED_NO_IT, MED_UNDEF_DT,
44 MED_CELL, type, MED_NODAL, MED_FULL_INTERLACE, 2,
45 type == MED_TRIA3 ? triangles : tets, MED_FALSE,
46 nullptr, numbered ? MED_TRUE : MED_FALSE,
47 numbered ? numbers : nullptr, MED_TRUE, families);
48 }
49 CHKERR MEDfileClose(fid);
51}
52
53MoFEMErrorCode checkElementIds() {
55 const char *numbered = "reading_med_numbered_elements.med";
56 const char *unnumbered = "reading_med_unnumbered_elements.med";
57 CHKERR writeElementIdFixture(numbered, true);
58 CHKERR writeElementIdFixture(unnumbered, false);
59 moab::Core mb;
60 MoFEM::Core core(mb);
61 MoFEM::Interface &m_field = core;
62 auto testing = m_field.getInterface<TestingInterface>();
63 auto read = [&](const char *file, const std::vector<int> &expected) {
65 Range before;
66 CHKERR mb.get_entities_by_handle(0, before);
67 MedInterface reader(core);
68 CHKERR reader.readMed(file, 0);
69 for (int dim : {2, 3}) {
70 Range entities;
71 CHKERR mb.get_entities_by_dimension(0, dim, entities);
72 const auto added = subtract(entities, before);
73 std::vector<int> ids(added.size());
74 CHKERR mb.tag_get_data(mb.globalId_tag(), added, ids.data());
75 CHKERR testing->checkValue<TestType::SCALAR>(
76 "MED imported element numbers", ids == expected ? 1 : 0, 1, {0, 0});
77 ids.resize(entities.size());
78 CHKERR mb.tag_get_data(mb.globalId_tag(), entities, ids.data());
79 const std::set<int> unique(ids.begin(), ids.end());
80 CHKERR testing->checkValue<TestType::SCALAR>(
81 "MED element IDs unique per dimension", unique.size(), ids.size(),
82 {0, 0});
83 }
85 };
86 CHKERR read(unnumbered, {1, 2});
87 // Existing IDs 1,2 do not collide with 41,7, which must be preserved exactly.
88 CHKERR read(numbered, {41, 7});
89 CHKERR read(numbered, {82, 48});
90 CHKERR read(unnumbered, {83, 84});
91 std::filesystem::remove(numbered);
92 std::filesystem::remove(unnumbered);
94}
95
96} // namespace
97
98int main(int argc, char *argv[]) {
99
100 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
101
102 try {
103
104 CHKERR checkElementIds();
105
106 moab::Core mb_instance;
107 moab::Interface &moab = mb_instance;
108
109 // Create MoFEM (Joseph) database
110 MoFEM::Core core(moab);
111 MoFEM::Interface &m_field = core;
112
113 MedInterface *med_interface_ptr;
114 CHKERR m_field.getInterface(med_interface_ptr);
115
116 CHKERR med_interface_ptr->readMed();
117 CHKERR med_interface_ptr->medGetFieldNames();
118
119 // read field tags
120 for (std::map<std::string, MedInterface::FieldData>::iterator fit =
121 med_interface_ptr->fieldNames.begin();
122 fit != med_interface_ptr->fieldNames.end(); fit++) {
123 CHKERR med_interface_ptr->readFields(med_interface_ptr->medFileName,
124 fit->first, false, 1);
125 }
126
127 PetscBool check = PETSC_TRUE;
128 CHKERR PetscOptionsGetBool(PETSC_NULLPTR, "", "-check", &check, PETSC_NULLPTR);
129
130 int ii = 0;
131 const int check_list[] = {2163, 624, 65, 104};
133 EntityHandle meshset = cit->getMeshset();
134 int nb_ents;
135 CHKERR moab.get_number_entities_by_handle(meshset, nb_ents, true);
136 CHKERR PetscPrintf(PETSC_COMM_WORLD, "Nb of ents in %s %d\n",
137 cit->getName().c_str(), nb_ents);
138 CHKERRG(ierr);
139 if (check && nb_ents != check_list[ii]) {
140 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
141 "Wrong numbers of entities in meshset %d != %d", nb_ents,
142 check_list[ii]);
143 }
144 ii++;
145 }
146
147 // Vertex GLOBAL_IDs are the MED node numbers: positive and distinct.
148 auto testing = m_field.getInterface<TestingInterface>();
149 const auto vertex_ids = [](moab::Interface &mb) {
150 Range verts;
151 CHK_MOAB_THROW(mb.get_entities_by_type(0, MBVERTEX, verts), "vertices");
152 std::map<std::array<double, 3>, int> ids;
153 for (auto v : verts) {
154 std::array<double, 3> x;
155 int id;
156 CHK_MOAB_THROW(mb.get_coords(&v, 1, x.data()), "coordinates");
157 CHK_MOAB_THROW(mb.tag_get_data(mb.globalId_tag(), &v, 1, &id), "id");
158 ids.emplace(x, id);
159 }
160 return std::make_pair(verts.size(), ids);
161 };
162 const auto [nb_verts, read_ids] = vertex_ids(moab);
163 std::set<int> distinct;
164 int not_positive = 0;
165 for (const auto &[x, id] : read_ids) {
166 not_positive += id <= 0;
167 distinct.insert(id);
168 }
169 CHKERR testing->checkValue<TestType::SCALAR>(
170 "MED vertex GLOBAL_ID not positive", not_positive, 0, {0, 0});
171 CHKERR testing->checkValue<TestType::SCALAR>(
172 "MED vertex GLOBAL_ID distinct", distinct.size(), nb_verts, {0, 0});
173
174 // Written and read back, every vertex keeps its GLOBAL_ID. The IDs are
175 // first renumbered, in reverse and with gaps, so they differ from the
176 // positions of the nodes in the file.
177 {
178 Range verts;
179 CHKERR moab.get_entities_by_type(0, MBVERTEX, verts);
180 std::vector<int> renumbered(verts.size());
181 CHKERR moab.tag_get_data(moab.globalId_tag(), verts, renumbered.data());
182 for (auto &id : renumbered)
183 id = 2 * (static_cast<int>(nb_verts) + 1 - id) + 7;
184 CHKERR moab.tag_set_data(moab.globalId_tag(), verts, renumbered.data());
185 const auto ids = vertex_ids(moab).second;
186
187 auto written = boost::make_shared<Range>();
188 for (auto meshset : med_interface_ptr->meshMeshsets) {
189 Range ents;
190 CHKERR moab.get_entities_by_handle(meshset, ents, true);
191 written->merge(ents);
192 }
193 const std::string file = "reading_med_file_ids.med";
194 std::filesystem::remove(file);
195 boost::shared_ptr<std::vector<const CubitMeshSets *>> meshsets_ptr;
196 CHKERR med_interface_ptr->getMeshsets(meshsets_ptr);
197 CHKERR med_interface_ptr->writeMed(file, meshsets_ptr, written, 0);
198
199 moab::Core mb_read;
200 MoFEM::Core core_read(mb_read);
201 MoFEM::Interface &m_field_read = core_read;
202 CHKERR m_field_read.getInterface<MedInterface>()->readMed(file, 0);
203 const auto [nb_read, ids_read] = vertex_ids(mb_read);
204 int kept = 0;
205 for (const auto &[x, id] : ids_read) {
206 const auto it = ids.find(x);
207 kept += it != ids.end() && it->second == id;
208 }
209 CHKERR testing->checkValue<TestType::SCALAR>(
210 "MED vertex GLOBAL_ID kept after writing", kept, nb_read, {0, 0});
211 CHKERR testing->checkValue<TestType::SCALAR>(
212 "MED vertices written and read", nb_read, nb_verts, {0, 0});
213 }
214
215 MeshsetsManager *meshset_manager_ptr;
216 CHKERR m_field.getInterface(meshset_manager_ptr);
217 for (_IT_CUBITMESHSETS_BY_SET_TYPE_FOR_LOOP_((*meshset_manager_ptr),
218 BLOCKSET, mit)) {
219 EntityHandle meshset = mit->getMeshset();
220 std::string name = mit->getName();
221 PetscPrintf(m_field.get_comm(), "Write mesh %s\n", name.c_str());
222 CHKERR moab.write_file(("out_" + mit->getName() + ".vtk").c_str(), NULL,
223 NULL, &meshset, 1);
224 }
225 }
227
229
230 return 0;
231}
const Triangles & triangles
Definition CGALTools.cpp:60
moab::Interface & mb
std::vector< std::array< double, 3 > > coordinates
std::string type
It it
Definition Schur.cpp:2568
std::string name
int main()
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define CHKERRG(n)
Check error code of MoFEM/MOAB/PETSc function.
#define CHK_MOAB_THROW(err, msg)
Check error code of MoAB function and throw MoFEM exception.
@ BLOCKSET
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define _IT_CUBITMESHSETS_BY_SET_TYPE_FOR_LOOP_(MESHSET_MANAGER, CUBITBCTYPE, IT)
Iterator that loops over a specific Cubit MeshSet having a particular BC meshset in a moFEM field.
PetscBool testing
const double v
phase velocity of light in medium (cm/ns)
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
PetscErrorCode PetscOptionsGetBool(PetscOptions *, const char pre[], const char name[], PetscBool *bval, PetscBool *set)
static char help[]
virtual MPI_Comm & get_comm() const =0
Core (interface) class.
Definition Core.hpp:83
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
Deprecated interface functions.
Interface for load MED files.
MoFEMErrorCode readMed(const string &file, int verb=1)
read MED file
MoFEMErrorCode writeMed(boost::shared_ptr< Range > range_ptr=nullptr, int verb=1)
write MED file
std::vector< EntityHandle > meshMeshsets
meshset for each mesh
std::map< std::string, FieldData > fieldNames
std::string medFileName
MED file name.
MoFEMErrorCode getMeshsets(boost::shared_ptr< std::vector< const CubitMeshSets * > > &meshsets_ptr)
MoFEMErrorCode readFields(const std::string &file_name, const std::string &field_name, const bool load_series=false, const int only_step=-1, int verb=1)
MoFEMErrorCode medGetFieldNames(const string &file, int verb=1)
Get field names in MED file.
Interface for managing meshsets containing materials and boundary conditions.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.