Swarm-NG  1.1
Resume an integration

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
2 from os import path
3 
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

1 cfg = swarmng.Config.load(args.config)
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) :
2  fn = snapshotFileName
3 else :
4  fn = inputFile
5 ext = path.splitext(fn)[1]
6 if ext == "txt" : ens = swarmng.DefaultEnsemble.load_from_text(fn)

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

1 swarmng.init(cfg)
2 integ = swarmng.Integrator.create( cfg )
3 integ.ensemble = ens
4 integ.destination_time = dt
5 integ.integrate()

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: