suanPan
🧮 An Open Source, Parallel and Heterogeneous Finite Element Analysis Framework
Loading...
Searching...
No Matches
Storage.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2026 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
27#ifndef STORAGE_H
28#define STORAGE_H
29
30#include <Toolbox/container.h>
31
32class Amplitude;
33class Expression;
34class Constraint;
35class Converger;
36class Criterion;
37class Database;
38class Domain;
39class DomainBase;
40class Element;
41class Group;
42class Integrator;
43class Interaction;
44class Load;
45class Material;
46class Modifier;
47class Node;
48class Orientation;
49class Recorder;
50class Section;
51class Solver;
52
53template<typename T> const char* StorageType() { return "Unknown"; }
54
55template<> inline const char* StorageType<Amplitude>() { return "Amplitude"; }
56
57template<> inline const char* StorageType<Expression>() { return "Expression"; }
58
59template<> inline const char* StorageType<Constraint>() { return "Constraint"; }
60
61template<> inline const char* StorageType<Converger>() { return "Converger"; }
62
63template<> inline const char* StorageType<Criterion>() { return "Criterion"; }
64
65template<> inline const char* StorageType<Database>() { return "Database"; }
66
67template<> inline const char* StorageType<Domain>() { return "Domain"; }
68
69template<> inline const char* StorageType<DomainBase>() { return "Domain"; }
70
71template<> inline const char* StorageType<Element>() { return "Element"; }
72
73template<> inline const char* StorageType<Group>() { return "Group"; }
74
75template<> inline const char* StorageType<Integrator>() { return "Integrator"; }
76
77template<> inline const char* StorageType<Interaction>() { return "Interaction"; }
78
79template<> inline const char* StorageType<Load>() { return "Load"; }
80
81template<> inline const char* StorageType<Material>() { return "Material"; }
82
83template<> inline const char* StorageType<Modifier>() { return "Modifier"; }
84
85template<> inline const char* StorageType<Node>() { return "Node"; }
86
87template<> inline const char* StorageType<Orientation>() { return "Orientation"; }
88
89template<> inline const char* StorageType<Recorder>() { return "Recorder"; }
90
91template<> inline const char* StorageType<Section>() { return "Section"; }
92
93template<> inline const char* StorageType<Solver>() { return "Solver"; }
94
95template<typename T> class Storage : public std::enable_shared_from_this<Storage<T>> {
96 const char* type = StorageType<object_type>();
97
98 std::vector<shared_ptr<T>> fish;
100 const shared_ptr<T> empty = nullptr;
101
102 using const_iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::const_iterator;
103 using iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::iterator;
104
108public:
109 using object_type = T;
110
111 Storage() = default;
112 Storage(const Storage&) = delete;
113 Storage(Storage&&) = delete;
114 Storage& operator=(const Storage&) = delete;
116 ~Storage() = default;
117
118 const_iterator cbegin() const;
119 const_iterator cend() const;
120 iterator begin();
121 iterator end();
122
123 bool insert(const shared_ptr<T>&);
124 shared_ptr<T>& operator[](unsigned);
125 const shared_ptr<T>& at(unsigned) const;
126
127 const std::vector<shared_ptr<T>>& get() const;
128
129 [[nodiscard]] bool find(unsigned) const;
130 bool erase(unsigned);
131 void enable(unsigned);
132 void disable(unsigned);
133
134 void update();
135 void enable();
136 void reset();
137 void clear();
138
139 [[nodiscard]] size_t size() const;
140};
141
142template<typename T> typename Storage<T>::const_iterator Storage<T>::cbegin() const { return pond.cbegin(); }
143
144template<typename T> typename Storage<T>::const_iterator Storage<T>::cend() const { return pond.cend(); }
145
146template<typename T> typename Storage<T>::iterator Storage<T>::begin() { return pond.begin(); }
147
148template<typename T> typename Storage<T>::iterator Storage<T>::end() { return pond.end(); }
149
150template<typename T> bool Storage<T>::insert(const shared_ptr<T>& I) {
151 auto flag = pond.insert({I->get_tag(), I}).second;
152 if(!flag)
153 suanpan_warning("Fail to insert {} {}.\n", type, I->get_tag());
154 return flag;
155}
156
157template<typename T> shared_ptr<T>& Storage<T>::operator[](const unsigned L) { return pond[L]; }
158
159template<typename T> const shared_ptr<T>& Storage<T>::at(const unsigned L) const { return pond.contains(L) ? pond.at(L) : empty; }
160
161template<typename T> const std::vector<shared_ptr<T>>& Storage<T>::get() const { return fish; }
162
163template<typename T> bool Storage<T>::find(const unsigned L) const { return pond.contains(L); }
164
165template<typename T> bool Storage<T>::erase(const unsigned L) {
166#ifdef SUANPAN_MT
167 return pond.unsafe_erase(L) == 1;
168#else
169 return pond.erase(L) == 1;
170#endif
171}
172
173template<typename T> void Storage<T>::enable(const unsigned L) {
174 if(find(L)) pond[L]->enable();
175}
176
177template<typename T> void Storage<T>::disable(const unsigned L) {
178 if(find(L)) pond[L]->disable();
179}
180
181template<typename T> void Storage<T>::update() {
182 reset();
183 fish.reserve(size());
184 for(const auto& [tag, obj] : pond)
185 if(obj->is_active()) fish.push_back(obj);
186 else bait.insert(tag);
187}
188
189template<typename T> void Storage<T>::enable() {
190 for(const auto& I : pond) I.second->enable();
191}
192
193template<typename T> void Storage<T>::reset() {
194 fish.clear();
195 bait.clear();
196}
197
198template<typename T> void Storage<T>::clear() {
199 pond.clear();
200 reset();
201}
202
203template<typename T> size_t Storage<T>::size() const { return pond.size(); }
204
205template<typename T> typename Storage<T>::const_iterator cbegin(const Storage<T>& S) { return S.cbegin(); }
206
207template<typename T> typename Storage<T>::const_iterator cend(const Storage<T>& S) { return S.cend(); }
208
209template<typename T> typename Storage<T>::iterator begin(Storage<T>& S) { return S.begin(); }
210
211template<typename T> typename Storage<T>::iterator end(Storage<T>& S) { return S.end(); }
212
213template<typename T> using dual = std::pair<unsigned, shared_ptr<T>>;
214
235
236#endif
237
void reset(const ExternalMaterialData *data, int *info)
Definition ElasticExternal.cpp:74
const char * StorageType< Criterion >()
Definition Storage.hpp:63
const char * StorageType()
Definition Storage.hpp:53
std::pair< unsigned, shared_ptr< T > > dual
Definition Storage.hpp:213
const char * StorageType< Modifier >()
Definition Storage.hpp:83
Storage< T >::iterator end(Storage< T > &S)
Definition Storage.hpp:211
const char * StorageType< Interaction >()
Definition Storage.hpp:77
const char * StorageType< Converger >()
Definition Storage.hpp:61
const char * StorageType< Element >()
Definition Storage.hpp:71
const char * StorageType< Constraint >()
Definition Storage.hpp:59
const char * StorageType< Orientation >()
Definition Storage.hpp:87
Storage< Domain > DomainStorage
Definition Storage.hpp:221
const char * StorageType< Expression >()
Definition Storage.hpp:57
const char * StorageType< Solver >()
Definition Storage.hpp:93
const char * StorageType< DomainBase >()
Definition Storage.hpp:69
const char * StorageType< Group >()
Definition Storage.hpp:73
const char * StorageType< Amplitude >()
Definition Storage.hpp:55
const char * StorageType< Load >()
Definition Storage.hpp:79
Storage< T >::const_iterator cend(const Storage< T > &S)
Definition Storage.hpp:207
Storage< T >::const_iterator cbegin(const Storage< T > &S)
Definition Storage.hpp:205
const char * StorageType< Integrator >()
Definition Storage.hpp:75
const char * StorageType< Domain >()
Definition Storage.hpp:67
const char * StorageType< Section >()
Definition Storage.hpp:91
const char * StorageType< Node >()
Definition Storage.hpp:85
Storage< T >::iterator begin(Storage< T > &S)
Definition Storage.hpp:209
const char * StorageType< Recorder >()
Definition Storage.hpp:89
const char * StorageType< Database >()
Definition Storage.hpp:65
const char * StorageType< Material >()
Definition Storage.hpp:81
An Amplitude class that can generate Amplitude pattern.
Definition Amplitude.h:59
A Constraint class.
Definition Constraint.h:36
The Converger class handles converger test to indicate if the iteration converges according to variou...
Definition Converger.h:44
A Criterion class.
Definition Criterion.h:38
A Database class is a top level container.
Definition Database.h:33
The DomainBase class is a template.
Definition DomainBase.h:94
A Domain class holds all FE model components.
Definition Domain.h:38
A Element class.
Definition Element.h:118
A Expression class represents a maths expression.
Definition Expression.h:34
The Group class.
Definition Group.h:36
The Integrator class is basically a wrapper of the DomainBase class with regard to some status changi...
Definition Integrator.h:46
Definition Interaction.h:65
A Load class.
Definition Load.h:37
A Material abstract base class.
Definition Material.h:114
A Modifier class.
Definition Modifier.h:36
The Node class holds the number of DoFs, coordinate, displacement, velocity and acceleration.
Definition Node.h:79
A Orientation class.
Definition Orientation.h:40
A Recorder class.
Definition Recorder.h:35
A Section class.
Definition Section.h:77
A Solver class defines solvers used in analysis.
Definition Solver.h:38
A candidate Storage class that stores FEM objects.
Definition Storage.hpp:95
bool insert(const shared_ptr< T > &)
Definition Storage.hpp:150
void reset()
Definition Storage.hpp:193
shared_ptr< T > & operator[](unsigned)
Definition Storage.hpp:157
bool find(unsigned) const
Definition Storage.hpp:163
void enable(unsigned)
Definition Storage.hpp:173
Storage & operator=(Storage &&)=delete
Storage(const Storage &)=delete
void enable()
Definition Storage.hpp:189
~Storage()=default
Storage & operator=(const Storage &)=delete
size_t size() const
Definition Storage.hpp:203
iterator end()
Definition Storage.hpp:148
const shared_ptr< T > & at(unsigned) const
Definition Storage.hpp:159
Storage()=default
void clear()
Definition Storage.hpp:198
void update()
Definition Storage.hpp:181
const_iterator cend() const
Definition Storage.hpp:144
iterator begin()
Definition Storage.hpp:146
bool erase(unsigned)
Definition Storage.hpp:165
void disable(unsigned)
Definition Storage.hpp:177
const std::vector< shared_ptr< T > > & get() const
Definition Storage.hpp:161
Storage(Storage &&)=delete
T object_type
Definition Storage.hpp:109
const_iterator cbegin() const
Definition Storage.hpp:142
std::unordered_set< T > unordered_set
Definition container.h:55
std::unordered_map< T, D > unordered_map
Definition container.h:57
#define suanpan_warning(...)
Definition suanPan.h:348