-
-
Notifications
You must be signed in to change notification settings - Fork 12
first basic model switching impl #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 12 commits
4d1ea98
6f6505b
b49ab5b
cc6aedf
ac2e847
29bafff
50b609f
75708a1
762b3a3
0d061b0
01d7c55
0b9cadd
38bc30b
0a95e74
cc8ee34
2b030d1
0e66284
f60a7f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,163 @@ | ||||||||||
| --- | ||||||||||
| title: Adaptive switching of simulation models | ||||||||||
| permalink: tooling-micro-manager-model-adaptivity.html | ||||||||||
| keywords: tooling, macro-micro, two-scale, model-adaptivity | ||||||||||
| summary: Micro Manager can switch micro models adaptively. | ||||||||||
Snapex2409 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| --- | ||||||||||
|
|
||||||||||
| ## Main Concept | ||||||||||
|
|
||||||||||
| For scenarios such as FE2 simulations computing all or perhaps only active micro simulations | ||||||||||
Snapex2409 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| may be still computationally infeasible. The alternative here is to reduce model complexity via reduced order models (ROMs) or similar. | ||||||||||
Snapex2409 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| Towards this the model adaptivity feature allows for the definition of multiple | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| model fidelities and the switching between those at run-time. | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ### Iterative Process | ||||||||||
|
|
||||||||||
| **Without** model adaptivity the call to `micro_sim_solve(micro_sims_input, dt)` within the micro_manager would just | ||||||||||
| provide each micro simulation with its input, solve it and return the output. | ||||||||||
|
Comment on lines
+17
to
+18
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| **With** model adaptivity this becomes an iterative process, as a model may not be sufficiently accurate (given the current input). | ||||||||||
| Thus, the call to `micro_sim_solve(micro_sims_input, dt)` with model adaptivity results in the following: | ||||||||||
|
Comment on lines
+20
to
+21
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| ```python | ||||||||||
| self._model_adaptivity_controller.initialise_solve() | ||||||||||
|
|
||||||||||
| active_sim_ids = None | ||||||||||
| if self._is_adaptivity_on: | ||||||||||
| active_sim_ids = self._adaptivity_controller.get_active_sim_local_ids() | ||||||||||
| output = None | ||||||||||
|
|
||||||||||
| while self._model_adaptivity_controller.should_iterate(): | ||||||||||
| self._model_adaptivity_controller.switch_models( | ||||||||||
| self._mesh_vertex_coords, | ||||||||||
| self._t, | ||||||||||
| micro_sims_input, | ||||||||||
| output, | ||||||||||
| self._micro_sims, | ||||||||||
| active_sim_ids, | ||||||||||
| ) | ||||||||||
| output = solve_variant(micro_sims_input, dt) | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this |
||||||||||
| self._model_adaptivity_controller.check_convergence( | ||||||||||
| self._mesh_vertex_coords, | ||||||||||
| self._t, | ||||||||||
| micro_sims_input, | ||||||||||
| output, | ||||||||||
| self._micro_sims, | ||||||||||
| active_sim_ids, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| self._model_adaptivity_controller.finalise_solve() | ||||||||||
| return output | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| Here, after initialization and active sim acquisition, models will be switched, evaluated and checked for convergence | ||||||||||
| as long as the `switching_function` contains values other than 0. | ||||||||||
| Model evaluation - in the call `solve_variant(micro_sims_input, dt)` - is delegated to the regular | ||||||||||
| (non-model-adaptive) `micro_sim_solve(micro_sims_input, dt)` method. | ||||||||||
|
|
||||||||||
| ### Interfaces | ||||||||||
|
|
||||||||||
| ```python | ||||||||||
| class MicroSimulation: # Name is fixed | ||||||||||
| def __init__(self, sim_id): | ||||||||||
| """ | ||||||||||
| Constructor of class MicroSimulation. | ||||||||||
| Parameters | ||||||||||
| ---------- | ||||||||||
| sim_id : int | ||||||||||
| ID of the simulation instance, that the Micro Manager has set for it. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def initialize(self) -> dict: | ||||||||||
| """ | ||||||||||
| Initialize the micro simulation and return initial data which will be used in computing adaptivity before the first time step. | ||||||||||
| Defining this function is OPTIONAL. | ||||||||||
| Returns | ||||||||||
| ------- | ||||||||||
| initial_data : dict | ||||||||||
| Dictionary with names of initial data as keys and the initial data itself as values. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def solve(self, macro_data: dict, dt: float) -> dict: | ||||||||||
| """ | ||||||||||
| Solve one time step of the micro simulation for transient problems or solve until steady state for steady-state problems. | ||||||||||
| Parameters | ||||||||||
| ---------- | ||||||||||
| macro_data : dict | ||||||||||
| Dictionary with names of macro data as keys and the data as values. | ||||||||||
| dt : float | ||||||||||
| Current time step size. | ||||||||||
| Returns | ||||||||||
| ------- | ||||||||||
| micro_data : dict | ||||||||||
| Dictionary with names of micro data as keys and the updated micro data a values. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def set_state(self, state): | ||||||||||
| """ | ||||||||||
| Set the state of the micro simulation. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def get_state(self): | ||||||||||
| """ | ||||||||||
| Return the state of the micro simulation. | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| def output(self): | ||||||||||
| """ | ||||||||||
| This function writes output of the micro simulation in some form. | ||||||||||
| It will be called with frequency set by configuration option `simulation_params: micro_output_n` | ||||||||||
| This function is *optional*. | ||||||||||
| """ | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| For this the default MicroSimulation still serves as the model interface, while the `(set)|(get)_state()` methods | ||||||||||
| are called to transfer internal model parameters from one to another. | ||||||||||
| The list of provided models is interpreted in decreasing fidelity order. In other words, the first one | ||||||||||
| is likely to be the full order model, while subsequent ones are ROMs. | ||||||||||
|
|
||||||||||
| ```python | ||||||||||
| def switching_function( | ||||||||||
| resolutions: np.ndarray, | ||||||||||
| locations: np.ndarray, | ||||||||||
| t: float, | ||||||||||
| inputs: list[dict], | ||||||||||
| prev_output: dict, | ||||||||||
| active: np.ndarray, | ||||||||||
| ) -> np.ndarray: | ||||||||||
| """ | ||||||||||
| Switching interface function, use as reference | ||||||||||
| Parameters | ||||||||||
| ---------- | ||||||||||
| resolutions : np.array - shape(N,) | ||||||||||
| Array with resolution information as get_sim_class_resolution would return for a sim obj. | ||||||||||
| locations : np.array - shape(N,D) | ||||||||||
| Array with gaussian points for all sims. D is the mesh dimension. | ||||||||||
|
Comment on lines
+141
to
+142
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about having macro locations as part of the switching function inputs. I could imagine the switching being solely based on the macro- and micro-scale data available at a particular point.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it might be useful to enforce FOM at certain locations, where the user can already expect high fluctuations in input/output or where maximal accuracy should be guaranteed.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that should be possible and required. But my comment was still about why the macro-scale coordinates need to be passed to the switching function. I look at the switching function as a function to be called individually at each location. If the switching mechanism requires a macro-scale location, it can be passed on a per-micro-scale simulation basis. I think having a single call-type switching mechanism is beneficial for the user. |
||||||||||
| t : float | ||||||||||
| Current time in simulation. | ||||||||||
| inputs : list[dict] | ||||||||||
| List of input objects. | ||||||||||
| prev_output : [None, dict-like] | ||||||||||
| Contains the outputs of the previous model evaluation. | ||||||||||
| active : np.array - shape(N,) | ||||||||||
| Bool array indicating whether the model is active or not. | ||||||||||
| """ | ||||||||||
| return np.zeros_like(resolutions) | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| The switching of models is governed by the `switching_function`. | ||||||||||
| The output is expected to be a np.ndarray of shape (N,) and is interpreted in the following manner: | ||||||||||
|
|
||||||||||
| Value | Action | ||||||||||
| --- | --- | ||||||||||
| 0 | No resolution change | ||||||||||
| -1 | Increase model fidelity by one (go back one in list) | ||||||||||
| 1 | Decrease model fidelity by one (go one ahead in list) | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def switching_function(resolutions, locations, t, inputs, prev_output, active): | ||
| output = np.zeros_like(resolutions) | ||
| mask_loc = locations[:, 0] % 2 == 0 | ||
| mask_res = resolutions == 0 | ||
| output[mask_loc * mask_res] = 1 | ||
| return output |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "micro_file_name": "python-dummy/micro_dummy", | ||
| "coupling_params": { | ||
| "precice_config_file_name": "precice-config.xml", | ||
| "macro_mesh_name": "macro-mesh", | ||
| "read_data_names": ["macro-scalar-data", "macro-vector-data"], | ||
| "write_data_names": ["micro-scalar-data", "micro-vector-data"] | ||
| }, | ||
| "simulation_params": { | ||
| "micro_dt": 1.0, | ||
| "macro_domain_bounds": [0.0, 25.0, 0.0, 25.0, 0.0, 25.0], | ||
| "model_adaptivity": true, | ||
| "model_adaptivity_settings": { | ||
| "micro_file_names": ["python-dummy/micro_dummy", "python-dummy/micro_dummy", "python-dummy/micro_dummy"], | ||
| "switching_function": "mada_switcher" | ||
| } | ||
| }, | ||
| "diagnostics": { | ||
| "output_micro_sim_solve_time": true | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.