G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsStructuralAnalysisUtils.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 
17 namespace gismo
18 {
19 
20 template<class T>
21 class gsStructuralAnalysisOutput
22 {
23 public:
24  gsStructuralAnalysisOutput(const std::string name)
25  :
26  m_fname(name),
27  m_precision(5),
28  m_nPointHeaders(0),
29  m_nOtherHeaders(0)
30  {
31 
32  }
33 
34  gsStructuralAnalysisOutput(const std::string name, const gsMatrix<T> & points)
35  :
36  gsStructuralAnalysisOutput(name)
37  {
38  m_points = points;
39  }
40 
41  void clean()
42  {
43  std::ofstream file;
44  file.open(m_fname, std::ofstream::out | std::ofstream::trunc);
45  file.close();
46  }
47 
48  void _initPointHeader(const std::string name, std::vector<std::string> pointHeaders)
49  {
50  std::ofstream file;
51  file.open(name,std::ofstream::out | std::ofstream::app);
52  for (index_t p=0; p!=m_points.cols(); p++)
53  for (size_t h=0; h!=pointHeaders.size(); h++)
54  file<< "point"<<p<<"-"<<pointHeaders[h]<< ",";
55  file.close();
56  }
57 
58  void _initOtherHeader(const std::string name, std::vector<std::string> otherHeaders)
59  {
60  std::ofstream file;
61  file.open(name,std::ofstream::out | std::ofstream::app);
62  for (size_t h=0; h!=otherHeaders.size(); h++)
63  file<<otherHeaders[h]<< ",";
64  file.close();
65  }
66 
67  void _writePointData(const std::string name, const gsMatrix<T> & pointData)
68  {
69  std::ofstream file;
70  file.open(name,std::ofstream::out | std::ofstream::app);
71  for (index_t p=0; p!=pointData.cols(); p++)
72  for (index_t c=0; c!=pointData.rows(); c++)
73  file << pointData(c,p) << ",";
74  file.close();
75  }
76 
77  void _writeOtherData(const std::string name, const gsVector<T> & otherData)
78  {
79  std::ofstream file;
80  file.open(name,std::ofstream::out | std::ofstream::app);
81  for (index_t c=0; c!=otherData.size(); c++)
82  file << otherData.at(c) << ",";
83  file.close();
84  }
85 
86  void writePointCoords(const std::string name)
87  {
88  std::ofstream file;
89  file.open(name,std::ofstream::out);
90  file << std::setprecision(m_precision);
91  _initPointHeader(file);
92 
93  for (index_t j=0; j!=m_points.cols(); j++)
94  for (index_t i=0; i!=m_points.rows(); i++)
95  file<<m_points(i,j)<<",";
96  file.close();
97  }
98 
99  void init(std::vector<std::string> pointHeaders, std::vector<std::string> otherHeaders)
100  {
101  m_nPointHeaders = pointHeaders.size();
102  m_nOtherHeaders = otherHeaders.size();
103  std::ofstream file;
104  file.open(m_fname,std::ofstream::out);
105  file << std::setprecision(m_precision);
106  file.close();
107 
108  this->_initPointHeader(m_fname, pointHeaders);
109  this->_initOtherHeader(m_fname, otherHeaders);
110 
111  file.open(m_fname,std::ofstream::out | std::ofstream::app);
112  file << "\n";
113  file.close();
114  }
115 
116  void init(std::vector<std::string> pointHeaders)
117  {
118  std::vector<std::string> empty;
119  this->init(pointHeaders,empty);
120  }
121 
122  void add(const gsMatrix<T> & pointSolutions, const gsVector<T> & otherData)
123  {
124  GISMO_ASSERT(pointSolutions.rows()==m_nPointHeaders,"Number of solutions per point is different from the defined number of headers. "<<pointSolutions.rows()<<" = pointSolutions.rows()==m_nPointHeaders = "<<m_nPointHeaders);
125  GISMO_ASSERT(otherData.size()==m_nOtherHeaders,"Number of other data is different from the defined number of headers");
126  std::ofstream file;
127  file.open(m_fname,std::ofstream::out | std::ofstream::app);
128  file << std::setprecision(m_precision);
129  file.close();
130 
131  this->_writePointData(m_fname,pointSolutions);
132  this->_writeOtherData(m_fname,otherData);
133 
134  file.open(m_fname,std::ofstream::out | std::ofstream::app);
135  file << "\n";
136  file.close();
137  }
138 
139 
140  void add(const gsMatrix<T> & pointSolutions)
141  {
142  gsVector<T> empty;
143  this->add(pointSolutions,empty);
144  }
145 
146  void setPrecision(index_t precision) { m_precision = precision; }
147 
148 protected:
149  gsMatrix<T> m_points;
150  std::string m_fname;
151  std::ofstream m_file;
152  index_t m_precision;
153  index_t m_nPointHeaders, m_nOtherHeaders;
154 
155 };
156 
157 } // namespace gismo
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89