v0.16.0
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | 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
 
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 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.
 

Private Types

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

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
 

Detailed Description

Definition at line 10 of file JsonConfigManager.hpp.

Member Typedef Documentation

◆ MeshsetKey

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

Definition at line 32 of file JsonConfigManager.hpp.

◆ MeshsetParams

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

Definition at line 33 of file JsonConfigManager.hpp.

Constructor & Destructor Documentation

◆ JsonConfigManager()

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

Definition at line 386 of file JsonConfigManager.cpp.

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

◆ ~JsonConfigManager()

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

Member Function Documentation

◆ applyMeshsets()

MoFEMErrorCode MoFEM::JsonConfigManager::applyMeshsets ( )

Definition at line 524 of file JsonConfigManager.cpp.

524 {
527 auto &meshsets_manager = cOre.getInterface<MeshsetsManager &>();
529 if (const auto *primary_meshsets =
531 primary_meshsets && !primary_meshsets->empty()) {
532 const char *section_key =
534 const auto primary_mesh_context = meshContext(0, section_key);
535 std::vector<ParsedMeshsetEntry> parsed_primary_meshsets;
536 std::set<int> used_blockset_ids;
537 CHKERR parseMeshsets(*primary_meshsets, primary_mesh_context, configFileName,
538 &meshsets_manager, parsed_primary_meshsets);
539 CHKERR cacheResolvedBlocksetParams(parsed_primary_meshsets, configFileName,
541 used_blockset_ids);
542
543 // JSON stays authoritative for block attributes. Imported blockset names
544 // are kept only for meshsets that JSON actually refers to.
545 CHKERR discardResolvedBlocksetConfiguration(
546 meshsets_manager, configFileName, used_blockset_ids);
547 CHKERR applyParsedMeshsets(meshsets_manager, parsed_primary_meshsets,
549 CHKERR cacheAppliedBlocksetParams(parsed_primary_meshsets, configFileName,
551 }
552 const char *section_key = nullptr;
553 if (const auto *meshes_array =
554 getMeshesArray(configRoot.as_object(), configFileName, &section_key);
555 meshes_array) {
556 for (size_t mesh_index = 1; mesh_index != meshes_array->size();
557 ++mesh_index) {
558 if (const auto *meshsets =
560 mesh_index);
561 meshsets && !meshsets->empty()) {
562 CHKERR validateMeshsets(*meshsets, meshContext(mesh_index, section_key),
564 }
565 }
566 }
567 meshsetsApplied = true;
568 }
570}
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 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, 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)
MoFEMErrorCode cacheAppliedBlocksetParams(const std::vector< ParsedMeshsetEntry > &parsed_meshsets, const std::string &file_name, BlocksetParamsByKey &params_by_key)
#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
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 490 of file JsonConfigManager.cpp.

490 {
492 if (configLoaded) {
493 const auto &root_object = configRoot.as_object();
494 std::map<std::string, std::string> seen_option_contexts;
495 const char *section_key = nullptr;
496 if (const auto *meshes_array =
497 getMeshesArray(root_object, configFileName, &section_key);
498 meshes_array && !meshes_array->empty()) {
499 const auto primary_mesh_context = meshContext(0, section_key);
500 const auto &primary_mesh = requireObject((*meshes_array)[0], configFileName,
501 primary_mesh_context);
502 CHKERR setPetscOptionIfUnset(
503 "-file_name",
504 requireString(primary_mesh.at("file_name"), configFileName,
505 makeContext(primary_mesh_context, "file_name")));
506 }
507
508 if (const auto petsc_it = root_object.find("petsc");
509 petsc_it != root_object.end()) {
510 CHKERR flattenPetscObject(petsc_it->value().as_object(), "",
511 configFileName, "petsc",
512 seen_option_contexts);
513 }
514 if (const auto mofem_it = root_object.find("mofem");
515 mofem_it != root_object.end()) {
516 CHKERR flattenPetscObject(mofem_it->value().as_object(), "",
517 configFileName, "mofem",
518 seen_option_contexts);
519 }
520 }
522}

