Swarm-NG  1.1
resume_tutorial.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 ## @file resume_tutorial.py Tutorial on resuming integration from saved snapshots
3 #
4 # refer to @ref TutorialPythonResume for formatted version.
5 
6 
7 # @page TutorialPythonResume Resume an integration
8 # In this tutorial, we explore more details about data files and integration.
9 #
10 # The goals of this utility is to:
11 # * Run a basic integration from a configuration file
12 # * Automatically save the output
13 # * Automatically resume from the last saved output
14 # * Integrate for a specific period of time
15 #
16 # Detail:
17 # * Do the integration based on a cfg file, input file is required
18 # * Save the state of data file in a snapshot file
19 # * Find the median of integrated time from the ensemble and
20 # add the predefined amount to it
21 #
22 # Source code for this tutorial can be found at @ref py/resume_tutorial.py
23 # \section ai Arguments
24 #
25 # First, we parse the comand line arguments using argparse
26 #
27 #
28 import swarmng, argparse
29 from os import path
30 
31 parser = argparse.ArgumentParser()
32 parser.add_argument("-c", "--config" , help="Config file", required = True)
33 parser.add_argument("-d", "--duration" , help ="Duration of integration in AU", default=10.0, type=float )
34 args = parser.parse_args()
35 
36 # \section cc Configuration and Initial conditions
37 #
38 # We load the Config object from a file
39 #
40 cfg = swarmng.Config.load(args.config)
41 inputFile = cfg["input"]
42 snapshotFileName = inputFile + ".snapshot"
43 
44 #
45 # Here we check for existence of the snapshot file, if it exists, then we load
46 # it instead of the initial conditions.
47 if path.exists(snapshotFileName) :
48  fn = snapshotFileName
49 else :
50  fn = inputFile
51 ext = path.splitext(fn)[1]
52 if ext == "txt" : ens = swarmng.DefaultEnsemble.load_from_text(fn)
54 
55 # \section Resuming the integration
56 # Using some functional features, we can easily calculate the median of
57 # all system times to estimate a starting time for the whole ensemble.
58 times = sorted(map(lambda s : s.time, ens))
59 median_of_system_time = times[len(times)/2]
60 #
61 # We set the starting time to the median of all system times.
62 # We add the duration to the starting time to find a suitable end time.
63 #
64 starting_time = median_of_system_time
65 dt = min(float(cfg["destination_time"]), starting_time + args.duration)
66 print("Integrating from {0} to {1}".format(starting_time, dt))
67 
68 #
69 # Let the integration begin, it is the same as the @ref TutorialPython "first tutorial"
70 swarmng.init(cfg)
71 integ = swarmng.Integrator.create( cfg )
72 integ.ensemble = ens
73 integ.destination_time = dt
74 integ.integrate()
75 
76 #
77 # Finally we have to save the snapshot for resuming the integration later
78 ens.save_to_bin(snapshotFileName)
79 #
80 # \section conc Conclusion
81 #
82 # In this tutorial, we explored how to save/load snapshot files.
83 # You can use snapshot files in many integration schemes
84 # where complex and long simulations are required and
85 # you may need to end them early sometimes.
86 #
87 # Where to go from here:
88 # * Read documentation for @ref swarmng.DefaultEnsemble for details of load/save methods.
89 # * Read documentation for @ref swammng.System and @ref swarmng.Body for other attributes on systems and bodies.
90 # * Follow along to @ref TutorialPythonIntegrationPlot "next tutorial"