v0.16.0
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
MoFEM::JsonConfigManager Struct Reference

#include "src/interfaces/JsonConfigManager.hpp"

Inheritance diagram for MoFEM::JsonConfigManager:
[legend]
Collaboration diagram for MoFEM::JsonConfigManager:
[legend]

Public Member Functions

MoFEMErrorCode query_interface (boost::typeindex::type_index type_index, UnknownInterface **iface) const
 
 JsonConfigManager (const MoFEM::Core &core)
 
virtual ~JsonConfigManager ()=default
 
MoFEMErrorCode getSubInterfaceOptions ()
 
MoFEMErrorCode loadFromFile (const std::string &file_name)
 
MoFEMErrorCode applyMeshsets ()
 
std::string getPythonScriptByKey (const std::string &key) const
 
std::string getMoabMeshByKey (const std::string &key) const
 
std::map< std::string, doublegetParamsFromBlockset (const std::string &type_name, int meshset_id) const
 
std::map< std::string, std::string > getStringParamsFromBlockset (const std::string &type_name, int meshset_id) const
 
bool hasMeshsetsSection () const
 
- Public Member Functions inherited from MoFEM::UnknownInterface
template<class IFACE >
MoFEMErrorCode registerInterface (bool error_if_registration_failed=true)
 Register interface.
 
template<class IFACE >
MoFEMErrorCode getInterface (IFACE *&iface) const
 Get interface reference to pointer of interface.
 
template<class IFACE >
MoFEMErrorCode getInterface (IFACE **const iface) const
 Get interface pointer to pointer of interface.
 
template<class IFACE , typename boost::enable_if< boost::is_pointer< IFACE >, int >::type = 0>
IFACE getInterface () const
 Get interface pointer to pointer of interface.
 
template<class IFACE , typename boost::enable_if< boost::is_reference< IFACE >, int >::type = 0>
IFACE getInterface () const
 Get reference to interface.
 
template<class IFACE >
IFACE * getInterface () const
 Function returning pointer to interface.
 
virtual ~UnknownInterface ()=default
 

Static Public Member Functions

static const std::string & supportedVersion ()
 
static MoFEMErrorCode addCanonicalAttributeNames (const std::vector< std::string > &names)
 
static MoFEMErrorCode setMeshFileFromJson ()
 Set -file_name from JSON before Core is available.
 
- Static Public Member Functions inherited from MoFEM::UnknownInterface
static MoFEMErrorCode getLibVersion (Version &version)
 Get library version.
 
static MoFEMErrorCode getFileVersion (moab::Interface &moab, Version &version)
 Get database major version.
 
static MoFEMErrorCode setFileVersion (moab::Interface &moab, Version version=Version(MoFEM_VERSION_MAJOR, MoFEM_VERSION_MINOR, MoFEM_VERSION_BUILD))
 Get database major version.
 
static MoFEMErrorCode getInterfaceVersion (Version &version)
 Get database major version.
 

Static Public Attributes

static std::vector< std::string > canonical_attributes_ordering
 

Private Types

using MeshsetKey = std::pair< std::string, int >
 
using MeshsetParams = std::map< std::string, double >
 
using MeshsetStringParams = std::map< std::string, std::string >
 

Private Member Functions

MoFEMErrorCode loadFromCommandLine ()
 
MoFEMErrorCode applyPetscOptions ()
 

Private Attributes

MoFEM::CorecOre
 
boost::json::value configRoot
 
std::string configFileName
 
bool configLoaded = false
 
bool hasMeshsets = false
 
bool meshsetsApplied = false
 
std::map< std::string, std::string > moabMeshesByKey
 
std::map< std::string, std::string > pythonScriptsByKey
 
std::map< MeshsetKey, MeshsetParamsprimaryMeshsetParamsByKey
 
std::map< MeshsetKey, MeshsetStringParamsprimaryMeshsetStringParamsByKey
 

Detailed Description

Examples
/home/lk58p/mofem_install/vanilla_dev_release/mofem-cephas/mofem/users_modules/eshelbian_plasticity/src/impl/EshelbianPlasticity.cpp.

Definition at line 12 of file JsonConfigManager.hpp.

Member Typedef Documentation

◆ MeshsetKey

using MoFEM::JsonConfigManager::MeshsetKey = std::pair<std::string, int>
private

Definition at line 53 of file JsonConfigManager.hpp.

◆ MeshsetParams

using MoFEM::JsonConfigManager::MeshsetParams = std::map<std::string, double>
private

Definition at line 54 of file JsonConfigManager.hpp.

◆ MeshsetStringParams

