ClumpInput.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
10 #ifndef CLUMP_INPUT_H
11 #define CLUMP_INPUT_H
12 
13 #include <algorithm>
14 #include <dirent.h>
15 #include <sys/types.h>
16 #include <iostream>
17 #include <fstream>
18 #include <vector>
19 #include <string>
20 #include <cmath>
21 #include <sstream>
22 #include "Math/Matrix.h"
23 #include <CMakeDefinitions.h>
24 
25 // Useful containers
26 typedef std::vector<double> DoubleVector;
27 typedef std::vector<DoubleVector> Double2DVector;
28 typedef std::vector<std::string> StringVector;
29 
30 // Helper function for alphabetical sorting of clump names
32 
33 // Helper function to generate a random double in range (0, MAX)
34 double RandomDouble(double Max)
35 {return Max*((double) rand() / (RAND_MAX));}
36 
41 struct ClumpData
42 {
43 public:
44 
45  std::string path; // Path to MClump working directory
46  StringVector clumpNames; // Array of names of Clumps that will be used
47  DoubleVector mass; // clump mass
48  Double2DVector pebblesX; // Pebbles geometry (outer index goes over Clumps, inner - over pebbles)
52 
53  Double2DVector toi; // Clump tensor of inertia (I11, I12, I13, I21..I33)
54  Double2DVector pd; // Clump principal directions v1, v2, v3
55 
56 };
57 
58 
64 {
65  // Path to MCLump tool
66  a.path = getMercuryDPMSourceDir() + "/Tools/MClump/Clumps/";
67 
68  struct dirent *entry;
69  DIR *dir = opendir(a.path.c_str());
70  while ((entry = readdir(dir)) != NULL) {
71  if (entry->d_type == DT_DIR){
72  a.clumpNames.push_back(entry->d_name);
73  }
74  }
75  closedir(dir);
76 
77  // remove "." and ".." from the list of dirs (position of those in the list is OS-sensitive, hence this code)
78 
79  std::sort(a.clumpNames.begin(), a.clumpNames.end(), CompareFunction);//sort the vector
80  a.clumpNames.erase(a.clumpNames.begin());
81  a.clumpNames.erase(a.clumpNames.begin());
82 
83  // Show the names of available Clumps
84  for (int i = 0; i<a.clumpNames.size(); i++) std::cout << a.clumpNames[i] << std::endl;
85 
86 }
87 
93 {
94  logger(INFO, "Loading clump pebbles...");
95 
96  a.pebblesX.resize(a.clumpNames.size());
97  a.pebblesY.resize(a.clumpNames.size());
98  a.pebblesZ.resize(a.clumpNames.size());
99  a.pebblesR.resize(a.clumpNames.size());
100 
101  for (int i = 0; i < a.clumpNames.size(); i++ ){
102  std::ifstream infile((a.path + a.clumpNames[i] + "/Clump/Clump.txt").c_str(), std::ios::in | std::ios::binary);
103  std::string line{};
104  while (std::getline(infile, line)) {
105  std::istringstream iss(line);
106  std::string substring{};
108  while (std::getline(iss, substring, ',')) val.push_back(substring);
109  a.pebblesX[i].push_back(std::stof(val[0]));
110  a.pebblesY[i].push_back(std::stof(val[1]));
111  a.pebblesZ[i].push_back(std::stof(val[2]));
112  a.pebblesR[i].push_back(std::stof(val[3]));
113  }
114  infile.close();
115  }
116  logger(INFO, "\t OK\n");
117 }
118 
123 {
124  logger(INFO, "Loading clump masses...");
125 
126  a.mass.resize(a.clumpNames.size());
127 
128  for (int i = 0; i < a.clumpNames.size(); i++ ){
129  std::ifstream infile((a.path + a.clumpNames[i] + "/Inertia/Mass.txt").c_str(), std::ios::in | std::ios::binary);
130  std::string mass;
131  infile >> mass;
132  a.mass[i] = std::stof(mass);
133  infile.close();
134  }
135  logger(INFO, "\t OK\n");
136 }
137 
143 {
144  logger(INFO, "Loading clump TOI..");
145  a.toi.resize(a.clumpNames.size());
146 
147  for (int i = 0; i < a.clumpNames.size(); i++ ){
148  std::ifstream infile((a.path + a.clumpNames[i] + "/Inertia/TOI.txt").c_str(), std::ios::in | std::ios::binary);
149  std::string line{};
150  while (std::getline(infile, line)) {
151  std::istringstream iss(line);
152  std::string substring{};
154  while (std::getline(iss, substring, ',')) val.push_back(substring);
155  for (int k = 0; k < 3; k++) a.toi[i].push_back(std::stof(val[k]));
156  }
157  infile.close();
158  }
159  logger(INFO, "\t OK\n");
160 }
161 
167 {
168  logger(INFO, "Loading clump PD..");
169  a.pd.resize(a.clumpNames.size());
170 
171  for (int i = 0; i < a.clumpNames.size(); i++ ){
172  std::ifstream infile((a.path + a.clumpNames[i] + "/Inertia/PD.txt").c_str(), std::ios::in | std::ios::binary);
173  std::string line{};
174  while (std::getline(infile, line)) {
175  std::istringstream iss(line);
176  std::string substring{};
178  while (std::getline(iss, substring, ',')) val.push_back(substring);
179  for (int k = 0; k < 3; k++) a.pd[i].push_back(std::stof(val[k]));
180  }
181  infile.close();
182  }
183  logger(INFO, "\t OK\n");
184 }
185 
186 
191 void LoadClumps(ClumpData &data, bool VERBOSE = false)
192 {
193 
194  logger(INFO, "LOAD CLUMP DATA");
195  LoadConf(data);
196  LoadPebbles(data);
197  LoadMass(data);
198  LoadTOI(data);
199  LoadPD(data);
200  if (VERBOSE) {
201  std::cout<<"LOADED CLUMPS"<<std::endl;
202  for (int i = 0; i < data.pebblesX.size(); i++) {
203  std::cout << data.clumpNames[i] << " mass:" << data.mass[i] << std::endl;
204  std::cout << data.clumpNames[i] << " list of pebbles:" << std::endl;
205  for (int j = 0; j < data.pebblesX[i].size(); j++) {
206  std::cout << "Pebble " << j << ": (" << data.pebblesX[i][j] << "," << data.pebblesY[i][j] << ","
207  << data.pebblesZ[i][j] << ")," << data.pebblesR[i][j] << std::endl;
208  }
209 
210  std::cout << data.clumpNames[i] << " TOI:" << std::endl;
211  std::cout << data.toi[i][0] << "," << data.toi[i][1] << "," << data.toi[i][2] << std::endl;
212  std::cout << data.toi[i][3] << "," << data.toi[i][4] << "," << data.toi[i][5] << std::endl;
213  std::cout << data.toi[i][6] << "," << data.toi[i][7] << "," << data.toi[i][8] << std::endl;
214 
215  std::cout << data.clumpNames[i] << " Principal directions:" << std::endl;
216  std::cout << data.pd[i][0] << "," << data.pd[i][1] << "," << data.pd[i][2] << std::endl;
217  std::cout << data.pd[i][3] << "," << data.pd[i][4] << "," << data.pd[i][5] << std::endl;
218  std::cout << data.pd[i][6] << "," << data.pd[i][7] << "," << data.pd[i][8] << std::endl;
219  }
220  }
221 
222 }
223 
229 {
230  ClumpData new_data = data;
231  new_data.pd[clump_index] = new_pd;
232  return new_data;
233 }
234 
235 
236 
237 
238 
239 
244 
245  Vec3D n1, n2, n3, ref;
246 
247  // basis vector n1
248  double r1 = RandomDouble(2) - 1.0;
249  double r2 = RandomDouble(1);
250 
251  double theta = acos(r1); // Note that for isotropy of n1 theta is NOT uniform!
252  double phi = 2 * M_PI * r2;
253 
254  n1 = Vec3D(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
255 
256  // basis vector n2
257  r1 = RandomDouble(1);
258  r2 = RandomDouble(1);
259 
260  theta = acos(2 * r1 - 1); // Note that for isotropy of n1 theta is NOT uniform!
261  phi = 2 * M_PI * r2;
262 
263  ref = Vec3D(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
264  n2 = Vec3D::cross(ref, n1); n2.normalise();
265  n3 = Vec3D::cross(n1, n2); n3.normalise();
266  return DoubleVector{n1.X, n1.Y, n1.Z, n2.X, n2.Y, n2.Z, n3.X, n3.Y, n3.Z};
267 
268 }
269 
270 
271 #endif // CLUMP_INPUT_H
AnnoyingScalar cos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:136
AnnoyingScalar acos(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:138
AnnoyingScalar sin(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:137
int i
Definition: BiCGSTAB_step_by_step.cpp:9
void LoadConf(ClumpData &a)
Definition: ClumpInput.h:63
void LoadPD(ClumpData &a)
Definition: ClumpInput.h:166
double RandomDouble(double Max)
Definition: ClumpInput.h:34
void LoadPebbles(ClumpData &a)
Definition: ClumpInput.h:92
void LoadClumps(ClumpData &data, bool VERBOSE=false)
Definition: ClumpInput.h:191
void LoadTOI(ClumpData &a)
Definition: ClumpInput.h:142
DoubleVector UniformRandomPDs()
Definition: ClumpInput.h:243
std::vector< double > DoubleVector
loads clump configuration
Definition: ClumpInput.h:26
std::vector< std::string > StringVector
Definition: ClumpInput.h:28
void LoadMass(ClumpData &a)
Definition: ClumpInput.h:122
bool CompareFunction(std::string a, std::string b)
Definition: ClumpInput.h:31
std::vector< DoubleVector > Double2DVector
Definition: ClumpInput.h:27
ClumpData RotateClump(ClumpData data, int clump_index, DoubleVector new_pd)
Definition: ClumpInput.h:228
const std::string getMercuryDPMSourceDir()
This file is used for generating definitions that give access to CMakeVariables from within a cpp fil...
Definition: Configuration/CMakeDefinitions.cc:10
LL< Log::VERBOSE > VERBOSE
Verbose information.
Definition: Logger.cc:36
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
int data[]
Definition: Map_placement_new.cpp:1
Scalar * b
Definition: benchVecAdd.cpp:17
Definition: Kernel/Math/Vector.h:30
Mdouble Y
Definition: Kernel/Math/Vector.h:45
Mdouble Z
Definition: Kernel/Math/Vector.h:45
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:143
Mdouble X
the vector components
Definition: Kernel/Math/Vector.h:45
void normalise()
Makes this Vec3D unit length.
Definition: Vector.cc:103
const Scalar * a
Definition: level2_cplx_impl.h:32
char char char int int * k
Definition: level2_impl.h:374
#define M_PI
Definition: main.h:121
#define INFO(i)
Definition: mumps_solver.h:54
double theta
Definition: two_d_biharmonic.cc:236
val
Definition: calibrate.py:119
line
Definition: calibrate.py:103
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
Definition: ClumpInput.h:42
Double2DVector toi
Definition: ClumpInput.h:53
Double2DVector pd
Definition: ClumpInput.h:54
StringVector clumpNames
Definition: ClumpInput.h:46
Double2DVector pebblesX
Definition: ClumpInput.h:48
DoubleVector mass
Definition: ClumpInput.h:47
Double2DVector pebblesZ
Definition: ClumpInput.h:50
Double2DVector pebblesY
Definition: ClumpInput.h:49
Double2DVector pebblesR
Definition: ClumpInput.h:51
std::string path
Definition: ClumpInput.h:45
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2