Skip to content

Commit

Permalink
Add switch to turn adaptivity on and off in configuration (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
IshaanDesai authored Apr 10, 2024
1 parent 6b721ec commit e6641ba
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
7 changes: 4 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ The Micro Manager can adaptively control micro simulations. The adaptivity strat

All the adaptivity parameters are chosen from the second publication.

To turn on adaptivity, the following options need to be set in `simulation_params` under the sub-heading `adaptivity`:
To turn on adaptivity, set `"adaptivity": True` in `simulation_params`. Then under `adaptivity_settings` set the following variables:

Parameter | Description
--- | ---
Expand All @@ -119,12 +119,13 @@ Parameter | Description
`every_implicit_iteration` | If True, adaptivity is calculated in every implicit iteration. <br> If False, adaptivity is calculated once at the start of the time window and then reused in every implicit time iteration.
`similarity_measure`| Similarity measure to be used for adaptivity. Can be either `L1`, `L2`, `L1rel` or `L2rel`. By default, `L1` is used. The `rel` variants calculate the respective relative norms. This parameter is *optional*.

Example of adaptivity configuration
Example of adaptivity configuration is

```json
"simulation_params": {
"macro_domain_bounds": [0, 1, 0, 1, 0, 1],
"adaptivity" {
"adaptivity": "True",
"adaptivity_settings" {
"type": "local",
"data": ["temperature", "porosity"],
"history_param": 0.5,
Expand Down
3 changes: 2 additions & 1 deletion examples/micro-manager-adaptivity-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"simulation_params": {
"macro_domain_bounds": [0.0, 25.0, 0.0, 25.0, 0.0, 25.0],
"adaptivity": {
"adaptivity": "True",
"adaptivity_settings": {
"type": "local",
"data": ["macro-scalar-data", "macro-vector-data"],
"history_param": 0.5,
Expand Down
34 changes: 21 additions & 13 deletions micro_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,33 @@ def read_json(self, config_filename):
)

try:
if data["simulation_params"]["adaptivity"]:
if data["simulation_params"]["adaptivity"] == "True":
self._adaptivity = True
if not data["simulation_params"]["adaptivity_settings"]:
raise Exception(
"Adaptivity is turned on but no adaptivity settings are provided."
)
else:
self._adaptivity = False
if data["simulation_params"]["adaptivity_settings"]:
raise Exception(
"Adaptivity settings are provided but adaptivity is turned off."
)
except BaseException:
self._logger.info(
"Micro Manager will not adaptively run micro simulations, but instead will run all micro simulations in all time steps."
"Micro Manager will not adaptively run micro simulations, but instead will run all micro simulations."
)

if self._adaptivity:
if data["simulation_params"]["adaptivity"]["type"] == "local":
if data["simulation_params"]["adaptivity_settings"]["type"] == "local":
self._adaptivity_type = "local"
elif data["simulation_params"]["adaptivity"]["type"] == "global":
elif data["simulation_params"]["adaptivity_settings"]["type"] == "global":
self._adaptivity_type = "global"
else:
raise Exception("Adaptivity type can be either local or global.")

exchange_data = {**self._read_data_names, **self._write_data_names}
for dname in data["simulation_params"]["adaptivity"]["data"]:
for dname in data["simulation_params"]["adaptivity_settings"]["data"]:
self._data_for_adaptivity[dname] = exchange_data[dname]

if self._data_for_adaptivity.keys() == self._write_data_names.keys():
Expand All @@ -152,19 +160,19 @@ def read_json(self, config_filename):
" please include macro simulation data as well."
)

self._adaptivity_history_param = data["simulation_params"]["adaptivity"][
"history_param"
]
self._adaptivity_history_param = data["simulation_params"][
"adaptivity_settings"
]["history_param"]
self._adaptivity_coarsening_constant = data["simulation_params"][
"adaptivity"
"adaptivity_settings"
]["coarsening_constant"]
self._adaptivity_refining_constant = data["simulation_params"][
"adaptivity"
"adaptivity_settings"
]["refining_constant"]

if "similarity_measure" in data["simulation_params"]["adaptivity"]:
if "similarity_measure" in data["simulation_params"]["adaptivity_settings"]:
self._adaptivity_similarity_measure = data["simulation_params"][
"adaptivity"
"adaptivity_settings"
]["similarity_measure"]
else:
self._logger.info(
Expand All @@ -173,7 +181,7 @@ def read_json(self, config_filename):
self._adaptivity_similarity_measure = "L1"

adaptivity_every_implicit_iteration = data["simulation_params"][
"adaptivity"
"adaptivity_settings"
]["every_implicit_iteration"]

if adaptivity_every_implicit_iteration == "True":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"simulation_params": {
"macro_domain_bounds": [0, 1, 0, 1, 0, 1],
"decomposition": [2, 1, 1],
"adaptivity": {
"adaptivity": "True",
"adaptivity_settings": {
"type": "global",
"data": ["macro-scalar-data", "micro-vector-data"],
"history_param": 0.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"simulation_params": {
"macro_domain_bounds": [0, 1, 0, 1, 0, 1],
"adaptivity": {
"adaptivity": "True",
"adaptivity_settings": {
"type": "global",
"data": ["macro-scalar-data", "macro-vector-data"],
"history_param": 0.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"simulation_params": {
"macro_domain_bounds": [0, 1, 0, 1, 0, 1],
"adaptivity": {
"adaptivity": "True",
"adaptivity_settings": {
"type": "local",
"data": ["macro-scalar-data", "macro-vector-data"],
"history_param": 0.5,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/micro-manager-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"simulation_params": {
"macro_domain_bounds": [0.0, 25.0, 0.0, 25.0, 0.0, 25.0],
"adaptivity": {
"adaptivity": "True",
"adaptivity_settings": {
"type": "local",
"data": ["macro-scalar-data", "macro-vector-data"],
"history_param": 0.5,
Expand Down

0 comments on commit e6641ba

Please sign in to comment.