MDStressLab++
Stress.h
Go to the documentation of this file.
1 /*
2  * Stress.h
3  *
4  * Created on: Nov 5, 2019
5  * Author: Nikhil
6  */
7 
8 #ifndef STRESS_H_
9 #define STRESS_H_
10 
11 #include <vector>
12 #include "Grid.h"
13 #include "typedef.h"
14 #include "SpatialHash.h"
15 #include "Method.h"
16 #include <string>
17 #include <fstream>
18 #include <iostream>
19 
20 template<typename TMethod,
21  StressType stressType,
22  typename TGrid = typename std::conditional<stressType==Piola,Grid<Reference>,Grid<Current>>::type>
23 class Stress {
24 public:
25  std::vector<Matrix3d> field;
26  TGrid* pgrid;
28  std::string name;
29 
30  Stress(std::string name,
31  const Method<TMethod>& method,
32  TGrid* pgrid): name(name),pgrid(pgrid),method(method)
33  {
34  field.resize(pgrid->ngrid);
35  for(auto& matrix : field)
36  matrix= Matrix3d::Zero();
37  }
38  Stress(const Method<TMethod>& method,
39  TGrid* pgrid): pgrid(pgrid),method(method)
40  {
41  field.resize(pgrid->ngrid);
42  for(auto& matrix : field)
43  matrix= Matrix3d::Zero();
44  }
45 
46  void write()
47  {
48  if (name.empty())
49  MY_ERROR("Stress object created without specifying a name. Use write(filename) instead of write()");
50  std::ofstream file(name+".stress");
51 
52  file << field.size() << "\n";
53  file << "\n";
54  int index= 0;
55  Eigen::IOFormat fmt(Eigen::FullPrecision, 0, " ", "\n", "", "", "");
56  for (auto& stress : field)
57  {
58  Eigen::Map<Eigen::Matrix<double,1,DIM*DIM>> stressRow(stress.data(), stress.size());
59  file << pgrid->coordinates[index].format(fmt) << std::setw(5) << stressRow.format(fmt) << std::endl;
60  index++;
61  }
62  }
63 
64  void write(const std::string& filename)
65  {
66  if (name.empty())
67  name= filename;
68  else
69  std::cout << "Stress object created with name " << name << ". Ignoring the filename: " << filename << "." << std::endl;
70  write();
71  }
72 
74  {
75  // TODO Auto-generated destructor stub
76  }
77 
78 
79 };
80 
81 #endif /* STRESS_H_ */
std::string name
Definition: Stress.h:28
~Stress()
Definition: Stress.h:73
StressType
Definition: typedef.h:63
TGrid * pgrid
Definition: Stress.h:26
Definition: Grid.h:31
void write()
Definition: Stress.h:46
Definition: Stress.h:23
const Method< TMethod > & method
Definition: Stress.h:27
std::vector< Matrix3d > field
Definition: Stress.h:25
Definition: Method.h:14
#define MY_ERROR(message)
Definition: typedef.h:17
Stress(std::string name, const Method< TMethod > &method, TGrid *pgrid)
Definition: Stress.h:30
void write(const std::string &filename)
Definition: Stress.h:64
Stress(const Method< TMethod > &method, TGrid *pgrid)
Definition: Stress.h:38