v0.16.3
Loading...
Searching...
No Matches
tag_transfer.cpp
Go to the documentation of this file.
1/** \file tag_transfer.cpp
2 * \brief Environmental transfer between different meshes before UMAT setup.
3 */
4#include <MoFEM.hpp>
6using namespace MoFEM;
7
8namespace {
9const std::array<std::string, 3> prefixes{
10 "MED_IRRA_ALLIRRA_", "MED_TEMP_ALLNEUT_", "MED_CORR_ALLNEUT_"};
11
12double value(int quantity, int step, const double *x) {
13 return 100 * quantity + 10 * step + x[0] + 2 * x[1] + 3 * x[2];
14}
15
16MoFEMErrorCode require(bool condition, const char *message) {
18 if (!condition)
19 SETERRQ(PETSC_COMM_SELF, MOFEM_ATOM_TEST_INVALID, "%s", message);
21}
22
23MoFEMErrorCode writeSource(const char *file) {
25 moab::Core mb;
26 const double coordinates[8][3] = {{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0},
27 {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 1, 1}};
28 EntityHandle nodes[8], cell;
29 for (int i = 0; i != 8; ++i) {
30 CHKERR mb.create_vertex(coordinates[i], nodes[i]);
31 const int id = i + 1;
32 CHKERR mb.tag_set_data(mb.globalId_tag(), nodes + i, 1, &id);
33 }
34 CHKERR mb.create_element(MBHEX, nodes, 8, cell);
35 const int id = 1;
36 CHKERR mb.tag_set_data(mb.globalId_tag(), &cell, 1, &id);
37 for (int q = 0; q != 3; ++q)
38 for (int step = 0; step != 3; ++step) {
39 Tag tag;
40 CHKERR mb.tag_get_handle((prefixes[q] + std::to_string(step)).c_str(), 1,
41 MB_TYPE_DOUBLE, tag,
42 MB_TAG_SPARSE | MB_TAG_CREAT);
43 for (int i = 0; i != 8; ++i) {
44 const double v = value(q, step, coordinates[i]);
45 CHKERR mb.tag_set_data(tag, nodes + i, 1, &v);
46 }
47 }
48 CHKERR mb.write_file(file);
50}
51
54 field.getInterface<JsonConfigManager>();
55 char source_file[PETSC_MAX_PATH_LEN] = "";
56 CHKERR PetscOptionsGetString(nullptr, nullptr, "-tag_transfer_file",
57 source_file, sizeof(source_file), nullptr);
58 int local_error = field.get_comm_rank() == 0 ? writeSource(source_file) : 0;
59 int error;
60 CHKERR MPI_Allreduce(&local_error, &error, 1, MPI_INT, MPI_MAX,
61 field.get_comm());
62 CHKERR error;
63 auto &mb = field.get_moab();
64 const double shift = .01 * field.get_comm_rank();
65 const double coordinates[4][3] = {{.1 + shift, .1, .1},
66 {.8 + shift, .1, .1},
67 {.1 + shift, .8, .1},
68 {.1 + shift, .1, .8}};
69 EntityHandle nodes[4], cell;
70 for (int i = 0; i != 4; ++i) {
71 CHKERR mb.create_vertex(coordinates[i], nodes[i]);
72 const int id = 101 + 4 * field.get_comm_rank() + i;
73 CHKERR mb.tag_set_data(mb.globalId_tag(), nodes + i, 1, &id);
74 }
75 CHKERR mb.create_element(MBTET, nodes, 4, cell);
76 const int id = field.get_comm_rank() + 101;
77 CHKERR mb.tag_set_data(mb.globalId_tag(), &cell, 1, &id);
78 Tag existing;
79 const double default_value = -17;
80 CHKERR mb.tag_get_handle("MED_IRRA_ALLIRRA_99", 1, MB_TYPE_DOUBLE, existing,
81 MB_TAG_DENSE | MB_TAG_CREAT, &default_value);
82 Range before;
83 CHKERR mb.get_entities_by_handle(0, before);
84 Range target_elements;
85 target_elements.insert(cell);
86 AgingTagTransfer transfer(field);
87 CHKERR transfer.transfer(source_file, target_elements);
88 Range after;
89 CHKERR mb.get_entities_by_handle(0, after);
90 CHKERR require(before == after, "Tag transfer left source entities behind");
91 const auto check = [&]() {
93 for (int q = 0; q != 3; ++q)
94 for (int step = 0; step != 3; ++step) {
95 Tag tag;
96 CHKERR mb.tag_get_handle((prefixes[q] + std::to_string(step)).c_str(),
97 tag);
98 double values[4];
99 CHKERR mb.tag_get_data(tag, nodes, 4, values);
100 for (int i = 0; i != 4; ++i)
101 CHKERR require(std::abs(values[i] - value(q, step, coordinates[i])) <
102 1e-10,
103 "Environmental snapshot interpolation is incorrect");
104 }
105 double untouched[4];
106 CHKERR mb.tag_get_data(existing, nodes, 4, untouched);
107 for (double v : untouched)
108 CHKERR require(v == default_value,
109 "Target-only snapshot was overwritten");
111 };
112 CHKERR check();
113 for (int i = 0; i != 4; ++i) {
114 auto x = std::array<double, 3>{coordinates[i][0] + 2, coordinates[i][1],
115 coordinates[i][2]};
116 CHKERR mb.set_coords(nodes + i, 1, x.data());
117 }
118 CHKERR PetscPushErrorHandler(PetscReturnErrorHandler, nullptr);
119 error = transfer.transfer(source_file, target_elements);
120 CHKERR PetscPopErrorHandler();
121 CHKERR require(error != 0, "Transfer accepted vertices outside the source");
122 CHKERR check();
123 CHKERR mb.get_entities_by_handle(0, after);
124 CHKERR require(before == after,
125 "Failed transfer left source entities behind");
126 MOFEM_LOG("WORLD", Sev::inform) << "UMAT tag transfer checks passed";
128}
129} // namespace
130
131int main(int argc, char **argv) {
132 MoFEM::Core::Initialize(&argc, &argv, nullptr, nullptr);
133 try {
134 moab::Core mb;
135 MoFEM::Core core(mb);
136 CHKERR run(core);
137 }
140 return 0;
141}
Reusable transfer of ageing environmental snapshot tags.
moab::Interface & mb
Tag tag
ProjectedField & field
std::vector< std::array< double, 3 > > coordinates
SmartPetscObj< Vec > values
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 ...
@ 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 MOFEM_LOG(channel, severity)
Log.
FTensor::Index< 'i', SPACE_DIM > i
const double v
phase velocity of light in medium (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
double q
MoFEMErrorCode run(MoFEM::Interface &m_field)
Definition sdf_python.cpp:7
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.