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 std::vector<shared_ptr<T>> fish;
98 const shared_ptr<T> empty = nullptr;
99
100 using const_iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::const_iterator;
101 using iterator = typename suanpan::unordered_map<unsigned, shared_ptr<T>>::iterator;
102
106public:
107 using object_type = T;
108
109 Storage() = default;
110 Storage(const Storage&) = delete;
111 Storage(Storage&&) = delete;
112 Storage& operator=(const Storage&) = delete;
114 ~Storage() = default;
115
116 const_iterator cbegin() const;
117 const_iterator cend() const;
118 iterator begin();
119 iterator end();
120
121 bool insert(const shared_ptr<T>&);
122 shared_ptr<T>& operator[](unsigned);
123 const shared_ptr<T>& at(unsigned) const;
124
125 const std::vector<shared_ptr<T>>& get() const;
126
127 [[nodiscard]] bool find(unsigned) const;
128 bool erase(unsigned);
129 void enable(unsigned);
130 void disable(unsigned);
131
132 void update();
133 void enable();
134 void reset();
135 void clear();
136
137 [[nodiscard]] size_t size() const;
138};
139
140template<typename T> typename Storage<T>::const_iterator Storage<T>::cbegin() const { return pond.cbegin(); }
141
142template<typename T> typename Storage<T>::const_iterator Storage<T>::cend() const { return pond.cend(); }
143
144template<typename T> typename Storage<T>::iterator Storage<T>::begin() { return pond.begin(); }
145
146template<typename T> typename Storage<T>::iterator Storage<T>::end() { return pond.end(); }
147
148template<typename T> bool Storage<T>::insert(const shared_ptr<T>& I) {
149 auto flag = pond.insert({I->get_tag(), I}).second;
150 if(!flag)
151 suanpan_warning("Fail to insert {} {}.\n", StorageType<T>(), I->get_tag());
152 return flag;
153}
154
155template<typename T> shared_ptr<T>& Storage<T>::operator[](const unsigned L) { return pond[L]; }
156
157template<typename T> const shared_ptr<T>& Storage<T>::at(const unsigned L) const { return pond.contains(L) ? pond.at(L) : empty; }
158
159template<typename T> const std::vector<shared_ptr<T>>& Storage<T>::get() const { return fish; }
160
161template<typename T> bool Storage<T>::find(const unsigned L) const { return pond.contains(L); }
162
163template<typename T> bool Storage<T>::erase(const unsigned L) {
164#ifdef SUANPAN_MT
165 return pond.unsafe_erase(L) == 1;
166#else
167 return pond.erase(L) == 1;
168#endif
169}
170
171template<typename T> void Storage<T>::enable(const unsigned L) {
172 if(find(L)) pond[L]->enable();
173}
174
175template<typename T> void Storage<T>::disable(const unsigned L) {
176 if(find(L)) pond[L]->disable();
177}
178
179template<typename T> void Storage<T>::update() {
180 reset();
181 fish.reserve(size());
182 for(const auto& [tag, obj] : pond)
183 if(obj->is_active()) fish.push_back(obj);
184 else bait.insert(tag);
185}
186
187template<typename T> void Storage<T>::enable() {
188 for(const auto& I : pond) I.second->enable();
189}
190
191template<typename T> void Storage<T>::reset() {
192 fish.clear();
193 bait.clear();
194}
195
196template<typename T> void Storage<T>::clear() {
197 pond.clear();
198 reset();
199}
200
201template<typename T> size_t Storage<T>::size() const { return pond.size(); }
202
203template<typename T> typename Storage<T>::const_iterator cbegin(const Storage<T>& S) { return S.cbegin(); }
204
205template<typename T> typename Storage<T>::const_iterator cend(const Storage<T>& S) { return S.cend(); }
206
207template<typename T> typename Storage<T>::iterator begin(Storage<T>& S) { return S.begin(); }
208
209template<typename T> typename Storage<T>::iterator end(Storage<T>& S) { return S.end(); }
210
211template<typename T> using dual = std::pair<unsigned, shared_ptr<T>>;
212
233
234#endif
235
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:211
const char * StorageType< Modifier >()
Definition Storage.hpp:83
Storage< T >::iterator end(Storage< T > &S)
Definition Storage.hpp:209
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:219
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:205
Storage< T >::const_iterator cbegin(const Storage< T > &S)
Definition Storage.hpp:203
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:207
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:96
A Domain class holds all FE model components.
Definition Domain.h:38
A Element class.
Definition Element.h:120
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:116
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:148
void reset()
Definition Storage.hpp:191
shared_ptr< T > & operator[](unsigned)
Definition Storage.hpp:155
bool find(unsigned) const
Definition Storage.hpp:161
void enable(unsigned)
Definition Storage.hpp:171
Storage & operator=(Storage &&)=delete
Storage(const Storage &)=delete
void enable()
Definition Storage.hpp:187
~Storage()=default
Storage & operator=(const Storage &)=delete
size_t size() const
Definition Storage.hpp:201
iterator end()
Definition Storage.hpp:146
const shared_ptr< T > & at(unsigned) const
Definition Storage.hpp:157
Storage()=default
void clear()
Definition Storage.hpp:196
void update()
Definition Storage.hpp:179
const_iterator cend() const
Definition Storage.hpp:142
iterator begin()
Definition Storage.hpp:144
bool erase(unsigned)
Definition Storage.hpp:163
void disable(unsigned)
Definition Storage.hpp:175
const std::vector< shared_ptr< T > > & get() const
Definition Storage.hpp:159
Storage(Storage &&)=delete
T object_type
Definition Storage.hpp:107
const_iterator cbegin() const
Definition Storage.hpp:140
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