using MoFEM::JsonConfigManager::MeshsetStringParams = std::map<std::string, std::string>
private

Definition at line 55 of file JsonConfigManager.hpp.

Constructor & Destructor Documentation

◆ JsonConfigManager()

MoFEM::JsonConfigManager::JsonConfigManager ( const MoFEM::Core core)

Definition at line 448 of file JsonConfigManager.cpp.

449 : cOre(const_cast<Core &>(core)) {}
CoreTmp< 0 > Core
Definition Core.hpp:1154

◆ ~JsonConfigManager()

virtual MoFEM::JsonConfigManager::~JsonConfigManager ( )
virtualdefault

Member Function Documentation

◆ addCanonicalAttributeNames()

MoFEMErrorCode MoFEM::JsonConfigManager::addCanonicalAttributeNames ( const std::vector< std::string > &  names)
static

Register additional numeric JSON attribute names.

Definition at line 419 of file JsonConfigManager.cpp.

420 {
422 for (const auto &name : names) {
423 if (name.empty()) {
424 continue;
425 }
426
427 const auto lower_name = toLowerCopy(name);
428 const auto it = std::find_if(
430 canonical_attributes_ordering.end(), [&](const std::string &existing) {
431 return toLowerCopy(existing) == lower_name;
432 });
433 if (it == canonical_attributes_ordering.end()) {
434 canonical_attributes_ordering.push_back(lower_name);
435 }
436 }
437
439}
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
static std::vector< std::string > canonical_attributes_ordering

◆ applyMeshsets()

MoFEMErrorCode MoFEM::JsonConfigManager::applyMeshsets ( )

Definition at line 607 of file JsonConfigManager.cpp.

