RockyML  0.0.1
A High-Performance Scientific Computing Framework
analysis.h
1 /*
2  Copyright (C) 2022 Amirabbas Asadi , All Rights Reserved
3  distributed under Apache-2.0 license
4 */
5 #ifndef ROCKY_ZAGROS_ANALYSIS
6 #define ROCKY_ZAGROS_ANALYSIS
7 
8 #include <fstream>
9 #include<sstream>
10 
11 #include <rocky/zagros/strategies/strategy.h>
12 
13 namespace rocky{
14 namespace zagros{
15 
22 template<typename T_e, int T_dim>
23 class analysis_strategy: public basic_strategy<T_e, T_dim>{};
24 
29 template<typename T_e, int T_dim>
30 class loss_projection_2d: public analysis_strategy<T_e, T_dim>{
31 protected:
32  // objective system
33  system<T_e>* problem_;
34  // output setting
35  std::string label_;
36  // loss boundaries
37  T_e x_min_;
38  T_e x_max_;
39  T_e y_min_;
40  T_e y_max_;
41  // image size
42  int width_;
43  int height_;
44  // resolution settings
45  double delta_x_;
46  double delta_y_;
47  int l_width_;
48  int l_height_;
49  // step
50  int step_;
51  // loss function values
52  std::vector<std::vector<T_e>> z;
53 
54 public:
55  loss_projection_2d(system<T_e>* problem, std::string label, int width, int height, T_e x_min, T_e y_min, T_e x_max, T_e y_max){
56  problem_ = problem;
57  label_ = label;
58  width_ = width;
59  height_ = height;
60  x_min_ = x_min;
61  y_min_ = y_min;
62  x_max_ = x_max;
63  y_max_ = y_max;
64  step_ = 0;
65  // obtain resolution settings
66  delta_x_ = (x_max_ - x_min_) / static_cast<double>(width_);
67  delta_y_ = (y_max_ - y_min_) / static_cast<double>(height_);
68  // real image size after possible numerical errors
69  l_width_ = (x_max_ - x_min_) / delta_x_ ;
70  l_height_ = (y_max_ - y_min_) / delta_y_;
71  // allocate memory for the mesh
72  z.resize(l_width_);
73  for(int x=0; x<l_width_; x++)
74  z[x].resize(l_height_);
75  }
76  void save_mesh(){
77  std::string path = fmt::format("zagros_{}_{}.data", label_, step_);
78  std::fstream handler(path, std::fstream::out);
79  // writing metadata
80  handler << step_ << std::endl;
81  handler << fmt::format("{} {} {} {}", x_min_, y_min_, x_max_, y_max_) << std::endl;
82  handler << fmt::format("{} {}", l_height_, l_width_) << std::endl;
83  // writing the mesh values
84  for(int x=0; x<l_width_; x++)
85  for(int y=0; y<l_height_; y++)
86  handler << z[x][y] << " ";
87  handler.close();
88  }
89  virtual void apply(){
90  // traverse the 2D mesh in parallel
91  tbb::parallel_for(0, this->l_width_, [&](auto x){
92  tbb::parallel_for(0, this->l_height_, [&](auto y){
93  T_e thread_point[2];
94  thread_point[0] = x * this->delta_x_ + this->x_min_;
95  thread_point[1] = y * this->delta_y_ + this->y_min_;
96  this->z[x][y] = this->problem_->objective(thread_point);
97  });
98  });
99  save_mesh();
100  step_++;
101  }
102 };
103 
105  std::string filename;
106  std::fstream log_output;
107  size_t step;
108  int dims;
109  bool initialized;
110 
111  container_analysis_handler(std::string filename){
112  this->filename = filename;
113  this->step = 0;
114  this->dims = -1;
115  this->initialized = false;
116  // openning the log file
117  this->open();
118  // write the headers
119  this->write_header();
120  }
121  void open(){
122  // create a specific filename for this rank
123  int mpi_rank;
124  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
125  std::string process_spc_filename = fmt::format("proc_{}_{}", mpi_rank, filename);
126  // initialize the output file
127  log_output.open(process_spc_filename, std::fstream::out);
128  }
129  std::fstream& stream(){
130  return log_output;
131  }
132  void write_header(){
133  this->stream() << "particle";
134  for(int i=0; i<dims; i++)
135  this->stream() << ",x" << std::to_string(i);
136  this->stream() << std::endl;
137  this->initialized = true;
138  }
139  virtual void save(){
140  if(this->log_output.is_open())
141  this->log_output.close();
142  }
143  virtual ~container_analysis_handler(){
144  this->save();
145  }
146 };
147 
148 template<typename T_e, int T_dim>
150 protected:
151  system<T_e>* problem_;
152  basic_scontainer<T_e, T_dim>* container_;
153  container_analysis_handler* handler_;
154 
155 public:
157  this->problem_ = problem;
158  this->container_ = container;
159  this->handler_ = handler;
160  }
161  virtual void apply(){
162  if (!(this->handler_->initialized)){
163  this->handler_->dims = T_dim;
164  this->handler_->write_header();
165  }
166  std::stringstream buffer;
167  for(int p=0; p<this->container_->n_particles(); p++){
168  buffer << p;
169  for(int d=0; d<T_dim; d++)
170  buffer << "," << this->container_->particles[p][d];
171  buffer << "\n";
172  }
173  this->handler_->stream() << buffer.str();
174  this->handler_->step++;
175  };
176 };
177 
178 
179 
180 };
181 };
182 
183 #endif
rocky::zagros::analysis_strategy
base class for strategies for analysing loss functions
Definition: analysis.h:23
rocky::zagros::loss_projection_2d
creating a 2d projection of loss function
Definition: analysis.h:30
rocky::zagros::basic_scontainer
a data container representing a scontainer
Definition: scontainer.h:31
rocky::zagros::system
Definition: system.h:20
rocky::zagros::basic_strategy
Interface for all strategies.
Definition: strategy.h:31
rocky::zagros::container_analysis_handler
Definition: analysis.h:104
rocky::zagros::container_position_recorder
Definition: analysis.h:149