Skip to content
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

Remove the scalar and vector keywords from data names in configuration #142

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## latest

- Remove the `scalar` and `vector` keyword values from data names in configuration https://github.com/precice/micro-manager/pull/142
- Set default logger to stdout and add output directory setting option for file loggers https://github.com/precice/micro-manager/pull/139
- Remove the `adaptivity_data` data structure and handle all adaptivity data internally https://github.com/precice/micro-manager/pull/137
- Improve logging by wrapping Python logger in a class https://github.com/precice/micro-manager/pull/133
Expand Down
8 changes: 4 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ The Micro Manager is configured with a JSON file. An example configuration file
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-mesh",
"read_data_names": {"temperature": "scalar", "heat-flux": "vector"},
"write_data_names": {"porosity": "scalar", "conductivity": "vector"}
"read_data_names": ["temperature", "heat-flux"],
"write_data_names": ["porosity", "conductivity"]
},
"simulation_params": {
"macro_domain_bounds": [0.0, 1.0, 0.0, 1.0, 0.0, 1.0],
Expand All @@ -40,8 +40,8 @@ Parameter | Description
--- | ---
`precice_config_file_name` | Path to the preCICE XML configuration file from the current working directory.
`macro_mesh_name` | Name of the macro mesh as stated in the preCICE configuration.
`read_data_names` | A Python dictionary with the names of the data to be read from preCICE as keys and `"scalar"` or `"vector"` as values depending on the nature of the data.
`write_data_names` | A Python dictionary with the names of the data to be written to preCICE as keys and `"scalar"` or `"vector"` as values depending on the nature of the data.
`read_data_names` | A Python list with the names of the data to be read from preCICE.
`write_data_names` | A Python list with the names of the data to be written to preCICE.
IshaanDesai marked this conversation as resolved.
Show resolved Hide resolved

## Simulation Parameters

Expand Down
4 changes: 2 additions & 2 deletions examples/micro-manager-cpp-adaptivity-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config-adaptivity.xml",
"macro_mesh_name": "macro-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/micro-manager-cpp-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/micro-manager-python-adaptivity-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config-adaptivity.xml",
"macro_mesh_name": "macro-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/micro-manager-python-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
76 changes: 20 additions & 56 deletions micro_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def __init__(self, config_file_name):

self._precice_config_file_name = None
self._macro_mesh_name = None
self._read_data_names = dict()
self._write_data_names = dict()
self._read_data_names = None
self._write_data_names = None
self._micro_dt = None

self._macro_domain_bounds = None
self._ranks_per_axis = None
self._micro_output_n = 1
self._diagnostics_data_names = dict()
self._diagnostics_data_names = None

self._output_micro_sim_time = False

Expand Down Expand Up @@ -103,17 +103,8 @@ def _read_json(self, config_file_name):
try:
self._write_data_names = self._data["coupling_params"]["write_data_names"]
assert isinstance(
self._write_data_names, dict
), "Write data entry is not a dictionary"
for key, value in self._write_data_names.items():
if value == "scalar":
self._write_data_names[key] = False
elif value == "vector":
self._write_data_names[key] = True
else:
raise Exception(
"Write data dictionary as a value other than 'scalar' or 'vector'"
)
self._write_data_names, list
), "Write data entry is not a list"
Comment on lines +106 to +107
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have the schema, such error message will no longer be needed 😊

except BaseException:
self._logger.log_info_one_rank(
"No write data names provided. Micro manager will only read data from preCICE."
Expand All @@ -122,17 +113,8 @@ def _read_json(self, config_file_name):
try:
self._read_data_names = self._data["coupling_params"]["read_data_names"]
assert isinstance(
self._read_data_names, dict
), "Read data entry is not a dictionary"
for key, value in self._read_data_names.items():
if value == "scalar":
self._read_data_names[key] = False
elif value == "vector":
self._read_data_names[key] = True
else:
raise Exception(
"Read data dictionary as a value other than 'scalar' or 'vector'"
)
self._read_data_names, list
), "Read data entry is not a list"
except BaseException:
self._logger.log_info_one_rank(
"No read data names provided. Micro manager will only write data to preCICE."
Expand All @@ -143,7 +125,7 @@ def _read_json(self, config_file_name):
try:
if self._data["diagnostics"]["output_micro_sim_solve_time"] == "True":
self._output_micro_sim_time = True
self._write_data_names["solve_cpu_time"] = False
self._write_data_names.append("solve_cpu_time")
except BaseException:
self._logger.log_info_one_rank(
"Micro manager will not output time required to solve each micro simulation."
Expand Down Expand Up @@ -204,11 +186,11 @@ def read_json_micro_manager(self):
else:
raise Exception("Adaptivity type can be either local or global.")

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

if self._data_for_adaptivity.keys() == self._write_data_names.keys():
if self._data_for_adaptivity == self._write_data_names:
self._logger.log_info_one_rank(
"Only micro simulation data is used for similarity computation in adaptivity. This would lead to the"
" same set of active and inactive simulations for the entire simulation time. If this is not intended,"
Expand Down Expand Up @@ -261,8 +243,8 @@ def read_json_micro_manager(self):
"Micro Manager will compute adaptivity once at the start of every time window"
)

self._write_data_names["active_state"] = False
self._write_data_names["active_steps"] = False
self._write_data_names.append("active_state")
self._write_data_names.append("active_steps")

try:
if (
Expand All @@ -272,7 +254,7 @@ def read_json_micro_manager(self):
== "True"
):
self._adaptivity_output_cpu_time = True
self._write_data_names["adaptivity_cpu_time"] = False
self._write_data_names.append("adaptivity_cpu_time")
except BaseException:
self._logger.log_info_one_rank(
"Micro Manager will not output CPU time of the adaptivity computation."
Expand All @@ -286,7 +268,7 @@ def read_json_micro_manager(self):
== "True"
):
self._adaptivity_output_mem_usage = True
self._write_data_names["adaptivity_mem_usage"] = False
self._write_data_names.append("adaptivity_mem_usage")
except BaseException:
self._logger.log_info_one_rank(
"Micro Manager will not output CPU time of the adaptivity computation."
Expand All @@ -299,17 +281,8 @@ def read_json_micro_manager(self):
try:
diagnostics_data_names = self._data["diagnostics"]["data_from_micro_sims"]
assert isinstance(
diagnostics_data_names, dict
), "Diagnostics data is not a dictionary"
for key, value in diagnostics_data_names.items():
if value == "scalar":
self._write_data_names[key] = False
elif value == "vector":
self._write_data_names[key] = True
else:
raise Exception(
"Diagnostics data dictionary as a value other than 'scalar' or 'vector'"
)
diagnostics_data_names, list
), "Diagnostics data is not a list"
except BaseException:
self._logger.log_info_one_rank(
"No diagnostics data is defined. Micro Manager will not output any diagnostics data."
Expand Down Expand Up @@ -349,17 +322,8 @@ def read_json_snapshot(self):
try:
diagnostics_data_names = self._data["diagnostics"]["data_from_micro_sims"]
assert isinstance(
diagnostics_data_names, dict
), "Diagnostics data is not a dictionary"
for key, value in diagnostics_data_names.items():
if value == "scalar":
self._write_data_names[key] = False
elif value == "vector":
self._write_data_names[key] = True
else:
raise Exception(
"Diagnostics data dictionary has a value other than 'scalar' or 'vector'"
)
diagnostics_data_names, list
), "Diagnostics data is not a list"
except BaseException:
self._logger.log_info_one_rank(
"No diagnostics data is defined. Snapshot computation will not output any diagnostics data."
Expand Down
35 changes: 12 additions & 23 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ def __init__(self, config_file: str) -> None:
self._adaptivity_data_names = self._config.get_data_for_adaptivity()

# Names of macro data to be used for adaptivity computation
self._adaptivity_macro_data_names = dict()
self._adaptivity_macro_data_names: list = []

# Names of micro data to be used for adaptivity computation
self._adaptivity_micro_data_names = dict()
for name, is_data_vector in self._adaptivity_data_names.items():
self._adaptivity_micro_data_names: list = []
for name in self._adaptivity_data_names:
if name in self._read_data_names:
self._adaptivity_macro_data_names[name] = is_data_vector
self._adaptivity_macro_data_names.append(name)
if name in self._write_data_names:
self._adaptivity_micro_data_names[name] = is_data_vector
self._adaptivity_micro_data_names.append(name)

self._adaptivity_in_every_implicit_step = (
self._config.is_adaptivity_required_in_every_implicit_iteration()
Expand Down Expand Up @@ -356,20 +356,8 @@ def initialize(self) -> None:
)

if self._is_adaptivity_on:
for name, is_data_vector in self._adaptivity_data_names.items():
if is_data_vector:
self._data_for_adaptivity[name] = np.zeros(
(
self._local_number_of_sims,
self._participant.get_data_dimensions(
self._macro_mesh_name, name
),
)
)
else:
self._data_for_adaptivity[name] = np.zeros(
(self._local_number_of_sims)
)
for name in self._adaptivity_data_names:
self._data_for_adaptivity[name] = np.zeros((self._local_number_of_sims))

# Create lists of local and global IDs
sim_id = np.sum(nms_all_ranks[: self._rank])
Expand Down Expand Up @@ -572,10 +560,11 @@ def _read_data_from_precice(self, dt) -> list:
List of dicts in which keys are names of data being read and the values are the data from preCICE.
"""
read_data: Dict[str, list] = dict()
for name in self._read_data_names.keys():

for name in self._read_data_names:
read_data[name] = []

for name in self._read_data_names.keys():
for name in self._read_data_names:
read_data.update(
{
name: self._participant.read_data(
Expand Down Expand Up @@ -608,15 +597,15 @@ def _write_data_to_precice(self, data: list) -> None:
for name, values in d.items():
data_dict[name].append(values)

for dname in self._write_data_names.keys():
for dname in self._write_data_names:
self._participant.write_data(
self._macro_mesh_name,
dname,
self._mesh_vertex_ids,
data_dict[dname],
)
else:
for dname in self._write_data_names.keys():
for dname in self._write_data_names:
self._participant.write_data(
self._macro_mesh_name, dname, [], np.array([])
)
Expand Down
14 changes: 7 additions & 7 deletions micro_manager/snapshot/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,17 @@ def read_hdf(self, file_path: str, data_names: dict, start: int, end: int) -> li
parameter_data = dict()
output = []
# Read data by iterating over the relevant datasets
for key in data_names.keys():
parameter_data[key] = np.asarray(parameter_file[key][start:end])
my_key = (
key # Save one key to be able to iterate over the length of the data
for name in data_names:
parameter_data[name] = np.asarray(parameter_file[name][start:end])
my_name = (
name # Save one name to be able to iterate over the length of the data
)
# Iterate over len of data. In each iteration write data from all macro data sets
# to a dictionary and append it to the output list of dicts.
for i in range(len(parameter_data[my_key])):
for i in range(len(parameter_data[my_name])):
current_data = dict()
for key in data_names.keys():
current_data[key] = parameter_data[key][i]
for name in data_names:
current_data[name] = parameter_data[name][i]
output.append(current_data)
return output

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "precice-config.xml",
"macro_mesh_name": "macro-cube-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 1.0,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/micro-manager-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"coupling_params": {
"precice_config_file_name": "dummy-config.xml",
"macro_mesh_name": "dummy-macro-mesh",
"read_data_names": {"macro-scalar-data": "scalar", "macro-vector-data": "vector"},
"write_data_names": {"micro-scalar-data": "scalar", "micro-vector-data": "vector"}
"read_data_names": ["macro-scalar-data", "macro-vector-data"],
"write_data_names": ["micro-scalar-data", "micro-vector-data"]
},
"simulation_params": {
"micro_dt": 0.1,
Expand Down
Loading
Loading