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.
io_utilities.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: Baumgärtner Daniel, https://github.com/dbaumgaertner
11 // Octaviano Malfavón Farías
12 // Eric Gonzales
13 // Philipp Hofer
14 // Erich Wehrle
15 //
16 // ==============================================================================
17 
18 #if !defined(KRATOS_IO_UTILITIES_H_INCLUDED)
19 #define KRATOS_IO_UTILITIES_H_INCLUDED
20 
21 // System includes
22 
23 // External includes
24 
25 // Project includes
26 
27 
28 namespace Kratos
29 {
30 
33 
37 
38 
42 
46 
50 
52 
57 {
58 public:
59 
62 
65 
69 
72  {
73  }
74 
76  virtual ~IOUtilities()
77  {
78  }
79 
83 
84 
88 
89  // ---------------------------------------------------------------------------------------------------------------------------------------------
90  // --------------------------------- Save optimization results in a restart file (mdpa) --------------------------------------------------------
91  // ---------------------------------------------------------------------------------------------------------------------------------------------
92 
93  void SaveOptimizationResults( const char* RestartInputFile, ModelPart& rModelPart, const char* RestartOutputFile )
94  {
95  KRATOS_TRY;
96 
97  KRATOS_INFO("[TopOpt]") << "::[Saving optimization results as restart file]::"<< std::endl;
98 
99  // Create an empty .mdpa restart file
100  std::ofstream FileToBeCreated;
101  FileToBeCreated.open(RestartOutputFile, std::ios::trunc);
102 
103  // Read the original .mdpa file
104  std::ifstream FileToBeRead(RestartInputFile);
105  if(!FileToBeRead.is_open())
106  KRATOS_THROW_ERROR(std::invalid_argument, "Specified restart input file does not exist: ",RestartInputFile);
107 
108  // Write the given input file except the block covering X_PHYS (this is to be replaced by the current optimization results)
109  bool DensityBlockActive = false;
110  std::string LineString;
111  while (std::getline(FileToBeRead, LineString))
112  {
113  if(LineString.find("Begin ElementalData X_PHYS")!=std::string::npos)
114  DensityBlockActive = true;
115  else if(LineString.find("End ElementalData")!=std::string::npos and DensityBlockActive)
116  {
117  DensityBlockActive = false;
118  continue;
119  }
120  else if(DensityBlockActive == false)
121  FileToBeCreated << LineString << "\n";
122  }
123 
124  // Write the actual X_PHYS elemental data
125  FileToBeCreated << "\nBegin ElementalData X_PHYS\n";
126  for(ModelPart::ElementsContainerType::iterator elem_i = rModelPart.ElementsBegin(); elem_i!=rModelPart.ElementsEnd(); elem_i++)
127  {
128  FileToBeCreated << " " << elem_i->Id() << " " << elem_i->GetValue(X_PHYS) << "\n";
129  }
130  FileToBeCreated << "End ElementalData\n";
131 
132  // Close files
133  FileToBeCreated.close();
134  FileToBeRead.close();
135 
136  KRATOS_INFO("[TopOpt]") <<" Restart File succesfully generated under the name " << RestartOutputFile <<std::endl;
137 
138  KRATOS_CATCH("");
139  }
140 
141  // ---------------------------------------------------------------------------------------------------------------------------------------------
142  // --------------------------------- WRITE STL FILE FROM SURFACE MESH -------------------------------------------------------------------------
143  // ---------------------------------------------------------------------------------------------------------------------------------------------
144 
146  void WriteSurfaceAsSTLFile(const char* file_name, ModelPart& rSurfaceModelPart)
147  {
148  KRATOS_TRY;
149 
150  KRATOS_INFO("[TopOpt]") <<"\n::[Generating STL file]::"<<std::endl;
151 
152  // Write stl of surface model part
153  std::ofstream myfile;
154  myfile.open (file_name);
155  myfile << "solid Layer0\n";
156  for ( ModelPart::ConditionIterator cond_i = rSurfaceModelPart.ConditionsBegin(); cond_i != rSurfaceModelPart.ConditionsEnd(); ++cond_i )
157  {
158  array_1d<double,3> area_normal = cond_i->GetValue(NORMAL);
159  myfile << " facet normal " << area_normal[0] <<" " << area_normal[1] << " " << area_normal[2] <<"\n";
160  myfile << " outer loop\n";
161 
162  Element::GeometryType& rNodes = cond_i->GetGeometry();
163  if (rNodes.size()==3)
164  {
165  for(unsigned int i = 0; i<rNodes.size(); i++)
166  myfile << " vertex "<< rNodes[i].X() <<" " << rNodes[i].Y() << " " << rNodes[i].Z() <<"\n";
167 
168  myfile << " end loop\n";
169  myfile << " end facet\n";
170  }
171  else
172  {
173  KRATOS_ERROR << "The number of nodes is: "<< rNodes.size() << ". This is not feasible for STL files!"<< std::endl;
174  }
175 
176  }
177  myfile << "endsolid Layer0\n";
178  myfile.close();
179 
180  KRATOS_INFO("[TopOpt]") <<" STL File succesfully generated under the name " << file_name <<std::endl;
181 
182  KRATOS_CATCH("");
183  }
184 
188 
189 
193 
194 
198 
200  virtual std::string Info() const
201  {
202  return "IOUtilities";
203  }
204 
206  virtual void PrintInfo(std::ostream& rOStream) const
207  {
208  rOStream << "IOUtilities";
209  }
210 
212  virtual void PrintData(std::ostream& rOStream) const
213  {
214  }
215 
216 
220 
221 
223 
224 protected:
227 
228 
232 
233 
237 
238 
242 
243 
247 
248 
252 
253 
257 
258 
260 
261 private:
264 
265 
269 
273 
274 
278 
279 
283 
284 
288 
289 
293 
295  //IOUtilities& operator=(IOUtilities const& rOther);
296 
298  //IOUtilities(IOUtilities const& rOther);
299 
300 
302 
303 }; // Class IOUtilities
304 
306 
309 
310 
314 
316 
317 
318 } // namespace Kratos.
319 
320 #endif /* KRATOS_IO_UTILITIES_H_INCLUDED */
Geometry base class.
Definition: geometry.h:71
SizeType size() const
Definition: geometry.h:518
Solution utility to filter results.
Definition: io_utilities.h:57
virtual ~IOUtilities()
Destructor.
Definition: io_utilities.h:76
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: io_utilities.h:206
IOUtilities()
Default constructor.
Definition: io_utilities.h:71
virtual std::string Info() const
Turn back information as a string.
Definition: io_utilities.h:200
void SaveOptimizationResults(const char *RestartInputFile, ModelPart &rModelPart, const char *RestartOutputFile)
Definition: io_utilities.h:93
void WriteSurfaceAsSTLFile(const char *file_name, ModelPart &rSurfaceModelPart)
Generates a .STL file from a a provided surface mesh.
Definition: io_utilities.h:146
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: io_utilities.h:212
KRATOS_CLASS_POINTER_DEFINITION(IOUtilities)
Pointer definition of IOUtilities.
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ElementIterator ElementsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1169
ConditionIterator ConditionsBegin(IndexType ThisIndex=0)
Definition: model_part.h:1361
ElementIterator ElementsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1179
MeshType::ConditionIterator ConditionIterator
Definition: model_part.h:189
ConditionIterator ConditionsEnd(IndexType ThisIndex=0)
Definition: model_part.h:1371
#define KRATOS_THROW_ERROR(ExceptionType, ErrorMessage, MoreInfo)
Definition: define.h:77
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_INFO(label)
Definition: logger.h:250
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
string file_name
Definition: sp_statistics.py:6
integer i
Definition: TensorModule.f:17