607 {
610 auto &meshsets_manager = cOre.getInterface<MeshsetsManager &>();
613 if (const auto *primary_meshsets =
615 primary_meshsets && !primary_meshsets->empty()) {
616 const char *section_key =
618 const auto primary_mesh_context = meshContext(0, section_key);
619 std::vector<ParsedMeshsetEntry> parsed_primary_meshsets;
620 std::set<int> used_blockset_ids;
621 CHKERR parseMeshsets(*primary_meshsets, primary_mesh_context, configFileName,
622 &meshsets_manager, parsed_primary_meshsets);
623 CHKERR cacheResolvedBlocksetParams(parsed_primary_meshsets, configFileName,
626 used_blockset_ids);
627
628 // JSON stays authoritative for block attributes. Imported blockset names
629 // are kept only for meshsets that JSON actually refers to.
630 CHKERR discardResolvedBlocksetConfiguration(
631 meshsets_manager, configFileName, used_blockset_ids);
632 CHKERR applyParsedMeshsets(meshsets_manager, parsed_primary_meshsets,
634 CHKERR cacheAppliedBlocksetParams(parsed_primary_meshsets, configFileName,
637 }
638 const char *section_key = nullptr;
639 if (const auto *meshes_array =
640 getMeshesArray(configRoot.as_object(), configFileName, &section_key);
641 meshes_array) {
642 for (size_t mesh_index = 1; mesh_index != meshes_array->size();
643 ++mesh_index) {
644 if (const auto *meshsets =
646 mesh_index);
647 meshsets && !meshsets->empty()) {
648 CHKERR validateMeshsets(*meshsets, meshContext(mesh_index, section_key),
650 }
651 }
652 }
653 meshsetsApplied = true;
654 }
656}
std::string meshContext(const size_t mesh_index, const char *section_key=MESHES_KEY)
const JsonArray * getMeshsetsArray(const JsonObject &root_object, const std::string &file_name, const size_t mesh_index)
const char * getMeshSectionKey(const JsonObject &root_object, const std::string &file_name)
const JsonArray * getMeshesArray(const JsonObject &root_object, const std::string &file_name, const char **section_key=nullptr)
MoFEMErrorCode validateMeshsets(const JsonArray &meshsets, const std::string &mesh_context, const std::string &file_name)
MoFEMErrorCode cacheAppliedBlocksetParams(const std::vector< ParsedMeshsetEntry > &parsed_meshsets, const std::string &file_name, BlocksetParamsByKey &params_by_key, BlocksetStringParamsByKey &string_params_by_key)
MoFEMErrorCode applyParsedMeshsets(MeshsetsManager &meshsets_manager, std::vector< ParsedMeshsetEntry > &parsed_meshsets, const std::string &file_name)
MoFEMErrorCode cacheResolvedBlocksetParams(const std::vector< ParsedMeshsetEntry > &parsed_meshsets, const std::string &file_name, BlocksetParamsByKey &params_by_key, BlocksetStringParamsByKey &string_params_by_key, std::set< int > &used_blockset_ids)
MoFEMErrorCode parseMeshsets(const JsonArray &meshsets, const std::string &mesh_context, const std::string &file_name, MeshsetsManager *meshsets_manager_ptr, std::vector< ParsedMeshsetEntry > &parsed_meshsets)
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
std::map< MeshsetKey, MeshsetParams > primaryMeshsetParamsByKey
std::map< MeshsetKey, MeshsetStringParams > primaryMeshsetStringParamsByKey
boost::json::value configRoot
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface reference to pointer of interface.

◆ applyPetscOptions()

MoFEMErrorCode MoFEM::JsonConfigManager::applyPetscOptions ( )
private

Definition at line 573 of file JsonConfigManager.cpp.

573 {
575 if (configLoaded) {
576 const auto &root_object = configRoot.as_object();
577 std::map<std::string, std::string> seen_option_contexts;
578 const char *section_key = nullptr;
579 if (const auto *meshes_array =
580 getMeshesArray(root_object, configFileName, &section_key);
581 meshes_array && !meshes_array->empty()) {
582 const auto primary_mesh_context = meshContext(0, section_key);
583 const auto &primary_mesh = requireObject((*meshes_array)[0], configFileName,
584 primary_mesh_context);
585 CHKERR setPetscOptionIfUnset(
586 "-file_name",
587 requireString(primary_mesh.at("file_name"), configFileName,
588 makeContext(primary_mesh_context, "file_name")));
589 }
590
591 if (const auto petsc_it = root_object.find("petsc");
592 petsc_it != root_object.end()) {
593 CHKERR flattenPetscObject(petsc_it->value().as_object(), "",
594 configFileName, "petsc",
595 seen_option_contexts);
596 }
597 if (const auto mofem_it = root_object.find("mofem");
598 mofem_it != root_object.end()) {
599 CHKERR flattenPetscObject(mofem_it->value().as_object(), "",
600 configFileName, "mofem",
601 seen_option_contexts);
602 }
603 }
605}

◆ getMoabMeshByKey()

std::string MoFEM::JsonConfigManager::getMoabMeshByKey ( const std::string &  key) const

Definition at line 667 of file JsonConfigManager.cpp.

667 {
668 if (const auto it = moabMeshesByKey.find(key); it != moabMeshesByKey.end()) {
669 return it->second;
670 }
671 return "";
672}
std::string key
std::map< std::string, std::string > moabMeshesByKey

◆ getParamsFromBlockset()

std::map< std::string, double > MoFEM::JsonConfigManager::getParamsFromBlockset ( const std::string &  type_name,
int  meshset_id 
) const

Definition at line 675 of file JsonConfigManager.cpp.

676 {
677 const auto it =
678 primaryMeshsetParamsByKey.find({toLowerCopy(type_name), meshset_id});
679 if (it != primaryMeshsetParamsByKey.end()) {
680 return it->second;
681 }
682 return {};
683}

◆ getPythonScriptByKey()

std::string MoFEM::JsonConfigManager::getPythonScriptByKey ( const std::string &  key) const

Definition at line 659 of file JsonConfigManager.cpp.

659 {
660 if (const auto it = pythonScriptsByKey.find(key);
661 it != pythonScriptsByKey.end()) {
662 return it->second;
663 }
664 return "";
665}
std::map< std::string, std::string > pythonScriptsByKey

◆ getStringParamsFromBlockset()

std::map< std::string, std::string > MoFEM::JsonConfigManager::getStringParamsFromBlockset ( const std::string &  type_name,
int  meshset_id 
) const

Definition at line 686 of file JsonConfigManager.cpp.

687 {
688 const auto it = primaryMeshsetStringParamsByKey.find(
689 {toLowerCopy(type_name), meshset_id});
690 if (it != primaryMeshsetStringParamsByKey.end()) {
691 return it->second;
692 }
693 return {};
694}

◆ getSubInterfaceOptions()

MoFEMErrorCode MoFEM::JsonConfigManager::getSubInterfaceOptions ( )

Definition at line 486 of file JsonConfigManager.cpp.

486 {
487 return loadFromCommandLine();
488}
MoFEMErrorCode loadFromCommandLine()

◆ hasMeshsetsSection()

bool MoFEM::JsonConfigManager::hasMeshsetsSection ( ) const
inline

Definition at line 50 of file JsonConfigManager.hpp.

50{ return hasMeshsets; }

◆ loadFromCommandLine()

MoFEMErrorCode MoFEM::JsonConfigManager::loadFromCommandLine ( )
private

Definition at line 696 of file JsonConfigManager.cpp.

696 {
698 if (!configLoaded) {
699 char json_file_name[PETSC_MAX_PATH_LEN] = "";
700 PetscBool has_json = PETSC_FALSE;
701 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-json_config",
702 json_file_name, sizeof(json_file_name),
703 &has_json);
704 if (has_json) {
705 // IMPORTANT !!!
706 // Clear any PETSc options that might have been set by param_file.petsc
707 CHKERR clearPetscParamFileOptionsForJson(json_file_name);
708 CHKERR loadFromFile(json_file_name);
709
710 if (hasMeshsets) {
711 char meshsets_cfg_file[PETSC_MAX_PATH_LEN] = "";
712 PetscBool has_meshsets_cfg = PETSC_FALSE;
713 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR,
714 "-meshsets_config", meshsets_cfg_file,
715 sizeof(meshsets_cfg_file),
716 &has_meshsets_cfg);
717 if (has_meshsets_cfg) {
718 CHKERR failConfig(
719 configFileName, "meshsets",
720 "JSON meshsets cannot be used together with -meshsets_config");
721 }
722 }
723
725 // Consume and apply any log_* options injected from JSON after PETSc
726 // initialization so they do not remain as "unused database options".
728 }
729 }
731}
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
MoFEMErrorCode loadFromFile(const std::string &file_name)
static MoFEMErrorCode getOptions()
Get logger option.

