OGS
Project.cpp
Go to the documentation of this file.
1
11#include "Project.h"
12
13#include <algorithm>
14
15#include "BaseLib/Algorithm.h"
16#include "BaseLib/FileTools.h"
17#include "MeshLib/Mesh.h"
18
19namespace DataHolderLib
20{
21void Project::addMesh(std::unique_ptr<MeshLib::Mesh> mesh)
22{
23 std::string name = mesh->getName();
25 mesh->setName(name);
26 _mesh_vec.push_back(std::move(mesh));
27}
28
29std::vector<std::unique_ptr<MeshLib::Mesh>>::const_iterator
30Project::findMeshByName(std::string const& name) const
31{
32 return const_cast<Project&>(*this).findMeshByName(name);
33}
34
35std::vector<std::unique_ptr<MeshLib::Mesh>>::iterator Project::findMeshByName(
36 std::string const& name)
37{
38 return std::find_if(_mesh_vec.begin(), _mesh_vec.end(),
39 [&name](std::unique_ptr<MeshLib::Mesh>& mesh)
40 { return mesh && (name == mesh->getName()); });
41}
42
43const MeshLib::Mesh* Project::getMesh(const std::string& name) const
44{
45 auto it = findMeshByName(name);
46 return (it == _mesh_vec.end() ? nullptr : it->get());
47}
48
49bool Project::removeMesh(const std::string& name)
50{
51 auto it = findMeshByName(name);
52 if (it != _mesh_vec.end())
53 {
54 delete it->release();
55 _mesh_vec.erase(it);
56 return true;
57 }
58 return false;
59}
60
61bool Project::getUniqueName(std::string& name) const
62{
63 int count(0);
64 bool isUnique(false);
65 std::string cpName;
66
67 while (!isUnique)
68 {
69 isUnique = true;
70 cpName = name;
71
72 count++;
73 // If the original name already exists we start to add numbers to name
74 // for as long as it takes to make the name unique.
75 if (count > 1)
76 {
77 cpName = cpName + "-" + std::to_string(count);
78 }
79
80 for (auto& mesh : _mesh_vec)
81 {
82 if (cpName == mesh->getName())
83 {
84 isUnique = false;
85 }
86 }
87 }
88
89 // At this point cpName is a unique name and isUnique is true.
90 // If cpName is not the original name, "name" is changed and isUnique is set
91 // to false, indicating that a vector with the original name already exists.
92 if (count > 1)
93 {
94 isUnique = false;
95 name = cpName;
96 }
97 return isUnique;
98}
99
100void Project::removePrimaryVariable(std::string const& primary_var_name)
101{
102 std::size_t const n_bc(_boundary_conditions.size());
103 for (int i = n_bc - 1; i >= 0; --i)
104 {
105 if (_boundary_conditions[i]->getProcessVarName() == primary_var_name)
106 {
107 removeBoundaryCondition(primary_var_name,
108 _boundary_conditions[i]->getParamName());
109 }
110 }
111
112 std::size_t const n_st(_source_terms.size());
113 for (int i = n_st - 1; i >= 0; --i)
114 {
115 if (_source_terms[i]->getProcessVarName() == primary_var_name)
116 {
117 removeSourceTerm(primary_var_name,
118 _source_terms[i]->getParamName());
119 }
120 }
121}
122
123void Project::removeBoundaryCondition(std::string const& primary_var_name,
124 std::string const& param_name)
125{
126 std::size_t const n_bc(_boundary_conditions.size());
127 for (std::size_t i = 0; i < n_bc; ++i)
128 {
129 if (_boundary_conditions[i]->getProcessVarName() == primary_var_name &&
130 _boundary_conditions[i]->getParamName() == param_name)
131 {
132 _boundary_conditions[i].reset();
134 return;
135 }
136 }
137}
138
139void Project::removeSourceTerm(std::string const& primary_var_name,
140 std::string const& param_name)
141{
142 std::size_t const n_st(_source_terms.size());
143 for (std::size_t i = 0; i < n_st; ++i)
144 {
145 if (_source_terms[i]->getProcessVarName() == primary_var_name &&
146 _source_terms[i]->getParamName() == param_name)
147 {
148 _source_terms[i].reset();
149 _source_terms.erase(_source_terms.begin() + i);
150 return;
151 }
152 }
153}
154
155} // namespace DataHolderLib
Filename manipulation routines.
Definition of the Mesh class.
void removeSourceTerm(std::string const &primary_var_name, std::string const &param_name)
Remove one source term.
Definition: Project.cpp:139
bool removeMesh(const std::string &name)
Definition: Project.cpp:49
std::vector< std::unique_ptr< SourceTerm > > _source_terms
Definition: Project.h:118
void addMesh(std::unique_ptr< MeshLib::Mesh > mesh)
Definition: Project.cpp:21
void removePrimaryVariable(std::string const &primary_var_name)
Removes a primary variable incl. all associated conditions.
Definition: Project.cpp:100
const MeshLib::Mesh * getMesh(const std::string &name) const
Definition: Project.cpp:43
std::vector< std::unique_ptr< MeshLib::Mesh > >::const_iterator findMeshByName(std::string const &name) const
Returns an iterator to the first found mesh with the given name.
Definition: Project.cpp:30
void removeBoundaryCondition(std::string const &primary_var_name, std::string const &param_name)
Removes one boundary condition.
Definition: Project.cpp:123
bool getUniqueName(std::string &name) const
Definition: Project.cpp:61
std::vector< std::unique_ptr< BoundaryCondition > > _boundary_conditions
Definition: Project.h:117
std::vector< std::unique_ptr< MeshLib::Mesh > > _mesh_vec
Definition: Project.h:116