MercuryDPM  0.11
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
centerofmass.cpp
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
27 
28 #include <string.h>
29 #include <iomanip>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 
38 class CFile {
39 
40 public:
41 
43  CFile(std::string name) {
44  //set file names
45  data_filename.str("");
46  data_filename << name << ".data";
47  com_filename.str("");
48  com_filename << name << ".com";
49 
50  //open in-streams
51  data_file.open(data_filename.str().c_str(), std::fstream::in);
52 
53  if (data_file.fail())
54  {
55  std::cerr << "ERROR: Input file " << data_filename.str() << " not found" << std::endl;
56  data_file.close();
57  exit(-1);
58  } else {
59  std::cout << "Files opened: " << data_filename.str() << std::endl;
60  }
61 
62  //open out-stream
63  com_file.open(com_filename.str().c_str(), std::fstream::out);
64 
65  if (com_file.fail())
66  {
67  std::cerr << "ERROR: Output file " << com_filename.str() << " not found" << std::endl;
68  com_file.close();
69  exit(-1);
70  } else {
71  std::cout << "Files opened: " << com_filename.str() << std::endl;
72  }
73 
75  splittinginfo=false;
76  }
77 
79  ~CFile() {
80  data_file.close();
81  std::cout << "Files closed: " << data_filename.str() << std::endl;
82  com_file.close();
83  std::cout << "Files closed: " << com_filename.str() << std::endl;
84  }
85 
86  bool copy() {
87  unsigned int N;
88  double X, Y, Z;
89  double CX, CY, CZ;
90  double CX1, CY1, CZ1;
91  double CX2, CY2, CZ2;
92  double VX, VY, VZ;
93  double AX, AY, AZ;
94  double WX, WY, WZ;
95  double T, R, M, M1, M2, mass, info;
96  std::string line;
97  std::stringstream output_filename;
98  std::fstream output_file;
99 
100  data_file >> N;
101  while (data_file.good()) {
102  M = M1 = M2 = 0.0;
103  data_file >> T;
104  //open, write, close output file
105  getline(data_file,line);
106  CX = CY = CZ = 0.0;
107  CX1 = CY1 = CZ1 = 0.0;
108  CX2 = CY2 = CZ2 = 0.0;
109  for (unsigned int i=0; i<N; i++) {
110  data_file >> X >> Y >> Z >> VX >> VY >> VZ >> R >> AX >> AY >> AZ >> WX >> WY >> WZ >> info;
111  mass=R*R*R;
112  getline(data_file,line);
113 
114  if (splittingradius){
115  CX += X*mass; CY += Y*mass; CZ += Z*mass; M+=mass;
116  }
117  if (splittinginfo){
118  if(info==info0||info==info1){
119  CX += X*mass; CY += Y*mass; CZ += Z*mass; M+=mass;
120  }
121  }
122  if (splittingradius) {
123  if (R<splittingradius) {
124  CX1 += X*mass; CY1 += Y*mass; CZ1 += Z*mass; M1+=mass;
125  } else {
126  CX2 += X*mass; CY2 += Y*mass; CZ2 += Z*mass; M2+=mass;
127  }
128  }
129  if (splittinginfo) {
130  //std::cout << "hello" << std::endl;
131  if (info==info0) {
132  //std::cout << "hello" << std::endl;
133  CX1 += X*mass; CY1 += Y*mass; CZ1 += Z*mass; M1+=mass;
134  } else if (info==info1) {
135  CX2 += X*mass; CY2 += Y*mass; CZ2 += Z*mass; M2+=mass;
136  }
137  }
138  }
140  com_file << T << " " << CX/M << " " << CY/M << " " << CZ/M;
141  com_file << " " << CX1/M1 << " " << CY1/M1 << " " << CZ1/M1;
142  com_file << " " << CX2/M2 << " " << CY2/M2 << " " << CZ2/M2 << std::endl;
143  } else {
144  com_file << T << " " << CX/M << " " << CY/M << " " << CZ/M << std::endl;
145  }
146  data_file >> N;
147  }
148  return true;
149  }
150 
151 
152 private:
154  std::stringstream data_filename;
155  std::stringstream com_filename;
156 
158  std::fstream data_file;
159  std::fstream com_file;
160 
161 public:
162  //
165  int info0, info1;
166 };
167 
168 int main(int argc, char *argv[])
169 {
170  if (argc<2) {
171  std::cerr << "Please enter problem name as first argument" << std::endl;
172  return -1;
173  }
174  std::string name(argv[1]);
175  std::cout << "Name: " << name << std::endl;
176 
177  CFile files(name);
178 
179  //defines the splitting radius
180 // if (argc>2) files.splittingradius = atof(argv[2]);
181  if (argc>2) {
182  if (!strcmp(argv[2],"-info")) {
183  //std::cout << "hello" << std::endl;
184  if (argc>4) {
185  files.splittinginfo = true;
186  files.info0 = atoi(argv[3]);
187  files.info1 = atoi(argv[4]);
188  } else {
189  std::cerr << "Please provide two info values" << std::endl;
190  }
191  } else {
192  files.splittingradius = atof(argv[2]);
193  }
194  }
195 
196 
197  files.copy();
198  std::cout << "finished writing files: " << name << std::endl;
199  return 0;
200 }
~CFile()
Destructor.
std::stringstream com_filename
bool copy()
takes data and fstat files and splits them into *.data.???? and *.fstat.???? files ...
double splittingradius
int main(int argc, char *argv[])
std::fstream data_file
Stream used for data files.
std::fstream com_file
CFile(std::string name)
Constructor.
bool splittinginfo
std::stringstream data_filename
These store the save file names,.