Minimal Example

Testing basic functions of the ctwrap package using the minimal module (which does not use Cantera, but just ‘sleeps’ for a specified amount of seconds).

Set up Simulation object

The Simulation object wraps the module, which can be run with default arguments.

[1]:
import ctwrap as cw
[2]:
sim = cw.Simulation.from_module(cw.modules.minimal)
sim
[2]:
<ctwrap.wrapper.Minimal at 0x7ff6e9463fd0>

Set up Simulation Handler object

The SimulationHandler object uses a YAML file as input.

[3]:
fname = 'minimal.yaml'
sh = cw.SimulationHandler.from_yaml(fname, strategy='sequence', verbosity=1)
sh
Simulations for entry `foo` with values: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,
0.8]
[3]:
<ctwrap.handler.SimulationHandler at 0x7ff6eaeddf90>
[4]:
# list simulation tasks
sh.tasks
[4]:
{'case_0': {'foo': 0.1},
 'case_1': {'foo': 0.2},
 'case_2': {'foo': 0.3},
 'case_3': {'foo': 0.4},
 'case_4': {'foo': 0.5},
 'case_5': {'foo': 0.6},
 'case_6': {'foo': 0.7},
 'case_7': {'foo': 0.8}}

Run serial simulation

The run_serial function loops through simulation tasks one by one.

[5]:
%%time
sh.run_serial(sim, verbosity=0)
    - `minimal`: sleeping for 0.1 * 1 = 0.1 seconds ...
    - `minimal`: sleeping for 0.2 * 1 = 0.2 seconds ...
    - `minimal`: sleeping for 0.3 * 1 = 0.3 seconds ...
    - `minimal`: sleeping for 0.4 * 1 = 0.4 seconds ...
    - `minimal`: sleeping for 0.5 * 1 = 0.5 seconds ...
    - `minimal`: sleeping for 0.6 * 1 = 0.6 seconds ...
    - `minimal`: sleeping for 0.7 * 1 = 0.7 seconds ...
    - `minimal`: sleeping for 0.8 * 1 = 0.8 seconds ...
CPU times: user 11.9 ms, sys: 4.01 ms, total: 15.9 ms
Wall time: 3.61 s
[5]:
True

Run parallel simulation

The run_parallel function uses Python’s multiprocessing to process multiple single-threaded simulation jobs at the same time.

[6]:
%%time
sh.run_parallel(sim, verbosity=1)
 * Starting parallel batch simulation using 1 cores
 * running `case_0` (Process-1)
    - `minimal`: sleeping for 0.1 * 1 = 0.1 seconds ...
 * running `case_1` (Process-1)
    - `minimal`: sleeping for 0.2 * 1 = 0.2 seconds ...
 * running `case_2` (Process-1)
    - `minimal`: sleeping for 0.3 * 1 = 0.3 seconds ...
 * running `case_3` (Process-1)
    - `minimal`: sleeping for 0.4 * 1 = 0.4 seconds ...
 * running `case_4` (Process-1)
    - `minimal`: sleeping for 0.5 * 1 = 0.5 seconds ...
 * running `case_5` (Process-1)
    - `minimal`: sleeping for 0.6 * 1 = 0.6 seconds ...
 * running `case_6` (Process-1)
    - `minimal`: sleeping for 0.7 * 1 = 0.7 seconds ...
 * running `case_7` (Process-1)
    - `minimal`: sleeping for 0.8 * 1 = 0.8 seconds ...
CPU times: user 20.8 ms, sys: 11.4 ms, total: 32.2 ms
Wall time: 3.69 s
[6]:
True

Run Batch Matrix: Set up Simulation Handler object

The SimulationHandler object uses a YAML file as input.

[7]:
fname = 'minimal.yaml'
sh = cw.SimulationHandler.from_yaml(fname, strategy='matrix', verbosity=1)
sh
Simulations for entries ['foo', 'bar']
[7]:
<ctwrap.handler.SimulationHandler at 0x7ff710b5c310>
[8]:
# list simulation tasks
sh.tasks
[8]:
{'case_0': {'foo': 0.1, 'bar': 2},
 'case_1': {'foo': 0.1, 'bar': 1},
 'case_2': {'foo': 0.1, 'bar': 0},
 'case_3': {'foo': 0.2, 'bar': 2},
 'case_4': {'foo': 0.2, 'bar': 1},
 'case_5': {'foo': 0.2, 'bar': 0},
 'case_6': {'foo': 0.3, 'bar': 2},
 'case_7': {'foo': 0.3, 'bar': 1},
 'case_8': {'foo': 0.3, 'bar': 0}}

Run serial simulation

The run_serial function loops through simulation tasks one by one.

[9]:
%%time
sh.run_serial(sim, verbosity=0)
    - `minimal`: sleeping for 0.1 * 2 = 0.2 seconds ...
    - `minimal`: sleeping for 0.1 * 1 = 0.1 seconds ...
    - `minimal`: sleeping for 0.1 * 0 = 0.0 seconds ...
    - `minimal`: sleeping for 0.2 * 2 = 0.4 seconds ...
    - `minimal`: sleeping for 0.2 * 1 = 0.2 seconds ...
    - `minimal`: sleeping for 0.2 * 0 = 0.0 seconds ...
    - `minimal`: sleeping for 0.3 * 2 = 0.6 seconds ...
    - `minimal`: sleeping for 0.3 * 1 = 0.3 seconds ...
    - `minimal`: sleeping for 0.3 * 0 = 0.0 seconds ...
CPU times: user 9.22 ms, sys: 3.57 ms, total: 12.8 ms
Wall time: 1.81 s
[9]:
True

Run parallel simulation

The run_parallel function uses Python’s multiprocessing to process multiple single-threaded simulation jobs at the same time.

[10]:
%%time
sh.run_parallel(sim, verbosity=1)
 * Starting parallel batch simulation using 1 cores
 * running `case_0` (Process-2)
    - `minimal`: sleeping for 0.1 * 2 = 0.2 seconds ...
 * running `case_1` (Process-2)
    - `minimal`: sleeping for 0.1 * 1 = 0.1 seconds ...
 * running `case_2` (Process-2)
    - `minimal`: sleeping for 0.1 * 0 = 0.0 seconds ...
 * running `case_3` (Process-2)
    - `minimal`: sleeping for 0.2 * 2 = 0.4 seconds ...
 * running `case_4` (Process-2)
    - `minimal`: sleeping for 0.2 * 1 = 0.2 seconds ...
 * running `case_5` (Process-2)
    - `minimal`: sleeping for 0.2 * 0 = 0.0 seconds ...
 * running `case_6` (Process-2)
    - `minimal`: sleeping for 0.3 * 2 = 0.6 seconds ...
 * running `case_7` (Process-2)
    - `minimal`: sleeping for 0.3 * 1 = 0.3 seconds ...
 * running `case_8` (Process-2)
    - `minimal`: sleeping for 0.3 * 0 = 0.0 seconds ...
CPU times: user 8.76 ms, sys: 22.1 ms, total: 30.9 ms
Wall time: 1.89 s
[10]:
True