OGS
CreateMFront.cpp
Go to the documentation of this file.
1 
10 #include "CreateMFront.h"
11 
12 #ifndef _WIN32
13 #include <dlfcn.h>
14 #endif
15 
16 #include "BaseLib/FileTools.h"
17 #include "MFront.h"
18 #include "ParameterLib/Utils.h"
19 namespace
20 {
22 void varInfo(std::string const& msg,
23  std::vector<mgis::behaviour::Variable> const& vars,
24  mgis::behaviour::Hypothesis hypothesis)
25 {
26  INFO("#{:s}: {:d} (array size {:d}).",
27  msg,
28  vars.size(),
29  mgis::behaviour::getArraySize(vars, hypothesis));
30  for (auto& var : vars)
31  {
32  INFO(" --> type `{:s}' with name `{:s}', size {:d}, offset {:d}.",
34  var.name,
35  mgis::behaviour::getVariableSize(var, hypothesis),
36  mgis::behaviour::getVariableOffset(vars, var.name, hypothesis));
37  }
38 }
39 
41 void varInfo(std::string const& msg, std::vector<std::string> const& parameters)
42 {
43  INFO("#{:s}: {:d}.", msg, parameters.size());
44  // mgis::behaviour::getArraySize(vars, hypothesis));
45  for (auto const& parameter : parameters)
46  {
47  INFO(" --> with name `{:s}'.", parameter);
48  }
49 }
50 } // anonymous namespace
51 
52 namespace MaterialLib
53 {
54 namespace Solids
55 {
56 namespace MFront
57 {
58 template <int DisplacementDim>
59 std::unique_ptr<MechanicsBase<DisplacementDim>> createMFront(
60  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
61  std::optional<ParameterLib::CoordinateSystem> const&
62  local_coordinate_system,
63  BaseLib::ConfigTree const& config)
64 {
65  INFO("### MFRONT ########################################################");
66 
68  config.checkConfigParameter("type", "MFront");
69 
70  auto const library_name =
72  config.getConfigParameterOptional<std::string>("library");
73  auto const lib_path =
74  library_name
76  : "libOgsMFrontBehaviour";
77 
78  auto const behaviour_name =
80  config.getConfigParameter<std::string>("behaviour");
81 
82  static_assert(DisplacementDim == 2 || DisplacementDim == 3,
83  "Given DisplacementDim not supported.");
84 
85  mgis::behaviour::Hypothesis hypothesis;
86  if (DisplacementDim == 2)
87  {
88  // TODO support the axial symmetry modelling hypothesis.
89  WARN(
90  "The model is defined in 2D. On the material level currently a "
91  "plane strain setting is used. In particular it is not checked if "
92  "axial symmetry or plane stress are assumed. Special material "
93  "behaviour for these settings is currently not supported.");
94  hypothesis = mgis::behaviour::Hypothesis::PLANESTRAIN;
95  }
96  else if (DisplacementDim == 3)
97  {
98  hypothesis = mgis::behaviour::Hypothesis::TRIDIMENSIONAL;
99  }
100 
101  // Fix for https://gitlab.opengeosys.org/ogs/ogs/-/issues/3073
102  // Pre-load dependencies of mfront lib
103 #ifndef _WIN32
104  dlopen("libTFELNUMODIS.so", RTLD_NOW);
105  dlopen("libTFELUtilities.so", RTLD_NOW);
106  dlopen("libTFELException.so", RTLD_NOW);
107 #endif
108 
109  auto behaviour =
110  mgis::behaviour::load(lib_path, behaviour_name, hypothesis);
111 
112  INFO("Behaviour: `{:s}'.", behaviour.behaviour);
113  INFO("Hypothesis: `{:s}'.", mgis::behaviour::toString(hypothesis));
114  INFO("Source: `{:s}'.", behaviour.source);
115  INFO("TFEL version: `{:s}'.", behaviour.tfel_version);
116  INFO("Behaviour type: `{:s}'.", btypeToString(behaviour.btype));
117  INFO("Kinematic: `{:s}'.", toString(behaviour.kinematic));
118  INFO("Symmetry: `{:s}'.", toString(behaviour.symmetry));
119 
120  varInfo("Mat. props.", behaviour.mps, hypothesis);
121  varInfo("Gradients", behaviour.gradients, hypothesis);
122  varInfo("Thdyn. forces", behaviour.thermodynamic_forces, hypothesis);
123  varInfo("Int. StVars.", behaviour.isvs, hypothesis);
124  varInfo("Ext. StVars.", behaviour.esvs, hypothesis);
125 
126  // TODO read parameters from prj file, not yet (2018-11-05) supported by
127  // MGIS library.
128  varInfo("Real-valued parameters", behaviour.params);
129  varInfo("Integer parameters", behaviour.iparams);
130  varInfo("Unsigned parameters", behaviour.usparams);
131 
132  std::vector<ParameterLib::Parameter<double> const*> material_properties;
133 
134  if (!behaviour.mps.empty())
135  {
136  std::map<std::string, std::string> map_name_to_param;
137 
138  // gather material properties from the prj file
140  auto const mps_config = config.getConfigSubtree("material_properties");
141  for (
142  auto const mp_config :
144  mps_config.getConfigParameterList("material_property"))
145  {
147  auto name = mp_config.getConfigAttribute<std::string>("name");
148  auto const param_name =
150  mp_config.getConfigAttribute<std::string>("parameter");
151 
152  map_name_to_param.emplace(std::move(name), std::move(param_name));
153  }
154 
155  for (auto& mp : behaviour.mps)
156  {
157  auto const it = map_name_to_param.find(mp.name);
158  if (it == map_name_to_param.end())
159  OGS_FATAL(
160  "Material Property `{:s}' has not been configured in the "
161  "project file.",
162  mp.name);
163 
164  auto const param_name = it->second;
165  auto const num_comp =
166  mgis::behaviour::getVariableSize(mp, hypothesis);
167  auto const* param = &ParameterLib::findParameter<double>(
168  param_name, parameters, num_comp);
169 
170  INFO("Using OGS parameter `{:s}' for material property `{:s}'.",
171  param_name, mp.name);
172 
173  using V = mgis::behaviour::Variable;
174  if (mp.type == V::STENSOR || mp.type == V::TENSOR)
175  {
176  WARN(
177  "Material property `{:s}' is a tensorial quantity. You, "
178  "the "
179  "user, have to make sure that the component order of "
180  "parameter `{:s}' matches the one required by MFront!",
181  mp.name, param_name);
182  }
183 
184  material_properties.push_back(param);
185  map_name_to_param.erase(it);
186  }
187 
188  if (!map_name_to_param.empty())
189  {
190  ERR("Some material parameters that were configured are not used by "
191  "the material model.");
192  ERR("These parameters are:");
193 
194  for (auto& e : map_name_to_param)
195  {
196  ERR(" name: `{:s}', parameter: `{:s}'.", e.first, e.second);
197  }
198 
199  OGS_FATAL(
200  "Configuration errors occurred. Please fix the project file.");
201  }
202  }
203 
204  INFO("### MFRONT END ####################################################");
205 
206  return std::make_unique<MFront<DisplacementDim>>(
207  std::move(behaviour), std::move(material_properties),
208  local_coordinate_system);
209 }
210 } // namespace MFront
211 } // namespace Solids
212 } // namespace MaterialLib
213 
214 namespace MaterialLib
215 {
216 namespace Solids
217 {
218 namespace MFront
219 {
220 template std::unique_ptr<MechanicsBase<2>> createMFront<2>(
221  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
222  std::optional<ParameterLib::CoordinateSystem> const&
223  local_coordinate_system,
224  BaseLib::ConfigTree const& config);
225 template std::unique_ptr<MechanicsBase<3>> createMFront<3>(
226  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
227  std::optional<ParameterLib::CoordinateSystem> const&
228  local_coordinate_system,
229  BaseLib::ConfigTree const& config);
230 } // namespace MFront
231 } // namespace Solids
232 } // namespace MaterialLib
#define OGS_FATAL(...)
Definition: Error.h:26
Filename manipulation routines.
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
void checkConfigParameter(std::string const &param, T const &value) const
std::optional< T > getConfigParameterOptional(std::string const &param) const
T getConfigParameter(std::string const &param) const
ConfigTree getConfigSubtree(std::string const &root) const
Definition: ConfigTree.cpp:146
std::string const & getProjectDirectory()
Returns the directory where the prj file resides.
Definition: FileTools.cpp:217
std::string joinPaths(std::string const &pathA, std::string const &pathB)
Definition: FileTools.cpp:212
const char * btypeToString(int btype)
Converts MGIS behaviour type to a string representation.
Definition: MFront.cpp:136
template std::unique_ptr< MechanicsBase< 3 > > createMFront< 3 >(std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters, std::optional< ParameterLib::CoordinateSystem > const &local_coordinate_system, BaseLib::ConfigTree const &config)
const char * toString(mgis::behaviour::Behaviour::Kinematic kin)
Converts MGIS kinematic to a string representation.
Definition: MFront.cpp:103
const char * varTypeToString(int v)
Converts MGIS variable type to a string representation.
Definition: MFront.cpp:150
template std::unique_ptr< MechanicsBase< 2 > > createMFront< 2 >(std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters, std::optional< ParameterLib::CoordinateSystem > const &local_coordinate_system, BaseLib::ConfigTree const &config)
std::unique_ptr< MechanicsBase< DisplacementDim > > createMFront(std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters, std::optional< ParameterLib::CoordinateSystem > const &local_coordinate_system, BaseLib::ConfigTree const &config)
void varInfo(std::string const &msg, std::vector< std::string > const &parameters)
Prints info about MFront parameters.