KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
json_io.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ \.
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 //
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 #if !defined(KRATOS_JSON_IO_H_INCLUDED )
24 #define KRATOS_JSON_IO_H_INCLUDED
25 
26 
27 
28 // System includes
29 #include <string>
30 // #include <fstream>
31 #include <cstdio>
32 #include <iostream>
33 #include <set>
34 #include <map>
35 
36 // External includes
37 
38 
39 // Project includes
40 #include "includes/define.h"
41 #include "includes/io.h"
42 #include "utilities/timer.h"
43 #include "containers/flags.h"
44 #include "includes/mesh.h"
45 
46 #include "rapidjson/filereadstream.h"
47 #include "rapidjson/document.h"
48 
49 namespace Kratos
50 {
51 
54 
58 
62 
66 
70 
72 
74 class KratosJsonIO : public IO
75 {
76 public:
79 
82 
83  typedef IO BaseType;
84 
86 
88 
90 
92 
94 
96 
98 
99 // typedef std::vector<std::ofstream*> OutputFilesContainerType;
100 
101  typedef std::size_t SizeType;
102 
106 
109  std::string const& Filename,
110  const Flags Options = IO::READ | IO::IGNORE_VARIABLES_ERROR.AsFalse())
111  :IO()
112  {
113  mFilename = Filename;
114  mOptions = Options;
115  }
116 
117  virtual void ReadModelPart(ModelPart & rThisModelPart)
118  {
119  //read mFilename
120  FILE* fp = fopen(mFilename.c_str(), "rb"); // non-Windows use "r"
121  char readBuffer[65536];
122  rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
123  rapidjson::Document d;
124  d.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(is);
125  fclose(fp);
126 
127  bool consecutive_reordering = true; //TODO: pass this as a flag in the options
128  if(consecutive_reordering)
130 
131 
132 
133  //construct the model part
134  FillModelPart(d, rThisModelPart);
135 
136  }
137 
138 
139 
140 
142 
144  virtual ~KratosJsonIO() {};
145 
146 
150 
151 
155 
156 
157 
161 
162 
166 
167 
171 
173  virtual std::string Info() const
174  {
175  return "KratosJsonIO";
176  }
177 
179  virtual void PrintInfo(std::ostream& rOStream) const
180  {
181  rOStream << "KratosJsonIO";
182  }
183 
184 
186  virtual void PrintData(std::ostream& rOStream) const
187  {
188  }
189 
190 
194 
195 
197 
198 protected:
201 
202 
206 
207 
211 
212 
216 
220 
221 
225 
226 
230 
231 
233 
234 protected:
237 
238 
242 
243  std::string mFilename;
245 
246 
250 
251 
255  void FillModelPart(rapidjson::Document& d, ModelPart & rThisModelPart)
256  {
257  KRATOS_TRY
258 
259  //get the section of the model part related to nodes
260  rapidjson::Value& json_model_part = d["model_part"]; // Using a reference for consecutive access is handy and faster.
261 
263  if(json_model_part.HasMember("Nodes") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection Nodes of the model_part","")
264  {
265  rapidjson::Value& json_nodes = json_model_part["Nodes"]; // Using a reference for consecutive access is handy and faster.
266  if(json_nodes.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Nodes section is not an array as expected!!","")
267 
268  std::map<std::size_t, std::size_t> nodes_id_map; //create a list of nodes -- Create New node uses insert hence it is slow if input is not ordered
269  for (rapidjson::SizeType i = 0; i < json_nodes.Size(); i++) // rapidjson uses SizeType instead of size_t.
270  {
271  std::size_t node_id = json_nodes[i][0].GetInt() ;
272  nodes_id_map[ node_id ] = i; //store in the map the position in the array
273  }
274 
275  for(std::map<std::size_t, std::size_t>::iterator it = nodes_id_map.begin(); it!= nodes_id_map.end(); it++)
276  {
277  std::size_t pos = it->second;
278 // KRATOS_WATCH(it->first);
279 // KRATOS_WATCH(it->second);
280  const rapidjson::Value& node_input = json_nodes[pos]; //obtain the row with the node data
281  rThisModelPart.CreateNewNode( node_input[0].GetInt(), node_input[1].GetDouble(), node_input[2].GetDouble(), node_input[3].GetDouble());
282  }
283  //here nodes are not any more needed in the json so remove them to free the memory
284  d.RemoveMember(json_nodes);
285  }
286 
288  if(json_model_part.HasMember("Properties") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection Properties of the model_part","")
289  {
290  const rapidjson::Value& json_properties = json_model_part["Properties"]; // Using a reference for consecutive access is handy and faster.
291  if(json_properties.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Properties section is not a json object as expected!!","")
292  for (rapidjson::Value::ConstMemberIterator itr = json_properties.MemberBegin(); itr != json_properties.MemberEnd(); ++itr)
293  {
294  unsigned int prop_id = atoi( itr->name.GetString() );
295  if(itr->value.HasMember("Variables") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection Variables of the Properties subsection of model_part","");
296 
297  const rapidjson::Value& prop_variables = itr->value["Variables"];
298  for (rapidjson::Value::ConstMemberIterator iii = prop_variables.MemberBegin(); iii != prop_variables.MemberEnd(); ++iii)
299  {
300  if( CheckAndAssignPropertyValue< Variable<double> >( rThisModelPart.pGetProperties(prop_id), iii->name.GetString(), iii->value) ) {}
301  else if( CheckAndAssignPropertyValue< Variable<bool> >( rThisModelPart.pGetProperties(prop_id), iii->name.GetString(), iii->value) ) {}
302  else if( CheckAndAssignPropertyValue< Variable<int> >( rThisModelPart.pGetProperties(prop_id), iii->name.GetString(), iii->value) ) {}
303  else if( CheckAndAssignPropertyValue< Variable<unsigned int> >( rThisModelPart.pGetProperties(prop_id), iii->name.GetString(), iii->value) ) {}
304  else
305  {
306  typedef VariableComponent< VectorComponentAdaptor<array_1d<double, 3> > > component_type;
307  if( CheckAndAssignPropertyValue< component_type >( rThisModelPart.pGetProperties(prop_id), iii->name.GetString(), iii->value) ) {}
308  else
309  {
310  KRATOS_THROW_ERROR(std::invalid_argument, "can not read a variable type from the property block -- variable is ",iii->name.GetString());
311  }
312  }
313  }
314  }
315  }
316 
317  //read Elements
318  if(json_model_part.HasMember("Elements"))
319  {
320  const rapidjson::Value& json_elements = json_model_part["Elements"]; // Using a reference for consecutive access is handy and faster.
321  if(json_elements.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Elements section is not a json object as expected!!","")
322  for (rapidjson::Value::ConstMemberIterator itr = json_elements.MemberBegin(); itr != json_elements.MemberEnd(); ++itr)
323  {
324  std::string element_name = itr->name.GetString();
325  KRATOS_WATCH(element_name);
326  if(itr->value["connectivity"].IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the connectivity section of the Elements is not an array as expected!!","")
327 
328  const rapidjson::Value& connectivity = itr->value["connectivity"];
329  for (SizeType i = 0; i < connectivity.Size(); i++)
330  {
331  const rapidjson::Value& local_data = connectivity[i];
332  unsigned int elem_id = local_data[0].GetInt();
333  unsigned int prop_id = local_data[1].GetInt();
334 
335  unsigned int nnodes = local_data.Size()-2;
336  std::vector< ModelPart::IndexType > node_ids(nnodes);
337  for(unsigned int j=0; j<nnodes; j++) node_ids[j]= local_data[j+2].GetInt() ;
338 
339  rThisModelPart.CreateNewElement(element_name, elem_id, node_ids, rThisModelPart.pGetProperties(prop_id) );
340  }
341  }
342  }
343 
344 
345 
346  //read Conditions
347  if(json_model_part.HasMember("Conditions"))
348  {
349  const rapidjson::Value& json_conditions = json_model_part["Conditions"]; // Using a reference for consecutive access is handy and faster.
350  if(json_conditions.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Conditions section is not a json object as expected!!","")
351  for (rapidjson::Value::ConstMemberIterator itr = json_conditions.MemberBegin(); itr != json_conditions.MemberEnd(); ++itr)
352  {
353  std::string condition_name = itr->name.GetString();
354  if(itr->value["connectivity"].IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the connectivity section of the Conditions is not an array as expected!!","")
355 
356  const rapidjson::Value& connectivity = itr->value["connectivity"];
357  for (SizeType i = 0; i < connectivity.Size(); i++)
358  {
359  const rapidjson::Value& local_data = connectivity[i];
360  unsigned int elem_id = local_data[0].GetInt();
361  unsigned int prop_id = local_data[1].GetInt();
362 
363  unsigned int nnodes = local_data.Size()-2;
364  std::vector< ModelPart::IndexType > node_ids(nnodes);
365  for(unsigned int j=0; j<nnodes; j++) node_ids[j]= local_data[j+2].GetInt() ;
366 
367  rThisModelPart.CreateNewCondition(condition_name, elem_id, node_ids, rThisModelPart.pGetProperties(prop_id) );
368  }
369  }
370  }
371 
372  //read Meshes
373  if(json_model_part.HasMember("Meshes"))
374  {
375  const rapidjson::Value& json_meshes = json_model_part["Meshes"]; // Using a reference for consecutive access is handy and faster.
376  if(json_meshes.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Meshes section is not a json object as expected!!","")
377  for (rapidjson::Value::ConstMemberIterator itr = json_meshes.MemberBegin(); itr != json_meshes.MemberEnd(); ++itr)
378  {
379  //get the mesh
380  unsigned int mesh_id = atoi( itr->name.GetString() );
381 
382  //TODO: this is UGLY - there should be a better way to create a mesh
383  unsigned int number_of_meshes = rThisModelPart.GetMeshes().size();
384  ModelPart::MeshType empty_mesh;
385  for(ModelPart::IndexType i = number_of_meshes ; i < mesh_id + 1 ; i++)
386  rThisModelPart.GetMeshes().push_back(empty_mesh.Clone());
387 
388  ModelPart::MeshType& mesh = rThisModelPart.GetMesh(mesh_id);
389 
390  if((itr->value).HasMember("NodePointers"))
391  {
392  const rapidjson::Value& json_nodepointers = (itr->value)["NodePointers"];
393  if(json_nodepointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the NodePointers section is not a json array as expected!!","")
394  mesh.Nodes().reserve( json_nodepointers.Size() );
395  KRATOS_WATCH( json_nodepointers.Size() )
396  for(rapidjson::SizeType i=0; i<json_nodepointers.Size(); i++) mesh.Nodes().push_back( rThisModelPart.pGetNode( json_nodepointers[i].GetInt() ));
397  }
398 
399  if((itr->value).HasMember("ElementPointers"))
400  {
401  const rapidjson::Value& json_elementpointers = (itr->value)["ElementPointers"];
402  if(json_elementpointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the ElementPointers section is not a json array as expected!!","")
403  mesh.Elements().reserve( json_elementpointers.Size() );
404  for(rapidjson::SizeType i=0; i<json_elementpointers.Size(); i++) mesh.Elements().push_back( rThisModelPart.pGetElement( json_elementpointers[i].GetInt() ));
405  }
406 
407  if((itr->value).HasMember("ConditionPointers"))
408  {
409  const rapidjson::Value& json_conditionpointers = (itr->value)["ConditionPointers"];
410  if(json_conditionpointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the ConditionPointers section is not a json array as expected!!","")
411  mesh.Conditions().reserve( json_conditionpointers.Size() );
412  for(rapidjson::SizeType i=0; i<json_conditionpointers.Size(); i++) mesh.Conditions().push_back( rThisModelPart.pGetCondition( json_conditionpointers[i].GetInt() ));
413  }
414 
415  mesh.Nodes().Unique();
416  mesh.Elements().Unique();
417  mesh.Conditions().Unique();
418  }
419 
420 
421  }
422 
423 
424  //read NodalValues
425  if(json_model_part.HasMember("NodalData"))
426  {
427  {
428  const rapidjson::Value& json_nodaldata = json_model_part["NodalData"];
429  if(json_nodaldata.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the NodalData section is not a json object as expected!!","")
430  for (rapidjson::Value::ConstMemberIterator itr = json_nodaldata.MemberBegin(); itr != json_nodaldata.MemberEnd(); ++itr)
431  {
432  std::string variable_name = itr->name.GetString();
433 
434  if(itr->value.HasMember("stepindex") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection stepindex of the NodalData subsection of model_part","");
435  unsigned int stepindex = itr->value["stepindex"].GetInt();
436 
437  if(itr->value.HasMember("data") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection data of the NodalData subsection of model_part","");
438  const rapidjson::Value& json_nodaldata = itr->value["data"];
439 
440 // for (rapidjson::Value::ConstMemberIterator iii = json_nodaldata.MemberBegin(); iii != json_nodaldata.MemberEnd(); ++iii)
441 // {
442 // for(SizeType i = 0; i<json_nodaldata.Size(); i++)
443  {
444  if( CheckAndAssignNodalValue< Variable<double> >( rThisModelPart, variable_name, json_nodaldata, stepindex) ) {}
445  else if( CheckAndAssignNodalValue_ErrorIfFixed< Variable<bool> >( rThisModelPart, variable_name, json_nodaldata,stepindex) ) {}
446  else if( CheckAndAssignNodalValue_ErrorIfFixed< Variable<int> >( rThisModelPart, variable_name, json_nodaldata,stepindex) ) {}
447  else if( CheckAndAssignNodalValue_ErrorIfFixed< Variable<unsigned int> >( rThisModelPart, variable_name, json_nodaldata,stepindex) ) {}
448  else
449  {
450  typedef VariableComponent< VectorComponentAdaptor<array_1d<double, 3> > > component_type;
451  if( CheckAndAssignNodalComponentValue< component_type >( rThisModelPart, variable_name, json_nodaldata,stepindex) ) {}
452  else
453  {
454  KRATOS_THROW_ERROR(std::invalid_argument, "can not read a variable type from the property block -- variable is ",variable_name);
455  }
456  }
457  }
458  }
459  }
460  }
461 
462 
463  //read ElementalValues
464 
465  //read ElementalConditions
466 
467  KRATOS_CATCH("")
468  }
469 
470 
471 
472 
473  //this function changes the json data structure -- completely independent from the kratos data structure
474  void EnforceConsecutiveOrdering(rapidjson::Document& d)
475  {
476 
477  if(d["model_part"].HasMember("Nodes") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection Nodes of the model_part","");
478 
479  rapidjson::Value& json_nodes = d["model_part"]["Nodes"]; // Using a reference for consecutive access is handy and faster.
480  if(json_nodes.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Nodes section is not an array as expected!!","")
481 
482 
483 
484  //first ensure that nodes are ordered consecutively
486  std::set<std::size_t> original_ids_set; //create a list of nodes -- Create New node uses insert hence it is slow if input is not ordered
487  for (rapidjson::SizeType i = 0; i < json_nodes.Size(); i++) // rapidjson uses SizeType instead of size_t.
488  {
489  std::size_t node_id = json_nodes[i][0].GetInt();
490  original_ids_set.insert( node_id ); //store in the map the position in the array
491  }
492 
493  //generate a map assigning to every original id a new consecutive_id
494  std::map < std::size_t, std::size_t > consecutive_nodeids_map;
495  std::size_t consecutive_id = 1;
496  for(std::set<std::size_t>::iterator it = original_ids_set.begin(); it!= original_ids_set.end(); it++)
497  {
498  consecutive_nodeids_map[ *it ] = consecutive_id;
499  consecutive_id++;
500  }
501 
503  //now order consecutively elements - reuse original_ids_set since it is not needed anymore - note that it requires two loops!
505  original_ids_set.clear();
506  if(d["model_part"].HasMember("Elements"))
507  {
508  rapidjson::Value& json_elements = d["model_part"]["Elements"]; // Using a reference for consecutive access is handy and faster.
509  if(json_elements.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Elements section is not a json object as expected!!","");
510  for (rapidjson::Value::MemberIterator itr = json_elements.MemberBegin(); itr != json_elements.MemberEnd(); ++itr)
511  {
512 
513  rapidjson::Value& connectivity = itr->value["connectivity"];
514 
515  for (SizeType i = 0; i < connectivity.Size(); i++)
516  {
517  rapidjson::Value& local_data = connectivity[i];
518  std::size_t original_id = local_data[0].GetInt();
519  original_ids_set.insert(original_id);
520  }
521  }
522  }
523  std::map < std::size_t, std::size_t > consecutive_elementids_map;
524  consecutive_id = 1;
525  for(std::set<std::size_t>::iterator it = original_ids_set.begin(); it!= original_ids_set.end(); it++)
526  {
527  consecutive_elementids_map[ *it ] = consecutive_id;
528  consecutive_id++;
529  }
530 
532  //finally order consecutively conditions - reuse original_ids_set since it is not needed anymore - note that it requires two loops!
534  original_ids_set.clear();
535  if(d["model_part"].HasMember("Conditions"))
536  {
537  rapidjson::Value& json_conditions = d["model_part"]["Conditions"]; // Using a reference for consecutive access is handy and faster.
538  if(json_conditions.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Conditions section is not a json object as expected!!","");
539  for (rapidjson::Value::MemberIterator itr = json_conditions.MemberBegin(); itr != json_conditions.MemberEnd(); ++itr)
540  {
541 
542  rapidjson::Value& connectivity = itr->value["connectivity"];
543 
544  for (SizeType i = 0; i < connectivity.Size(); i++)
545  {
546  rapidjson::Value& local_data = connectivity[i];
547  std::size_t original_id = local_data[0].GetInt();
548  original_ids_set.insert(original_id);
549  }
550  }
551  }
552  std::map < std::size_t, std::size_t > consecutive_conditionids_map;
553  consecutive_id = 1;
554  for(std::set<std::size_t>::iterator it = original_ids_set.begin(); it!= original_ids_set.end(); it++)
555  {
556  consecutive_conditionids_map[ *it ] = consecutive_id;
557  consecutive_id++;
558  }
559 
560 
561  //now modify the json data inplace for the new consecutive_nodeids_map
562  for(rapidjson::SizeType i = 0; i < json_nodes.Size(); i++) // rapidjson uses SizeType instead of size_t.
563  {
564  json_nodes[i][0].SetInt( consecutive_nodeids_map[ json_nodes[i][0].GetInt() ] );
565  }
566 
567  if(d["model_part"].HasMember("NodalData"))
568  {
569  {
570  rapidjson::Value& json_nodaldata = d["model_part"]["NodalData"];
571  if(json_nodaldata.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the NodalData section is not a json object as expected!!","")
572  for (rapidjson::Value::MemberIterator itr = json_nodaldata.MemberBegin(); itr != json_nodaldata.MemberEnd(); ++itr)
573  {
574  if(itr->value.HasMember("data") == false) KRATOS_THROW_ERROR(std::invalid_argument, "missing the subsection data of the NodalData subsection of model_part","");
575  rapidjson::Value& json_nodaldata = itr->value["data"];
576 
577  for(rapidjson::SizeType i = 0; i<json_nodaldata.Size(); i++)
578  {
579  rapidjson::Value& value = json_nodaldata[i];
580 
581  value[0].SetInt( consecutive_nodeids_map[ value[0].GetInt() ] );
582  }
583  }
584  }
585  }
586 
587 
588  //loop over node pointers
589  if(d["model_part"].HasMember("Meshes"))
590  {
591  rapidjson::Value& json_meshes = d["model_part"]["Meshes"]; // Using a reference for consecutive access is handy and faster.
592  if(json_meshes.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Meshes section is not a json object as expected!!","")
593  for (rapidjson::Value::MemberIterator itr = json_meshes.MemberBegin(); itr != json_meshes.MemberEnd(); ++itr)
594  {
595  //get the mesh
596  if((itr->value).HasMember("NodePointers"))
597  {
598  rapidjson::Value& json_nodepointers = (itr->value)["NodePointers"];
599  if(json_nodepointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the NodePointers section is not a json array as expected!!","")
600  for(rapidjson::SizeType i=0; i<json_nodepointers.Size(); i++) json_nodepointers[i].SetInt( consecutive_nodeids_map[ json_nodepointers[i].GetInt()] );
601  }
602 
603  if((itr->value).HasMember("ElementPointers"))
604  {
605  rapidjson::Value& json_elementpointers = (itr->value)["ElementPointers"];
606  if(json_elementpointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the ElementPointers section is not a json array as expected!!","")
607  for(rapidjson::SizeType i=0; i<json_elementpointers.Size(); i++) json_elementpointers[i].SetInt( consecutive_elementids_map[ json_elementpointers[i].GetInt()] );
608  }
609 
610  if((itr->value).HasMember("ConditionPointers"))
611  {
612  rapidjson::Value& json_conditionpointers = (itr->value)["ConditionPointers"];
613  if(json_conditionpointers.IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the ConditionPointers section is not a json array as expected!!","")
614  for(rapidjson::SizeType i=0; i<json_conditionpointers.Size(); i++) json_conditionpointers[i].SetInt( consecutive_elementids_map[ json_conditionpointers[i].GetInt()] );
615  }
616  }
617  }
618 
619  //loop over nodal data
620 
621 
622  //now loop over all Nodes Elements and Conditions and change the ids to the one contained in the values of nodes_id_map
623  if(d["model_part"].HasMember("Elements"))
624  {
625  rapidjson::Value& json_elements = d["model_part"]["Elements"]; // Using a reference for consecutive access is handy and faster.
626  if(json_elements.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Elements section is not a json object as expected!!","")
627  for (rapidjson::Value::MemberIterator itr = json_elements.MemberBegin(); itr != json_elements.MemberEnd(); ++itr)
628  {
629 
630  rapidjson::Value& connectivity = itr->value["connectivity"];
631 
632  for (SizeType i = 0; i < connectivity.Size(); i++)
633  {
634  rapidjson::Value& local_data = connectivity[i];
635  local_data[0].SetInt( consecutive_elementids_map[ local_data[0].GetInt() ]) ; //change the id of the element
636  unsigned int nnodes = local_data.Size()-2;
637  for(unsigned int j=0; j<nnodes; j++) local_data[j+2].SetInt( consecutive_nodeids_map[ local_data[j+2].GetInt() ]) ;
638 
639  }
640  }
641  }
642 
643  //now loop over all Nodes Elements and Conditions and change the ids to the one contained in the values of nodes_id_map
644  if(d["model_part"].HasMember("Conditions"))
645  {
646  rapidjson::Value& json_elements = d["model_part"]["Conditions"]; // Using a reference for consecutive access is handy and faster.
647  if(json_elements.IsObject() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the Conditions section is not a json object as expected!!","")
648  for (rapidjson::Value::MemberIterator itr = json_elements.MemberBegin(); itr != json_elements.MemberEnd(); ++itr)
649  {
650  std::string element_name = itr->name.GetString();
651  if(itr->value["connectivity"].IsArray() == false) KRATOS_THROW_ERROR(std::invalid_argument, "the connectivity section of the Conditions is not an array as expected!!","");
652 
653  rapidjson::Value& connectivity = itr->value["connectivity"];
654  for (SizeType i = 0; i < connectivity.Size(); i++)
655  {
656  rapidjson::Value& local_data = connectivity[i];
657  local_data[0].SetInt( consecutive_conditionids_map[ local_data[0].GetInt() ]) ; //change the id of the condition
658  unsigned int nnodes = local_data.Size()-2;
659  for(unsigned int j=0; j<nnodes; j++) local_data[j+2].SetInt( consecutive_nodeids_map[ local_data[j+2].GetInt() ]) ;
660 
661  }
662  }
663  }
664 
665 
666 
667  }
668 
669  template< class TVarType >
670  bool CheckAndAssignNodalValue( ModelPart& r_model_part, std::string variable_name, const rapidjson::Value& json_value, unsigned int stepindex)
671  {
672  if( KratosComponents< TVarType >::Has( variable_name ) )
673  {
674  const TVarType& rVar = KratosComponents< TVarType >::Get( variable_name );
675  if( r_model_part.GetNodalSolutionStepVariablesList().Has( rVar ) == false )
676  KRATOS_THROW_ERROR(std::runtime_error,"trying to assign a variable that is not in the model_part - variable name is ",variable_name);
677 
678  for(rapidjson::SizeType i = 0; i<json_value.Size(); i++) //loop over all the nodes with prescribed velocity
679  {
680  const rapidjson::Value& value = json_value[i];
681 
682  unsigned int node_id = value[0].GetInt();
683  bool is_fixed = static_cast<bool>(value[1].GetInt());
684  double double_value = value[2].GetDouble();
685 
686  ModelPart::NodesContainerType::iterator itnode = r_model_part.Nodes().find( node_id );
687  if(itnode == r_model_part.NodesEnd() ) KRATOS_THROW_ERROR(std::runtime_error, "node not found when assigning NodalData --> Node Id is ", node_id);
688 
689  itnode->GetSolutionStepValue(rVar, stepindex) = double_value;
690  if(is_fixed) itnode->Fix(rVar);
691  }
692 
693  return true;
694  }
695  return false; //variable was not of the given type and nothing was done
696  }
697 
698  template< class TVarType >
699  bool CheckAndAssignNodalComponentValue( ModelPart& r_model_part, std::string variable_name, const rapidjson::Value& json_value, unsigned int stepindex)
700  {
701  if( KratosComponents< TVarType >::Has( variable_name ) )
702  {
703  const TVarType& rVar = KratosComponents< TVarType >::Get( variable_name );
704 
705  const std::string base_variable_name = rVar.GetSourceVariable().Name();
706  const Variable<array_1d<double,3> >& rVectorBaseVar = KratosComponents< Variable<array_1d<double,3> > >::Get( base_variable_name );
707 
708  if( r_model_part.GetNodalSolutionStepVariablesList().Has( rVectorBaseVar ) == false )
709  KRATOS_THROW_ERROR(std::runtime_error,"trying to assign a variable that is not in the model_part - variable name is ",variable_name);
710 
711  for(rapidjson::SizeType i = 0; i<json_value.Size(); i++) //loop over all the nodes with prescribed velocity
712  {
713  const rapidjson::Value& value = json_value[i];
714 
715  unsigned int node_id = value[0].GetInt();
716  bool is_fixed = static_cast<bool>(value[1].GetInt());
717  double double_value = value[2].GetDouble();
718 
719  ModelPart::NodesContainerType::iterator itnode = r_model_part.Nodes().find( node_id );
720  if(itnode == r_model_part.NodesEnd() ) KRATOS_THROW_ERROR(std::runtime_error, "node not found when assigning NodalData --> Node Id is ", node_id);
721 
722  itnode->GetSolutionStepValue(rVar, stepindex) = double_value;
723  if(is_fixed) itnode->Fix(rVar);
724  }
725 
726  return true;
727  }
728  return false; //variable was not of the given type and nothing was done
729  }
730 
731  template< class TVarType >
732  bool CheckAndAssignNodalValue_ErrorIfFixed( ModelPart& r_model_part, std::string variable_name, const rapidjson::Value& json_value, unsigned int stepindex)
733  {
734  if( KratosComponents< TVarType >::Has( variable_name ) )
735  {
736  const TVarType& rVar = KratosComponents< TVarType >::Get( variable_name );
737  if( r_model_part.GetNodalSolutionStepVariablesList().Has( rVar ) == false )
738  KRATOS_THROW_ERROR(std::runtime_error,"trying to assign a variable that is not in the model_part - variable name is ",variable_name);
739 
740  for(rapidjson::SizeType i = 0; i<json_value.Size(); i++) //loop over all the nodes with prescribed velocity
741  {
742  const rapidjson::Value& value = json_value[i];
743 
744  unsigned int node_id = value[0].GetInt();
745  bool is_fixed = static_cast<bool>(value[1].GetInt());
746  double double_value = value[2].GetDouble();
747 
748  ModelPart::NodesContainerType::iterator itnode = r_model_part.Nodes().find( node_id );
749  if(itnode == r_model_part.NodesEnd() ) KRATOS_THROW_ERROR(std::runtime_error, "node not found when assigning NodalData --> Node Id is ", node_id);
750 
751  itnode->GetSolutionStepValue(rVar, stepindex) = double_value;
752  if(is_fixed)
753  {
754  std::string err_msg = std::string("variable name is ")+variable_name; //+std::string(" node Id is ") + std::string(itoa(node_id);
755  KRATOS_THROW_ERROR(std::runtime_error, "sorry this variable is not of double or component type and can not be fixed -- ", err_msg);
756  }
757  }
758 
759  return true;
760  }
761  return false; //variable was not of the given type and nothing was done
762  }
763 
764 
765  template< class TVarType >
766  bool CheckAndAssignPropertyValue( Properties::Pointer pProperty, std::string variable_name, const rapidjson::Value& json_value)
767  {
768  if( KratosComponents< TVarType >::Has( variable_name ) )
769  {
770  const TVarType& rVar = KratosComponents< TVarType >::Get( variable_name );
771  pProperty->GetValue( rVar ) = json_value.GetDouble();
772  return true;
773  }
774  return false; //variable was not of the given type and nothing was done
775  }
776 
777 
781 
782 
786 
787 
791 
794 
796  KratosJsonIO(KratosJsonIO const& rOther);
797 
798 
800 
801 }; // Class KratosJsonIO
802 
804 
807 
808 
812 
813 
814 // /// input stream function
815 // inline std::istream& operator >> (std::istream& rIStream,
816 // KratosJsonIO& rThis);
817 
818 // /// output stream function
819 // inline std::ostream& operator << (std::ostream& rOStream,
820 // const KratosJsonIO& rThis)
821 // {
822 // rThis.PrintInfo(rOStream);
823 // rOStream << std::endl;
824 // rThis.PrintData(rOStream);
825 
826 // return rOStream;
827 // }
828 // ///@}
829 
830 
831 } // namespace Kratos.
832 
833 #endif // KRATOS_JSON_IO_H_INCLUDED defined
Definition: flags.h:58
IO provides different implementation of input output procedures which can be used to read and write w...
Definition: io.h:58
std::vector< std::vector< std::size_t > > ConnectivitiesContainerType
Definition: io.h:91
std::size_t SizeType
Definition: io.h:97
KratosComponents class encapsulates a lookup table for a family of classes in a generic way.
Definition: kratos_components.h:49
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
An IO class for reading and writing a modelpart.
Definition: json_io.h:75
bool CheckAndAssignPropertyValue(Properties::Pointer pProperty, std::string variable_name, const rapidjson::Value &json_value)
Definition: json_io.h:766
BaseType::MeshType MeshType
Definition: json_io.h:87
BaseType::ConnectivitiesContainerType ConnectivitiesContainerType
Definition: json_io.h:97
bool CheckAndAssignNodalComponentValue(ModelPart &r_model_part, std::string variable_name, const rapidjson::Value &json_value, unsigned int stepindex)
Definition: json_io.h:699
bool CheckAndAssignNodalValue_ErrorIfFixed(ModelPart &r_model_part, std::string variable_name, const rapidjson::Value &json_value, unsigned int stepindex)
Definition: json_io.h:732
IO BaseType
Definition: json_io.h:83
KratosJsonIO(std::string const &Filename, const Flags Options=IO::READ|IO::IGNORE_VARIABLES_ERROR.AsFalse())
Constructor with filenames.
Definition: json_io.h:108
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: json_io.h:179
KRATOS_CLASS_POINTER_DEFINITION(KratosJsonIO)
Pointer definition of KratosJsonIO.
BaseType::NodeType NodeType
Definition: json_io.h:85
virtual void ReadModelPart(ModelPart &rThisModelPart)
This method reads the model part.
Definition: json_io.h:117
void FillModelPart(rapidjson::Document &d, ModelPart &rThisModelPart)
Definition: json_io.h:255
virtual ~KratosJsonIO()
Constructor with filenames.
Definition: json_io.h:144
std::string mFilename
Definition: json_io.h:243
bool CheckAndAssignNodalValue(ModelPart &r_model_part, std::string variable_name, const rapidjson::Value &json_value, unsigned int stepindex)
Definition: json_io.h:670
Flags mOptions
Definition: json_io.h:244
BaseType::PropertiesContainerType PropertiesContainerType
Definition: json_io.h:91
std::size_t SizeType
Definition: json_io.h:101
BaseType::NodesContainerType NodesContainerType
Definition: json_io.h:89
KratosJsonIO(KratosJsonIO const &rOther)
Copy constructor.
KratosJsonIO & operator=(KratosJsonIO const &rOther)
Assignment operator.
BaseType::ElementsContainerType ElementsContainerType
Definition: json_io.h:93
virtual std::string Info() const
Turn back information as a string.
Definition: json_io.h:173
void EnforceConsecutiveOrdering(rapidjson::Document &d)
Definition: json_io.h:474
BaseType::ConditionsContainerType ConditionsContainerType
Definition: json_io.h:95
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: json_io.h:186
Mesh is the second level of abstraction in the data structure which hold Nodes, Elements and Conditio...
Definition: mesh.h:69
Mesh Clone()
Definition: mesh.h:207
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
PropertiesType::Pointer pGetProperties(IndexType PropertiesId, IndexType MeshIndex=0)
Returns the Properties::Pointer corresponding to it's identifier.
Definition: model_part.cpp:664
NodeType::Pointer CreateNewNode(int Id, double x, double y, double z, VariablesList::Pointer pNewVariablesList, IndexType ThisIndex=0)
Definition: model_part.cpp:270
ElementType::Pointer CreateNewElement(std::string ElementName, IndexType Id, std::vector< IndexType > ElementNodeIds, PropertiesType::Pointer pProperties, IndexType ThisIndex=0)
Creates new element with a node ids list.
std::size_t IndexType
Pointer definition of ModelPart.
Definition: model_part.h:105
ElementType::Pointer pGetElement(IndexType ElementId, IndexType ThisIndex=0)
Definition: model_part.h:1109
ConditionType::Pointer pGetCondition(IndexType ConditionId, IndexType ThisIndex=0)
Definition: model_part.h:1301
ConditionType::Pointer CreateNewCondition(std::string ConditionName, IndexType Id, std::vector< IndexType > ConditionNodeIds, PropertiesType::Pointer pProperties, IndexType ThisIndex=0)
Creates new condition with a node ids list.
MeshesContainerType & GetMeshes()
Definition: model_part.h:1801
NodeType::Pointer pGetNode(IndexType NodeId, IndexType ThisIndex=0)
Definition: model_part.h:421
NodeIterator NodesEnd(IndexType ThisIndex=0)
Definition: model_part.h:497
MeshType & GetMesh(IndexType ThisIndex=0)
Definition: model_part.h:1791
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
VariablesList & GetNodalSolutionStepVariablesList()
Definition: model_part.h:549
This class defines the node.
Definition: node.h:65
size_type size() const
Definition: pointer_vector.h:255
void push_back(const TPointerType &x)
Definition: pointer_vector.h:270
A sorted associative container similar to an STL set, but uses a vector to store pointers to its data...
Definition: pointer_vector_set.h:72
iterator find(const key_type &Key)
Find an element with the specified key.
Definition: pointer_vector_set.h:678
bool Has(const VariableData &rThisVariable) const
Definition: variables_list.h:372
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_WATCH(variable)
Definition: define.h:806
#define KRATOS_TRY
Definition: define.h:109
std::size_t SizeType
Definition: nurbs_utilities.h:41
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
string err_msg
Definition: fluid_chimera_analysis.py:21
int d
Definition: ode_solve.py:397
int j
Definition: quadrature.py:648
int elem_id
Definition: read_stl.py:13
int node_id
Definition: read_stl.py:12
mesh
Definition: read_stl.py:7
int nnodes
Definition: sensitivityMatrix.py:24
integer i
Definition: TensorModule.f:17