v0.16.3
Loading...
Searching...
No Matches
AgingTagTransfer.cpp
Go to the documentation of this file.
1/** \file AgingTagTransfer.cpp
2 * \brief Transfer ageing environmental snapshots between volume meshes.
3 */
5#include <regex>
6
7namespace MoFEM {
8
9MoFEMErrorCode AgingTagTransfer::transfer(const std::string &source_file,
10 const Range &target_elements) {
12 if (source_file.empty())
14 "Environmental tag transfer requires a source mesh file");
15
16 auto &mb = mField.get_moab();
19 target.parallelCommId = mField.get_basic_entity_data_ptr()->pcommID;
20 target.elements = target_elements;
21 Range before;
22 CHKERR mb.get_entities_by_handle(0, before);
23 auto coupler = mField.getInterface<CouplerInterface>();
25 const auto apply = [&]() {
27 std::vector<CouplerInterface::TagMap> maps;
28 const auto load = [&]() {
30 // Inspect the file in isolation: existing target tags must not invent
31 // source snapshots through their dense defaults. Release before import.
32 {
33 moab::Core metadata;
34 CHKERR metadata.load_file(source_file.c_str());
35 std::vector<Tag> tags;
36 CHKERR metadata.tag_get_tags(tags);
37 const std::regex environmental(
38 "MED_(IRRA_ALLIRRA|TEMP_ALLNEUT|CORR_ALLNEUT)_[0-9]+");
39 for (auto tag : tags) {
40 std::string name;
41 CHKERR metadata.tag_get_name(tag, name);
42 if (!std::regex_match(name, environmental))
43 continue;
44 int length;
45 DataType type;
46 CHKERR metadata.tag_get_length(tag, length);
47 CHKERR metadata.tag_get_data_type(tag, type);
48 if (length != 1 || type != MB_TYPE_DOUBLE)
49 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
50 "Environmental tag %s must contain one double per node",
51 name.c_str());
52 maps.push_back(
54 }
55 }
56 if (maps.empty())
57 SETERRQ(PETSC_COMM_SELF, MOFEM_INVALID_DATA,
58 "No Amentum environmental snapshot tags in %s",
59 source_file.c_str());
60 std::sort(maps.begin(), maps.end(), [](const auto &a, const auto &b) {
61 return a.sourceTag < b.sourceTag;
62 });
63 EntityHandle root;
64 CHKERR mb.create_meshset(MESHSET_SET, root);
65 // H5M mesh tags can overwrite the target Core's field/DM build state.
66 const EntityHandle root_set = 0;
67 Tag build_tag;
68 int build_state;
69 CHKERR mb.tag_get_handle("_MoFEMBuild", 1, MB_TYPE_INTEGER, build_tag);
70 CHKERR mb.tag_get_data(build_tag, &root_set, 1, &build_state);
71 const auto load_error = mb.load_file(source_file.c_str(), &root);
72 const auto restore_error =
73 mb.tag_set_data(build_tag, &root_set, 1, &build_state);
74 CHKERR load_error;
75 CHKERR restore_error;
76 CHKERR mb.get_entities_by_dimension(root, 3, source.elements, true);
78 };
79 const int local_error = load();
80 int global_error;
81 CHKERR MPI_Allreduce(&local_error, &global_error, 1, MPI_INT, MPI_MAX,
83 if (global_error)
84 SETERRQ(mField.get_comm(), global_error,
85 "Could not load environmental tag source %s",
86 source_file.c_str());
87
92 options.absoluteTolerance = 5.e-10;
93 CHKERR coupler->createTransfer(source, options, transfer);
96 CHKERR coupler->locateTargets(transfer, target, locations, report);
97 CHKERR coupler->transferTags(transfer, locations, maps, report);
98 MOFEM_LOG("WORLD", Sev::inform)
99 << "Transferred " << maps.size() << " environmental snapshot tags from "
100 << source_file << " to " << report.globalLocated << " mesh vertices";
102 };
103 const auto error = apply();
104 const auto close_error = coupler->destroyTransfer(transfer);
105 Range after;
106 CHKERR mb.get_entities_by_handle(0, after);
107 const auto imported = subtract(after, before);
108 const auto cleanup = [&]() {
110 CHKERR mb.delete_entities(imported.subset_by_type(MBENTITYSET));
111 // MOAB checks vertex adjacency before deleting a batch. Cells must be
112 // gone before their corner nodes, including after a failed transfer.
113 for (int dim = 3; dim >= 0; --dim)
114 CHKERR mb.delete_entities(imported.subset_by_dimension(dim));
116 };
117 const auto cleanup_error = cleanup();
118 CHKERR error;
119 CHKERR close_error;
120 CHKERR cleanup_error;
122}
123
124} // namespace MoFEM
Reusable transfer of ageing environmental snapshot tags.
Report & report
std::vector< int > source
double length
moab::Interface & mb
Tag tag
const Field * target
std::string type
std::string name
constexpr double a
MoFEMErrorCode transfer(const std::string &source_file, const Range &target_elements)
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_INVALID_DATA
Definition definitions.h:36
#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.
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
virtual boost::shared_ptr< BasicEntityData > & get_basic_entity_data_ptr()=0
Get pointer to basic entity data.
boost::shared_ptr< const Locations > LocationsPtr
boost::shared_ptr< Transfer > TransferPtr
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.