◆ getMoabMeshByKey()

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

Definition at line 581 of file JsonConfigManager.cpp.

581 {
582 if (const auto it = moabMeshesByKey.find(key); it != moabMeshesByKey.end()) {
583 return it->second;
584 }
585 return "";
586}
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 589 of file JsonConfigManager.cpp.

590 {
591 const auto it =
592 primaryMeshsetParamsByKey.find({toLowerCopy(type_name), meshset_id});
593 if (it != primaryMeshsetParamsByKey.end()) {
594 return it->second;
595 }
596 return {};
597}

◆ getPythonScriptByKey()

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

Definition at line 573 of file JsonConfigManager.cpp.

573 {
574 if (const auto it = pythonScriptsByKey.find(key);
575 it != pythonScriptsByKey.end()) {
576 return it->second;
577 }
578 return "";
579}
std::map< std::string, std::string > pythonScriptsByKey

◆ getSubInterfaceOptions()

MoFEMErrorCode MoFEM::JsonConfigManager::getSubInterfaceOptions ( )

Definition at line 389 of file JsonConfigManager.cpp.

389 {
390 return loadFromCommandLine();
391}
MoFEMErrorCode loadFromCommandLine()

◆ hasMeshsetsSection()

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

Definition at line 29 of file JsonConfigManager.hpp.

29{ return hasMeshsets; }

◆ loadFromCommandLine()

MoFEMErrorCode MoFEM::JsonConfigManager::loadFromCommandLine ( )
private

Definition at line 599 of file JsonConfigManager.cpp.

599 {
601 if (!configLoaded) {
602 char json_file_name[PETSC_MAX_PATH_LEN] = "";
603 PetscBool has_json = PETSC_FALSE;
604 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR, "-json_config",
605 json_file_name, sizeof(json_file_name),
606 &has_json);
607 if (has_json) {
608 // IMPORTANT !!!
609 // Clear any PETSc options that might have been set by param_file.petsc
610 CHKERR clearPetscParamFileOptionsForJson(json_file_name);
611 CHKERR loadFromFile(json_file_name);
612
613 if (hasMeshsets) {
614 char meshsets_cfg_file[PETSC_MAX_PATH_LEN] = "";
615 PetscBool has_meshsets_cfg = PETSC_FALSE;
616 CHKERR PetscOptionsGetString(PETSC_NULLPTR, PETSC_NULLPTR,
617 "-meshsets_config", meshsets_cfg_file,
618 sizeof(meshsets_cfg_file),
619 &has_meshsets_cfg);
620 if (has_meshsets_cfg) {
621 CHKERR failConfig(
622 configFileName, "meshsets",
623 "JSON meshsets cannot be used together with -meshsets_config");
624 }
625 }
626
628 // Consume and apply any log_* options injected from JSON after PETSc
629 // initialization so they do not remain as "unused database options".
631 }
632 }
634}
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 393 of file JsonConfigManager.cpp.

