Geomi
DiscSyst.hpp
1 #ifndef DEF_COMMON_DISCSYST
2 #define DEF_COMMON_DISCSYST
3 
4 #include <iostream>
5 #include <fstream>
6 #include <cmath>
7 #include <vector>
8 
9 //#include "Common_Syst.hpp"
10 
17 template <typename T_M, typename T_Q>
18 class DiscSyst
19 {
20 protected:
21  std::vector<Syst<T_M,T_Q>> m_node;
22 
23  int
24  __write2csv__ (const std::string filename, std::ios_base::openmode mode, const std::string sep)
25  {
26  std::ofstream file;
27  file.open(filename,mode);
28  if (file.is_open()) {
29  for (int i=0; i<size(); i++) {
30  file << m_node[i].csvString(sep) << std::endl;
31  }
32  file.close();
33  return 0;
34  } else {
35  return 1;
36  }
37  }
38 
39 public:
41  { }
42 
43  Syst<T_M,T_Q> const&
44  operator[] (size_t index) const
45  {
46  if (index<0 || index>=m_node.size()) {
47  throw std::out_of_range("Error : DiscSyst[] index out of range");
48  }
49  return m_node[index];
50  }
51 
53  operator[] (size_t index)
54  {
55  if (index<0 || index>=m_node.size()) {
56  throw std::out_of_range("Error : DiscSyst[] index out of range");
57  }
58  return m_node[index];
59  }
60 
61  size_t
62  size() const
63  { return m_node.size(); }
64 
65  /* Accessors and setters */
66 
67  T_M
68  base (const size_t& i) const
69  { return m_node[i].base(); }
70 
71  T_Q
72  pos (const size_t& i) const
73  { return m_node[i].pos(); }
74 
75  void
76  base (const size_t& i, const T_M& b)
77  { m_node[i].base(b); }
78 
79  void
80  pos (const size_t& i, const T_Q& p)
81  { m_node[i].pos(p); }
82 
83  static const unsigned int
84  dof ( )
85  { return T_Q::DOF; }
86 
92  void
93  baselinspace (const T_M& inf_lim, const T_M& sup_lim, const size_t& n_steps)
94  {
95  if (n_steps<0)
96  throw std::domain_error("Error : DiscSyst.linspace size argument must be null or positive");
97  m_node = std::vector<Syst<T_M,T_Q>>();
98  if (n_steps>0) {
99  float fn_steps = (float)n_steps;
100  for (float i=0.0f; i<fn_steps; i+=1.0f) {
101  m_node.push_back(Syst<T_M,T_Q>(inf_lim + (sup_lim-inf_lim)*i/(fn_steps-1.0f)));
102  }
103  }
104  }
105 
111  void
112  baselinstep (T_M inf_lim, T_M step_size, size_t n_steps)
113  {
114  if (n_steps<0)
115  throw std::domain_error("Error : DiscSyst.linstep size argument must be null or positive");
116  m_node = std::vector<Syst<T_M,T_Q>>();
117  if (n_steps>0) {
118  for (int i=0; i<n_steps; i++) {
119  m_node.push_back(Syst<T_M,T_Q>(inf_lim+i*step_size));
120  }
121  }
122  }
123 
124  friend std::ostream&
125  operator<< (std::ostream& stream, const DiscSyst<T_M,T_Q>& ds)
126  {
127  for (int i=0; i<ds.size(); i++) {
128  stream << ds[i];
129  if (i!=ds.size())
130  stream << "\t";
131  else
132  stream << std::endl;
133  }
134  return stream;
135  }
136 
137  int
138  append2csv (const std::string filename, const std::string sep=",")
139  { return __write2csv__(filename,std::ios_base::app,sep); }
140 
141  int
142  write2csv (const std::string filename, const std::string sep=",")
143  { return __write2csv__(filename,std::ios_base::trunc,sep); }
144 };
145 
146 #endif
Definition: DiscSyst.hpp:18
Definition: Syst.hpp:12
void baselinspace(const T_M &inf_lim, const T_M &sup_lim, const size_t &n_steps)
Definition: DiscSyst.hpp:93
void baselinstep(T_M inf_lim, T_M step_size, size_t n_steps)
Definition: DiscSyst.hpp:112