-
-
Notifications
You must be signed in to change notification settings - Fork 17
New feature: model adaptivity #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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4d1ea98
first basic model switching impl
Snapex2409 6f6505b
added file based switching and full docstrings
Snapex2409 b49ab5b
fix style issues
Snapex2409 cc6aedf
add missing dep in setup description
Snapex2409 ac2e847
add input to switching fun
Snapex2409 29bafff
integrate adaptivity with new micro sim loading
Snapex2409 50b609f
use pre-commit fix
Snapex2409 75708a1
fix tests
Snapex2409 762b3a3
use pre-commit fix
Snapex2409 0d061b0
clean up and documentation
Snapex2409 01d7c55
Merge branch 'develop' into model-adaptivity
Snapex2409 0b9cadd
fix linting
Snapex2409 38bc30b
Update docs/configuration.md
Snapex2409 0a95e74
Update docs/configuration.md
Snapex2409 cc8ee34
Update docs/model-adaptivity.md
Snapex2409 2b030d1
Update docs/model-adaptivity.md
Snapex2409 0e66284
Update docs/model-adaptivity.md
Snapex2409 f60a7f1
Update model_adaptivity.py
Snapex2409 456df3d
Merge branch 'develop' into model-adaptivity
Snapex2409 a2d59b6
switch to per micro-sim switching func evaluation
Snapex2409 a3f48f4
Apply documentation changes
IshaanDesai 65aa0a2
Apply suggested changes
Snapex2409 f5a4be5
apply linting fixes
Snapex2409 23e5302
Merge remote-tracking branch 'origin/develop' into model-adaptivity
Snapex2409 94e0cfa
Remove package name from proj toml
Snapex2409 825b530
Make changelog entry more descriptive
IshaanDesai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
|
||
| --- | ||
|
|
||
| ## Main Concept | ||
|
|
||
| For scenarios such as FE2 simulations computing all or perhaps only active micro simulations | ||
|
Snapex2409 marked this conversation as resolved.
Outdated
|
||
| 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
|
||
| Towards this the model adaptivity feature allows for the definition of multiple | ||
|
IshaanDesai marked this conversation as resolved.
Outdated
|
||
| model fidelities and the switching between those at run-time. | ||
|
IshaanDesai marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### 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. | ||
|
IshaanDesai marked this conversation as resolved.
Outdated
|
||
|
|
||
| **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: | ||
|
IshaanDesai marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```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) | ||
|
IshaanDesai marked this conversation as resolved.
|
||
| 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. | ||
|
IshaanDesai marked this conversation as resolved.
|
||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.