PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
bcBaseDeck.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef INP_BCBASEDECK_H
12#define INP_BCBASEDECK_H
13
14#include "deckField.h"
15#include "geom/geomIncludes.h"
16#include "util/json.h"
17
18namespace inp {
19
26 struct BCBaseDeck {
28 std::string d_type;
29
39 std::string d_selectionType;
40
47
50
52 std::vector<size_t> d_pList;
53
55 std::vector<size_t> d_pNotList;
56
68 std::string d_timeFnType;
69
81 std::string d_spatialFnType;
82
90 std::vector<size_t> d_direction;
91
93 std::vector<double> d_timeFnParams;
94
96 std::vector<double> d_spatialFnParams;
97
100
102 std::string d_icType;
103
106
110 BCBaseDeck(const json &j = json({}), std::string type = "Force_BC") :
111 d_type(type), d_isRegionActive(false), d_isDisplacementZero(false) {
112 readFromJson(j, type);
113 };
114
118 BCBaseDeck(bool isRegionActive,
119 geom::GeomData regionGeomData = geom::GeomData(),
120 std::vector<size_t> pList = std::vector<size_t>(),
121 std::vector<size_t> pNotList = std::vector<size_t>(),
122 std::string timeFnType = "",
123 std::vector<double> timeFnParams = std::vector<double>(),
124 std::string spatialFnType = "",
125 std::vector<double> spatialFnParams = std::vector<double>(),
126 std::vector<size_t> direction = std::vector<size_t>(),
127 bool isDisplacementZero = false)
128 : d_isRegionActive(isRegionActive), d_regionGeomData(regionGeomData),
129 d_pList(pList), d_pNotList(pNotList), d_timeFnType(timeFnType), d_timeFnParams(timeFnParams),
130 d_spatialFnType(spatialFnType), d_spatialFnParams(spatialFnParams),
131 d_direction(direction), d_isDisplacementZero(isDisplacementZero) {
132
133 // fix selection type
134 if (d_isRegionActive) {
135 d_selectionType = "region";
136 d_isRegionActive = true;
137 if (d_pList.size() > 0)
138 d_selectionType += "_with_include_list";
139
140 if (d_pNotList.size() > 0)
141 d_selectionType += "_with_exclude_list";
142 } else {
143 if (d_pList.size() > 0)
144 d_selectionType = "particle";
145 }
146
147 if (d_isRegionActive) {
148 // create a geometry object based on the data
150 }
151 };
152
153
162 static const std::vector<Field<BCBaseDeck>> &fields() {
163 static const std::vector<Field<BCBaseDeck>> f = {
164 field<BCBaseDeck, std::vector<size_t>>(
165 &BCBaseDeck::d_pList, "Particle_List", {},
166 "Particles the condition applies to", {},
167 [](const std::vector<size_t> &v) { return !v.empty(); }),
168 field<BCBaseDeck, std::vector<size_t>>(
169 &BCBaseDeck::d_pNotList, "Particle_Exclude_List", {},
170 "Particles the condition does not apply to", {},
171 [](const std::vector<size_t> &v) { return !v.empty(); }),
172 field<BCBaseDeck, std::vector<size_t>>(
173 &BCBaseDeck::d_direction, "Direction", {},
174 "Components the condition acts on, counted from one", {},
175 [](const std::vector<size_t> &v) { return !v.empty(); }),
176 field<BCBaseDeck, bool>(
177 &BCBaseDeck::d_isDisplacementZero, "Zero_Displacement", false,
178 "Hold the displacement at zero", {},
179 [](const bool &v) { return v; }),
180 };
181 return f;
182 }
183
188 static const std::vector<Field<BCBaseDeck>> &timeFunctionFields() {
189 static const std::vector<Field<BCBaseDeck>> f = {
190 field(&BCBaseDeck::d_timeFnType, "Type", std::string(),
191 "Time dependence of the condition"),
192 field<BCBaseDeck, std::vector<double>>(
193 &BCBaseDeck::d_timeFnParams, "Parameters", {},
194 "Parameters of the time dependence", {},
195 [](const std::vector<double> &v) { return !v.empty(); }),
196 };
197 return f;
198 }
199
204 static const std::vector<Field<BCBaseDeck>> &spatialFunctionFields() {
205 static const std::vector<Field<BCBaseDeck>> f = {
206 field(&BCBaseDeck::d_spatialFnType, "Type", std::string(),
207 "Spatial dependence of the condition"),
208 field<BCBaseDeck, std::vector<double>>(
209 &BCBaseDeck::d_spatialFnParams, "Parameters", {},
210 "Parameters of the spatial dependence", {},
211 [](const std::vector<double> &v) { return !v.empty(); }),
212 };
213 return f;
214 }
215
233 static json getExampleJson(const json &given = json::object(),
234 const geom::GeomData *region = nullptr) {
235 const std::vector<std::string> extra = {
236 "Type", "Time_Function", "Spatial_Function", "IC_Type", "IC_Vector"};
237 checkKeys(given, fields(), extra);
238
239 const auto type = given.value("Type", std::string("Force_BC"));
240
241 auto j = json({});
242
243 if (region != nullptr) {
244 auto j_geom = json({});
245 geom::writeGeometry(j_geom, *region);
246 // Must be an object: readFromJson does j.at("Region").at("Geometry").
247 j["Region"] = json{{"Geometry", j_geom}};
248 }
249
250 json fieldsGiven =
251 json{{"Particle_List",
252 given.value("Particle_List", std::vector<size_t>())},
253 {"Particle_Exclude_List",
254 given.value("Particle_Exclude_List", std::vector<size_t>())}};
255 if (type != "IC") {
256 fieldsGiven["Direction"] =
257 given.value("Direction", std::vector<size_t>());
258 fieldsGiven["Zero_Displacement"] =
259 given.value("Zero_Displacement", false);
260 }
261 j.update(applyGiven(fieldsGiven, fields()));
262
263 if (given.find("Time_Function") != given.end())
264 j["Time_Function"] =
265 applyGiven(given.at("Time_Function"), timeFunctionFields());
266
267 if (given.find("Spatial_Function") != given.end())
268 j["Spatial_Function"] =
269 applyGiven(given.at("Spatial_Function"), spatialFunctionFields());
270
271 if (type == "IC") {
272 const auto icType = given.value("IC_Type", std::string());
273 if (icType == "Constant_Velocity")
274 j[icType]["Velocity_Vector"] =
275 given.value("IC_Vector", std::vector<double>());
276 }
277
278 return j;
279 }
280
287 static json getExampleJson(const std::string &) = delete;
289 static json getExampleJson(const char *) = delete;
290
294 void readFromJson(const json &j, std::string type) {
295 if (j.empty())
296 return;
297
298 // fix selection type
299 if (j.find("Region") != j.end()) {
300 d_selectionType = "region";
301 d_isRegionActive = true;
302 if (j.find("Particle_List") != j.end())
303 d_selectionType += "_with_include_list";
304
305 if (j.find("Particle_Exclude_List") != j.end())
306 d_selectionType += "_with_exclude_list";
307 } else {
308 d_isRegionActive = false;
309 if (j.find("Particle_List") != j.end())
310 d_selectionType = "particle";
311 }
312
313 if (d_isRegionActive) {
314 geom::readGeometry(j.at("Region").at("Geometry"), d_regionGeomData);
315 // create a geometry object based on the data
317 }
318
319 readFields(*this, j, fields());
320
321 if (j.find("Time_Function") != j.end())
322 readFields(*this, j.at("Time_Function"), timeFunctionFields());
323
324 if (j.find("Spatial_Function") != j.end())
325 readFields(*this, j.at("Spatial_Function"), spatialFunctionFields());
326
327 if (type != "IC" && j.find("Direction") == j.end())
328 throw std::runtime_error(
329 "Direction must be specified for boundary condition");
330
331 // if it is initial condition
332 if (type == "IC") {
333 if (j.find("Constant_Velocity") != j.end()) {
334 d_icType = "Constant_Velocity";
335 d_icVec = util::Point(j.at("Constant_Velocity").value("Velocity_Vector", std::vector<double>({0., 0., 0.})));
336 }
337 }
338 }
339
347 std::string printStr(int nt = 0, int lvl = 0) const {
348
349 auto tabS = util::io::getTabS(nt);
350 std::ostringstream oss;
351 oss << tabS << "------- BCBaseDeck --------" << std::endl << std::endl;
352 oss << tabS << "Selection type = " << d_selectionType << std::endl;
353 oss << tabS << "Is region active = " << d_isRegionActive << std::endl;
354 if (d_regionGeomData.d_geom_p != nullptr) {
355 oss << tabS << "Region geometry info: " << std::endl;
356 oss << d_regionGeomData.printStr(nt + 1, lvl);
357 }
358 if (!d_pList.empty())
359 oss << tabS << "Particle list = ["
360 << util::io::printStr<size_t>(d_pList, 0) << "]" << std::endl;
361 if (!d_pNotList.empty())
362 oss << tabS << "Particle excluded list = ["
363 << util::io::printStr<size_t>(d_pNotList, 0) << "]" << std::endl;
364 oss << tabS << "Time function type = " << d_timeFnType << std::endl;
365 oss << tabS << "Time function parameters = ["
366 << util::io::printStr<double>(d_timeFnParams, 0) << "]" << std::endl;
367 oss << tabS << "Spatial function type = " << d_spatialFnType << std::endl;
368 oss << tabS << "Spatial function parameters = ["
369 << util::io::printStr<double>(d_spatialFnParams, 0) << "]" << std::endl;
370 oss << tabS << "Direction = [" << util::io::printStr<size_t>(d_direction, 0)
371 << "]" << std::endl;
372 oss << tabS << "Is displacement zero = " << d_isDisplacementZero << std::endl;
373 oss << tabS << std::endl;
374
375 return oss.str();
376 }
377
384 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
385 };
386
389} // namespace inp
390
391#endif // INP_BCBASEDECK_H
nlohmann::ordered_json json
void createGeomObject(const std::string &geom_type, const std::vector< double > &params, const std::vector< std::string > &vec_type, const std::vector< std::string > &vec_flag, std::shared_ptr< geom::GeomObject > &obj, bool perform_check)
void readGeometry(const json &j, geom::GeomData &geomData)
void writeGeometry(json &j, const geom::GeomData &geomData)
Collection of methods and database related to input.
Definition pairForce.h:20
Field< Deck > field(T Deck::*m, std::string key, T def, std::string doc, Accepts< T > accepts={}, std::function< bool(const T &)> emitIf=[](const T &) { return true;})
Declares a field that maps one key to one member.
Definition deckField.h:144
void readFields(Deck &d, const json &j, const std::vector< Field< Deck > > &fs)
Reads every field of the table from the block.
Definition deckField.h:230
json applyGiven(const json &given, const std::vector< Field< Deck > > &fs, const std::vector< std::string > &extra={})
The default block with the given keys replaced.
Definition deckField.h:443
void checkKeys(const json &given, const std::vector< Field< Deck > > &fs, const std::vector< std::string > &extra={})
Rejects a key that no field declares.
Definition deckField.h:421
std::string getTabS(int nt)
Returns tab spaces of a given size.
Definition io.h:82
Input data for geometrical objects.
std::shared_ptr< geom::GeomObject > d_geom_p
Zone geometry.
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
User-input data for particle neighbor search.
Definition bcBaseDeck.h:26
BCBaseDeck(bool isRegionActive, geom::GeomData regionGeomData=geom::GeomData(), std::vector< size_t > pList=std::vector< size_t >(), std::vector< size_t > pNotList=std::vector< size_t >(), std::string timeFnType="", std::vector< double > timeFnParams=std::vector< double >(), std::string spatialFnType="", std::vector< double > spatialFnParams=std::vector< double >(), std::vector< size_t > direction=std::vector< size_t >(), bool isDisplacementZero=false)
Constructor.
Definition bcBaseDeck.h:118
std::vector< size_t > d_pList
List of particles (if any)
Definition bcBaseDeck.h:52
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
Definition bcBaseDeck.h:347
static json getExampleJson(const std::string &)=delete
Rejects a value given where a set of named values is expected.
static const std::vector< Field< BCBaseDeck > > & spatialFunctionFields()
The fields of the Spatial_Function sub-block.
Definition bcBaseDeck.h:204
std::vector< size_t > d_pNotList
List of particles to not include (if any)
Definition bcBaseDeck.h:55
geom::GeomData d_regionGeomData
Region geometry (if any)
Definition bcBaseDeck.h:49
void readFromJson(const json &j, std::string type)
Reads from json object.
Definition bcBaseDeck.h:294
std::vector< double > d_timeFnParams
List of parameters for function wrt time.
Definition bcBaseDeck.h:93
std::vector< double > d_spatialFnParams
List of parameters for function wrt spatial coordinate.
Definition bcBaseDeck.h:96
static json getExampleJson(const json &given=json::object(), const geom::GeomData *region=nullptr)
Returns example JSON object for ModelDeck configuration.
Definition bcBaseDeck.h:233
std::vector< size_t > d_direction
List of dofs on which this bc will be applied.
Definition bcBaseDeck.h:90
std::string d_icType
Type.
Definition bcBaseDeck.h:102
void print(int nt=0, int lvl=0) const
Prints the information about the object.
Definition bcBaseDeck.h:384
static const std::vector< Field< BCBaseDeck > > & timeFunctionFields()
The fields of the Time_Function sub-block.
Definition bcBaseDeck.h:188
static const std::vector< Field< BCBaseDeck > > & fields()
The fields of the block itself.
Definition bcBaseDeck.h:162
bool d_isDisplacementZero
Specify if this bc corresponds to zero displacement condition.
Definition bcBaseDeck.h:99
BCBaseDeck(const json &j=json({}), std::string type="Force_BC")
Constructor.
Definition bcBaseDeck.h:110
bool d_isRegionActive
Flag that indicates if region-based application of boundary condition is active. So cases of 'region'...
Definition bcBaseDeck.h:46
std::string d_type
Method for applying force. E.g., Force_BC, Displacement_BC, IC.
Definition bcBaseDeck.h:28
static json getExampleJson(const char *)=delete
Rejects a value given where a set of named values is expected.
std::string d_selectionType
Method for applying force e.g.
Definition bcBaseDeck.h:39
util::Point d_icVec
Initial velocity vector.
Definition bcBaseDeck.h:105
std::string d_timeFnType
Name of the formula with respect to time.
Definition bcBaseDeck.h:68
std::string d_spatialFnType
Name of the formula of with respect to spatial coordinate.
Definition bcBaseDeck.h:81
A structure to represent 3d vectors.
Definition point.h:30