PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
inp Namespace Reference

Collection of methods and database related to input. More...

Data Structures

struct  Accepts
 What a field accepts. More...
 
struct  BCBaseDeck
 User-input data for particle neighbor search. More...
 
struct  BCDeck
 Structure to read and store test-related input data. More...
 
struct  ContactDeck
 Structure to read and store particle-particle contact related input data. More...
 
struct  ContactPairDeck
 Structure to read and store particle-particle contact related input data. More...
 
struct  Field
 One field of a deck, declared once. More...
 
class  Input
 A class to read input file. More...
 
struct  MatData
 Structure for elastic properties and fracture properties. More...
 
struct  MaterialDeck
 Structure to read and store material related data. More...
 
struct  MeshDeck
 Structure to read and store mesh related input data. More...
 
struct  ModelDeck
 Structure to read and store model related input data. More...
 
struct  OneOf
 A quantity that can be given in more than one way. More...
 
struct  OutputDeck
 Structure to read input data for performing simulation output. More...
 
struct  ParticleDeck
 Structure to read and store particle related input data. More...
 
struct  PGenDeck
 Structure to read and store particle generation data, such as particle locations and group file. More...
 
struct  PNeighborDeck
 User-input data for particle neighbor search. More...
 
struct  RestartDeck
 Structure to read and store restart related data input. More...
 
struct  TestDeck
 Structure to read and store test-related input data. More...
 

Functions

template<class T >
std::string typeName ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< bool > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< int > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< std::size_t > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< double > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< std::string > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< std::vector< double > > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< std::vector< std::string > > ()
 Name of the type of a field, reported by the schema.
 
template<>
std::string typeName< std::vector< std::size_t > > ()
 Name of the type of a field, reported by the schema.
 
template<class T >
void printValue (std::ostringstream &oss, const T &v)
 Appends a value to a stream.
 
template<class T >
void printValue (std::ostringstream &oss, const std::vector< T > &v)
 Appends the entries of a vector, separated by commas.
 
template<class Deck , class T >
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.
 
template<class Deck , class T >
Field< Deck > requiredField (T Deck::*m, std::string key, std::string doc, Accepts< T > accepts={})
 Declares a field that has no default and must be present.
 
template<class Deck >
void readFields (Deck &d, const json &j, const std::vector< Field< Deck > > &fs)
 Reads every field of the table from the block.
 
template<class Deck >
void writeFields (const Deck &d, json &j, const std::vector< Field< Deck > > &fs)
 Writes every field of the table into the block.
 
template<class Deck >
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.
 
template<class Deck >
json defaultsJson (const std::vector< Field< Deck > > &fs)
 The block with every field at its default.
 
template<class Deck >
json schemaJson (const std::vector< Field< Deck > > &fs)
 Key, type, default and description of every field.
 
void checkGroups (const json &block, const std::vector< OneOf > &groups)
 Checks that each group has exactly one of its keys.
 
std::size_t editDistance (const std::string &a, const std::string &b)
 Number of single character edits between two strings.
 
void checkNames (const json &given, const std::vector< std::string > &names)
 Rejects a key that is not among those named.
 
template<class Deck >
void checkKeys (const json &given, const std::vector< Field< Deck > > &fs, const std::vector< std::string > &extra={})
 Rejects a key that no field declares.
 
template<class Deck >
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.
 

Detailed Description

Collection of methods and database related to input.

This namespace provides methods and data members specific to reading input data. We partition the input data into number of small collection of input data referred to as decks, e.g. inp::FractureDeck, inp::MeshDeck, etc. We use structs to define the deck.

  • Each deck is unique and is designed to initialize the higher level object associated to it without needing information from other decks. For example, mesh::Mesh is initialized by inp::MeshDeck.

The namespace consists of Input and Policy member classes. Input class is the main class responsible of reading input data into the decks.

Function Documentation

◆ applyGiven()

template<class Deck >
json inp::applyGiven ( const json &  given,
const std::vector< Field< Deck > > &  fs,
const std::vector< std::string > &  extra = {} 
)