393 {
395
396 std::ifstream input(file_name.c_str(), std::ifstream::in);
397 if (!input.good()) {
398 CHKERR failConfig(file_name, "", "unable to open JSON config file");
399 }
400
401 std::ostringstream buffer;
402 buffer << input.rdbuf();
403
404 boost::system::error_code ec;
405 auto root = boost::json::parse(buffer.str(), ec);
406 if (ec) {
407 CHKERR failConfig(file_name, "", "invalid JSON: " + ec.message());
408 }
409
410 const auto &root_object = requireObject(root, file_name, "root");
411 boost::json::value selected_config;
412 CHKERR selectProgramConfig(root_object, file_name, selected_config);
413 const auto &config_object = requireObject(selected_config, file_name, "root");
414
415 bool has_version = false;
416 for (const auto &item : config_object) {
417 const auto key = toStdString(item.key());
418 if (key == "$schema")
419 continue;
420 if (key == "version") {
421 has_version = true;
422 const auto version =
423 requireString(item.value(), file_name, makeContext("root", key));
424 if (version != supportedVersion()) {
425 CHKERR failConfig(file_name, makeContext("root", key),
426 "unsupported config version '" + version +
427 "', supported version is '" + supportedVersion() +
428 "'");
429 }
430 continue;
431 }
432 if (key == "petsc") {
433 requireObject(item.value(), file_name, makeContext("root", key));
434 continue;
435 }
436 if (key == "mofem") {
437 requireObject(item.value(), file_name, makeContext("root", key));
438 continue;
439 }
440 if (key == "meshes" || key == "input_files") {
441 requireArray(item.value(), file_name, makeContext("root", key));
442 continue;
443 }
444 if (key == "file_name") {
445 CHKERR failConfig(
446 file_name, makeContext("root", key),
447 "legacy root-level file_name is not supported; use "
448 "root.input_files[0].file_name or root.meshes[0].file_name");
449 }
450 if (key == "meshsets") {
451 CHKERR failConfig(
452 file_name, makeContext("root", key),
453 "legacy root-level meshsets is not supported; use "
454 "root.input_files[i].meshsets or root.meshes[i].meshsets");
455 }
456 CHKERR failConfig(file_name, makeContext("root", key),
457 "unknown top-level key");
458 }
459 if (!has_version) {
460 CHKERR failConfig(file_name, "root.version",
461 "missing required version field");
462 }
463 hasMeshsets = false;
464
465 std::vector<ParsedMeshEntry> new_meshes;
466 bool config_has_meshsets = false;
467 CHKERR collectMeshes(config_object, file_name, new_meshes,
468 &config_has_meshsets);
469
470 configRoot = std::move(selected_config);
471 configFileName = file_name;
472 configLoaded = true;
473 moabMeshesByKey.clear();
474 pythonScriptsByKey.clear();
475 for (const auto &entry : new_meshes) {
476 if (entry.type == MOAB_MESH_TYPE && !entry.key.empty()) {
477 moabMeshesByKey[entry.key] = entry.fileName;
478 }
479 if (isPythonScriptMeshType(entry.type)) {
480 pythonScriptsByKey[entry.key] = entry.fileName;
481 }
482 }
483 hasMeshsets = config_has_meshsets;
484 meshsetsApplied = false;
486
488}
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
MoFEMErrorCode selectProgramConfig(const JsonObject &root_object, const std::string &file_name, boost::json::value &selected_config)
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 380 of file JsonConfigManager.cpp.

381 {
382 *iface = const_cast<JsonConfigManager *>(this);
383 return 0;
384}
JsonConfigManager(const MoFEM::Core &core)

◆ 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

◆ configFileName

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

Definition at line 37 of file JsonConfigManager.hpp.

◆ configLoaded

bool MoFEM::JsonConfigManager::configLoaded = false
private

Definition at line 38 of file JsonConfigManager.hpp.

◆ configRoot

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

Definition at line 36 of file JsonConfigManager.hpp.

◆ cOre

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

Definition at line 35 of file JsonConfigManager.hpp.

◆ hasMeshsets

bool MoFEM::JsonConfigManager::hasMeshsets = false
private

Definition at line 39 of file JsonConfigManager.hpp.

◆ meshsetsApplied

bool MoFEM::JsonConfigManager::meshsetsApplied = false
private

Definition at line 40 of file JsonConfigManager.hpp.

◆ moabMeshesByKey

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

Definition at line 41 of file JsonConfigManager.hpp.

◆ primaryMeshsetParamsByKey

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

Definition at line 43 of file JsonConfigManager.hpp.

◆ pythonScriptsByKey

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

Definition at line 42 of file JsonConfigManager.hpp.


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