Skip to content

Commit f5e0cac

Browse files
committed
WIP: Add Python code for couple your code section
1 parent 320a6e1 commit f5e0cac

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

pages/docs/couple-your-code/couple-your-code-preparing-your-solver.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ summary: "If you want to couple your own code you need to properly understand it
77

88
Let's say you want to prepare a fluid solver for fluid-structure interaction and that your code looks like this:
99

10+
<ul id="apiTabs" class="nav nav-tabs">
11+
<li class="active"><a href="#cpp" data-toggle="tab">C++</a></li>
12+
<li><a href="#python" data-toggle="tab">Python</a></li>
13+
</ul>
14+
<div class="tab-content">
15+
<div role="tabpanel" class="tab-pane active" id="cpp" markdown="1">
16+
1017
```cpp
1118
turnOnSolver(); //e.g. setup and partition mesh
1219

@@ -18,8 +25,25 @@ while (not simulationDone()){ // time loop
1825
endTimeStep(); // e.g. update variables, increment time
1926
}
2027
turnOffSolver();
28+
29+
```
30+
31+
</div>
32+
<div role="tabpanel" class="tab-pane" id="python" markdown="1">
33+
34+
```python
35+
turn_on_solver() #e.g. setup and partition mesh
36+
37+
while not simulation_done(): # time loop
38+
dt = begin_time_step() #e.g compute adaptive dt
39+
solve_time_step(dt)
40+
end_time_step() #e.g. update variables, increment time
41+
42+
turn_off_solver()
2143
```
2244

45+
</div>
46+
</div>
2347
Probably most solvers have such a structures: something in the beginning (reading input, domain decomposition), a big time loop, and something in the end. Each time step also falls into three parts: some pre-computations (e.g. computing an adaptive time step size), the actual computation (solving a linear or non-linear equation system), and something in the end (updating variables, incrementing time). Try to identify these parts in the code you want to couple.
2448

2549
In the following steps, we will slowly add more and more calls to the preCICE API in this code snippet. Some part of the preCICE API is briefly described in each step. More precisely (no pun intended :grin:), we use the native `C++` API of preCICE. The API is, however, also available in other scientific programming languages: plain `C`, `Fortran`, `Python`, `Matlab`, `Julia`, and `Rust` (see [Application Programming Interface](couple-your-code-api)).

pages/docs/couple-your-code/couple-your-code-steering-methods.md

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,65 @@ summary: "In this step, you get to know the most important API functions of preC
99
As a first preparation step, you need to include the preCICE library headers. In C++, you need to include the file [precice.hpp](https://github.com/precice/precice/blob/develop/src/precice/precice.hpp).
1010
The handle to the preCICE API is the class `precice::Participant`. Its constructor requires the participant's name, the preCICE configuration file's name and the `rank` and `size` of the current thread. Once the basic preCICE interface is set up, we can _steer_ the behaviour of preCICE. For that we need the following functions:
1111

12+
<ul id="apiTabs" class="nav nav-tabs">
13+
<li class="active"><a href="#cpp-1" data-toggle="tab">C++</a></li>
14+
<li><a href="#python-1" data-toggle="tab">Python</a></li>
15+
</ul>
16+
<div class="tab-content">
17+
<div role="tabpanel" class="tab-pane active" id="cpp-1" markdown="1">
18+
1219
```cpp
1320
Participant( String participantName, String configurationFileName, int rank, int size );
1421
void initialize();
1522
void advance ( double computedTimeStepSize );
1623
void finalize();
1724
```
1825
26+
</div>
27+
<div role="tabpanel" class="tab-pane" id="python-1" markdown="1">
28+
29+
```python
30+
"""
31+
Parameters:
32+
participant_name: string
33+
Name of the solver
34+
configuration_file_name: string
35+
Name of preCICE config file
36+
rank: int
37+
Rank of the process
38+
size: int
39+
Size of the process
40+
"""
41+
participant = Participant(participant_name, configuration_file_name, rank, size)
42+
43+
participant.initialize()
44+
45+
"""
46+
Parameters:
47+
computed_timestep_size: double
48+
Length of timestep used by solver
49+
"""
50+
participant.advance(computed_timestep_size)
51+
52+
participant.finalize()
53+
54+
```
55+
56+
</div>
57+
</div>
1958
What do they do?
2059

2160
* `initialize` establishes communication channels and sets up data structures of preCICE.
2261
* `advance` needs to be called after the computation of every time step to _advance_ the coupling. As an argument, you have to pass the solver's last time step size (`dt`). Additionally, it maps coupling data between the coupling meshes, it communicates coupling data between the coupled participants, and it accelerates coupling data. One could say the complete coupling happens within this single function.
2362
* `finalize` frees the preCICE data structures and closes communication channels.
2463

2564
The following function allows us to query the maximum allowed time step size from preCICE:
26-
27-
2865
<ul id="apiTabs" class="nav nav-tabs">
29-
<li class="active"><a href="#cpp" data-toggle="tab">C++</a></li>
30-
<li><a href="#python" data-toggle="tab">Python</a></li>
66+
<li class="active"><a href="#cpp-2" data-toggle="tab">C++</a></li>
67+
<li><a href="#python-2" data-toggle="tab">Python</a></li>
3168
</ul>
3269
<div class="tab-content">
33-
<div role="tabpanel" class="tab-pane active" id="cpp" markdown="1">
70+
<div role="tabpanel" class="tab-pane active" id="cpp-2" markdown="1">
3471
```cpp
3572
double getMaxTimeStepSize();
3673
```
@@ -60,8 +97,10 @@ while (not simulationDone()){ // time loop
6097
precice.finalize(); // frees data structures and closes communication channels
6198
turnOffSolver();
6299
```
100+
63101
</div>
64-
<div role="tabpanel" class="tab-pane" id="python" markdown="1">
102+
<div role="tabpanel" class="tab-pane" id="python-2" markdown="1">
103+
65104
```python
66105
import precice
67106
@@ -82,7 +121,8 @@ while t < t_end: # time loop
82121
t = t + dt
83122
84123
precice.finalize() # frees data structures and closes communication channels
124+
85125
```
126+
86127
</div>
87128
</div>
88-

0 commit comments

Comments
 (0)