micro-manager-precice 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,46 @@
1
+ Metadata-Version: 2.1
2
+ Name: micro-manager-precice
3
+ Version: 0.0.0
4
+ Summary: A tool which facilitates two-scale macro-micro coupled simulations using preCICE.
5
+ Author-email: The preCICE Developers <info@precice.org>
6
+ Maintainer-email: Ishaan Desai <ishaan.desai@ipvs.uni-stuttgart.de>
7
+ License: GPLv3
8
+ Project-URL: Homepage, https://precice.org
9
+ Project-URL: Documentation, https://precice.org/tooling-micro-manager-overview.html
10
+ Project-URL: Repository, https://github.com/precice/micro-manager
11
+ Project-URL: Bug Tracker, https://github.com/precice/micro-manager/issues
12
+ Keywords: preCICE,multiscale,coupling
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.8
17
+ Classifier: Topic :: Scientific/Engineering
18
+ Requires-Python: >=3.8
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: pyprecice>=3.0.0.0
22
+ Requires-Dist: numpy
23
+ Requires-Dist: mpi4py
24
+ Requires-Dist: scikit-learn
25
+
26
+ # Micro Manager
27
+
28
+ <a style="text-decoration: none" href="https://github.com/precice/micro-manager/blob/develop/LICENSE" target="_blank">
29
+ <img src="https://img.shields.io/github/license/precice/micro-manager.svg" alt="GNU LGPL license">
30
+ </a>
31
+
32
+ <a style="text-decoration: none" href="https://github.com/precice/fenics-adapter/actions/workflows/build-and-test.yml" target="_blank">
33
+ <img src="https://github.com/precice/micro-manager/actions/workflows/run-adaptivity-test.yml/badge.svg" alt="Test Adaptivity">
34
+ </a>
35
+
36
+ <a style="text-decoration: none" href="https://pypi.org/project/micro-manager-precice/" target="_blank">
37
+ <img src="https://github.com/precice/micro-manager/actions/workflows/pythonpublish.yml/badge.svg" alt="Upload Python Package">
38
+ </a>
39
+
40
+ [![DOI](https://joss.theoj.org/papers/10.21105/joss.05842/status.svg)](https://doi.org/10.21105/joss.05842)
41
+
42
+ A tool to facilitate solving two-scale (macro-micro) coupled problems using the coupling library [preCICE](https://precice.org/).
43
+
44
+ The main documentation is rendered on the [preCICE website](https://precice.org/tooling-micro-manager-overview.html).
45
+
46
+ Please report any [issues](https://github.com/precice/micro-manager/issues) and give us feedback through [one of our community channels](https://precice.org/community-channels.html).
@@ -0,0 +1,21 @@
1
+ # Micro Manager
2
+
3
+ <a style="text-decoration: none" href="https://github.com/precice/micro-manager/blob/develop/LICENSE" target="_blank">
4
+ <img src="https://img.shields.io/github/license/precice/micro-manager.svg" alt="GNU LGPL license">
5
+ </a>
6
+
7
+ <a style="text-decoration: none" href="https://github.com/precice/fenics-adapter/actions/workflows/build-and-test.yml" target="_blank">
8
+ <img src="https://github.com/precice/micro-manager/actions/workflows/run-adaptivity-test.yml/badge.svg" alt="Test Adaptivity">
9
+ </a>
10
+
11
+ <a style="text-decoration: none" href="https://pypi.org/project/micro-manager-precice/" target="_blank">
12
+ <img src="https://github.com/precice/micro-manager/actions/workflows/pythonpublish.yml/badge.svg" alt="Upload Python Package">
13
+ </a>
14
+
15
+ [![DOI](https://joss.theoj.org/papers/10.21105/joss.05842/status.svg)](https://doi.org/10.21105/joss.05842)
16
+
17
+ A tool to facilitate solving two-scale (macro-micro) coupled problems using the coupling library [preCICE](https://precice.org/).
18
+
19
+ The main documentation is rendered on the [preCICE website](https://precice.org/tooling-micro-manager-overview.html).
20
+
21
+ Please report any [issues](https://github.com/precice/micro-manager/issues) and give us feedback through [one of our community channels](https://precice.org/community-channels.html).
@@ -0,0 +1,2 @@
1
+ from .config import Config
2
+ from .micro_manager import MicroManager
@@ -0,0 +1,318 @@
1
+ """
2
+ Functionality for adaptive initialization and control of micro simulations
3
+ """
4
+ import sys
5
+ from math import exp
6
+ from typing import Callable
7
+ from warnings import warn
8
+
9
+ import numpy as np
10
+
11
+
12
+ class AdaptivityCalculator:
13
+ def __init__(self, configurator, logger) -> None:
14
+ """
15
+ Class constructor.
16
+
17
+ Parameters
18
+ ----------
19
+ configurator : object of class Config
20
+ Object which has getter functions to get parameters defined in the configuration file.
21
+ logger : Logger defined from the standard package logging
22
+ """
23
+ self._refine_const = configurator.get_adaptivity_refining_const()
24
+ self._coarse_const = configurator.get_adaptivity_coarsening_const()
25
+ self._hist_param = configurator.get_adaptivity_hist_param()
26
+ self._adaptivity_data_names = configurator.get_data_for_adaptivity()
27
+ self._adaptivity_type = configurator.get_adaptivity_type()
28
+
29
+ self._logger = logger
30
+
31
+ self._coarse_tol = 0.0
32
+ self._ref_tol = 0.0
33
+
34
+ self._similarity_measure = self._get_similarity_measure(
35
+ configurator.get_adaptivity_similarity_measure()
36
+ )
37
+
38
+ def _get_similarity_dists(
39
+ self, dt: float, similarity_dists: np.ndarray, data: dict
40
+ ) -> np.ndarray:
41
+ """
42
+ Calculate metric which determines if two micro simulations are similar enough to have one of them deactivated.
43
+
44
+ Parameters
45
+ ----------
46
+ dt : float
47
+ Current time step
48
+ similarity_dists : numpy array
49
+ 2D array having similarity distances between each micro simulation pair
50
+ data : dict
51
+ Data to be used in similarity distance calculation
52
+
53
+ Returns
54
+ -------
55
+ similarity_dists : numpy array
56
+ Updated 2D array having similarity distances between each micro simulation pair
57
+ """
58
+ _similarity_dists = np.copy(similarity_dists)
59
+
60
+ data_diff = np.zeros_like(_similarity_dists)
61
+ for name in data.keys():
62
+ data_vals = data[name]
63
+ if data_vals.ndim == 1:
64
+ # If the adaptivity-data is a scalar for each simulation,
65
+ # expand the dimension to make it a 2D array to unify the calculation.
66
+ # The axis is later reduced with a norm.
67
+ data_vals = np.expand_dims(data_vals, axis=1)
68
+
69
+ data_diff += self._similarity_measure(data_vals)
70
+
71
+ return exp(-self._hist_param * dt) * _similarity_dists + dt * data_diff
72
+
73
+ def _update_active_sims(
74
+ self, similarity_dists: np.ndarray, is_sim_active: np.ndarray
75
+ ) -> np.ndarray:
76
+ """
77
+ Update set of active micro simulations. Active micro simulations are compared to each other
78
+ and if found similar, one of them is deactivated.
79
+
80
+ Parameters
81
+ ----------
82
+ similarity_dists : numpy array
83
+ 2D array having similarity distances between each micro simulation pair
84
+ is_sim_active : numpy array
85
+ 1D array having state (active or inactive) of each micro simulation
86
+
87
+ Returns
88
+ -------
89
+ _is_sim_active : numpy array
90
+ Updated 1D array having state (active or inactive) of each micro simulation
91
+ """
92
+ max_similarity_dist = np.amax(similarity_dists)
93
+
94
+ if max_similarity_dist == 0.0:
95
+ warn(
96
+ "All similarity distances are zero, probably because all the data for adaptivity is the same. Coarsening tolerance will be manually set to minimum float number."
97
+ )
98
+ self._coarse_tol = sys.float_info.min
99
+ else:
100
+ self._coarse_tol = (
101
+ self._coarse_const * self._refine_const * max_similarity_dist
102
+ )
103
+
104
+ _is_sim_active = np.copy(
105
+ is_sim_active
106
+ ) # Input is_sim_active is not longer used after this point
107
+
108
+ # Update the set of active micro sims
109
+ for i in range(_is_sim_active.size):
110
+ if _is_sim_active[i]: # if sim is active
111
+ if self._check_for_deactivation(i, similarity_dists, _is_sim_active):
112
+ _is_sim_active[i] = False
113
+
114
+ return _is_sim_active
115
+
116
+ def _associate_inactive_to_active(
117
+ self,
118
+ similarity_dists: np.ndarray,
119
+ is_sim_active: np.ndarray,
120
+ sim_is_associated_to: np.ndarray,
121
+ ) -> np.ndarray:
122
+ """
123
+ Associate inactive micro simulations to most similar active micro simulation.
124
+
125
+ Parameters
126
+ ----------
127
+ similarity_dists : numpy array
128
+ 2D array having similarity distances between each micro simulation pair
129
+ is_sim_active : numpy array
130
+ 1D array having state (active or inactive) of each micro simulation
131
+ sim_is_associated_to : numpy array
132
+ 1D array with values of associated simulations of inactive simulations. Active simulations have None
133
+
134
+ Returns
135
+ -------
136
+ _sim_is_associated_to : numpy array
137
+ 1D array with values of associated simulations of inactive simulations. Active simulations have None
138
+ """
139
+ active_ids = np.where(is_sim_active)[0]
140
+ inactive_ids = np.where(is_sim_active == False)[0]
141
+
142
+ _sim_is_associated_to = np.copy(sim_is_associated_to)
143
+
144
+ # Associate inactive micro sims to active micro sims
145
+ for inactive_id in inactive_ids:
146
+ dist_min = sys.float_info.max
147
+ for active_id in active_ids:
148
+ # Find most similar active sim for every inactive sim
149
+ if similarity_dists[inactive_id, active_id] < dist_min:
150
+ associated_active_id = active_id
151
+ dist_min = similarity_dists[inactive_id, active_id]
152
+
153
+ _sim_is_associated_to[inactive_id] = associated_active_id
154
+
155
+ return _sim_is_associated_to
156
+
157
+ def _check_for_activation(
158
+ self, inactive_id: int, similarity_dists: np.ndarray, is_sim_active: np.ndarray
159
+ ) -> bool:
160
+ """
161
+ Check if an inactive simulation needs to be activated.
162
+
163
+ Parameters
164
+ ----------
165
+ inactive_id : int
166
+ ID of inactive simulation which is checked for activation.
167
+ similarity_dists : numpy array
168
+ 2D array having similarity distances between each micro simulation pair.
169
+ is_sim_active : numpy array
170
+ 1D array having state (active or inactive) of each micro simulation.
171
+
172
+ Return
173
+ ------
174
+ tag : bool
175
+ True if the inactive simulation needs to be activated, False otherwise.
176
+ """
177
+ active_sim_ids = np.where(is_sim_active)[0]
178
+
179
+ dists = similarity_dists[inactive_id, active_sim_ids]
180
+
181
+ # If inactive sim is not similar to any active sim, activate it
182
+ return min(dists) > self._ref_tol
183
+
184
+ def _check_for_deactivation(
185
+ self, active_id: int, similarity_dists: np.ndarray, is_sim_active: np.ndarray
186
+ ) -> bool:
187
+ """
188
+ Check if an active simulation needs to be deactivated.
189
+
190
+ Parameters
191
+ ----------
192
+ active_id : int
193
+ ID of active simulation which is checked for deactivation.
194
+ similarity_dists : numpy array
195
+ 2D array having similarity distances between each micro simulation pair.
196
+ is_sim_active : numpy array
197
+ 1D array having state (active or inactive) of each micro simulation.
198
+
199
+ Return
200
+ ------
201
+ tag : bool
202
+ True if the active simulation needs to be deactivated, False otherwise.
203
+ """
204
+ active_sim_ids = np.where(is_sim_active)[0]
205
+
206
+ for active_id_2 in active_sim_ids:
207
+ if active_id != active_id_2: # don't compare active sim to itself
208
+ # If active sim is similar to another active sim, deactivate it
209
+ if similarity_dists[active_id, active_id_2] < self._coarse_tol:
210
+ return True
211
+ return False
212
+
213
+ def _get_similarity_measure(
214
+ self, similarity_measure: str
215
+ ) -> Callable[[np.ndarray], np.ndarray]:
216
+ """
217
+ Get similarity measure to be used for similarity calculation
218
+
219
+ Parameters
220
+ ----------
221
+ similarity_measure : str
222
+ String specifying the similarity measure to be used
223
+
224
+ Returns
225
+ -------
226
+ similarity_measure : function
227
+ Function to be used for similarity calculation. Takes data as input and returns similarity measure
228
+ """
229
+ if similarity_measure == "L1":
230
+ return self._l1
231
+ elif similarity_measure == "L2":
232
+ return self._l2
233
+ elif similarity_measure == "L1rel":
234
+ return self._l1rel
235
+ elif similarity_measure == "L2rel":
236
+ return self._l2rel
237
+ else:
238
+ raise ValueError(
239
+ 'Similarity measure not supported. Currently supported similarity measures are "L1", "L2", "L1rel", "L2rel".'
240
+ )
241
+
242
+ def _l1(self, data: np.ndarray) -> np.ndarray:
243
+ """
244
+ Calculate L1 norm of data
245
+
246
+ Parameters
247
+ ----------
248
+ data : numpy array
249
+ Data to be used in similarity distance calculation
250
+
251
+ Returns
252
+ -------
253
+ similarity_dists : numpy array
254
+ Updated 2D array having similarity distances between each micro simulation pair
255
+ """
256
+ return np.linalg.norm(data[np.newaxis, :] - data[:, np.newaxis], ord=1, axis=-1)
257
+
258
+ def _l2(self, data: np.ndarray) -> np.ndarray:
259
+ """
260
+ Calculate L2 norm of data
261
+
262
+ Parameters
263
+ ----------
264
+ data : numpy array
265
+ Data to be used in similarity distance calculation
266
+
267
+ Returns
268
+ -------
269
+ similarity_dists : numpy array
270
+ Updated 2D array having similarity distances between each micro simulation pair
271
+ """
272
+ return np.linalg.norm(data[np.newaxis, :] - data[:, np.newaxis], ord=2, axis=-1)
273
+
274
+ def _l1rel(self, data: np.ndarray) -> np.ndarray:
275
+ """
276
+ Calculate L1 norm of relative difference of data.
277
+ The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points.
278
+
279
+ Parameters
280
+ ----------
281
+ data : numpy array
282
+ Data to be used in similarity distance calculation
283
+
284
+ Returns
285
+ -------
286
+ similarity_dists : numpy array
287
+ Updated 2D array having similarity distances between each micro simulation pair
288
+ """
289
+ pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis]
290
+ # divide by data to get relative difference
291
+ # divide i,j by max(data[i],data[j]) to get relative difference
292
+ relative = np.nan_to_num(
293
+ (pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis]))
294
+ )
295
+ return np.linalg.norm(relative, ord=1, axis=-1)
296
+
297
+ def _l2rel(self, data: np.ndarray) -> np.ndarray:
298
+ """
299
+ Calculate L2 norm of relative difference of data.
300
+ The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points.
301
+
302
+ Parameters
303
+ ----------
304
+ data : numpy array
305
+ Data to be used in similarity distance calculation
306
+
307
+ Returns
308
+ -------
309
+ similarity_dists : numpy array
310
+ Updated 2D array having similarity distances between each micro simulation pair
311
+ """
312
+ pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis]
313
+ # divide by data to get relative difference
314
+ # divide i,j by max(data[i],data[j]) to get relative difference
315
+ relative = np.nan_to_num(
316
+ (pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis]))
317
+ )
318
+ return np.linalg.norm(relative, ord=2, axis=-1)