Swarm-NG  1.1
snapshot.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Saleh Dindar and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
25 #include "snapshot.hpp"
26 
27 namespace swarm {
28 
29 namespace snapshot {
30 
31 template<class T>
32 void readfromFILE(FILE*f,T& t,const string& filename = ""){
33  if(fread(&t,1,sizeof(t),f) != sizeof(t)){
34  throw readfileexception(filename,"File I/O error");
35  }
36 }
37 template<class T>
38 void writetoFILE(FILE*f,const T& t,const string& filename = ""){
39  if(fwrite(&t,1,sizeof(t),f) != sizeof(t)){
40  throw writefileexception(filename,"File I/O error");
41  }
42 }
43 
44 defaultEnsemble load(const string& filename) throw (readfileexception){
45  FILE* f = fopen(filename.c_str(),"rb");
46 
47  header h; sys s; body b;
48 
49  readfromFILE(f,h,filename);
50 
51  if(h.nsysattr > ensemble::NUM_SYS_ATTRIBUTES)
52  throw readfileexception(filename, "The file requires more system attributes than the library can handle");
53  if(h.nbodattr > ensemble::NUM_BODY_ATTRIBUTES)
54  throw readfileexception(filename, "The file requires more planet attributes than the library can handle");
55 
56  hostEnsemble ens = hostEnsemble::create(h.nbod,h.nsys);
57 
58  for(int i = 0; i < h.nsys; i++){
59  ensemble::SystemRef sr = ens[i];
60 
61  readfromFILE(f,s,filename);
62 
63  sr.id() = s.id;
64  sr.time() = s.time; sr.state() = s.state;
65 
66  for(int l = 0; l < h.nsysattr ; l++)
67  sr.attribute(l) = s.attribute[l];
68 
69  for(int j = 0; j < h.nbod; j++){
70  readfromFILE(f,b,filename);
71 
72  sr[j].mass() = b.mass;
73  sr[j][0].pos() = b.pos[0];
74  sr[j][1].pos() = b.pos[1];
75  sr[j][2].pos() = b.pos[2];
76  sr[j][0].vel() = b.vel[0];
77  sr[j][1].vel() = b.vel[1];
78  sr[j][2].vel() = b.vel[2];
79  for(int l = 0; l < h.nbodattr ; l++)
80  sr[j].attribute(l) = b.attribute[l];
81  }
82  }
83  fclose(f);
84  return ens;
85 }
86 
87 
88 const char* DEFAULT_IO_TAG = "SwarmDataFile";
89 const int CURRENT_IO_VERSION = 2;
90 
91 defaultEnsemble load_text(const string& filename) throw (readfileexception){
92  FILE* f = fopen(filename.c_str(),"r");
93 
94  header h; sys s; body b;
95 
96  char tag[1024];
97  int version = 0;
98 
99  fscanf(f, "%s %i\n" , tag, &version);
100  if(strcmp( DEFAULT_IO_TAG, tag ) != 0)
101  throw readfileexception(filename,"Invalid file, header doesn't match");
102 
103  if(version != CURRENT_IO_VERSION )
104  throw readfileexception(filename, "Incorrect version");
105 
106  fscanf(f,"%i %i %i %i\n\n\n",&h.nbod,&h.nsys,&h.nsysattr, &h.nbodattr);
107  if(h.nsysattr > ensemble::NUM_SYS_ATTRIBUTES)
108  throw readfileexception(filename, "The file requires more system attributes than the library can handle");
109  if(h.nbodattr > ensemble::NUM_BODY_ATTRIBUTES)
110  throw readfileexception(filename, "The file requires more planet attributes than the library can handle");
111 
112  hostEnsemble ens = hostEnsemble::create(h.nbod,h.nsys);
113 
114 
115  for(int i = 0; i < h.nsys; i++){
116  ensemble::SystemRef sr = ens[i];
117 
118  fscanf(f,"%i %le %i\n", &sr.id(), &sr.time(), &sr.state());
119 
120  for(int l = 0; l < h.nsysattr; l++)
121  fscanf(f, "%le ", &sr.attribute(l));
122 
123  for(int j = 0; j < h.nbod; j++){
124  fscanf(f,"\t%le\n\t%le %le %le\n\t%le %le %le\n\n",
125  &sr[j].mass(),
126  &sr[j][0].pos(),
127  &sr[j][1].pos(),
128  &sr[j][2].pos(),
129  &sr[j][0].vel(),
130  &sr[j][1].vel(),
131  &sr[j][2].vel()
132  );
133  for(int l = 0; l < h.nbodattr; l++)
134  fscanf(f, "%le", &sr[j].attribute(l));
135  }
136  }
137  fclose(f);
138  return ens;
139 }
140 
141 void save(defaultEnsemble& ens, const string& filename) throw (writefileexception){
142  FILE* f = fopen(filename.c_str(),"wb");
143 
144  header h; sys s; body b;
145 
146  h.nsys = ens.nsys(), h.nbod = ens.nbod();
148 
149  writetoFILE(f,h,filename);
150 
151  for(int i = 0; i < h.nsys; i++){
152 
153  ensemble::SystemRef sr = ens[i];
154  s.id = sr.id();
155  s.time = sr.time(), s.state = sr.state();
156 
157 
158  for(int l = 0; l < h.nsysattr ; l++)
159  s.attribute[l] = sr.attribute(l);
160 
161  writetoFILE(f,s,filename);
162 
163  for(int j = 0; j < h.nbod; j++){
164  b.pos[0] = sr[j][0].pos();
165  b.pos[1] = sr[j][1].pos();
166  b.pos[2] = sr[j][2].pos();
167  b.vel[0] = sr[j][0].vel();
168  b.vel[1] = sr[j][1].vel();
169  b.vel[2] = sr[j][2].vel();
170  b.mass = sr[j].mass();
171  for(int l = 0; l < h.nbodattr ; l++)
172  b.attribute[l] = sr[j].attribute(l);
173 
174  writetoFILE(f,b,filename);
175  }
176 
177  }
178  fclose(f);
179 }
180 
181 void save_text(defaultEnsemble& ens, const string& filename) throw (writefileexception){
182  FILE* f = fopen(filename.c_str(),"w");
183 
184 
185  fprintf(f, "%s %i\n" , DEFAULT_IO_TAG, CURRENT_IO_VERSION );
186  fprintf(f,"%i %i %i %i\n\n\n", ens.nbod(), ens.nsys(), ensemble::NUM_SYS_ATTRIBUTES, ensemble::NUM_BODY_ATTRIBUTES );
187 
188  for(int i = 0; i < ens.nsys(); i++){
189 
190  ensemble::SystemRef sr = ens[i];
191  fprintf(f,"%i %.15le %i\n", sr.id(), sr.time(), sr.state());
192 
193  for(int l = 0; l < ensemble::NUM_SYS_ATTRIBUTES; l++)
194  fprintf(f,"%.15le ", sr.attribute(l));
195  fprintf(f, "\n");
196 
197  for(int j = 0; j < ens.nbod(); j++){
198  fprintf(f,"\t%.15le\n\t%.15le %.15le %.15le\n\t%.15le %.15le %.15le\n\t",
199  sr[j].mass(),
200  sr[j][0].pos(),
201  sr[j][1].pos(),
202  sr[j][2].pos(),
203  sr[j][0].vel(),
204  sr[j][1].vel(),
205  sr[j][2].vel()
206  );
207  for(int l = 0; l < ensemble::NUM_BODY_ATTRIBUTES; l++)
208  fprintf(f,"%.15le ", sr[j].attribute(l));
209  fprintf(f, "\n\n");
210  }
211 
212  fprintf(f,"\n");
213 
214  }
215  fclose(f);
216 }
217 
218 
219 }
220 }