The default block with the given keys replaced.

The name of every key and the value of every field are checked here, so that a deck is rejected where it is built rather than where it is read.

Parameters
givenKeys and values to replace, checked against the table
fsField table of the deck
extraKeys the deck reads outside the table
Returns
j The block

Definition at line 443 of file deckField.h.

444 {}) {
445 checkKeys(given, fs, extra);
446 json j = defaultsJson(fs);
447 if (!given.is_object())
448 return j;
449
450 for (const auto &item : given.items()) {
451 bool emit = true;
452 for (const auto &f : fs)
453 if (f.key == item.key()) {
454 f.check(item.value());
455 emit = f.emit(item.value());
456 }
457 // A field that is not written at its default is not written when the
458 // same value is given, so that the block does not depend on whether the
459 // caller named the field.
460 if (emit)
461 j[item.key()] = item.value();
462 else
463 j.erase(item.key());
464 }
465 return j;
466}
nlohmann::ordered_json json

Referenced by inp::ContactDeck::getExampleJson(), inp::ContactPairDeck::getExampleJson(), inp::MaterialDeck::getExampleJson(), inp::MeshDeck::getExampleJson(), inp::ModelDeck::getExampleJson(), inp::OutputDeck::getExampleJson(), inp::PGenDeck::getExampleJson(), inp::PNeighborDeck::getExampleJson(), inp::RestartDeck::getExampleJson(), inp::TestDeck::getExampleJson(), inp::BCBaseDeck::getExampleJson(), anonymous_namespace{testDeckField.cpp}::testKeysAreNamed(), anonymous_namespace{testDeckField.cpp}::testReadAndWrite(), and anonymous_namespace{testDeckField.cpp}::testValuesCheckedWhenBuilt().

Here is the caller graph for this function:

◆ checkGroups()

void inp::checkGroups ( const json &  block,
const std::vector< OneOf > &  groups 
)
inline

Checks that each group has exactly one of its keys.

Parameters
blockThe block to check
groupsGroups of the deck

Definition at line 317 of file deckField.h.

317 {
318 for (const auto &g : groups) {
319 std::vector<std::string> present;
320 for (const auto &k : g.keys)
321 if (block.find(k) != block.end())
322 present.push_back(k);
323
324 if (present.size() == 1 || (present.empty() && !g.required))
325 continue;
326
327 std::ostringstream oss;
328 oss << "Error: give exactly one of";
329 for (const auto &k : g.keys)
330 oss << " " << k;
331 oss << " for " << g.what << ". ";
332 if (present.empty())
333 oss << "None was given.\n";
334 else {
335 oss << "These were given:";
336 for (const auto &k : present)
337 oss << " " << k;
338 oss << ".\n";
339 }
340 throw std::runtime_error(oss.str());
341 }
342}

Referenced by inp::ContactPairDeck::getExampleJson(), inp::MaterialDeck::getExampleJson(), inp::ContactPairDeck::readFromJson(), inp::MaterialDeck::readFromJson(), and anonymous_namespace{testDeckField.cpp}::testGroups().

Here is the caller graph for this function:

◆ checkKeys()

template<class Deck >
void inp::checkKeys ( const json &  given,
const std::vector< Field< Deck > > &  fs,
const std::vector< std::string > &  extra = {} 
)

Rejects a key that no field declares.

Parameters
givenKeys the caller supplied
fsField table of the deck
extraKeys the deck reads outside the table

Definition at line 421 of file deckField.h.

422 {}) {
423 std::vector<std::string> names;
424 names.reserve(fs.size() + extra.size());
425 for (const auto &f : fs)
426 names.push_back(f.key);
427 names.insert(names.end(), extra.begin(), extra.end());
428 checkNames(given, names);
429}

Referenced by inp::ContactPairDeck::getExampleJson(), and inp::BCBaseDeck::getExampleJson().

Here is the caller graph for this function:

◆ checkNames()

void inp::checkNames ( const json &  given,
const std::vector< std::string > &  names 
)
inline

Rejects a key that is not among those named.

