MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
IOToml Class Reference

#include <iotoml.h>

Collaboration diagram for IOToml:
[legend]

Public Member Functions

assemblyreadPropertyFile (const MString &fileName)
 
MInt solverCount ()
 
MString rawText () const
 

Private Member Functions

void buildDefaultZone ()
 
void makeProperty (MProperty *, const maia::io::toml::Property &prop)
 
MBool checkZoneConsistency ()
 
MBool checkPropertyConsistency ()
 

Private Attributes

assemblym_assembly = nullptr
 
MInt m_noSolvers = -1
 
std::pair< propertyIterator, propertyIteratorm_pair
 
propertyMapm_propertyMap = nullptr
 
propertyMapm_propertyMapLowercase = nullptr
 
zoneMapm_zoneMap = nullptr
 
MString m_rawText = ""
 

Detailed Description

Definition at line 25 of file iotoml.h.

Member Function Documentation

◆ buildDefaultZone()

void IOToml::buildDefaultZone ( )
private

This function creates a default zone for all solvers, that do not appear in a zone definition.

Definition at line 99 of file iotoml.cpp.

99 {
100 TRACE();
101 /* create a zone named "default" */
102 MZone* zone;
103 zone = new MZone;
104 zone->name.append("default");
105
106 /* search for all solvers that are defined */
107 list<MInt> solverList;
108 for(zoneMap::const_iterator it = m_zoneMap->begin(); it != m_zoneMap->end(); it++) {
109 for(MInt i = 0; i < it->second->noSolvers; i++) {
110 solverList.push_back(it->second->solvers[i]);
111 }
112 }
113 solverList.sort();
114
115 /* The number of undefined solvers equals the number of total
116 solvers minus the number of defined solvers ... */
117 zone->noSolvers = (m_noSolvers - solverList.size());
118 zone->solvers = new MInt[zone->noSolvers];
119
120 /* this adds all solverId's that aren't defined to the default zone*/
121 MInt i = 0;
122 MInt simpleIterator = 0;
123 list<MInt>::const_iterator solverIt = solverList.begin();
124
125 while(i < m_noSolvers) {
126 if((!solverList.empty()) && (*solverIt == i)) {
127 i++;
128 solverIt++;
129 } else {
130 zone->solvers[simpleIterator] = i;
131 DEBUG("IOToml::buildDefaultZone: added solver " << i << " to the default zone. ", MAIA_DEBUG_IO);
132 i++;
133 simpleIterator++;
134 }
135 }
136
137 /* insert the default zone in the zone map */
138 m_zoneMap->insert(make_pair(zone->name, zone));
139 DEBUG("IOToml::buildDefaultZone: default zone has " << zone->noSolvers << " solvers.", MAIA_DEBUG_IO);
140}
MInt m_noSolvers
Definition: iotoml.h:33
zoneMap * m_zoneMap
Definition: iotoml.h:37
int32_t MInt
Definition: maiatypes.h:62
z { MString name MZone
define array structures
Definition: maiatypes.h:70

◆ checkPropertyConsistency()

MBool IOToml::checkPropertyConsistency ( )
private

Definition at line 278 of file iotoml.cpp.

278 {
279 TRACE();
280
281 for(propertyIterator i = m_propertyMap->begin(); i != m_propertyMap->end(); i++) {
282 /* if default property exists, then take next property */
283
284 std::string propertyName = i->second->name;
285
286 // name doesnot contain a solverId so is a default value
287 std::size_t found = propertyName.find('.');
288 if(found == std::string::npos) {
289 continue;
290 }
291
292 const auto range = m_propertyMap->equal_range(propertyName);
293
294 MInt size = 0;
295 for(auto j = range.first; j != range.second; j++) {
296 size++;
297 }
298 if(size == m_noSolvers) {
299 continue;
300 } else {
301 std::cerr << "Did not find property " << propertyName << " for all solvers" << endl;
302 return false;
303 }
304 }
305 return true;
306}
propertyMap * m_propertyMap
Definition: iotoml.h:35
propertyMap::const_iterator propertyIterator
Definition: contexttypes.h:16

◆ checkZoneConsistency()

MBool IOToml::checkZoneConsistency ( )
private

Checks the consistency of the zones, by creating a list, that contains all integer values from 0 to the number of the solvers, and comparing it to a list of all actual registered solverId's. For a consistent zoneList both lists must be equal.

Definition at line 313 of file iotoml.cpp.

313 {
314 TRACE();
315 list<MInt> solverList;
316 list<MInt> compareList;
317 MInt index = 0;
318 for(zoneMap::const_iterator it = m_zoneMap->begin(); it != m_zoneMap->end(); it++) {
319 for(MInt i = 0; i < it->second->noSolvers; i++) {
320 solverList.push_back(it->second->solvers[i]);
321 compareList.push_back(index++);
322 }
323 }
324 solverList.sort();
325 if(solverList == compareList) {
326 return true;
327 } else {
328 return false;
329 }
330}
IdType index(const FloatType *const x, const IdType level)
Return Hilbert index for given location and level in 2D or 3D.
Definition: hilbert.h:165

◆ makeProperty()

void IOToml::makeProperty ( MProperty p,
const maia::io::toml::Property prop 
)
private

Definition at line 23 of file iotoml.cpp.

23 {
24 // TRACE();
25
26 switch(prop.type()) {
27 case MINT: {
28 DEBUG("IOToml::makeProperty: found integer property :", MAIA_DEBUG_IO);
29 p->propertyType = MINT;
30 p->elements = prop.size();
31 p->intField = new MInt[prop.size()];
32 copy(prop.asInt().begin(), prop.asInt().end(), p->intField);
33 break;
34 }
35
36 case MFLOAT: {
37 DEBUG("IOToml::makeProperty: found float property :", MAIA_DEBUG_IO);
38 p->propertyType = MFLOAT;
39 p->elements = prop.size();
40 p->floatField = new MFloat[prop.size()];
41 copy(prop.asFloat().begin(), prop.asFloat().end(), p->floatField);
42 break;
43 }
44
45 case MBOOL: {
46 DEBUG("IOToml::makeProperty: found bool property :", MAIA_DEBUG_IO);
47 p->propertyType = MBOOL;
48 p->elements = prop.size();
49 p->boolField = new MBool[prop.size()];
50 copy(prop.asBool().begin(), prop.asBool().end(), p->boolField);
51 break;
52 }
53
54 case MSTRING: {
55 DEBUG("IOToml::makeProperty: found char property :", MAIA_DEBUG_IO);
56 p->propertyType = MSTRING;
57 p->elements = prop.size();
58 p->stringField = new MString[prop.size()];
59 copy(prop.asString().begin(), prop.asString().end(), p->stringField);
60 break;
61 }
62
63 default: {
64 stringstream errorMessage;
65 errorMessage << "IOToml::makeProperty Error: Unsupported variable input type encountered!" << endl;
66 TERMM(1, errorMessage.str());
67 }
68 }
69
70 // Insert property into property map
71 const pair<const MString, MProperty*> mp(p->name, p);
72
73 // produce the lowercase variant of the name to warn if property exists with different case
74 MString nameL = p->name;
75 std::transform(nameL.begin(), nameL.end(), nameL.begin(), [](unsigned char c) { return std::tolower(c); });
76
77 const pair<const MString, MProperty*> mpL(nameL, p);
78
79 // check if the lowercase variant of this property already exists but no regular-case variant
80 if(!(m_propertyMapLowercase->find(nameL) == m_propertyMapLowercase->end())
81 && (m_propertyMap->find(p->name) == m_propertyMap->end())) {
82 mTerm(1, AT_,
83 "There are multiple occurrences of the property " + p->name
84 + " with different cases. This should not happen!");
85 }
86 m_propertyMap->insert(mp);
87 m_propertyMapLowercase->insert(mpL);
88
89 DEBUG("IOToml::makeProperty: created default property ", MAIA_DEBUG_IO);
90 DEBUG("IOToml::makeProperty: elements = " << p->elements, MAIA_DEBUG_IO);
91 DEBUG("IOToml::makeProperty: m_noProperties = " << m_propertyMap->size(), MAIA_DEBUG_IO);
92}
propertyMap * m_propertyMapLowercase
Definition: iotoml.h:36
const std::vector< MInt > & asInt() const
Definition: tomlutils.h:96
const std::vector< MBool > & asBool() const
Definition: tomlutils.h:108
const std::vector< MFloat > & asFloat() const
Definition: tomlutils.h:102
const std::vector< MString > & asString() const
Definition: tomlutils.h:90
VariableType type() const
Definition: tomlutils.h:84
MLong size() const
Definition: tomlutils.h:86
@ MINT
Definition: enums.h:269
@ MFLOAT
Definition: enums.h:269
@ MBOOL
Definition: enums.h:269
@ MSTRING
Definition: enums.h:269
void mTerm(const MInt errorCode, const MString &location, const MString &message)
Definition: functions.cpp:29
std::basic_string< char > MString
Definition: maiatypes.h:55
double MFloat
Definition: maiatypes.h:52
bool MBool
Definition: maiatypes.h:58
constexpr std::underlying_type< FcCell >::type p(const FcCell property)
Converts property name to underlying integer value.

◆ rawText()

MString IOToml::rawText ( ) const
inline

Definition at line 29 of file iotoml.h.

29{ return m_rawText; }
MString m_rawText
Definition: iotoml.h:42

◆ readPropertyFile()

assembly * IOToml::readPropertyFile ( const MString fileName)

Definition at line 143 of file iotoml.cpp.

143 {
144 TRACE();
145
146 m_noSolvers = 0;
149 m_zoneMap = new zoneMap;
150
151 // Read in file in serial and distribute it to all domains
152 if(globalDomainId() == 0) {
153 // Read property file
154 ifstream ifs(fileName);
155 if(!ifs) {
156 TERMM(1, "could not open property file '" + fileName + "'");
157 }
158 stringstream ss;
159 ss << ifs.rdbuf();
160 if(ss.str().empty()) {
161 TERMM(1, "property file '" + fileName + "' appears to be empty");
162 }
163
164 // Determine length of null-terminated character string
165 MInt length = ss.str().size() + 1;
166
167 // Convert to char array
168 vector<MChar> text(length);
169 strcpy(text.data(), ss.str().c_str());
170
171 // Broadcast string length and contents
172 MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD, AT_, "length");
173 MPI_Bcast(text.data(), length, MPI_CHAR, 0, MPI_COMM_WORLD, AT_, "text.data()");
174
175 // Store file content in string
176 m_rawText = ss.str();
177 } else {
178 // On all other domains, first receive string length
179 MInt length = -1;
180 MPI_Bcast(&length, 1, MPI_INT, 0, MPI_COMM_WORLD, AT_, "length");
181
182 // Then receive file contents
183 vector<MChar> text(length);
184 MPI_Bcast(text.data(), length, MPI_CHAR, 0, MPI_COMM_WORLD, AT_, "text.data()");
185
186 // Store file content in string
187 m_rawText = text.data();
188 }
189
190 // Create parser for TOML files and parse file contents
191 stringstream ss;
192 ss << m_rawText;
193
194 // collect solver aliases
195 const std::shared_ptr<cpptoml::table> table = cpptoml::parser{ss}.parse();
196 map<MString, MInt> solverAliases;
197 collectSolverAliases(table, solverAliases);
198
199 // Collect property information from TOML table
200 vector<maia::io::toml::Property> properties;
201 collectProperties(table, properties, solverAliases);
202
203 // Get the number of solvers
204 m_noSolvers = 1;
205 for(auto&& prop : properties) {
206 if(prop.name() == "noSolvers") {
207 m_noSolvers = prop.asInt()[0];
208 }
209 }
210
211 // Do not read in zones (deprecated), just build default zone
213
214 // Check zone consistency or die
215 if(!checkZoneConsistency()) {
216 stringstream errorMessage;
217 errorMessage << "IOToml::readPropertyFile Error: Inconsistent solver List!\n Make sure "
218 "that solvers start with index 0 and are consecutive!"
219 << endl;
220 TERMM(1, errorMessage.str());
221 }
222
223 // Create properties
224 for(auto&& prop : properties) {
225 /* if default property */
226 if(!strstr(prop.name().c_str(), ".")) {
227 // store default property in last solver,
228 // the others to their belonging id's
229 DEBUG("IOToml::readPropertyFile: default property : " << prop.name(), MAIA_DEBUG_IO);
230 MProperty* p = new MProperty;
231 p->solverId = m_noSolvers; // Insert default solver as last solver
232 p->name.append(prop.name());
233 makeProperty(p, prop); // create new Property
234 }
235 /* if a property defined for one or more solvers */
236 else {
237 const auto pname = prop.name(); // This is necessary to prevent use-after-free errors
238 const char* du;
239 du = strrchr(pname.c_str(), '.') + 1;
240 const MInt singleSolverId = atoi(du);
241
242 if(singleSolverId || *du == '0') {
243 // Determine name without '.' and solver id
244 const MString name = prop.name().substr(0, prop.name().find("."));
245
246 DEBUG("IOToml::readPropertyFile: Found single solver property definition for solver " << singleSolverId,
247 MAIA_DEBUG_IO);
248
249 // Create new property with specific solver id
250 MProperty* p = new MProperty;
251 p->solverId = singleSolverId;
252 p->name.append(name);
253 makeProperty(p, prop); // create new Property
254 }
255 }
256 }
257
258 // Check consistency
260 DEBUG("IOToml::readPropertyFile: property consistency check succeeded", MAIA_DEBUG_IO);
261 } else {
262 stringstream errorMessage;
263 errorMessage << "IOToml::readPropertyFile Error: some properties are not defined for "
264 "all solvers!"
265 << endl;
266 TERMM(1, errorMessage.str());
267 }
268
269 m_assembly = new assembly;
273
274 return m_assembly;
275}
assembly * m_assembly
Definition: iotoml.h:32
MBool checkPropertyConsistency()
Definition: iotoml.cpp:278
MBool checkZoneConsistency()
Definition: iotoml.cpp:313
void buildDefaultZone()
Definition: iotoml.cpp:99
void makeProperty(MProperty *, const maia::io::toml::Property &prop)
Definition: iotoml.cpp:23
std::shared_ptr< table > parse()
Definition: cpptoml.h:1561
std::map< MString, MZone * > zoneMap
Definition: contexttypes.h:17
std::multimap< MString, MProperty * > propertyMap
Definition: contexttypes.h:15
struct a assembly
MInt globalDomainId()
Return global domain id.
int MPI_Bcast(void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm, const MString &name, const MString &varname)
same as MPI_Bcast
void collectProperties(const std::shared_ptr< cpptoml::table > &tab, std::vector< std::string > &names, std::vector< Property > &properties, std::map< std::string, int > &solverAliases)
Recursively traverse TOML table and collect all properties with name, type, and count.
Definition: tomlutils.h:256
void collectSolverAliases(const std::shared_ptr< cpptoml::table > &tab, std::map< std::string, int > &solverAliases)
Non-recursive search for solver aliases.
Definition: tomlutils.h:230
propertyMap * properties
Definition: contexttypes.h:20
zoneMap * zones
Definition: contexttypes.h:22
propertyMap * propertiesLowercase
Definition: contexttypes.h:21

◆ solverCount()

MInt IOToml::solverCount ( )

Definition at line 20 of file iotoml.cpp.

20{ return m_noSolvers; }

Member Data Documentation

◆ m_assembly

assembly* IOToml::m_assembly = nullptr
private

Definition at line 32 of file iotoml.h.

◆ m_noSolvers

MInt IOToml::m_noSolvers = -1
private

Definition at line 33 of file iotoml.h.

◆ m_pair

std::pair<propertyIterator, propertyIterator> IOToml::m_pair
private

Definition at line 34 of file iotoml.h.

◆ m_propertyMap

propertyMap* IOToml::m_propertyMap = nullptr
private

Definition at line 35 of file iotoml.h.

◆ m_propertyMapLowercase

propertyMap* IOToml::m_propertyMapLowercase = nullptr
private

Definition at line 36 of file iotoml.h.

◆ m_rawText

MString IOToml::m_rawText = ""
private

Definition at line 42 of file iotoml.h.

◆ m_zoneMap

zoneMap* IOToml::m_zoneMap = nullptr
private

Definition at line 37 of file iotoml.h.


The documentation for this class was generated from the following files: