32 void readfromFILE(FILE*f,T& t,
const string& filename =
""){
33 if(fread(&t,1,
sizeof(t),f) !=
sizeof(t)){
38 void writetoFILE(FILE*f,
const T& t,
const string& filename =
""){
39 if(fwrite(&t,1,
sizeof(t),f) !=
sizeof(t)){
45 FILE* f = fopen(filename.c_str(),
"rb");
49 readfromFILE(f,h,filename);
52 throw readfileexception(filename,
"The file requires more system attributes than the library can handle");
54 throw readfileexception(filename,
"The file requires more planet attributes than the library can handle");
58 for(
int i = 0; i < h.nsys; i++){
59 ensemble::SystemRef sr = ens[i];
61 readfromFILE(f,s,filename);
64 sr.time() = s.time; sr.state() = s.state;
66 for(
int l = 0; l < h.nsysattr ; l++)
67 sr.attribute(l) = s.attribute[l];
69 for(
int j = 0; j < h.nbod; j++){
70 readfromFILE(f,b,filename);
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];
88 const char* DEFAULT_IO_TAG =
"SwarmDataFile";
89 const int CURRENT_IO_VERSION = 2;
92 FILE* f = fopen(filename.c_str(),
"r");
99 fscanf(f,
"%s %i\n" , tag, &version);
100 if(strcmp( DEFAULT_IO_TAG, tag ) != 0)
103 if(version != CURRENT_IO_VERSION )
106 fscanf(f,
"%i %i %i %i\n\n\n",&h.nbod,&h.nsys,&h.nsysattr, &h.nbodattr);
108 throw readfileexception(filename,
"The file requires more system attributes than the library can handle");
110 throw readfileexception(filename,
"The file requires more planet attributes than the library can handle");
115 for(
int i = 0; i < h.nsys; i++){
116 ensemble::SystemRef sr = ens[i];
118 fscanf(f,
"%i %le %i\n", &sr.id(), &sr.time(), &sr.state());
120 for(
int l = 0; l < h.nsysattr; l++)
121 fscanf(f,
"%le ", &sr.attribute(l));
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",
133 for(
int l = 0; l < h.nbodattr; l++)
134 fscanf(f,
"%le", &sr[j].attribute(l));
142 FILE* f = fopen(filename.c_str(),
"wb");
146 h.nsys = ens.nsys(), h.nbod = ens.nbod();
149 writetoFILE(f,h,filename);
151 for(
int i = 0; i < h.nsys; i++){
153 ensemble::SystemRef sr = ens[i];
155 s.time = sr.time(), s.state = sr.state();
158 for(
int l = 0; l < h.nsysattr ; l++)
159 s.attribute[l] = sr.attribute(l);
161 writetoFILE(f,s,filename);
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);
174 writetoFILE(f,b,filename);
182 FILE* f = fopen(filename.c_str(),
"w");
185 fprintf(f,
"%s %i\n" , DEFAULT_IO_TAG, CURRENT_IO_VERSION );
188 for(
int i = 0; i < ens.nsys(); i++){
190 ensemble::SystemRef sr = ens[i];
191 fprintf(f,
"%i %.15le %i\n", sr.id(), sr.time(), sr.state());
194 fprintf(f,
"%.15le ", sr.attribute(l));
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",
208 fprintf(f,
"%.15le ", sr[j].attribute(l));