◆ loadFromFile()

MoFEMErrorCode MoFEM::JsonConfigManager::loadFromFile ( const std::string &  file_name)

Definition at line 490 of file JsonConfigManager.cpp.

490 {
492
493 boost::json::value selected_config;
494 CHKERR loadSelectedConfigFromFile(file_name, selected_config);
495 const auto &config_object = requireObject(selected_config, file_name, "root");
496
497 bool has_version = false;
498 for (const auto &item : config_object) {
499 const auto key = toStdString(item.key());
500 if (key == "$schema")
501 continue;
502 if (key == "version") {
503 has_version = true;
504 const auto version =
505 requireString(item.value(), file_name, makeContext("root", key));
506 if (version != supportedVersion()) {
507 CHKERR failConfig(file_name, makeContext("root", key),
508 "unsupported config version '" + version +
509 "', supported version is '" + supportedVersion() +
510 "'");
511 }
512 continue;
513 }
514 if (key == "petsc") {
515 requireObject(item.value(), file_name, makeContext("root", key));
516 continue;
517 }
518 if (key == "mofem") {
519 requireObject(item.value(), file_name, makeContext("root", key));
520 continue;
521 }
522 if (key == "meshes" || key == "input_files") {
523 requireArray(item.value(), file_name, makeContext("root", key));
524 continue;
525 }
526 if (key == "file_name") {
527 CHKERR failConfig(
528 file_name, makeContext("root", key),
529 "legacy root-level file_name is not supported; use "
530 "root.input_files[0].file_name or root.meshes[0].file_name");
531 }
532 if (key == "meshsets") {
533 CHKERR failConfig(
534 file_name, makeContext("root", key),
535 "legacy root-level meshsets is not supported; use "
536 "root.input_files[i].meshsets or root.meshes[i].meshsets");
537 }
538 CHKERR failConfig(file_name, makeContext("root", key),
539 "unknown top-level key");
540 }
541 if (!has_version) {
542 CHKERR failConfig(file_name, "root.version",
543 "missing required version field");
544 }
545 hasMeshsets = false;
546
547 std::vector<ParsedMeshEntry> new_meshes;
548 bool config_has_meshsets = false;
549 CHKERR collectMeshes(config_object, file_name, new_meshes,
550 &config_has_meshsets);
551
552 configRoot = std::move(selected_config);
553 configFileName = file_name;
554 configLoaded = true;
555 moabMeshesByKey.clear();
556 pythonScriptsByKey.clear();
557 for (const auto &entry : new_meshes) {
558 if (entry.type == MOAB_MESH_TYPE && !entry.key.empty()) {
559 moabMeshesByKey[entry.key] = entry.fileName;
560 }
561 if (isPythonScriptMeshType(entry.type)) {
562 pythonScriptsByKey[entry.key] = entry.fileName;
563 }
564 }
565 hasMeshsets = config_has_meshsets;
566 meshsetsApplied = false;
569
571}
MoFEMErrorCode collectMeshes(const JsonObject &root_object, const std::string &file_name, std::vector< ParsedMeshEntry > &meshes, bool *has_meshsets)
bool isPythonScriptMeshType(const std::string &type)
constexpr auto MOAB_MESH_TYPE
static const std::string & supportedVersion()

◆ query_interface()

MoFEMErrorCode MoFEM::JsonConfigManager::query_interface ( boost::typeindex::type_index  type_index,
UnknownInterface **  iface 
) const
virtual

Implements MoFEM::UnknownInterface.

Definition at line 442 of file JsonConfigManager.cpp.

443 {
444 *iface = const_cast<JsonConfigManager *>(this);
445 return 0;
446}
JsonConfigManager(const MoFEM::Core &core)

◆ setMeshFileFromJson()

MoFEMErrorCode MoFEM::JsonConfigManager::setMeshFileFromJson ( )
static

Set -file_name from JSON before Core is available.

Use only in programs that must read the mesh before MoFEM::Core and the JsonConfigManager interface are initialised.

Definition at line 451 of file JsonConfigManager.cpp.

451 {
453
454 char json_file_name[PETSC_MAX_PATH_LEN] = "";
455 PetscBool has_json = PETSC_FALSE;
456 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-json_config",
457 json_file_name, sizeof(json_file_name),
458 &has_json);
459 if (!has_json || !std::strlen(json_file_name)) {
461 }
462
463 bool has_file_name_arg = false;
464 bool has_my_file_arg = false;
465 CHKERR hasCommandLineOption("-file_name", has_file_name_arg);
466 CHKERR hasCommandLineOption("-my_file", has_my_file_arg);
467 if (has_file_name_arg || has_my_file_arg) {
469 }
470
471 boost::json::value selected_config;
472 CHKERR loadSelectedConfigFromFile(json_file_name, selected_config);
473 const auto &config_object =
474 requireObject(selected_config, json_file_name, "root");
475
476 std::vector<ParsedMeshEntry> meshes;
477 CHKERR collectMeshes(config_object, json_file_name, meshes);
478 if (!meshes.empty()) {
479 CHKERR PetscOptionsSetValue(PETSC_NULLPTR, "-file_name",
480 meshes.front().fileName.c_str());
481 }
482
484}

◆ supportedVersion()

const std::string & MoFEM::JsonConfigManager::supportedVersion ( )
static

Definition at line 13 of file JsonConfigManager.cpp.

13 {
14 static const std::string version =
15 std::to_string(MoFEM_VERSION_MAJOR) + "." +
16 std::to_string(MoFEM_VERSION_MINOR);
17 return version;
18}

Member Data Documentation

◆ canonical_attributes_ordering

std::vector<std::string> MoFEM::JsonConfigManager::canonical_attributes_ordering
inlinestatic
Initial value:
=
std::vector< std::string > getDefaultJsonCanonicalAttributeNames()

Definition at line 14 of file JsonConfigManager.hpp.

◆ configFileName

std::string MoFEM::JsonConfigManager::configFileName
private

Definition at line 59 of file JsonConfigManager.hpp.

◆ configLoaded

bool MoFEM::JsonConfigManager::configLoaded = false
private

Definition at line 60 of file JsonConfigManager.hpp.

◆ configRoot

boost::json::value MoFEM::JsonConfigManager::configRoot
private

Definition at line 58 of file JsonConfigManager.hpp.

◆ cOre

MoFEM::Core& MoFEM::JsonConfigManager::cOre
private

Definition at line 57 of file JsonConfigManager.hpp.

◆ hasMeshsets

bool MoFEM::JsonConfigManager::hasMeshsets = false
private

Definition at line 61 of file JsonConfigManager.hpp.

◆ meshsetsApplied

bool MoFEM::JsonConfigManager::meshsetsApplied = false
private

Definition at line 62 of file JsonConfigManager.hpp.

◆ moabMeshesByKey

std::map<std::string, std::string> MoFEM::JsonConfigManager::moabMeshesByKey
private

Definition at line 63 of file JsonConfigManager.hpp.

◆ primaryMeshsetParamsByKey

std::map<MeshsetKey, MeshsetParams> MoFEM::JsonConfigManager::primaryMeshsetParamsByKey
private

Definition at line 65 of file JsonConfigManager.hpp.

◆ primaryMeshsetStringParamsByKey

std::map<MeshsetKey, MeshsetStringParams> MoFEM::JsonConfigManager::primaryMeshsetStringParamsByKey
private

Definition at line 66 of file JsonConfigManager.hpp.

◆ pythonScriptsByKey

std::map<std::string, std::string> MoFEM::JsonConfigManager::pythonScriptsByKey
private

Definition at line 64 of file JsonConfigManager.hpp.


The documentation for this struct was generated from the following files: