15#include <unordered_set>
18namespace fs = std::filesystem;
21 "Write the nodal history tags MED_IRRA_ALLIRRA_<k>, MED_TEMP_ALLNEUT_<k>\n"
22 "and MED_CORR_ALLNEUT_<k> of the Amentum ageing UMAT from files\n"
23 "<fvs_dir>/<prefix><label>.fvs with lines \"GLOBAL_ID, value\".\n\n";
26static bool isStepTag(
const std::string &
name,
const std::string &prefix) {
27 const auto start = prefix.size() + 1;
28 return name.size() > start &&
name.compare(0, prefix.size(), prefix) == 0 &&
29 name[prefix.size()] ==
'_' &&
30 std::all_of(
name.begin() + start,
name.end(),
31 [](
unsigned char c) { return std::isdigit(c); });
53 std::vector<std::vector<double>>
values;
57 {{
"dose",
"-irra_prefix",
"D_",
"MED_IRRA_ALLIRRA",
true,
false},
58 {
"temperature",
"-temp_prefix",
"T_",
"MED_TEMP_ALLNEUT",
false,
false},
59 {
"weight loss",
"-corr_prefix",
"WL_",
"MED_CORR_ALLNEUT",
true,
true}}};
102 "Run on one rank, on the mesh before partitioning");
105 char mesh_file[PETSC_MAX_PATH_LEN] =
"mesh.h5m";
106 char output_file[PETSC_MAX_PATH_LEN] =
"";
107 char fvs_dir[PETSC_MAX_PATH_LEN] =
".";
108 char history_file[PETSC_MAX_PATH_LEN] =
"";
109 std::array<std::array<char, PETSC_MAX_PATH_LEN>, 3> prefixes{};
111 std::strncpy(prefixes[
q].data(),
quantities[
q].filePrefix.c_str(),
112 PETSC_MAX_PATH_LEN - 1);
114 PetscOptionsBegin(
mField.
get_comm(),
"",
"Ageing UMAT field tags",
"none");
115 CHKERR PetscOptionsString(
"-file_name",
"input mesh",
"",
"mesh.h5m",
116 mesh_file, PETSC_MAX_PATH_LEN, PETSC_NULLPTR);
117 CHKERR PetscOptionsString(
"-output_file",
"output mesh",
"",
"", output_file,
118 PETSC_MAX_PATH_LEN, PETSC_NULLPTR);
119 CHKERR PetscOptionsString(
"-fvs_dir",
"directory of the nodal files",
"",
".",
120 fvs_dir, PETSC_MAX_PATH_LEN, PETSC_NULLPTR);
124 prefixes[
q].data(), PETSC_MAX_PATH_LEN,
126 CHKERR PetscOptionsString(
"-history_file",
"ageing history file to write",
"",
127 "", history_file, PETSC_MAX_PATH_LEN,
129 CHKERR PetscOptionsReal(
"-label_time",
"time of label 1, full-power years",
142 "Give the output mesh with -output_file");
146 "-output_file %s is the input mesh; give another name",
153 if (!fs::is_directory(
fvsDir))
157 const std::string ext =
".fvs";
159 const auto &prefix =
q.filePrefix;
160 for (
const auto &entry : fs::directory_iterator(
fvsDir)) {
161 const auto name = entry.path().filename().string();
162 if (
name.size() <= prefix.size() + ext.size() ||
163 name.compare(0, prefix.size(), prefix) != 0 ||
164 name.compare(
name.size() - ext.size(), ext.size(), ext) != 0)
167 name.substr(prefix.size(),
name.size() - prefix.size() - ext.size());
169 [](
unsigned char c) { return std::isdigit(c); }))
171 const auto [
it, inserted] =
172 q.files.emplace(std::stoi(
label), entry.path());
175 "%s and %s have the same label",
it->second.c_str(),
176 entry.path().c_str());
180 "No %s files %s<label>.fvs in %s (%s)",
q.name.c_str(),
181 prefix.c_str(),
fvsDir.c_str(),
q.option.c_str());
186 for (
const auto &
f :
q.files)
187 list += (list.empty() ?
"" :
" ") + std::to_string(
f.first);
194 "File labels differ: %s has %s, %s has %s",
195 first.filePrefix.c_str(),
labels(first).c_str(),
196 q.filePrefix.c_str(),
labels(
q).c_str());
199 <<
"Labels " <<
labels(first) <<
" are steps 0 to "
200 << first.files.size() - 1;
208 CHKERR moab.get_entities_by_dimension(0, 3, vols);
211 "%s has no 3D elements",
meshFile.c_str());
213 CHKERR moab.get_entities_by_type(0, MBVERTEX, verts);
220 "A node has GLOBAL_ID %d; number the vertices first, for "
221 "example by reading the MED file with read_med",
225 "GLOBAL_ID %d is on two nodes",
nodeIds[
i]);
228 const auto others = subtract(verts,
nodes);
229 std::vector<int> other_ids(others.size());
230 CHKERR moab.tag_get_data(moab.globalId_tag(), others, other_ids.data());
231 otherIds.insert(other_ids.begin(), other_ids.end());
234 <<
nodes.size() <<
" corner nodes of " << vols.size()
243 for (
const auto &
f :
q.files) {
245 q.values.emplace_back();
247 skipped += file_skipped;
250 <<
"Read " <<
q.files.size() <<
" " <<
q.name <<
" files";
253 <<
"Skipped " << skipped <<
" " <<
q.name
254 <<
" values of vertices that are not corner nodes of 3D elements";
263 std::vector<double> &
values,
264 int &skipped)
const {
266 std::ifstream in(file);
272 std::vector<bool> seen(
nodes.size(),
false);
273 std::unordered_set<int> seen_other;
277 for (
int line_nb = 1; std::getline(in, line); ++line_nb) {
278 const auto start = line.find_first_not_of(
" \t\r");
279 if (start == std::string::npos || line[start] ==
'#')
281 std::replace(line.begin(), line.end(),
',',
' ');
282 std::istringstream is(line);
285 if (!(is >>
id >> value) || !(is >> std::ws).eof())
287 "%s:%d: expected \"GLOBAL_ID, value\"", file.c_str(), line_nb);
288 if (!std::isfinite(value))
290 "%s:%d: value is not finite", file.c_str(), line_nb);
294 twice = seen[
it->second];
295 seen[
it->second] =
true;
298 twice = !seen_other.insert(
id).second;
302 "%s:%d: GLOBAL_ID %d is not a vertex of the mesh", file.c_str(),
307 "%s:%d: GLOBAL_ID %d given twice", file.c_str(), line_nb,
id);
310 std::vector<int> missing;
311 for (
int i = 0;
i != seen.size(); ++
i)
314 if (!missing.empty()) {
316 for (
int i = 0;
i != std::min<int>(missing.size(), 5); ++
i)
317 first +=
" " + std::to_string(missing[
i]);
319 "%s: no value for %zu nodes, GLOBAL_ID%s%s", file.c_str(),
320 missing.size(), first.c_str(), missing.size() > 5 ?
" ..." :
"");
330 double min = std::numeric_limits<double>::max();
331 double max = std::numeric_limits<double>::lowest();
333 for (
const auto &
f :
q.files) {
334 const auto [lo, hi] =
335 std::minmax_element(
q.values[step].begin(),
q.values[step].end());
337 <<
q.tagPrefix <<
"_" << step <<
" label " <<
f.first <<
" from "
338 << *lo <<
" to " << *hi;
339 min = std::min(min, *lo);
340 max = std::max(max, *hi);
344 <<
q.name <<
" from " << min <<
" to " << max;
348 for (
int k = 1;
k <
q.values.size(); ++
k)
349 for (
int i = 0;
i !=
nodes.size(); ++
i)
350 decreasing +=
q.values[
k][
i] <
q.values[
k - 1][
i];
353 <<
q.name <<
" decreases from one step to the next at "
354 << decreasing <<
" node values";
356 if (
q.percent && max > 0 && max <= 1)
358 <<
q.name <<
" is at most 1; the UMAT expects percent "
359 <<
"(10.0 is 10 %), not a fraction";
370 std::vector<Tag> tags;
371 CHKERR moab.tag_get_tags(tags);
373 for (
auto th : tags) {
385 <<
"Deleted " << deleted <<
" history tags of the input mesh";
388 for (
int k = 0;
k !=
q.values.size(); ++
k) {
389 const auto name =
q.tagPrefix +
"_" + std::to_string(
k);
391 CHKERR moab.tag_get_handle(
name.c_str(), 1, MB_TYPE_DOUBLE,
th,
392 MB_TAG_CREAT | MB_TAG_DENSE | MB_TAG_EXCL);
396 <<
"Set " <<
q.tagPrefix <<
"_0 to " <<
q.tagPrefix <<
"_"
397 <<
q.values.size() - 1 <<
" on " <<
nodes.size() <<
" nodes";
407 out << std::setprecision(12);
410 out <<
f.first *
labelTime <<
" " << step++ <<
"\n";
437 const std::vector<int>
labels{0, 5, 12};
438 const std::array<std::function<
double(
int,
int)>, 3> expected{
439 [](
int i,
int l) {
return i + 10. *
l; },
440 [](
int i,
int l) {
return 300. +
i +
l; },
441 [](
int i,
int l) {
return 0.5 *
i +
l; }};
446 CHKERR mb.get_entities_by_type(0, MBVERTEX, verts);
447 CHKERR mb.get_entities_by_dimension(0, 3, vols);
448 std::vector<int> ids(verts.size());
449 CHKERR mb.tag_get_data(
mb.globalId_tag(), verts, ids.data());
451 int nb_tags = 0, on_elements = 0;
453 std::vector<Tag> tags;
455 for (
auto th : tags) {
463 for (
auto e : vols) {
465 on_elements +=
mb.tag_get_data(
th, &e, 1, &
v) == MB_SUCCESS;
467 const int step = std::stoi(
name.substr(prefix.size() + 1));
468 if (step >=
labels.size())
470 std::vector<double>
values(verts.size());
472 for (
int i = 0;
i != verts.size(); ++
i)
480 double history_error = 0, time;
482 while (in >> time >> step) {
483 if (lines <
labels.size())
485 std::max(history_error, std::abs(time - 0.5 *
labels[lines]) +
486 std::abs(step - lines));
492 CHKERR testing->checkValue<TestType::SCALAR>(
"history tags on elements",
493 on_elements, 0, {0, 0});
494 CHKERR testing->checkValue<TestType::SCALAR>(
"nodal values", error, 0,
496 CHKERR testing->checkValue<TestType::SCALAR>(
"history lines", lines,
498 CHKERR testing->checkValue<TestType::SCALAR>(
"history times and steps",
499 history_error, 0, {1e-12, 0});
503int main(
int argc,
char *argv[]) {
507 moab::Core mb_instance;
508 moab::Interface &moab = mb_instance;
std::map< EntityHandle, int > labels
SmartPetscObj< Vec > values
#define CATCH_ERRORS
Catch errors.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_OPERATION_UNSUCCESSFUL
@ MOFEM_DATA_INCONSISTENCY
#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.
#define MOFEM_LOG_TAG(channel, tag)
Tag channel.
#define MOFEM_LOG_CHANNEL(channel)
Set and reset channel.
FTensor::Index< 'i', SPACE_DIM > i
const double c
speed of light (cm/ns)
const double v
phase velocity of light in medium (cm/ns)
FTensor::Index< 'l', 3 > l
FTensor::Index< 'k', 3 > k
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
virtual int get_comm_size() const =0
virtual moab::Interface & get_moab()=0
virtual MPI_Comm & get_comm() const =0
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Deprecated interface functions.
virtual MoFEMErrorCode addTestCommandLine(const std::string &prefix, int number, const std::string &name, TestHook hook)=0
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.