Skip to content

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: IntegratedModel_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])}
{'length': array([20.]), <MetaDataNames.error_code: 'error_code'>: array([0]), 'quadratic_moment': array([53333.33333333]), 'imposed_dplt_location': array([20.]), 'reaction_forces': array([-21000000.]), 'boundary': array([0.]), 'solver': array(['IVP'], dtype='<U3'), 'moment': array([4.2e+08, 0.0e+00]), 'moment_grid': array([ 0., 20.]), 'dplt': array([ 0.00000000e+00, -7.62651513e-04, -3.04029995e-03, -6.81748616e-03,
       -1.20787510e-02, -1.88086353e-02, -2.69916799e-02, -3.66124257e-02,
       -4.76554134e-02, -6.01051841e-02, -7.39462784e-02, -8.91632373e-02,
       -1.05740602e-01, -1.23662912e-01, -1.42914710e-01, -1.63480535e-01,
       -1.85344930e-01, -2.08492434e-01, -2.32907588e-01, -2.58574934e-01,
       -2.85479012e-01, -3.13604363e-01, -3.42935528e-01, -3.73457048e-01,
       -4.05153463e-01, -4.38009315e-01, -4.72009144e-01, -5.07137491e-01,
       -5.43378897e-01, -5.80717902e-01, -6.19139049e-01, -6.58626877e-01,
       -6.99165927e-01, -7.40740741e-01, -7.83335858e-01, -8.26935821e-01,
       -8.71525169e-01, -9.17088444e-01, -9.63610186e-01, -1.01107494e+00,
       -1.05946724e+00, -1.10877163e+00, -1.15897265e+00, -1.21005484e+00,
       -1.26200274e+00, -1.31480090e+00, -1.36843385e+00, -1.42288614e+00,
       -1.47814230e+00, -1.53418688e+00, -1.59100442e+00, -1.64857946e+00,
       -1.70689653e+00, -1.76594019e+00, -1.82569497e+00, -1.88614540e+00,
       -1.94727605e+00, -2.00907143e+00, -2.07151610e+00, -2.13459459e+00,
       -2.19829145e+00, -2.26259122e+00, -2.32747844e+00, -2.39293764e+00,
       -2.45895337e+00, -2.52551018e+00, -2.59259259e+00, -2.66018516e+00,
       -2.72827242e+00, -2.79683891e+00, -2.86586918e+00, -2.93534776e+00,
       -3.00525920e+00, -3.07558804e+00, -3.14631882e+00, -3.21743607e+00,
       -3.28892434e+00, -3.36076818e+00, -3.43295211e+00, -3.50546069e+00,
       -3.57827845e+00, -3.65138993e+00, -3.72477968e+00, -3.79843224e+00,
       -3.87233214e+00, -3.94646393e+00, -4.02081214e+00, -4.09536133e+00,
       -4.17009602e+00, -4.24500077e+00, -4.32006011e+00, -4.39525857e+00,
       -4.47058072e+00, -4.54601107e+00, -4.62153419e+00, -4.69713459e+00,
       -4.77279684e+00, -4.84850546e+00, -4.92424500e+00, -5.00000000e+00]), 'dplt_grid': array([ 0.        ,  0.2020202 ,  0.4040404 ,  0.60606061,  0.80808081,
        1.01010101,  1.21212121,  1.41414141,  1.61616162,  1.81818182,
        2.02020202,  2.22222222,  2.42424242,  2.62626263,  2.82828283,
        3.03030303,  3.23232323,  3.43434343,  3.63636364,  3.83838384,
        4.04040404,  4.24242424,  4.44444444,  4.64646465,  4.84848485,
        5.05050505,  5.25252525,  5.45454545,  5.65656566,  5.85858586,
        6.06060606,  6.26262626,  6.46464646,  6.66666667,  6.86868687,
        7.07070707,  7.27272727,  7.47474747,  7.67676768,  7.87878788,
        8.08080808,  8.28282828,  8.48484848,  8.68686869,  8.88888889,
        9.09090909,  9.29292929,  9.49494949,  9.6969697 ,  9.8989899 ,
       10.1010101 , 10.3030303 , 10.50505051, 10.70707071, 10.90909091,
       11.11111111, 11.31313131, 11.51515152, 11.71717172, 11.91919192,
       12.12121212, 12.32323232, 12.52525253, 12.72727273, 12.92929293,
       13.13131313, 13.33333333, 13.53535354, 13.73737374, 13.93939394,
       14.14141414, 14.34343434, 14.54545455, 14.74747475, 14.94949495,
       15.15151515, 15.35353535, 15.55555556, 15.75757576, 15.95959596,
       16.16161616, 16.36363636, 16.56565657, 16.76767677, 16.96969697,
       17.17171717, 17.37373737, 17.57575758, 17.77777778, 17.97979798,
       18.18181818, 18.38383838, 18.58585859, 18.78787879, 18.98989899,
       19.19191919, 19.39393939, 19.5959596 , 19.7979798 , 20.        ]), 'dplt_at_force_location': array([-5.]), 'maximum_dplt': array([-5.]), 'location_max_dplt': array([20.]), '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-09-03 23:52:32.964169'], dtype='<U26'), 'cpu_time': array([0.02663469]), 'user': array(['sebastien.bocquet'], dtype='<U17'), 'machine': array(['PCBM20250036'], dtype='<U12'), 'vims_git_version': array(['b7d1160379511fbe2c21194e6fa38c68e33d88e2'], dtype='<U40'), 'directory_archive_root': array(['C:\\Users\\sebastien.bocquet\\Dev\\vimseo\\docs\\runnable_examples\\model_runs\\archive\\input_space_transform'],
      dtype='<U101'), 'directory_archive_job': array(['C:\\Users\\sebastien.bocquet\\Dev\\vimseo\\docs\\runnable_examples\\model_runs\\archive\\input_space_transform\\IntegratedModel\\Beam_Cantilever\\17'],
      dtype='<U136'), 'directory_scratch_root': array(['C:\\Users\\sebastien.bocquet\\Dev\\vimseo\\docs\\runnable_examples\\model_runs\\scratch\\input_space_transform'],
      dtype='<U101'), '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.174 seconds)

Download Python source code: plot_04_input_space_transform.py

Download Jupyter notebook: plot_04_input_space_transform.ipynb

Gallery generated by mkdocs-gallery