Skip to content

Commit

Permalink
Merge branch 'develop' into Relax_similarity_condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Fujikawas authored Feb 8, 2024
2 parents 69a058e + 60755e6 commit 75cac0a
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 171 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pythonpublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py sdist
twine upload dist/*
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## latest

- Pass an ID to the micro simulation object so that it is aware of its own uniqueness https://github.com/precice/micro-manager/pull/66
- Resolve bug which led to an error when global adaptivity was used with unequal number of simulations on each rank https://github.com/precice/micro-manager/pull/78
- Make the `initialize()` method of the MicroManager class private https://github.com/precice/micro-manager/pull/77
- Add reference paper via a CITATION.cff file https://github.com/precice/micro-manager/commit/6c08889c658c889d6ab5d0867802522585abcee5
- Add JOSS DOI badge https://github.com/precice/micro-manager/commit/2e3c2a4c77732f56a957abbad9e4d0cb64029725
- Update pyprecice API calls to their newer variants https://github.com/precice/micro-manager/pull/51

## v0.3.0

- Add global variant to adaptivity (still experimental) https://github.com/precice/micro-manager/pull/42
Expand Down
7 changes: 6 additions & 1 deletion docs/micro-simulation-convert-to-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ Restructure your micro simulation code into a Python class with the structure gi

```python
class MicroSimulation: # Name is fixed
def __init__(self):
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 solve(self, macro_data: dict, dt: float) -> dict:
Expand Down
2 changes: 0 additions & 2 deletions docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ from micro_manager import MicroManager

manager = MicroManager("micro-manager-config.json")

manager.initialize()

manager.solve()
```

Expand Down
6 changes: 3 additions & 3 deletions examples/cpp-dummy/micro_cpp_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "micro_cpp_dummy.hpp"

// Constructor
MicroSimulation::MicroSimulation() : _micro_scalar_data(0), _state(0) {}
MicroSimulation::MicroSimulation(int sim_id) : _sim_id(sim_id), _micro_scalar_data(0), _state(0) {}

// Solve
py::dict MicroSimulation::solve(py::dict macro_data, double dt)
Expand Down Expand Up @@ -64,7 +64,7 @@ PYBIND11_MODULE(micro_dummy, m) {
m.doc() = "pybind11 micro dummy plugin";

py::class_<MicroSimulation>(m, "MicroSimulation")
.def(py::init())
.def(py::init<int>())
.def("solve", &MicroSimulation::solve)
.def("get_state", &MicroSimulation::get_state)
.def("set_state", &MicroSimulation::set_state)
Expand All @@ -77,7 +77,7 @@ PYBIND11_MODULE(micro_dummy, m) {
throw std::runtime_error("Invalid state!");

/* Create a new C++ instance */
MicroSimulation ms;
MicroSimulation ms(0);

ms.set_state(t);

