Note
Click here to download the full example code
Patch a pre-processor to apply transformation on input variables.¶
from __future__ import annotations
from gemseo.disciplines.analytic import AnalyticDiscipline
from numpy import atleast_1d
from vimseo import EXAMPLE_RUNS_DIR
from vimseo.api import create_model
from vimseo.core.base_integrated_model import IntegratedModel
from vimseo.core.components.discipline_wrapper_component import (
DisciplineWrapperComponent,
)
from vimseo.core.model_settings import IntegratedModelSettings
Instantiate the model to be transformed:
model = create_model("BendingTestAnalytical", "Cantilever")
Define the transformations between new input variables and the existing model inputs:
input_transform = AnalyticDiscipline({"length": "lengthOverWidth*width"})
Optionnaly, define default values for the existing model variables used in the transformations: input_transform.default_input_data.update({"lengthOverWidth": atleast_1d(1.0)}) input_transform.default_input_data.update( {"width": model.default_input_data["width"]} )
Define the transformations between new output variables and the existing model outputs. Note that model outputs or inputs can be used:
output_transform = AnalyticDiscipline({
"dplt_adim_at_force": "dplt_at_force_location/length"
})
# Optionnaly, define default values for the model variables
# used in the transformations:
# output_transform.default_input_data.update(
# {"length": model.default_input_data["length"]}
# )
The transformed space model has the following chain of components: [input_tranform, model.pre_processor, model.run_processor, model.post_processor, output_transform]
model_settings = IntegratedModelSettings(
directory_archive_root=EXAMPLE_RUNS_DIR / "archive/input_space_transform",
directory_scratch_root=EXAMPLE_RUNS_DIR / "scratch/input_space_transform",
cache_file_path=EXAMPLE_RUNS_DIR / "caches/input_space_transform/cache.hdf",
)
transformed_input_model = IntegratedModel(
"Beam_Cantilever",
[
DisciplineWrapperComponent("Beam_Cantilever", input_transform),
*list(model._chain.disciplines),
DisciplineWrapperComponent("Beam_Cantilever", output_transform),
],
**model_settings.model_dump(),
)
The grammar of the transformed model is a SIMPLER grammar (no bounds or type):
print("Transformed model input grammar: ", transformed_input_model.input_grammar)
print("Transformed model output grammar: ", transformed_input_model.output_grammar)
model.cache = None
transformed_input_model.cache = None
Out:
Transformed model input grammar: Grammar name: MDOChain_discipline_input
Transformed model output grammar: Grammar name: AnalyticDiscipline_discipline_output
Execute the transformed model:
output_data = transformed_input_model.execute({
"lengthOverWidth": atleast_1d(2.0),
"width": atleast_1d(10.0),
})
We show the input and output data of the transformed model:
print(transformed_input_model.get_input_data())
print(transformed_input_model.get_output_data())
Out:
{'lengthOverWidth': array([2.]), 'width': array([10.]), 'height': array([40.]), 'imposed_dplt': array([-5.]), 'relative_dplt_location': array([1.]), 'young_modulus': array([210000.]), 'nu_p': array([0.3])}
{<MetaDataNames.error_code: 'error_code'>: array([0]), 'dplt_adim_at_force': array([-0.25]), 'model': array(['IntegratedModel'], dtype='<U15'), 'load_case': array(['Beam_Cantilever'], dtype='<U15'), 'description': array([''], dtype='<U1'), 'job_name': array([''], dtype='<U1'), 'persistent_result_files': array([''], dtype='<U1'), 'n_cpus': array([1]), 'date': array(['2026-05-11 16:51:53.551826'], dtype='<U26'), 'cpu_time': array([0.02248335]), 'user': array(['sebastien.bocquet'], dtype='<U17'), 'machine': array(['IPF7101'], dtype='<U7'), 'vims_git_version': array(['45528c25944ac4bb00d0644363b39dcb47cf0899'], dtype='<U40'), 'directory_archive_root': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/archive/input_space_transform'],
dtype='<U110'), 'directory_archive_job': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/archive/input_space_transform/IntegratedModel/Beam_Cantilever/4'],
dtype='<U144'), 'directory_scratch_root': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/scratch/input_space_transform'],
dtype='<U110'), 'directory_scratch_job': array([''], dtype='<U1')}
And check for correctness of the transformations:
assert (
transformed_input_model._chain.disciplines[1].get_input_data()["length"]
== 2 * transformed_input_model.get_input_data()["width"]
)
assert (
output_data["dplt_adim_at_force"]
== transformed_input_model._chain.disciplines[-2].get_output_data()[
"dplt_at_force_location"
]
/ transformed_input_model._chain.disciplines[1].get_input_data()["length"]
)
Total running time of the script: ( 0 minutes 0.068 seconds)
Download Python source code: plot_04_input_space_transform.py
Download Jupyter notebook: plot_04_input_space_transform.ipynb