OGS 6.2.1-499-g3b941532c.dirty.20191012113459
CreateComponent.cpp
Go to the documentation of this file.
1 
13 #include "CreateComponent.h"
14 
15 #include "BaseLib/ConfigTree.h"
16 #include "ParameterLib/Parameter.h"
17 
18 #include "Components/Components.h"
19 #include "CreateProperty.h"
20 
21 namespace
22 {
23 std::unique_ptr<MaterialPropertyLib::Component> createComponent(
24  BaseLib::ConfigTree const& config,
25  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
26 {
27  using namespace MaterialPropertyLib;
28  // Parsing the component name
30  auto const& component_name = config.getConfigParameter<std::string>("name");
31 
32  // Check whether a name is given or not
33  if (component_name.empty())
34  {
35  OGS_FATAL("Component name is a mandatory field and cannot be empty.");
36  }
37 
38  // Parsing component properties. If a component name is given and this
39  // component is predefined in the class implementation, properties
40  // become optional. The default values of properties will be overwritten
41  // if specified.
42  std::unique_ptr<PropertyArray> properties =
44  createProperties(config.getConfigSubtreeOptional("properties"),
45  parameters);
46 
47  // If a name is given, it must conform with one of the derived component
48  // names in the following list:
49  if (boost::iequals(component_name, "water"))
50  {
51  return std::make_unique<Water>(std::move(properties));
52  }
53 
54  if (!properties)
55  {
56  // No component properties are provided. If the component is not
57  // specified, this results in a fatal error, since an unspecified
58  // component has no properties.
59  OGS_FATAL("No Properties defined for unspecified component");
60  }
61 
62  return std::make_unique<Component>(component_name, std::move(properties));
63 }
64 } // namespace
65 
66 namespace MaterialPropertyLib
67 {
68 std::vector<std::unique_ptr<Component>> createComponents(
69  boost::optional<BaseLib::ConfigTree> const& config,
70  std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters)
71 {
72  if (!config)
73  {
74  return {};
75  }
76 
77  std::vector<std::unique_ptr<Component>> components;
78  for (
79  auto const& component_config :
81  config->getConfigSubtreeList("component"))
82  {
83  auto component = createComponent(component_config, parameters);
84 
85  if (std::find_if(components.begin(),
86  components.end(),
87  [component_name = component->name()](auto const& c) {
88  return c->name() == component_name;
89  }) != components.end())
90  {
91  OGS_FATAL("Found duplicates with the same component name tag '%s'.",
92  component->name().c_str());
93  }
94 
95  components.push_back(std::move(component));
96  }
97 
98  return components;
99 }
100 } // namespace MaterialPropertyLib
std::vector< std::unique_ptr< Component > > createComponents(boost::optional< BaseLib::ConfigTree > const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
std::unique_ptr< PropertyArray > createProperties(boost::optional< BaseLib::ConfigTree > const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
T getConfigParameter(std::string const &param) const
std::unique_ptr< MaterialPropertyLib::Component > createComponent(BaseLib::ConfigTree const &config, std::vector< std::unique_ptr< ParameterLib::ParameterBase >> const &parameters)
#define OGS_FATAL(fmt,...)
Definition: Error.h:64
boost::optional< ConfigTree > getConfigSubtreeOptional(std::string const &root) const
Definition: ConfigTree.cpp:161