Creating yMMSL from Python
yMMSL files are YAML files, and yMMSL is usually written in YAML. In fact, even if you
want to create a yMMSL configuration in Python, it’s often convenient to just embed a
string containing a YAML description and converting it to objects using
ymmsl.load().
However, all the classes mentioned here are normal Python classes. They have constructors which you can use to create instances, and their attributes can be changed as needed.
This example shows creating a yMMSL configuration in Python and saving it to a file.
Creating a Configuration and saving it (
docs/from_python.py)from pathlib import Path
from ymmsl.v0_2 import (
Component, Conduit, Configuration, ExecutionModel, Model, MPICoresResReq, Ports,
Program, ThreadedResReq)
from ymmsl import save
components = [
Component(
'macro', Ports(o_i='out', s='in'), 'Macro model',
implementation='macro_model'),
Component(
'micro', Ports(f_init='in', o_f='out'), 'Micro model',
implementation='micro_model')]
conduits = [
Conduit('macro.out', 'micro.in'),
Conduit('micro.out', 'macro.in')]
model = Model(
'my_model', description='Example model created in Python',
components=components, conduits=conduits)
programs = [
Program(
'macro_model', description='Program implementing the macro model',
executable='/home/user/model'),
Program(
'micro_model', description='Micro model program',
modules='GCC/14.1.0 OpenMPI/5.0.3',
execution_model=ExecutionModel.OPENMPI, executable='/home/user/model2')]
resources = [
ThreadedResReq('macro', 1),
MPICoresResReq('micro', 8)]
config = Configuration(
'Example configuration', models=[model], programs=programs,
resources=resources)
save(config, Path('out.ymmsl'))
This will output:
Output of
from_python.py (docs/from_python.ymmsl)ymmsl_version: v0.2
description: |
Example configuration
models:
my_model:
description: |
Example model created in Python
components:
macro:
ports:
o_i: out
s: in
description: |
Macro model
implementation: macro_model
micro:
ports:
f_init: in
o_f: out
description: |
Micro model
implementation: micro_model
conduits:
macro.out: micro.in
micro.out: macro.in
programs:
macro_model:
description: |
Program implementing the macro model
executable: /home/user/model
micro_model:
description: |
Micro model program
modules:
- GCC/14.1.0
- OpenMPI/5.0.3
execution_model: openmpi
executable: /home/user/model2
resources:
macro:
threads: 1
micro:
mpi_processes: 8
Another use case is loading a yMMSL file and modifying it:
Loading, modifying, and saving a yMMSL file (
docs/load_modify_save.py)from pathlib import Path
import ymmsl
config = ymmsl.load(Path('from_python.ymmsl'))
config.settings['d'] = 0.12
ymmsl.save(config, Path('out.ymmsl'))
For more details about these classes and what you can do with them, we refer to the API documentation.