Note
Click here to download the full example code
A model composition that adds a post-processor to a model.¶
A new model is created by adding a post-processor to an existing model instance. The cache of the existing model is reused while running the composed model.
from __future__ import annotations
from numpy import atleast_1d
from vimseo import EXAMPLE_RUNS_DIR
from vimseo.api import create_model
from vimseo.core.components.post.post_processor import PostProcessor
from vimseo.core.model_settings import IntegratedModelSettings
A model is created and executed.
model = create_model(
"BendingTestAnalytical",
"Cantilever",
model_options=IntegratedModelSettings(
directory_archive_root=EXAMPLE_RUNS_DIR / "archive/post_processing_patch",
directory_scratch_root=EXAMPLE_RUNS_DIR / "scratch/post_processing_patch",
cache_file_path=EXAMPLE_RUNS_DIR
/ "caches/post_processing_patch/no_post_cache.hdf",
),
)
model.execute()
Out:
{'young_modulus': array([210000.]), 'nu_p': array([0.3]), 'length': array([600.]), 'width': array([30.]), 'height': array([40.]), 'imposed_dplt': array([-5.]), 'relative_dplt_location': array([1.]), 'cpu_time': array([0.01883388]), 'date': array(['2026-03-26 14:54:13.273055'], dtype='<U26'), 'description': array([''], dtype='<U1'), 'directory_archive_job': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/archive/post_processing_patch/BendingTestAnalytical/Cantilever/1'],
dtype='<U145'), 'directory_archive_root': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/archive/post_processing_patch'],
dtype='<U110'), 'directory_scratch_job': array([''], dtype='<U1'), 'directory_scratch_root': array(['/home/sebastien.bocquet/PycharmProjects/vimseo/docs/runnable_examples/model_runs/scratch/post_processing_patch'],
dtype='<U110'), '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_at_force_location': array([-5.]), 'dplt_grid': array([ 0. , 6.06060606, 12.12121212, 18.18181818,
24.24242424, 30.3030303 , 36.36363636, 42.42424242,
48.48484848, 54.54545455, 60.60606061, 66.66666667,
72.72727273, 78.78787879, 84.84848485, 90.90909091,
96.96969697, 103.03030303, 109.09090909, 115.15151515,
121.21212121, 127.27272727, 133.33333333, 139.39393939,
145.45454545, 151.51515152, 157.57575758, 163.63636364,
169.6969697 , 175.75757576, 181.81818182, 187.87878788,
193.93939394, 200. , 206.06060606, 212.12121212,
218.18181818, 224.24242424, 230.3030303 , 236.36363636,
242.42424242, 248.48484848, 254.54545455, 260.60606061,
266.66666667, 272.72727273, 278.78787879, 284.84848485,
290.90909091, 296.96969697, 303.03030303, 309.09090909,
315.15151515, 321.21212121, 327.27272727, 333.33333333,
339.39393939, 345.45454545, 351.51515152, 357.57575758,
363.63636364, 369.6969697 , 375.75757576, 381.81818182,
387.87878788, 393.93939394, 400. , 406.06060606,
412.12121212, 418.18181818, 424.24242424, 430.3030303 ,
436.36363636, 442.42424242, 448.48484848, 454.54545455,
460.60606061, 466.66666667, 472.72727273, 478.78787879,
484.84848485, 490.90909091, 496.96969697, 503.03030303,
509.09090909, 515.15151515, 521.21212121, 527.27272727,
533.33333333, 539.39393939, 545.45454545, 551.51515152,
557.57575758, 563.63636364, 569.6969697 , 575.75757576,
581.81818182, 587.87878788, 593.93939394, 600. ]), 'error_code': array([0]), 'job_name': array([''], dtype='<U1'), 'load_case': array(['Cantilever'], dtype='<U10'), 'location_max_dplt': array([600.]), 'machine': array(['IPF7101'], dtype='<U7'), 'maximum_dplt': array([-5.]), 'model': array(['BendingTestAnalytical'], dtype='<U21'), 'moment': array([1400000., 0.]), 'moment_grid': array([ 0., 600.]), 'n_cpus': array([1]), 'persistent_result_files': array([''], dtype='<U1'), 'reaction_forces': array([-2333.33333333]), 'user': array(['sebastien.bocquet'], dtype='<U17'), 'vims_git_version': array(['d0050d8de498c8354552cdde540fb3e524454871'], dtype='<U40')}
Then, we want to add a post-processing, without re-running the model. We first create the post-processor.
class MyPost(PostProcessor):
default_grammar_type = "SimpleGrammar"
def __init__(self, **options):
super().__init__(**options)
self.input_grammar.update_from_names(
list(model.output_grammar.names) + list(model.input_grammar.names)
)
self.output_grammar.update_from_data({"relative_max_dplt": atleast_1d(0.0)})
def _run(self, input_data):
output_data = {}
output_data["relative_max_dplt"] = (
input_data["dplt_at_force_location"] / input_data["length"]
)
return output_data
post_processor = MyPost()
Then create a new model chaining the above model and the post-processor: This model is declared as a new model composition:
class BendingTestWithPost(ModelComposition):
"""A model composition that adds a post-processor to the bending test analytical"""
problems.sandbox subpackage. module.
model_with_post = create_model(
"BendingTestWithPost",
"Cantilever",
base_model=model,
post_components=[post_processor],
model_options=IntegratedModelSettings(
directory_archive_root=EXAMPLE_RUNS_DIR / "archive/post_processing_patch",
directory_scratch_root=EXAMPLE_RUNS_DIR / "scratch/post_processing_patch",
cache_file_path=EXAMPLE_RUNS_DIR
/ "caches/post_processing_patch/with_post_cache.hdf",
),
)
The composed model is executed. Note that the cache is reused, so no computation is done here. Only the new post-processing is computed.
output_data = model_with_post.execute()
print("Relative max displacement:", output_data["relative_max_dplt"])
Out:
Relative max displacement: [-0.00833333]
Total running time of the script: ( 0 minutes 0.056 seconds)
Download Python source code: plot_041_post_processing_patch.py
Download Jupyter notebook: plot_041_post_processing_patch.ipynb