16int main(
int argc,
char *argv[]) {
21 moab::Core mb_instance;
22 moab::Interface &moab = mb_instance;
24 char mesh_file_name[255] =
"mesh.h5m";
25 char mesh_out_file[255] =
"out.h5m";
26 char tag_name[255] =
"new_tag";
27 char extract_tag_name[255] =
"old_tag_name";
28 int extract_component = 0;
30 PetscBool flg_extract_tag_name = PETSC_FALSE;
33 PetscBool flg_file = PETSC_FALSE;
34 PetscBool flg_analytical = PETSC_FALSE;
36 PetscOptionsBegin(PETSC_COMM_WORLD,
"",
"none",
"none");
37 CHKERR PetscOptionsString(
"-file_name",
"mesh file name",
"",
"mesh.h5m",
38 mesh_file_name, 255, &flg_file);
39 CHKERR PetscOptionsString(
"-create_tag_name",
"name of tag to create",
"",
40 "", tag_name, 255, PETSC_NULLPTR);
41 CHKERR PetscOptionsString(
"-extract_tag_name",
42 "name of tag to extract from",
"",
"",
43 extract_tag_name, 255, &flg_extract_tag_name);
44 CHKERR PetscOptionsInt(
"-component_to_extract",
45 "component of vector tag to extract (0-based index)",
46 "", extract_component, &extract_component,
48 CHKERR PetscOptionsString(
"-output_file",
"output mesh file name",
"",
49 "out.h5m", mesh_out_file, 255, PETSC_NULLPTR);
50 CHKERR PetscOptionsInt(
"-atom_test",
"flag to create tag for test",
"",
52 CHKERR PetscOptionsBool(
"-use_analytical_tag",
53 "bool to use coded analytical function",
"",
54 flg_analytical, &flg_analytical, PETSC_NULLPTR);
59 CHKERR moab.load_file(mesh_file_name, 0, option);
61 ParallelComm *pcomm = ParallelComm::get_pcomm(&moab,
MYPCOMM_INDEX);
63 pcomm =
new ParallelComm(&moab, PETSC_COMM_WORLD);
69 std::vector<double> new_tag_values;
71 EntityType entity_type = MBTET;
74 entity_type = MBVERTEX;
76 if (flg_extract_tag_name) {
78 CHKERR moab.get_entities_by_type(0, entity_type, ents);
81 auto rval = moab.tag_get_handle(extract_tag_name, extract_tag);
82 if (
rval != MB_SUCCESS) {
84 "Failed to get handle for tag '%s'", extract_tag_name);
88 CHKERR moab.tag_get_length(extract_tag, extract_tag_len);
90 <<
"Extracting component " << extract_component <<
" from tag '"
91 << extract_tag_name <<
"' with length " << extract_tag_len;
93 std::vector<double> extract_values(extract_tag_len * ents.size(), 0.0);
94 CHKERR moab.tag_get_data(extract_tag, ents, &extract_values[0]);
97 <<
"Extracted " << extract_values.size() <<
" values from tag '"
98 << extract_tag_name <<
"'";
101 for (
size_t i = 0;
i < ents.size(); ++
i) {
102 int idx = extract_component;
103 if (idx < extract_tag_len) {
104 new_tag_values.push_back(extract_values[
i * extract_tag_len + idx]);
108 "Component index to extract is out of bounds for the tag length");
111 MOFEM_LOG(
"WORLD", Sev::inform) <<
"Extracted component values for "
112 << new_tag_values.size() <<
" entities";
114 MOFEM_LOG(
"WORLD", Sev::warning) <<
"No tag specified for extraction.";
115 std::vector<double> test_data;
118 CHKERR moab.get_entities_by_type(0, entity_type, ents);
120 std::vector<double> test_data;
122 for (
size_t i = 0;
i < ents.size(); ++
i) {
124 for (
int idx = 0; idx < 5; ++idx) {
125 test_data.push_back(idx);
130 "atom test '%i' does not exist for setting a tag",
atom_test);
133 for (
const auto &val : test_data) {
134 new_tag_values.push_back(val);
136 }
else if (flg_analytical) {
139 const std::vector<double> &x,
140 const std::vector<double> &y,
141 const std::vector<double> &z, int) {
142 std::vector<double> youngs_modulus;
143 youngs_modulus.reserve(x.size());
145 for (
double xi : x) {
147 double local = E0 * std::exp(xi);
148 youngs_modulus.push_back(local);
151 return youngs_modulus;
154 std::vector<double> coords(3 * ents.size(), 0.0);
155 CHKERR moab.get_coords(ents, &coords[0]);
157 for (
size_t i = 0;
i < ents.size(); ++
i) {
158 double x = coords[3 *
i + 0];
159 double y = coords[3 *
i + 1];
160 double z = coords[3 *
i + 2];
162 double val = analytical_elastic(0.0, 0.0, {x}, {y}, {z}, 0)[0];
163 test_data.push_back(val);
166 for (
const auto &val : test_data) {
167 new_tag_values.push_back(val);
170 MOFEM_LOG(
"WORLD", Sev::warning) <<
"No tag has been created.";
176 std::minmax_element(new_tag_values.begin(), new_tag_values.end());
178 <<
"New tag '" << tag_name <<
"' value range: [" << *minmax.first
179 <<
", " << *minmax.second <<
"]";
182 std::vector<double> def_val;
185 def_val = {0.0, 0.0, 0.0, 0.0, 0.0};
191 CHKERR moab.tag_get_handle(tag_name, def_val.size(), MB_TYPE_DOUBLE, new_tag,
192 MB_TAG_CREAT | MB_TAG_DENSE, &def_val);
194 CHKERR moab.tag_set_data(new_tag, ents, &new_tag_values[0]);
197 CHKERR moab.write_file(mesh_out_file,
"MOAB",
"PARALLEL=WRITE_PART");
198 CHKERR moab.write_file(
"out.vtk",
"vtk");
202 std::vector<double> check_values(new_tag_values.size(), 0.0);
203 CHKERR moab.tag_get_data(new_tag, ents, &check_values[0]);
205 <<
"check_values size: " << check_values.size()
206 <<
", new_tag_values size: " << new_tag_values.size();
208 for (
size_t i = 0;
i < new_tag_values.size(); ++
i) {
209 if (std::abs(check_values[
i] - new_tag_values[
i]) > 1e-6) {
211 "Atom test failed: Value mismatch at index %zu: expected %f, "
213 i, new_tag_values[
i], check_values[
i]);
219 std::vector<double> expected_values(new_tag_values.size(), 0.0);
220 CHKERR moab.tag_get_data(new_tag, ents, &expected_values[0]);
222 <<
"check_values size: " << expected_values.size()
223 <<
", new_tag_values size: " << new_tag_values.size();
225 for (
size_t i = 0;
i < expected_values.size(); ++
i) {
226 if (std::abs(expected_values[
i] - extract_component) > 1e-6) {
228 "Tag extraction test failed: Value mismatch at index %zu: "
229 "expected %i, got %f",
230 i, extract_component, expected_values[
i]);
235 "atom test %i does not exist",
atom_test);