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