Expand Down
3 changes: 2 additions & 1 deletion examples/cpp-dummy/micro_cpp_dummy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ namespace py = pybind11;
class MicroSimulation
{
public:
MicroSimulation();
MicroSimulation(int sim_id);
// solve takes a python dict data, and the timestep dt as inputs, and returns a python dict
py::dict solve(py::dict macro_write_data, double dt);

void set_state(py::list state);
py::list get_state() const;

private:
int _sim_id;
double _micro_scalar_data;
std::vector<double> _micro_vector_data;
double _state;
Expand Down
2 changes: 0 additions & 2 deletions examples/cpp-dummy/run_micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@

manager = MicroManager(args.config)

manager.initialize()

manager.solve()
3 changes: 2 additions & 1 deletion examples/python-dummy/micro_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

class MicroSimulation:

def __init__(self):
def __init__(self, sim_id):
"""
Constructor of MicroSimulation class.
"""
self._sim_id = sim_id
self._dims = 3
self._micro_scalar_data = None
self._micro_vector_data = None
Expand Down
2 changes: 0 additions & 2 deletions examples/python-dummy/run_micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,4 @@

manager = MicroManager(args.config)

manager.initialize()

manager.solve()
11 changes: 9 additions & 2 deletions micro_manager/adaptivity/adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Callable
import re
import xml.etree.ElementTree as ET
from warnings import warn


class AdaptivityCalculator:
Expand Down Expand Up @@ -172,9 +173,15 @@ def _update_active_sims(
self._coarse_const = self._get_adaptive_similarity_const(self._coarse_const_input)
if self._adaptive_refine_const:
self._refine_const = self._get_adaptive_similarity_const(self._refine_const_input)
self._coarse_tol = self._coarse_const * self._refine_const * \
np.amax(similarity_dists)

max_similarity_dist = np.amax(similarity_dists)

if max_similarity_dist == 0.0:
warn("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.")
self._coarse_tol = sys.float_info.min
else:
self._coarse_tol = self._coarse_const * self._refine_const * max_similarity_dist

_is_sim_active = np.copy(is_sim_active) # Input is_sim_active is not longer used after this point

# Update the set of active micro sims
Expand Down
31 changes: 23 additions & 8 deletions micro_manager/adaptivity/global_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def __init__(
self,
configurator,
logger,
is_sim_on_this_rank: list,
rank_of_sim: np.ndarray,
global_number_of_sims: float,
global_ids: list,
rank: int,
comm) -> None:
Expand All @@ -32,10 +31,8 @@ def __init__(
Object which has getter functions to get parameters defined in the configuration file.
logger : object of logging
Logger defined from the standard package logging
is_sim_on_this_rank : list
global_number_of_sims : float
List of booleans. True if simulation is on this rank, False otherwise.
rank_of_sim : numpy array
1D array consisting of rank on which the simulation lives.
global_ids : list
List of global IDs of simulations living on this rank.
rank : int
Expand All @@ -44,12 +41,26 @@ def __init__(
Global communicator of MPI.
"""
super().__init__(configurator, logger)
self._is_sim_on_this_rank = is_sim_on_this_rank
self._rank_of_sim = rank_of_sim
self._global_ids = global_ids
self._comm = comm
self._rank = rank

local_number_of_sims = len(global_ids)

# Create a map of micro simulation global IDs and the ranks on which they are
micro_sims_on_this_rank = np.zeros(local_number_of_sims, dtype=np.intc)
for i in range(local_number_of_sims):
micro_sims_on_this_rank[i] = self._rank

self._rank_of_sim = np.zeros(global_number_of_sims, dtype=np.intc) # DECLARATION

self._comm.Allgatherv(micro_sims_on_this_rank, self._rank_of_sim)

self._is_sim_on_this_rank = [False] * global_number_of_sims # DECLARATION
for i in range(global_number_of_sims):
if self._rank_of_sim[i] == self._rank:
self._is_sim_on_this_rank[i] = True

def compute_adaptivity(
self,
dt: float,
Expand Down Expand Up @@ -94,11 +105,15 @@ def compute_adaptivity(
is_sim_active = self._update_active_sims(similarity_dists, is_sim_active_nm1)

is_sim_active, sim_is_associated_to = self._update_inactive_sims(
similarity_dists, is_sim_active_nm1, sim_is_associated_to_nm1, micro_sims)
similarity_dists, is_sim_active, sim_is_associated_to_nm1, micro_sims)

print("sim_is_associated_to: {}".format(sim_is_associated_to))

sim_is_associated_to = self._associate_inactive_to_active(
similarity_dists, is_sim_active, sim_is_associated_to)

print("sim_is_associated_to: {}".format(sim_is_associated_to))

self._logger.info(
"{} active simulations, {} inactive simulations".format(
np.count_nonzero(
Expand Down
2 changes: 1 addition & 1 deletion micro_manager/adaptivity/local_adaptivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def compute_adaptivity(
is_sim_active = self._update_active_sims(similarity_dists, is_sim_active_nm1)

is_sim_active, sim_is_associated_to = self._update_inactive_sims(
similarity_dists, is_sim_active_nm1, sim_is_associated_to_nm1, micro_sims)
similarity_dists, is_sim_active, sim_is_associated_to_nm1, micro_sims)

sim_is_associated_to = self._associate_inactive_to_active(
similarity_dists, is_sim_active, sim_is_associated_to)
Expand Down
7 changes: 7 additions & 0 deletions micro_manager/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import os
from warnings import warn


class Config:
Expand Down Expand Up @@ -125,6 +126,12 @@ def read_json(self, config_filename):
for dname in data["simulation_params"]["adaptivity"]["data"]:
self._data_for_adaptivity[dname] = exchange_data[dname]

if self._data_for_adaptivity.keys() == self._write_data_names.keys():
warn(
"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,"
" please include macro simulation data as well.")

self._adaptivity_history_param = data["simulation_params"]["adaptivity"]["history_param"]
self._adaptivity_coarsening_constant = data["simulation_params"]["adaptivity"]["coarsening_constant"]
self._adaptivity_refining_constant = data["simulation_params"]["adaptivity"]["refining_constant"]
Expand Down
Loading

0 comments on commit 75cac0a

Please sign in to comment.