00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00025 #include "snapshot.hpp"
00026
00027 namespace swarm {
00028
00029 namespace snapshot {
00030
00031 template<class T>
00032 void readfromFILE(FILE*f,T& t,const string& filename = ""){
00033 if(fread(&t,1,sizeof(t),f) != sizeof(t)){
00034 throw readfileexception(filename,"File I/O error");
00035 }
00036 }
00037 template<class T>
00038 void writetoFILE(FILE*f,const T& t,const string& filename = ""){
00039 if(fwrite(&t,1,sizeof(t),f) != sizeof(t)){
00040 throw writefileexception(filename,"File I/O error");
00041 }
00042 }
00043
00044 defaultEnsemble load(const string& filename) throw (readfileexception){
00045 FILE* f = fopen(filename.c_str(),"rb");
00046
00047 header h; sys s; body b;
00048
00049 readfromFILE(f,h,filename);
00050
00051 if(h.nsysattr > ensemble::NUM_SYS_ATTRIBUTES)
00052 throw readfileexception(filename, "The file requires more system attributes than the library can handle");
00053 if(h.nbodattr > ensemble::NUM_BODY_ATTRIBUTES)
00054 throw readfileexception(filename, "The file requires more planet attributes than the library can handle");
00055
00056 hostEnsemble ens = hostEnsemble::create(h.nbod,h.nsys);
00057
00058 for(int i = 0; i < h.nsys; i++){
00059 ensemble::SystemRef sr = ens[i];
00060
00061 readfromFILE(f,s,filename);
00062
00063 sr.id() = s.id;
00064 sr.time() = s.time; sr.state() = s.state;
00065
00066 for(int l = 0; l < h.nsysattr ; l++)
00067 sr.attribute(l) = s.attribute[l];
00068
00069 for(int j = 0; j < h.nbod; j++){
00070 readfromFILE(f,b,filename);
00071
00072 sr[j].mass() = b.mass;
00073 sr[j][0].pos() = b.pos[0];
00074 sr[j][1].pos() = b.pos[1];
00075 sr[j][2].pos() = b.pos[2];
00076 sr[j][0].vel() = b.vel[0];
00077 sr[j][1].vel() = b.vel[1];
00078 sr[j][2].vel() = b.vel[2];
00079 for(int l = 0; l < h.nbodattr ; l++)
00080 sr[j].attribute(l) = b.attribute[l];
00081 }
00082 }
00083 fclose(f);
00084 return ens;
00085 }
00086
00087
00088 const char* DEFAULT_IO_TAG = "SwarmDataFile";
00089 const int CURRENT_IO_VERSION = 2;
00090
00091 defaultEnsemble load_text(const string& filename) throw (readfileexception){
00092 FILE* f = fopen(filename.c_str(),"r");
00093
00094 header h; sys s; body b;
00095
00096 char tag[1024];
00097 int version = 0;
00098
00099 fscanf(f, "%s %i\n" , tag, &version);
00100 if(strcmp( DEFAULT_IO_TAG, tag ) != 0)
00101 throw readfileexception(filename,"Invalid file, header doesn't match");
00102
00103 if(version != CURRENT_IO_VERSION )
00104 throw readfileexception(filename, "Incorrect version");
00105
00106 fscanf(f,"%i %i %i %i\n\n\n",&h.nbod,&h.nsys,&h.nsysattr, &h.nbodattr);
00107 if(h.nsysattr > ensemble::NUM_SYS_ATTRIBUTES)
00108 throw readfileexception(filename, "The file requires more system attributes than the library can handle");
00109 if(h.nbodattr > ensemble::NUM_BODY_ATTRIBUTES)
00110 throw readfileexception(filename, "The file requires more planet attributes than the library can handle");
00111
00112 hostEnsemble ens = hostEnsemble::create(h.nbod,h.nsys);
00113
00114
00115 for(int i = 0; i < h.nsys; i++){
00116 ensemble::SystemRef sr = ens[i];
00117
00118 fscanf(f,"%i %le %i\n", &sr.id(), &sr.time(), &sr.state());
00119
00120 for(int l = 0; l < h.nsysattr; l++)
00121 fscanf(f, "%le ", &sr.attribute(l));
00122
00123 for(int j = 0; j < h.nbod; j++){
00124 fscanf(f,"\t%le\n\t%le %le %le\n\t%le %le %le\n\n",
00125 &sr[j].mass(),
00126 &sr[j][0].pos(),
00127 &sr[j][1].pos(),
00128 &sr[j][2].pos(),
00129 &sr[j][0].vel(),
00130 &sr[j][1].vel(),
00131 &sr[j][2].vel()
00132 );
00133 for(int l = 0; l < h.nbodattr; l++)
00134 fscanf(f, "%le", &sr[j].attribute(l));
00135 }
00136 }
00137 fclose(f);
00138 return ens;
00139 }
00140
00141 void save(defaultEnsemble& ens, const string& filename) throw (writefileexception){
00142 FILE* f = fopen(filename.c_str(),"wb");
00143
00144 header h; sys s; body b;
00145
00146 h.nsys = ens.nsys(), h.nbod = ens.nbod();
00147 h.nsysattr = ensemble::NUM_SYS_ATTRIBUTES, h.nbodattr = ensemble::NUM_BODY_ATTRIBUTES;
00148
00149 writetoFILE(f,h,filename);
00150
00151 for(int i = 0; i < h.nsys; i++){
00152
00153 ensemble::SystemRef sr = ens[i];
00154 s.id = sr.id();
00155 s.time = sr.time(), s.state = sr.state();
00156
00157
00158 for(int l = 0; l < h.nsysattr ; l++)
00159 s.attribute[l] = sr.attribute(l);
00160
00161 writetoFILE(f,s,filename);
00162
00163 for(int j = 0; j < h.nbod; j++){
00164 b.pos[0] = sr[j][0].pos();
00165 b.pos[1] = sr[j][1].pos();
00166 b.pos[2] = sr[j][2].pos();
00167 b.vel[0] = sr[j][0].vel();
00168 b.vel[1] = sr[j][1].vel();
00169 b.vel[2] = sr[j][2].vel();
00170 b.mass = sr[j].mass();
00171 for(int l = 0; l < h.nbodattr ; l++)
00172 b.attribute[l] = sr[j].attribute(l);
00173
00174 writetoFILE(f,b,filename);
00175 }
00176
00177 }
00178 fclose(f);
00179 }
00180
00181 void save_text(defaultEnsemble& ens, const string& filename) throw (writefileexception){
00182 FILE* f = fopen(filename.c_str(),"w");
00183
00184
00185 fprintf(f, "%s %i\n" , DEFAULT_IO_TAG, CURRENT_IO_VERSION );
00186 fprintf(f,"%i %i %i %i\n\n\n", ens.nbod(), ens.nsys(), ensemble::NUM_SYS_ATTRIBUTES, ensemble::NUM_BODY_ATTRIBUTES );
00187
00188 for(int i = 0; i < ens.nsys(); i++){
00189
00190 ensemble::SystemRef sr = ens[i];
00191 fprintf(f,"%i %.15le %i\n", sr.id(), sr.time(), sr.state());
00192
00193 for(int l = 0; l < ensemble::NUM_SYS_ATTRIBUTES; l++)
00194 fprintf(f,"%.15le ", sr.attribute(l));
00195 fprintf(f, "\n");
00196
00197 for(int j = 0; j < ens.nbod(); j++){
00198 fprintf(f,"\t%.15le\n\t%.15le %.15le %.15le\n\t%.15le %.15le %.15le\n\t",
00199 sr[j].mass(),
00200 sr[j][0].pos(),
00201 sr[j][1].pos(),
00202 sr[j][2].pos(),
00203 sr[j][0].vel(),
00204 sr[j][1].vel(),
00205 sr[j][2].vel()
00206 );
00207 for(int l = 0; l < ensemble::NUM_BODY_ATTRIBUTES; l++)
00208 fprintf(f,"%.15le ", sr[j].attribute(l));
00209 fprintf(f, "\n\n");
00210 }
00211
00212 fprintf(f,"\n");
00213
00214 }
00215 fclose(f);
00216 }
00217
00218
00219 }
00220 }