PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
pGenDeck.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_PGENDECK_H
12#define INP_PGENDECK_H
13
14#include "deckField.h"
15#include "util/io.h"
16#include "util/json.h"
17#include "geom/geomIncludes.h"
18#include <string>
19
20namespace inp {
21
28 struct PGenDeck {
29
38 std::string d_genMethod;
39
42
45
49 PGenDeck(const json &j = json({}))
50 : d_genMethod("Use_Particle_Geometry"),
52 readFromJson(j);
53 };
54
58 PGenDeck(std::string genMethod, json pGenJson = json({}), bool genWithRandomRotation = true)
59 : d_genMethod(genMethod),
60 d_pGenJson(pGenJson), d_genWithRandomRotation(genWithRandomRotation) {};
61
66 static const std::vector<Field<PGenDeck>> &fields() {
67 static const std::vector<Field<PGenDeck>> f = {
68 field(&PGenDeck::d_genMethod, "Method", std::string("From_File"),
69 "Where the particle positions come from",
70 {{"From_File", "Use_Particle_Geometry"}, {}, {}}),
71 field(&PGenDeck::d_genWithRandomRotation, "Random_Rotation", true,
72 "Give each particle a random orientation when none is given"),
73 };
74 return f;
75 }
76
82 static json getExampleJson(const json &given = json::object()) {
83 return applyGiven(given, fields(), {"Data"});
84 }
85
94 static json getExampleJson(const std::string &) = delete;
96 static json getExampleJson(const char *) = delete;
97
102 void readFromJson(const json &j) {
103 if (j.empty())
104 return;
105 readFields(*this, j, fields());
106
107 // The positions themselves are a block of their own, not a field.
108 if (d_genMethod == "From_File") {
109 if (j.find("Data") == j.end())
110 throw std::runtime_error("Need information inside key Data.");
111
112 d_pGenJson = j.at("Data");
113 }
114 }
115
121 json j = json::object();
122 writeFields(*this, j, fields());
123 if (d_genMethod == "From_File")
124 j["Data"] = d_pGenJson;
125 return j;
126 }
127
135 std::string printStr(int nt = 0, int lvl = 0) const {
136
137 auto tabS = util::io::getTabS(nt);
138 std::ostringstream oss;
139 oss << tabS << "------- PGenDeck --------" << std::endl << std::endl;
140 printFields(*this, oss, fields(), tabS);
141 size_t nParticles = d_pGenJson.value("N", 0);
142 oss << tabS << "Number of particles in json object = " << nParticles << std::endl;
143 oss << tabS << "Data for first five particles: " << std::endl;
144 if (nParticles > 0) {
145 nParticles = nParticles > 5 ? 5 : nParticles;
146 for (size_t i=0; i< nParticles; i++) {
147 oss << tabS << "Particle number = " << i << std::endl;
148 oss << tabS << d_pGenJson.at(std::to_string(i)) << std::endl;
149 }
150 }
151 oss << tabS << std::endl;
152
153 return oss.str();
154 }
155
162 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
163 };
164
167} // namespace inp
168
169#endif // INP_PGENDECK_H
nlohmann::ordered_json json
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
void writeFields(const Deck &d, json &j, const std::vector< Field< Deck > > &fs)
Writes every field of the table into the block.
Definition deckField.h:237
void printFields(const Deck &d, std::ostringstream &oss, const std::vector< Field< Deck > > &fs, const std::string &tab="")
Appends every field of the table to a stream.
Definition deckField.h:251
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
std::string getTabS(int nt)
Returns tab spaces of a given size.
Definition io.h:82
Structure to read and store particle generation data, such as particle locations and group file.
Definition pGenDeck.h:28
PGenDeck(std::string genMethod, json pGenJson=json({}), bool genWithRandomRotation=true)
Constructor.
Definition pGenDeck.h:58
json d_pGenJson
Json object loaded from the input json file or jason file for particle generation.
Definition pGenDeck.h:41
json writeToJson() const
Returns this deck as a JSON object.
Definition pGenDeck.h:120
static const std::vector< Field< PGenDeck > > & fields()
Returns example JSON object for ModelDeck configuration.
Definition pGenDeck.h:66
static json getExampleJson(const json &given=json::object())
Returns the block with the given fields set.
Definition pGenDeck.h:82
static json getExampleJson(const char *)=delete
Rejects a value given where a set of named values is expected.
void print(int nt=0, int lvl=0) const
Prints the information about the object.
Definition pGenDeck.h:162
static json getExampleJson(const std::string &)=delete
Rejects a value given where a set of named values is expected.
void readFromJson(const json &j)
Reads from json object.
Definition pGenDeck.h:102
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
Definition pGenDeck.h:135
bool d_genWithRandomRotation
Random rotation of particles if orientation is not provided.
Definition pGenDeck.h:44
PGenDeck(const json &j=json({}))
Constructor.
Definition pGenDeck.h:49
std::string d_genMethod
Particle generation method.
Definition pGenDeck.h:38