Skip to content

Commit

Permalink
Merge branch 'develop' into add-lc-circuit-python
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminRodenberg committed Aug 29, 2024
2 parents f6bcd44 + 293c25b commit dfca8d6
Show file tree
Hide file tree
Showing 28 changed files with 158 additions and 40 deletions.
1 change: 1 addition & 0 deletions changelog-entries/544.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed the [volume-coupled flow tutorial](https://precice.org/tutorials-volume-coupled-flow.html) to correctly assign all components of the read velocity field.
1 change: 1 addition & 0 deletions changelog-entries/545.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Updated the default suggested OpenFOAM version to v2406 [#545](https://github.com/precice/tutorials/pull/545).
1 change: 1 addition & 0 deletions changelog-entries/554.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Use `assign` function instead of directly accessing vectors in perpendicular-flap/solid-fenics. Fixes problem with checkpointing.
3 changes: 2 additions & 1 deletion channel-transport/fluid-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
3 changes: 2 additions & 1 deletion channel-transport/transport-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
2 changes: 1 addition & 1 deletion flow-over-heated-plate/solid-dunefem/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
dune-fem>=2.8
pyprecice==3
pyprecice~=3.0
3 changes: 2 additions & 1 deletion flow-over-heated-plate/solid-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
3 changes: 2 additions & 1 deletion partitioned-heat-conduction/neumann-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
2 changes: 2 additions & 0 deletions perpendicular-flap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Solid participant:

* OpenFOAM (solidDisplacementFoam). For more information, have a look at the [OpenFOAM plateHole tutorial](https://www.openfoam.com/documentation/tutorial-guide/5-stress-analysis/5.1-stress-analysis-of-a-plate-with-a-hole). The solidDisplacementFoam solver only supports linear geometry and this case is only provided for quick testing purposes, leading to outlier results. For general solid mechanics procedures in OpenFOAM, see solids4foam.

* Fake. A simple Python script that acts as a fake solver and provides an arbitrary time-dependent flap displacement in the x-direction, i.e., it performs a shear mapping on the resting flap. This solver can be used for debugging of the fluid participant and its adapter. It also technically works with implicit coupling, thus no changes to the preCICE configuration are necessary. Note that [ASTE's replay mode](https://precice.org/tooling-aste.html#replay-mode) has a similar use case and could also feed artificial or previously recorded real data, replacing an actual solver.

## Running the Simulation

All listed solvers can be used in order to run the simulation. OpenFOAM can be executed in parallel using `run.sh -parallel`. The default setting uses 4 MPI ranks. Open two separate terminals and start the desired fluid and solid participant by calling the respective run script `run.sh` located in the participant directory. For example:
Expand Down
4 changes: 2 additions & 2 deletions perpendicular-flap/fluid-fake/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
numpy
pyprecice==3
numpy >1, <2
pyprecice~=3.0
3 changes: 2 additions & 1 deletion perpendicular-flap/fluid-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==6
pyprecice==3
numpy >1, <2
pyprecice~=3.0
92 changes: 92 additions & 0 deletions perpendicular-flap/solid-fake/fake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from __future__ import division

import numpy as np
import precice


def displace_flap(x, y, t, flap_tip_y):
x_displ = np.zeros_like(x)
y_displ = np.zeros_like(y)
# get displacement independent of x, only dependent on y and t
max_x_displ = 0.5
period_fac = 3 * np.pi
damping_fac = 8 # damps the amplitude of the sine
# defines how much the sine is shifted in y-direction
shift = 0.95
# wiggles the flap periodically.
# the arcsin(-shift) in the sine evaluation is necessary to start at a flap displacement of 0 at t=0
# (i.e. sin(arcsin(-shift))+shift = 0)
x_displ = np.minimum(((np.sin(period_fac * t + np.arcsin(-shift)) + shift) /
damping_fac), max_x_displ) * y / flap_tip_y

dimensions = 2
displ = np.zeros((len(x), dimensions))
displ[:, 0] = x_displ
displ[:, 1] = y_displ

return displ


configuration_file_name = "../precice-config.xml"
participant_name = "Solid"
mesh_name = "Solid-Mesh"
write_data_name = 'Displacement'

solver_process_index = 0
solver_process_size = 1

# define mesh
H = 1
W = 0.1

interface = precice.Participant(participant_name, configuration_file_name, solver_process_index, solver_process_size)
dimensions = interface.get_mesh_dimensions(mesh_name)
assert (dimensions == 2)

x_left = 0.0 - 0.5 * W # left boundary of the flap
x_right = 0.5 * W # right boundary of the flap
y_bottom = 0.0 # bottom of the flap
y_top = y_bottom + H # top of the flap

n = 24 # Number of vertices per side
t = 0

vertices = np.zeros((2 * n, dimensions))
# define vertices of flap's left side
vertices[:n, 1] = np.linspace(y_bottom, y_top, n)
vertices[:n, 0] = x_left
# define vertices of flap's right side
vertices[n:, 1] = np.linspace(y_bottom, y_top, n)
vertices[n:, 0] = x_right

vertex_ids = interface.set_mesh_vertices(mesh_name, vertices)

if interface.requires_initial_data():
# initially, there should be no displacement
interface.write_data(np.zeros_like(vertices))

interface.initialize()
# change if necessary
solver_dt = np.inf
# for checkpointing
t_cp = 0

while interface.is_coupling_ongoing():
if interface.requires_writing_checkpoint():
t_cp = t

precice_dt = interface.get_max_time_step_size()
dt = min([solver_dt, precice_dt])
# wiggle the flap
write_data = displace_flap(vertices[:, 0], vertices[:, 1], t, H)

interface.write_data(mesh_name, write_data_name, vertex_ids, write_data)
interface.advance(dt)

if interface.requires_reading_checkpoint():
t = t_cp
else:
# update t
t += dt

interface.finalize()
2 changes: 2 additions & 0 deletions perpendicular-flap/solid-fake/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy >1, <2
pyprecice~=3.0
7 changes: 7 additions & 0 deletions perpendicular-flap/solid-fake/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env sh
set -e -u

python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
python3 fake.py
15 changes: 6 additions & 9 deletions perpendicular-flap/solid-fenics/solid.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ def update_v(a, u_old, v_old, a_old, ufl=True):
def update_fields(u, u_old, v_old, a_old):
"""Update all fields at the end of a timestep."""

u_vec, u0_vec = u.vector(), u_old.vector()
v0_vec, a0_vec = v_old.vector(), a_old.vector()

# call update functions
a_vec = update_a(u_vec, u0_vec, v0_vec, a0_vec, ufl=False)
v_vec = update_v(a_vec, u0_vec, v0_vec, a0_vec, ufl=False)
a_new = update_a(u, u_old, v_old, a_old)
v_new = update_v(u, u_old, v_old, a_old)

# assign u->u_old
v_old.vector()[:], a_old.vector()[:] = v_vec, a_vec
u_old.vector()[:] = u.vector()
# update values
a_old.assign(project(a_new, V))
v_old.assign(project(v_new, V))
u_old.assign(u)


def avg(x_old, x_new, alpha):
Expand Down Expand Up @@ -238,7 +236,6 @@ def avg(x_old, x_new, alpha):
n = n_cp
else:
update_fields(u_np1, u_n, v_n, a_n)
u_n.assign(u_np1)
t += float(dt)
n += 1

Expand Down
3 changes: 2 additions & 1 deletion perpendicular-flap/solid-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils>=8.5
pyprecice==3
numpy >1, <2
pyprecice~=3.0
12 changes: 6 additions & 6 deletions quickstart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ To get a feeling what preCICE does, watch a [short presentation](https://www.you
```bash
# Add the signing key, add the repository, update (check this):
wget -q -O - https://dl.openfoam.com/add-debian-repo.sh | sudo bash
# Install OpenFOAM v2312:
sudo apt install openfoam2312-dev
# Install OpenFOAM v2406:
sudo apt install openfoam2406-dev
# Enable OpenFOAM by default in your system and apply now:
echo "source /usr/lib/openfoam/openfoam2312/etc/bashrc" >> ~/.bashrc
echo "source /usr/lib/openfoam/openfoam2406/etc/bashrc" >> ~/.bashrc
source ~/.bashrc
```

Expand All @@ -55,9 +55,9 @@ To get a feeling what preCICE does, watch a [short presentation](https://www.you
4. Download and install the [OpenFOAM-preCICE adapter](https://precice.org/adapter-openfoam-get.html):

```bash
wget https://github.com/precice/openfoam-adapter/archive/refs/tags/v1.3.0.tar.gz
tar -xzf v1.3.0.tar.gz
cd openfoam-adapter-1.3.0/
wget https://github.com/precice/openfoam-adapter/archive/refs/tags/v1.3.1.tar.gz
tar -xzf v1.3.1.tar.gz
cd openfoam-adapter-1.3.1/
./Allwmake
cd ..
```
Expand Down
16 changes: 8 additions & 8 deletions resonant-circuit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ summary: We simulate a two-element LC circuit (one inductor and one capacitor).

## Setup

The purpose of this tutorial is to illustrate the usage of preCICE to couple MATLAB code. Two different MATLAB solvers will be coupled to simulate a two-element LC circuit. This type of circuit consists on a very simple system with one inductor and one capacitor:
The purpose of this tutorial is to illustrate the usage of preCICE to couple MATLAB code. Two different MATLAB solvers will be coupled to simulate a two-element LC circuit. This type of circuit consists of a very simple system with one inductor and one capacitor:

![LC circuit diagram [1]](images/tutorials-resonant-circuit-diagram.svg)

Expand All @@ -17,9 +17,9 @@ $U(t) = L \frac{\text{d}I}{\text{d}t}$

$I(t) = -C \frac{\text{d}U}{\text{d}t}$

where $I$ is the current and $U$ the voltage of the cirucit.
where $I$ is the current and $U$ the voltage of the circuit.

Each of these equations is going to be solved by a different MATLAB solver. Note that as only one scalar is solved per equation, this is a 0+1 dimensional problem.
Each of these equations is going to be solved by a different MATLAB solver. Note that, as only one scalar is solved per equation, this is a 0+1 dimensional problem.

## Configuration

Expand All @@ -37,23 +37,23 @@ preCICE configuration (image generated using the [precice-config-visualizer](htt

### MATLAB

For running this example, first get into one of folders with the solvers and open a MATLAB instance.
For running this example, first get into one of the solver folders and open a MATLAB instance.
Afterward, do the same for the second solver.
After adding the MATLAB bindings to the MATLAB path (in both instances), run the following commands:

In the first MATLAB instance one can run the solver for the current:
In the first MATLAB instance, one can run the solver for the current:

```MATLAB
coil
```

And in the second MATLAB instance the solver for the voltage:
And in the second MATLAB instance, the solver for the voltage:

```MATLAB
capacitor
```

The preCICE configuration file is available as `precice-config.xml`, and it is called directly in the solvers.
The preCICE configuration file is hard-coded as `precice-config.xml` in the solvers.

#### Running from terminal

Expand All @@ -68,7 +68,7 @@ The MATLAB participant for the current also records the current and voltage thro

After successfully running the coupling, one can find the curves in the folder `capacitor-matlab` as `Curves.png`.

The `Curves.png` plot could exemplarily look like the following:
Example of a `Curves.png` plot:
![Voltage and current plot of the resonant circuit](images/tutorials-resonant-circuit-curves.png)

## References
Expand Down
4 changes: 2 additions & 2 deletions tools/tests/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ calculix-adapter:
TUTORIALS_REF:
description: Tutorial git reference to use
default: "master"
CALULIX_VERSION:
CALCULIX_VERSION:
description: Version of Calculix to use
default: "2.20"
CALULIX_ADAPTER_REF:
CALCULIX_ADAPTER_REF:
description: Version of Calculix-Adapter to use
default: "master"

Expand Down
3 changes: 2 additions & 1 deletion two-scale-heat-conduction/macro-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"read_data_names": {"concentration": "scalar"}
},
"simulation_params": {
"micro_dt": 0.01,
"macro_domain_bounds": [0.0, 1.0, 0.0, 0.5],
"decomposition": [2, 1],
"adaptivity": "True",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"read_data_names": {"concentration": "scalar"}
},
"simulation_params": {
"micro_dt": 0.01,
"macro_domain_bounds": [0.0, 1.0, 0.0, 0.5],
"decomposition": [2, 1],
"adaptivity": "True",
Expand Down
3 changes: 2 additions & 1 deletion two-scale-heat-conduction/micro-nutils/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nutils==7
pyprecice==3
numpy >1, <2
pyprecice~=3.0
micro-manager-precice==0.4.0
4 changes: 3 additions & 1 deletion volume-coupled-flow/fluid-openfoam/constant/fvOptions
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ codedSource
for(auto cell : cells)
{
fld[cell].x() = U_vol[cell].x();
fld[cell].y() = U_vol[cell].y();
fld[cell].z() = U_vol[cell].z();
}
#};

Expand All @@ -39,4 +41,4 @@ codedSource
#{
return;
#};
}
}
Binary file modified volume-coupled-flow/images/tutorials-volume-coupled-flow-Ux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dfca8d6

Please sign in to comment.