Skip to content

Commit

Permalink
Update preCICE API calls for precice:develop (#51)
Browse files Browse the repository at this point in the history
* [WIP] Update preCICE API calls

* Changing mocked preCICE to v3

* Explicitly mention v3.0.0.0dev0 for pyprecice

* Use correct API function get_mesh_vertex_ids_and_coordinates

* Use pyprecice v3.0.0.0dev1

* Formatting

* Work with pyprecice v3.0.0.0dev2

* Partially fixing tests

* Fix tests

* Formatting

* Fix precice-configs

* Move dimensions parameter back to precice-configuration

* Move initialize after requires_initial_data()

* Remove initialize() from micro simulations, along with the writing of initial data to preCICE

* In time loop, get dt from preCICE

* Make precice findable in Action, remove initialize() from pybind11 wrapper, other fixes

* Port unit_cube.py to v3

* Port config used in adaptivity integration test

* Port config to v3, remove use of su for parallel tests

* Allow mpiexec to run as root for parallel tests, fix error in unit_cube.py for integration tests

* Modify LD_LIBRARY_PATH in Action, fix unit_cube.py

* Remove user precice in adaptivity parallel tests

* Use correct container and proper working directories in Actions

* Move dimensions attribute into mesh tag

* Pass correct dt to read_data(...) command

---------

Co-authored-by: Ishaan Desai <[email protected]>
  • Loading branch information
erikscheurer and IshaanDesai authored Aug 28, 2023
1 parent 5e46e96 commit 13819a9
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 336 deletions.
41 changes: 21 additions & 20 deletions .github/workflows/run-adaptivity-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test adaptivity
name: Test for functions in classes AdaptivityCalculator, LocalAdaptivityCalculator, GlobalAdaptivityCalculator
on:
push:
branches:
Expand All @@ -8,10 +8,10 @@ on:
branches:
- "*"
jobs:
integration_test:
name: Run integration test
adaptivity_integration_tests:
name: Run adaptivity integration tests
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -33,17 +33,21 @@ jobs:
- name: Run integration test with local adaptivity
timeout-minutes: 3
working-directory: micro-manager/tests/integration/test_unit_cube
run: python3 unit_cube.py & python3 run_micro_manager.py --config micro-manager-config-local-adaptivity.json
run: |
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
python3 unit_cube.py & python3 run_micro_manager.py --config micro-manager-config-local-adaptivity.json
- name: Run integration test serially with global adaptivity
- name: Run integration test with global adaptivity
timeout-minutes: 3
working-directory: micro-manager/tests/integration/test_unit_cube
run: python3 unit_cube.py & python3 run_micro_manager.py --config micro-manager-config-global-adaptivity.json
run: |
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
python3 unit_cube.py & python3 run_micro_manager.py --config micro-manager-config-global-adaptivity.json
unit_tests_serial:
name: Run unit tests (serial variants)
adaptivity_unit_tests_serial:
name: Run adaptivity unit tests in serial
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- name: Checkout Repository
uses: actions/checkout@v3
Expand All @@ -66,10 +70,10 @@ jobs:
working-directory: micro-manager/tests/unit
run: python3 -m unittest test_adaptivity_serial.py

unit_tests_parallel:
name: Run unit tests (parallel variants)
adaptivity_unit_tests_parallel:
name: Run adaptivity unit tests in parallel
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- name: Checkout Repository
uses: actions/checkout@v3
Expand All @@ -85,21 +89,18 @@ jobs:
- name: Use mpi4py
uses: mpi4py/setup-mpi@v1

- name: Add user precice
run: useradd -m -s /bin/bash precice

- name: Install Dependencies
working-directory: micro-manager
run: |
apt-get -qq update
apt-get -qq install python3-dev python3-pip git python-is-python3 pkg-config
su -c "python -m pip install --upgrade pip" precice
su -c "pip install setuptools wheel twine" precice
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Install Micro Manager
working-directory: micro-manager
run: su -c "pip3 install --user ." precice
run: pip3 install --user .

- name: Run unit tests
working-directory: micro-manager/tests/unit
run: su -c "mpiexec -n 2 python3 -m unittest test_adaptivity_parallel.py" precice
run: mpiexec -n 2 --allow-run-as-root python3 -m unittest test_adaptivity_parallel.py
36 changes: 14 additions & 22 deletions .github/workflows/run-domain-decomposition-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test domain decomposition
name: Test functions in class DomainDecomposer
on:
push:
branches:
Expand All @@ -8,10 +8,10 @@ on:
branches:
- "*"
jobs:
integration_test:
name: Run integration tests
domain_decomposition_integration_tests:
name: Run domain decomposition integration tests
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand All @@ -27,39 +27,31 @@ jobs:
- name: Use mpi4py
uses: mpi4py/setup-mpi@v1

- name: Add user precice
run: useradd -m -s /bin/bash precice

- name: Install Dependencies
working-directory: micro-manager
run: |
apt-get -qq install python3-dev python3-pip git python-is-python3 pkg-config
su -c "python -m pip install --upgrade pip" precice
su -c "pip install setuptools wheel twine" precice
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Install micro-manager
working-directory: micro-manager
run: su -c "pip3 install --user ." precice
run: pip3 install --user .

- name: Run integration test (variant 1)
timeout-minutes: 3
working-directory: micro-manager/tests/integration
run: |
chown -R precice test_unit_cube/
cd test_unit_cube/
su -c "mpiexec -n 2 python3 run_micro_manager.py --config micro-manager-config-parallel-1.json & python3 unit_cube.py" precice
working-directory: micro-manager/tests/integration/test_unit_cube
run: mpiexec -n 2 --allow-run-as-root python3 run_micro_manager.py --config micro-manager-config-parallel-1.json & python3 unit_cube.py

- name: Run integration test (variant 2)
timeout-minutes: 3
working-directory: micro-manager/tests/integration
run: |
cd test_unit_cube/
su -c "mpiexec -n 6 --oversubscribe python3 run_micro_manager.py --config micro-manager-config-parallel-2.json & python3 unit_cube.py" precice
working-directory: micro-manager/tests/integration/test_unit_cube
run: mpiexec -n 6 --oversubscribe --allow-run-as-root python3 run_micro_manager.py --config micro-manager-config-parallel-2.json & python3 unit_cube.py

unit_tests:
name: Run unit tests
domain_decomposition_unit_tests:
name: Run domain decomposition unit tests
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- name: Checkout Repository
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-macro-micro-dummy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
run_dummy:
name: Run dummy
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:

- name: Checkout Repository
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run unit tests
name: Run unit tests for functions in micro_manager.py
on:
push:
branches:
Expand All @@ -10,7 +10,7 @@ on:
jobs:
unit-tests:
runs-on: ubuntu-latest
container: precice/precice
container: precice/precice:develop
steps:
- uses: actions/checkout@v3
with:
Expand Down
10 changes: 0 additions & 10 deletions docs/micro-simulation-convert-to-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ class MicroSimulation: # Name is fixed
Constructor of class MicroSimulation.
"""

def initialize(self) -> dict:
"""
Initialize the micro simulation. This function is *optional*.
Returns
-------
data : dict
Python dictionary with names of micro data as keys and the data as values at the initial condition
"""

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.
Expand Down
2 changes: 1 addition & 1 deletion examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# preCICE related files
*.log
*events.json
precice-run/
precice-events/

# PyCharm related files
.idea
Expand Down
10 changes: 0 additions & 10 deletions examples/cpp-dummy/micro_cpp_dummy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@
// Constructor
MicroSimulation::MicroSimulation() : _micro_scalar_data(0), _state(0) {}

// Initialize
void MicroSimulation::initialize()
{
std::cout << "Initialize micro problem\n";
_micro_scalar_data = 0;
_micro_vector_data.clear();
_state = 0;
}

// Solve
py::dict MicroSimulation::solve(py::dict macro_data, double dt)
{
Expand Down Expand Up @@ -74,7 +65,6 @@ PYBIND11_MODULE(micro_dummy, m) {

py::class_<MicroSimulation>(m, "MicroSimulation")
.def(py::init())
.def("initialize", &MicroSimulation::initialize)
.def("solve", &MicroSimulation::solve)
.def("get_state", &MicroSimulation::get_state)
.def("set_state", &MicroSimulation::set_state)
Expand Down
1 change: 0 additions & 1 deletion examples/cpp-dummy/micro_cpp_dummy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class MicroSimulation
{
public:
MicroSimulation();
void initialize();
// 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);
MicroSimulation __deepcopy__(py::dict memo);
Expand Down
56 changes: 21 additions & 35 deletions examples/macro_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,59 @@ def main():
t = t_checkpoint = 0

# preCICE setup
interface = precice.Interface("Macro-dummy", "precice-config.xml", 0, 1)
interface = precice.Participant("Macro-dummy", "precice-config.xml", 0, 1)

# define coupling meshes
read_mesh_name = write_mesh_name = "macro-mesh"
read_mesh_id = interface.get_mesh_id(read_mesh_name)
read_data_names = {"micro-scalar-data": 0, "micro-vector-data": 1}

write_mesh_id = interface.get_mesh_id(write_mesh_name)
write_data_names = {"macro-scalar-data": 0, "macro-vector-data": 1}

# Coupling mesh
coords = np.zeros((nv, interface.get_dimensions()))
coords = np.zeros((nv, interface.get_mesh_dimensions(write_mesh_name)))
for x in range(nv):
for d in range(interface.get_dimensions()):
for d in range(interface.get_mesh_dimensions(write_mesh_name)):
coords[x, d] = x

# Define Gauss points on entire domain as coupling mesh
vertex_ids = interface.set_mesh_vertices(read_mesh_id, coords)

read_data_ids = dict()
# coupling data
for name, dim in read_data_names.items():
read_data_ids[name] = interface.get_data_id(name, read_mesh_id)

write_data_ids = dict()
for name, dim in write_data_names.items():
write_data_ids[name] = interface.get_data_id(name, write_mesh_id)

# initialize preCICE
dt = interface.initialize()
vertex_ids = interface.set_mesh_vertices(read_mesh_name, coords)

write_scalar_data = np.zeros(nv)
write_vector_data = np.zeros((nv, interface.get_dimensions()))
write_vector_data = np.zeros((nv, interface.get_data_dimensions(write_mesh_name, "macro-vector-data")))

for i in range(nv):
write_scalar_data[i] = i
for d in range(interface.get_dimensions()):
for d in range(interface.get_data_dimensions(write_mesh_name, "macro-vector-data")):
write_vector_data[i, d] = i

if interface.is_action_required(precice.action_write_initial_data()):
if interface.requires_initial_data():
for name, dim in write_data_names.items():
if dim == 0:
interface.write_block_scalar_data(write_data_ids[name], vertex_ids, write_scalar_data)
interface.write_block_scalar_data(name, vertex_ids, write_scalar_data)
elif dim == 1:
interface.write_block_vector_data(write_data_ids[name], vertex_ids, write_vector_data)
interface.mark_action_fulfilled(precice.action_write_initial_data())
interface.write_block_vector_data(name, vertex_ids, write_vector_data)

interface.initialize_data()
# initialize preCICE
interface.initialize()
dt = interface.get_max_time_step_size()

# time loop
while interface.is_coupling_ongoing():
# write checkpoint
if interface.is_action_required(precice.action_write_iteration_checkpoint()):
if interface.requires_writing_checkpoint():
print("Saving macro state")
t_checkpoint = t
n_checkpoint = n
interface.mark_action_fulfilled(precice.action_write_iteration_checkpoint())

for name, dim in read_data_names.items():
if dim == 0:
read_scalar_data = interface.read_block_scalar_data(read_data_ids[name], vertex_ids)
read_scalar_data = interface.read_data(read_mesh_name, name, vertex_ids, 1)
elif dim == 1:
read_vector_data = interface.read_block_vector_data(read_data_ids[name], vertex_ids)
read_vector_data = interface.read_data(read_mesh_name, name, vertex_ids, 1)

write_scalar_data[:] = read_scalar_data[:]
for i in range(nv):
for d in range(interface.get_dimensions()):
for d in range(interface.get_data_dimensions(read_mesh_name, "micro-vector-data")):
write_vector_data[i, d] = read_vector_data[i, d]
if t > 1: # to trigger adaptivity after some time
# ensure that the data is different from the previous time step
Expand All @@ -90,22 +76,22 @@ def main():

for name, dim in write_data_names.items():
if dim == 0:
interface.write_block_scalar_data(write_data_ids[name], vertex_ids, write_scalar_data)
interface.write_data(write_mesh_name, name, vertex_ids, write_scalar_data)
elif dim == 1:
interface.write_block_vector_data(write_data_ids[name], vertex_ids, write_vector_data)
interface.write_data(write_mesh_name, name, vertex_ids, write_vector_data)

# do the coupling
dt = interface.advance(dt)
interface.advance(dt)
dt = interface.get_max_time_step_size()

# advance variables
n += 1
t += dt

if interface.is_action_required(precice.action_read_iteration_checkpoint()):
if interface.requires_reading_checkpoint():
print("Reverting to old macro state")
t = t_checkpoint
n = n_checkpoint
interface.mark_action_fulfilled(precice.action_read_iteration_checkpoint())

interface.finalize()

Expand Down
Loading

0 comments on commit 13819a9

Please sign in to comment.