PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
meshDeck.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_MESHDECK_H
12#define INP_MESHDECK_H
13
14#include "deckField.h"
15#include "util/io.h"
16#include <stdexcept>
17#include "util/json.h"
18#include <string>
19
20namespace inp {
21
28struct MeshDeck {
29
31 std::string d_filename;
32
40 double d_hMeshing;
41
47
52 std::string d_createMeshInfo;
53
56
61 std::vector<std::vector<double>> d_voidRegions;
62
66 MeshDeck(const json &j = json({})) : d_hMeshing(0.), d_createMesh(false), d_writeMeshFile(true) {
67 readFromJson(j);
68 };
69
73 MeshDeck(std::string filename, double h_meshing = -1.)
74 : d_filename(std::move(filename)), d_hMeshing(h_meshing > 0. ? h_meshing : 0.),
75 d_createMesh(false), d_writeMeshFile(true) {}
76
81 static json getExampleJson(const json &given = json::object()) {
82 const std::vector<std::string> names = {
83 "File", "Mesh_Size", "Create_Mesh",
84 "Info", "Write_Mesh_File", "Void_Regions"};
85 checkNames(given, names);
86
87 const auto filename = given.value("File", std::string());
88 const auto h_meshing = given.value("Mesh_Size", -1.);
89 const auto createMeshInfo =
90 given.value("Info", std::string("gmsh_builtin_mesh"));
91 const auto writeMeshFile = given.value("Write_Mesh_File", true);
92 const auto voidRegions = given.value(
93 "Void_Regions", std::vector<std::vector<double>>());
94
95 auto j = json({});
96 if (!filename.empty())
97 j["File"] = filename;
98
99 // A mesh size with no filename requests generation. Create_Mesh lets a
100 // set both generate a mesh and name the file it is written to or read
101 // back from.
102 const bool create = given.value("Create_Mesh", false) ||
103 (filename.empty() && h_meshing > 0.);
104 if (create) {
105 if (!(h_meshing > 0.))
106 throw std::runtime_error(
107 "MeshDeck::getExampleJson: mesh creation needs a positive "
108 "Mesh_Size.");
109 auto cm = applyGiven(json{{"Flag", true},
110 {"Info", createMeshInfo},
111 {"Mesh_Size", h_meshing},
112 {"Write_Mesh_File", writeMeshFile}},
113 createMeshFields(), {"Void_Regions"});
114 for (const auto &r : voidRegions) {
115 if (r.size() != 6)
116 throw std::runtime_error(
117 "MeshDeck::getExampleJson: each Void_Regions entry needs 6 "
118 "values [xlo, ylo, zlo, xhi, yhi, zhi].");
119 }
120 if (!voidRegions.empty())
121 cm["Void_Regions"] = voidRegions;
122 j["CreateMesh"] = cm;
123 } else if (filename.empty()) {
124 throw std::runtime_error(
125 "MeshDeck::getExampleJson: a mesh set needs a File or a Mesh_Size.");
126 }
127 return j;
128 }
129
136 static json getExampleJson(const std::string &) = delete;
138 static json getExampleJson(const char *) = delete;
139
147 static const std::vector<Field<MeshDeck>> &fields() {
148 static const std::vector<Field<MeshDeck>> f = {
149 field(&MeshDeck::d_filename, "File", std::string(),
150 "Mesh file read, or written when one is generated"),
151 };
152 return f;
153 }
154
163 static const std::vector<Field<MeshDeck>> &createMeshFields() {
164 static const std::vector<Field<MeshDeck>> f = {
165 field(&MeshDeck::d_createMesh, "Flag", false,
166 "Generate the mesh rather than read it"),
167 field(&MeshDeck::d_createMeshInfo, "Info", std::string("uniform"),
168 "Mesh generator used",
169 {{"uniform", "gmsh_builtin_mesh"}, {}, {}}),
170 field(&MeshDeck::d_hMeshing, "Mesh_Size", 0.,
171 "Element size the generator is asked for"),
172 field(&MeshDeck::d_writeMeshFile, "Write_Mesh_File", true,
173 "Write the generated mesh to File"),
174 };
175 return f;
176 }
177
178 void readFromJson(const json &j) {
179 if (j.empty())
180 return;
181
182 readFields(*this, j, fields());
183 // Defaults of the sub-block apply whether or not it is present.
184 readFields(*this, json::object(), createMeshFields());
185
186 if (j.find("CreateMesh") != j.end()) {
187 const auto &cm = j.at("CreateMesh");
188 readFields(*this, cm, createMeshFields());
189
190 // A mesh size written beside the block rather than inside it.
191 if (cm.find("Mesh_Size") == cm.end() && d_createMesh &&
192 j.find("Mesh_Size") != j.end())
193 d_hMeshing = j.at("Mesh_Size").get<double>();
194
195 d_voidRegions.clear();
196 if (cm.find("Void_Regions") != cm.end()) {
197 for (const auto &r : cm.at("Void_Regions")) {
198 auto box = r.get<std::vector<double>>();
199 if (box.size() != 6)
200 throw std::runtime_error(
201 "CreateMesh.Void_Regions entries need 6 values [xlo,ylo,zlo,xhi,yhi,zhi].");
202 d_voidRegions.push_back(std::move(box));
203 }
204 }
205 }
206
207 if (d_createMesh && d_hMeshing <= 0.)
208 throw std::runtime_error(
209 "In-built mesh creation requires CreateMesh.Mesh_Size (or legacy top-level Mesh_Size).");
210
211 if (d_filename.empty() && !d_createMesh) {
212 throw std::runtime_error("Mesh filename can not be empty unless CreateMesh is enabled.");
213 }
214 }
215
223 std::string printStr(int nt = 0, int lvl = 0) const {
224
225 auto tabS = util::io::getTabS(nt);
226 std::ostringstream oss;
227 oss << tabS << "------- MeshDeck --------" << std::endl << std::endl;
228 printFields(*this, oss, fields(), tabS);
229 printFields(*this, oss, createMeshFields(), tabS + " ");
230 oss << tabS << "Void_Regions count = " << d_voidRegions.size() << std::endl;
231 oss << tabS << "The geometry of a generated mesh is Particle.Set_i at the "
232 << "same index." << std::endl;
233 oss << tabS << std::endl;
234
235 return oss.str();
236 }
237
244 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
245};
246
249} // namespace inp
250
251#endif // INP_MESHDECK_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 checkNames(const json &given, const std::vector< std::string > &names)
Rejects a key that is not among those named.
Definition deckField.h:379
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 mesh related input data.
Definition meshDeck.h:28
void readFromJson(const json &j)
Definition meshDeck.h:178
static json getExampleJson(const json &given=json::object())
Returns example JSON object for ModelDeck configuration.
Definition meshDeck.h:81
MeshDeck(std::string filename, double h_meshing=-1.)
Constructor for programmatic use (optional meshing size for tests).
Definition meshDeck.h:73
static json getExampleJson(const std::string &)=delete
Rejects a value given where a set of named values is expected.
double d_hMeshing
Target spacing for in-built meshers only (Gmsh uniform / uniform rectangle grid).
Definition meshDeck.h:40
MeshDeck(const json &j=json({}))
Constructor.
Definition meshDeck.h:66
bool d_writeMeshFile
If true (default), Gmsh-based create-mesh paths write a .msh file; set false for in-memory only.
Definition meshDeck.h:55
static const std::vector< Field< MeshDeck > > & createMeshFields()
The fields of the CreateMesh sub-block.
Definition meshDeck.h:163
bool d_createMesh
Specify if we create mesh using in-built gmsh or in-built routine for uniform discretization of recta...
Definition meshDeck.h:46
void print(int nt=0, int lvl=0) const
Prints the information about the object.
Definition meshDeck.h:244
std::vector< std::vector< double > > d_voidRegions
Axis-aligned boxes emptied from an in-built uniform grid (CreateMesh.Void_Regions: [xlo,...
Definition meshDeck.h:61
static const std::vector< Field< MeshDeck > > & fields()
Reads from json object.
Definition meshDeck.h:147
std::string d_createMeshInfo
Selector for in-built mesh creation (e.g. uniform rectangle, gmsh_builtin_mesh for built-in geometrie...
Definition meshDeck.h:52
static json getExampleJson(const char *)=delete
Rejects a value given where a set of named values is expected.
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
Definition meshDeck.h:223
std::string d_filename
Filename to read mesh data.
Definition meshDeck.h:31