A deck is built by naming keys. A key that is not declared would otherwise be dropped without a message, and the run would proceed with the default in place of the value the caller asked for. A key within an edit distance of three of a declared one is reported together with that name.

Parameters
givenKeys the caller supplied
namesEvery key the deck accepts

Definition at line 379 of file deckField.h.

380 {
381 if (given.is_null() || given.empty())
382 return;
383 if (!given.is_object())
384 throw std::runtime_error(
385 "Error: deck values must be given as name and value pairs.\n");
386
387 for (const auto &item : given.items()) {
388 const std::string key = item.key();
389 if (std::find(names.begin(), names.end(), key) != names.end())
390 continue;
391
392 std::string closest;
393 std::size_t best = std::string::npos;
394 for (const auto &n : names) {
395 const std::size_t d = editDistance(key, n);
396 if (d < best) {
397 best = d;
398 closest = n;
399 }
400 }
401 std::ostringstream oss;
402 oss << "Error: no field named " << key << ".";
403 if (best <= 3)
404 oss << " Closest declared field is " << closest << ".";
405 oss << "\nDeclared fields are:";
406 for (const auto &n : names)
407 oss << " " << n;
408 oss << "\n";
409 throw std::runtime_error(oss.str());
410 }
411}
std::size_t editDistance(const std::string &a, const std::string &b)
Number of single character edits between two strings.
Definition deckField.h:354

References editDistance().

Referenced by inp::BCDeck::getExampleJson(), and inp::MeshDeck::getExampleJson().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ defaultsJson()

template<class Deck >
json inp::defaultsJson ( const std::vector< Field< Deck > > &  fs)

The block with every field at its default.

Definition at line 260 of file deckField.h.

260 {
261 json j = json::object();
262 for (const auto &f : fs)
263 f.writeDefault(j);
264 return j;
265}

Referenced by schemaJson(), and anonymous_namespace{testDeckField.cpp}::testDefaultsAndSchema().

Here is the caller graph for this function:

◆ editDistance()

std::size_t inp::editDistance ( const std::string &  a,
const std::string &  b 
)
inline

Number of single character edits between two strings.

Used to name the closest key when a caller supplies one that is not declared.

Parameters
aFirst string
bSecond string
Returns
n Edit distance

Definition at line 354 of file deckField.h.

354 {
355 std::vector<std::size_t> prev(b.size() + 1), cur(b.size() + 1);
356 for (std::size_t j = 0; j <= b.size(); ++j)
357 prev[j] = j;
358 for (std::size_t i = 1; i <= a.size(); ++i) {
359 cur[0] = i;
360 for (std::size_t j = 1; j <= b.size(); ++j)
361 cur[j] = std::min({prev[j] + 1, cur[j - 1] + 1,
362 prev[j - 1] + (a[i - 1] == b[j - 1] ? 0 : 1)});
363 prev = cur;
364 }
365 return prev[b.size()];
366}

Referenced by checkNames(), and anonymous_namespace{testDeckField.cpp}::testEditDistance().

Here is the caller graph for this function:

◆ field()

template<class Deck , class T >
Field< Deck > inp::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.

Parameters
mPointer to the member
keyKey in the JSON block
defValue used when the key is absent
docOne line describing the field
acceptsWhat the field accepts
emitIfWritten only when this is true of the value. Use it for a key the deck omits rather than writes at its default, such as Horizon, which is absent when the horizon is set by Horizon_Mesh_Ratio instead
Returns
field The field

Definition at line 144 of file deckField.h.

146 {},
147 std::function<bool(const T &)> emitIf = [](const T &) { return true; }) {
148
149 // Accepted values and bounds apply to the scalar types. A field holding a
150 // list is checked by the deck if it needs to be.
151 constexpr bool checkable =
152 std::is_arithmetic_v<T> || std::is_same_v<T, std::string>;
153
154 auto check = [key, accepts](const T &v) {
155 if constexpr (checkable) {
156 if (!accepts.values.empty() &&
157 std::find(accepts.values.begin(), accepts.values.end(), v) ==
158 accepts.values.end()) {
159 std::ostringstream oss;
160 oss << "Error: " << key << " must be one of:";
161 for (const auto &a : accepts.values)
162 oss << " " << a;
163 oss << ". Given value is " << v << ".\n";
164 throw std::runtime_error(oss.str());
165 }
166 if (accepts.min.has_value() && v < accepts.min.value()) {
167 std::ostringstream oss;
168 oss << "Error: " << key << " must be at least " << accepts.min.value()
169 << ". Given value is " << v << ".\n";
170 throw std::runtime_error(oss.str());
171 }
172 if (accepts.max.has_value() && accepts.max.value() < v) {
173 std::ostringstream oss;
174 oss << "Error: " << key << " must be at most " << accepts.max.value()
175 << ". Given value is " << v << ".\n";
176 throw std::runtime_error(oss.str());
177 }
178 }
179 };
180
181 return Field<Deck>{
182 key, std::move(doc), typeName<T>(),
183 [m, key, def, check](Deck &d, const json &j) {
184 d.*m = j.value(key, def);
185 check(d.*m);
186 },
187 [m, key, emitIf](const Deck &d, json &j) {
188 if (emitIf(d.*m))
189 j[key] = d.*m;
190 },
191 [key, def, emitIf](json &j) {
192 if (emitIf(def))
193 j[key] = def;
194 },
195 [m, key](const Deck &d, std::ostringstream &oss,
196 const std::string &tab) {
197 oss << tab << key << " = ";
198 printValue(oss, d.*m);
199 oss << std::endl;
200 },
201 [check](const json &v) { check(v.get<T>()); },
202 [emitIf](const json &v) { return emitIf(v.get<T>()); }};
203}
void check(bool ok, const std::string &what)
void printValue(std::ostringstream &oss, const T &v)
Appends a value to a stream.
Definition deckField.h:101
std::optional< T > max
Upper bound, applied when it is set.
Definition deckField.h:127
std::vector< T > values
Accepted values, or empty when any value is accepted.
Definition deckField.h:121
std::optional< T > min
Lower bound, applied when it is set.
Definition deckField.h:124

Referenced by inp::MeshDeck::createMeshFields(), inp::BCBaseDeck::fields(), inp::ContactDeck::fields(), inp::ContactPairDeck::fields(), inp::MatData::fields(), inp::MaterialDeck::fields(), inp::MeshDeck::fields(), inp::ModelDeck::fields(), inp::OutputDeck::fields(), inp::PGenDeck::fields(), inp::PNeighborDeck::fields(), inp::RestartDeck::fields(), inp::TestDeck::fields(), anonymous_namespace{testDeckField.cpp}::SampleDeck::fields(), inp::MaterialDeck::influenceFields(), inp::BCBaseDeck::spatialFunctionFields(), and inp::BCBaseDeck::timeFunctionFields().

Here is the caller graph for this function:

◆ printFields()

template<class Deck >
void inp::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.

Parameters
dThe deck
ossStream to append to
fsField table of the deck
tabPrefix written before each field

Definition at line 251 of file deckField.h.

253 {
254 for (const auto &f : fs)
255 f.print(d, oss, tab);
256}

Referenced by inp::ContactDeck::printStr(), inp::MeshDeck::printStr(), inp::ModelDeck::printStr(), inp::OutputDeck::printStr(), inp::PGenDeck::printStr(), inp::PNeighborDeck::printStr(), inp::RestartDeck::printStr(), inp::TestDeck::printStr(), and anonymous_namespace{testDeckField.cpp}::testPrint().

Here is the caller graph for this function:

◆ printValue() [1/2]

template<class T >
void inp::printValue ( std::ostringstream &  oss,
const std::vector< T > &  v 
)

Appends the entries of a vector, separated by commas.

Definition at line 107 of file deckField.h.

107 {
108 oss << util::io::printStr<T>(v, 0);
109}

◆ printValue() [2/2]

template<class T >
void inp::printValue ( std::ostringstream &  oss,
const T &  v 
)

