In this tutorial, we explore more details about data files and integration.
The goals of this utility is to:
- Run a basic integration from a configuration file
- Automatically save the output
- Automatically resume from the last saved output
- Integrate for a specific period of time
Detail:
- Do the integration based on a cfg file, input file is required
- Save the state of data file in a snapshot file
- Find the median of integrated time from the ensemble and add the predefined amount to it
Source code for this tutorial can be found at py/resume_tutorial.py
Arguments
First, we parse the comand line arguments using argparse
1 import swarmng, argparse
4 parser = argparse.ArgumentParser()
5 parser.add_argument(
"-c",
"--config" , help=
"Config file", required =
True)
6 parser.add_argument(
"-d",
"--duration" , help =
"Duration of integration in AU", default=10.0, type=float )
7 args = parser.parse_args()
Compute the orbital elements
We load the Config object from a file
2 inputFile = cfg[
"input"]
3 snapshotFileName = inputFile +
".snapshot"
Here we check for existence of the snapshot file, if it exists, then we load it instead of the initial conditions.
1 if path.exists(snapshotFileName) :
5 ext = path.splitext(fn)[1]
the integration
Using some functional features, we can easily calculate the median of all system times to estimate a starting time for the whole ensemble.
1 times = sorted(map(
lambda s : s.time, ens))
2 median_of_system_time = times[len(times)/2]
We set the starting time to the median of all system times. We add the duration to the starting time to find a suitable end time.
1 starting_time = median_of_system_time
2 dt = min(float(cfg[
"destination_time"]), starting_time + args.duration)
3 print(
"Integrating from {0} to {1}".format(starting_time, dt))
Let the integration begin, it is the same as the first tutorial
4 integ.destination_time = dt
Finally we have to save the snapshot for resuming the integration later
1 ens.save_to_bin(snapshotFileName)
Conclusion
In this tutorial, we explored how to save/load snapshot files. You can use snapshot files in many integration schemes where complex and long simulations are required and you may need to end them early sometimes.
Where to go from here: