Commit 3cfc0dd5 authored by Stephen D's avatar Stephen D
Browse files

Saving/loading

parent 95443cbe
......@@ -8,11 +8,6 @@ The engine features full multithreading support when calculating forces.
## TODO:
- Config files (instead of requiring recompilation)
- Saving/restoring progress - Would also like autosaving for long runs, in case of a power failure or other issue.
- Automatic video processing - Export X frames, turn them into a video, delete the frames. Repeat until the simulation is done, and then stick the videos together. At the moment, a long simulation can take hundreds of GB to store all of the images.
- Allow processing over a distributed set of computers
......
......@@ -204,6 +204,34 @@ void saveImage(double frame)
out = BMP_Create(w, h, 24);
}
void saveState(int it)
{
FILE *fp;
char filename[300]; //lots of room
char buffer[15];
strcpy(filename, "saves/");
strcat(filename, s);
itoa(it, buffer);
strcat(filename, buffer);
strcat(filename, "-ID.sgs");
fp = fopen(filename, "wb");
//first, write the config parameters
config_t conf;
conf.nStars = nStars;
conf.imgW = w;
conf.imgH = h;
conf.savePeriod = a;
conf.iteration = it;
strcpy(conf.saveFile, s);
fwrite(&conf, sizeof(conf), 1, fp);
//then, write all the stars
fwrite(stars, sizeof(RigidMass_t), nStars, fp);
//close the file
fclose(fp);
}
void prnHelp(char *name)
{
//TODO: iterations per frame
......@@ -258,6 +286,7 @@ int main(int argc, char **argv)
else strcpy(s, optarg);
break;
case 'L':
load = true;
if(strlen(optarg) >= 255)
{
printf("ERROR: Load filename too long.\r\n");
......@@ -298,30 +327,64 @@ int main(int argc, char **argv)
//allocate array
stars = malloc(sizeof(RigidMass_t) * nStars);
srand(time(NULL));
generateStars();
}
else if(load) //technically redundant. can never be too sure
{
printf("Continuing %s...", l);
printf("%f stars loaded.", -1);
printf("Continuing %s...\r\n", l);
FILE *fp;
fp = fopen(l, "r");
//first, read the config parameters
config_t conf;
fread(&conf, sizeof(conf), 1, fp);
nStars = conf.nStars;
w = conf.imgW;
h = conf.imgH;
a = conf.savePeriod;
iteration = conf.iteration;
strcpy(s, conf.saveFile);
//second, read the stars
stars = malloc(sizeof(RigidMass_t) * nStars);
fread(stars, sizeof(RigidMass_t), nStars, fp);
//close the file
fclose(fp);
printf("%d stars loaded.\r\n", nStars);
printf("Continuing from i=%d\r\n", iteration);
}
srand(time(NULL));
out = BMP_Create(w, h, 24);
//Runs until CTRL+C
//TODO: CTRL+C should force autosave
bool justStarted = true;
while(1)
{
iterate();
addToImage();
if (iteration % 10 == 0)
//gives the next frame some iterations to build up
//without justStarted, the loaded iteration is rerendered, but only from
//a single iteration, which stands out quite a bit.
if(justStarted)
{
saveImage(iteration / 10);
justStarted = false;
}
else
{
if(iteration % 10 == 0)
{
saveImage(iteration / 10);
}
if(iteration % a == 0)
{
saveState(iteration);
}
}
iteration++;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment