v0.16.3
Loading...
Searching...
No Matches
Functions | Variables
reading_med_file.cpp File Reference

Testing interface for reading and writing med files. More...

#include <MoFEM.hpp>
#include <filesystem>
#include <med.h>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Variables

static char help [] = "...\n\n"
 

Detailed Description

Testing interface for reading and writing med files.

Definition in file reading_med_file.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 98 of file reading_med_file.cpp.

98 {
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}
moab::Interface & mb
It it
Definition Schur.cpp:2568
std::string name
#define CATCH_ERRORS
Catch errors.
#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_ATOM_TEST_INVALID
Definition definitions.h:40
#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 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::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.

Variable Documentation

◆ help

char help[] = "...\n\n"
static

Definition at line 14 of file reading_med_file.cpp.