Appends a value to a stream.

Definition at line 101 of file deckField.h.

101 {
102 oss << v;
103}

◆ readFields()

template<class Deck >
void inp::readFields ( Deck &  d,
const json &  j,
const std::vector< Field< Deck > > &  fs 
)

◆ requiredField()

template<class Deck , class T >
Field< Deck > inp::requiredField ( T Deck::*  m,
std::string  key,
std::string  doc,
Accepts< T >  accepts = {} 
)

Declares a field that has no default and must be present.

Parameters
mPointer to the member
keyKey in the JSON block
docOne line describing the field
acceptsWhat the field accepts
Returns
field The field

Definition at line 215 of file deckField.h.

216 {}) {
217 auto f = field(m, key, T{}, std::move(doc), accepts);
218 f.read = [m, key, accepts, f](Deck &d, const json &j) {
219 if (j.find(key) == j.end())
220 throw std::runtime_error("Error: " + key + " is required.\n");
221 json one = json::object();
222 one[key] = j.at(key);
223 f.read(d, one);
224 };
225 return f;
226}

◆ schemaJson()

template<class Deck >
json inp::schemaJson ( const std::vector< Field< Deck > > &  fs)

Key, type, default and description of every field.

The Python interface builds its arguments from this, so that a field added to the table needs no change on the Python side.

Parameters
fsField table of the deck
Returns
schema One object per field

Definition at line 276 of file deckField.h.

276 {
277 json out = json::array();
278 const json defaults = defaultsJson(fs);
279 for (const auto &f : fs) {
280 json entry = {{"key", f.key}, {"type", f.type}, {"doc", f.doc}};
281 if (defaults.find(f.key) != defaults.end())
282 entry["default"] = defaults.at(f.key);
283 out.push_back(entry);
284 }
285 return out;
286}
json defaultsJson(const std::vector< Field< Deck > > &fs)
The block with every field at its default.
Definition deckField.h:260

References defaultsJson().

Referenced by anonymous_namespace{testDeckField.cpp}::testDefaultsAndSchema().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ typeName()

template<class T >
std::string inp::typeName ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 75 of file deckField.h.

75{ return "value"; }

◆ typeName< bool >()

template<>
std::string inp::typeName< bool > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 77 of file deckField.h.

77{ return "bool"; }

◆ typeName< double >()

template<>
std::string inp::typeName< double > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 83 of file deckField.h.

83{ return "double"; }

◆ typeName< int >()

template<>
std::string inp::typeName< int > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 79 of file deckField.h.

79{ return "int"; }

◆ typeName< std::size_t >()

template<>
std::string inp::typeName< std::size_t > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 81 of file deckField.h.

81{ return "size_t"; }

◆ typeName< std::string >()

template<>
std::string inp::typeName< std::string > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 85 of file deckField.h.

85{ return "string"; }

◆ typeName< std::vector< double > >()

template<>
std::string inp::typeName< std::vector< double > > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 87 of file deckField.h.

87 {
88 return "double[]";
89}

◆ typeName< std::vector< std::size_t > >()

template<>
std::string inp::typeName< std::vector< std::size_t > > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 95 of file deckField.h.

95 {
96 return "size_t[]";
97}

◆ typeName< std::vector< std::string > >()

template<>
std::string inp::typeName< std::vector< std::string > > ( )
inline

Name of the type of a field, reported by the schema.

Definition at line 91 of file deckField.h.

91 {
92 return "string[]";
93}

◆ writeFields()

template<class Deck >
void inp::writeFields ( const Deck &  d,
json &  j,
const std::vector< Field< Deck > > &  fs 
)

Writes every field of the table into the block.

Definition at line 237 of file deckField.h.

237 {
238 for (const auto &f : fs)
239 f.write(d, j);
240}

Referenced by anonymous_namespace{testDeckField.cpp}::testReadAndWrite(), inp::PGenDeck::writeToJson(), inp::PNeighborDeck::writeToJson(), and inp::RestartDeck::writeToJson().

Here is the caller graph for this function: