-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add python variant of resonant circuit (#498)
* Add python version of resonator. * Add of python case in README.md * Add requirement.txt and modify run file to use venv --------- Co-authored-by: NiklasV <[email protected]> Co-authored-by: Gerasimos Chourdakis <[email protected]> Co-authored-by: Niklas <[email protected]> Co-authored-by: Benjamin Rodenberg <[email protected]>
- Loading branch information
1 parent
c4eb0ec
commit b80509a
Showing
9 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import precice | ||
import numpy as np | ||
from scipy.integrate import solve_ivp | ||
|
||
# Initialize and configure preCICE | ||
participant = precice.Participant("Capacitor", "../precice-config.xml", 0, 1) | ||
|
||
# Geometry IDs. As it is a 0-D simulation, only one vertex is necessary. | ||
mesh_name = "Capacitor-Mesh" | ||
|
||
dimensions = participant.get_mesh_dimensions(mesh_name) | ||
|
||
vertex_ids = np.array([participant.set_mesh_vertex(mesh_name, np.zeros(dimensions))]) | ||
|
||
# Data IDs | ||
read_data_name = "Current" | ||
write_data_name = "Voltage" | ||
|
||
# Simulation parameters and initial condition | ||
C = 2 # Capacitance of the capacitor | ||
L = 1 # Inductance of the coil | ||
t0 = 0 # Initial simulation time | ||
t_max = 10 # End simulation time | ||
Io = np.array([1]) # Initial current | ||
phi = 0 # Phase of the signal | ||
|
||
w0 = 1 / np.sqrt(L * C) # Resonant frequency | ||
U0 = -w0 * L * Io * np.sin(phi) # Initial condition for U | ||
|
||
|
||
def f_U(dt, max_allowed_dt): | ||
if dt > max_allowed_dt: # read_data will fail, if dt is outside of window | ||
return np.nan | ||
|
||
I = participant.read_data(mesh_name, read_data_name, vertex_ids, dt) | ||
return -I / C # Time derivative of U | ||
|
||
|
||
# Initialize simulation | ||
if participant.requires_initial_data(): | ||
participant.write_data(mesh_name, write_data_name, vertex_ids, U0) | ||
|
||
participant.initialize() | ||
|
||
solver_dt = participant.get_max_time_step_size() | ||
|
||
# Start simulation | ||
t = t0 | ||
while participant.is_coupling_ongoing(): | ||
|
||
# Record checkpoint if necessary | ||
if participant.requires_writing_checkpoint(): | ||
U0_checkpoint = U0 | ||
t_checkpoint = t | ||
|
||
# Make Simulation Step | ||
precice_dt = participant.get_max_time_step_size() | ||
dt = min([precice_dt, solver_dt]) | ||
t_span = [t, t + dt] | ||
sol = solve_ivp(lambda t, y: f_U(t - t_span[0], dt), t_span, U0, dense_output=True, rtol=1e-12, atol=1e-12) | ||
|
||
# Exchange data | ||
evals = max(len(sol.t), 3) # at least do 3 substeps to allow cubic interpolation | ||
for i in range(evals): | ||
U0 = sol.sol(t_span[0] + (i + 1) * dt / evals) | ||
participant.write_data(mesh_name, write_data_name, vertex_ids, np.array(U0)) | ||
participant.advance(dt / evals) | ||
|
||
t = t + dt | ||
|
||
# Recover checkpoint if not converged | ||
if participant.requires_reading_checkpoint(): | ||
U0 = U0_checkpoint | ||
t = t_checkpoint | ||
|
||
# Stop coupling | ||
participant.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
numpy >1, <2 | ||
scipy | ||
pyprecice~=3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
set -e -u | ||
|
||
. ../../tools/log.sh | ||
exec > >(tee --append "$LOGFILE") 2>&1 | ||
|
||
python3 -m venv .venv | ||
. .venv/bin/activate | ||
pip install -r requirements.txt | ||
python3 capacitor.py | ||
|
||
close_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import precice | ||
import numpy as np | ||
from scipy.integrate import solve_ivp | ||
|
||
# Initialize and configure preCICE | ||
participant = precice.Participant("Coil", "../precice-config.xml", 0, 1) | ||
|
||
# Geometry IDs. As it is a 0-D simulation, only one vertex is necessary. | ||
mesh_name = "Coil-Mesh" | ||
|
||
dimensions = participant.get_mesh_dimensions(mesh_name) | ||
|
||
vertex_ids = np.array([participant.set_mesh_vertex(mesh_name, np.zeros(dimensions))]) | ||
|
||
# Data IDs | ||
read_data_name = "Voltage" | ||
write_data_name = "Current" | ||
|
||
# Simulation parameters and initial condition | ||
C = 2 # Capacitance of the capacitor | ||
L = 1 # Inductance of the coil | ||
t0 = 0 # Initial simulation time | ||
Io = np.array([1]) # Initial current | ||
phi = 0 # Phase of the signal | ||
|
||
w0 = 1 / np.sqrt(L * C) # Resonant frequency | ||
I0 = Io * np.cos(phi) # Initial condition for I | ||
|
||
# to estimate cost | ||
global f_evals | ||
f_evals = 0 | ||
|
||
|
||
def f_I(dt, max_allowed_dt): | ||
global f_evals | ||
f_evals += 1 | ||
if dt > max_allowed_dt: # read_data will fail, if dt is outside of window | ||
return np.nan | ||
|
||
U = participant.read_data(mesh_name, read_data_name, vertex_ids, dt) | ||
return U / L # Time derivative of I; ODE determining capacitor | ||
|
||
|
||
# Initialize simulation | ||
if participant.requires_initial_data(): | ||
participant.write_data(mesh_name, write_data_name, vertex_ids, I0) | ||
|
||
participant.initialize() | ||
|
||
solver_dt = participant.get_max_time_step_size() | ||
|
||
# Start simulation | ||
t = t0 | ||
while participant.is_coupling_ongoing(): | ||
|
||
# Record checkpoint if necessary | ||
if participant.requires_writing_checkpoint(): | ||
I0_checkpoint = I0 | ||
t_checkpoint = t | ||
|
||
# Make Simulation Step | ||
precice_dt = participant.get_max_time_step_size() | ||
dt = min([precice_dt, solver_dt]) | ||
t_span = [t, t + dt] | ||
sol = solve_ivp(lambda t, y: f_I(t - t_span[0], dt), t_span, I0, dense_output=True, rtol=1e-12, atol=1e-12) | ||
|
||
# Exchange data | ||
evals = max(len(sol.t), 3) # at least do 3 substeps to allow cubic interpolation | ||
for i in range(evals): | ||
I0 = sol.sol(t_span[0] + (i + 1) * dt / evals) | ||
participant.write_data(mesh_name, write_data_name, vertex_ids, np.array(I0)) | ||
participant.advance(dt / evals) | ||
|
||
t = t + dt | ||
|
||
# Recover checkpoint if not converged | ||
if participant.requires_reading_checkpoint(): | ||
I0 = I0_checkpoint | ||
t = t_checkpoint | ||
|
||
# Stop coupling | ||
participant.finalize() | ||
|
||
|
||
def I_analytical(t): return Io * np.cos(t * w0 + phi) | ||
|
||
|
||
error = I0 - I_analytical(t) | ||
print(f"{error=}") | ||
print(f"{f_evals=}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
numpy >1, <2 | ||
scipy | ||
pyprecice~=3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
set -e -u | ||
|
||
. ../../tools/log.sh | ||
exec > >(tee --append "$LOGFILE") 2>&1 | ||
|
||
python3 -m venv .venv | ||
. .venv/bin/activate | ||
pip install -r requirements.txt | ||
python3 coil.py | ||
|
||
close_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/sh | ||
|
||
if [ "${1:-}" = "" ]; then | ||
echo "No target directory specified. Please specify the directory of the participant containing the watchpoint, e.g. ./plot-displacement.sh coil-python." | ||
exit 1 | ||
fi | ||
|
||
FILE="$1/precice-Capacitor-watchpoint-VoltageCurrent.log" | ||
|
||
if [ ! -f "$FILE" ]; then | ||
echo "Unable to locate the watchpoint file (precice-Capacitor-watchpoint-VoltageCurrent.log) in the specified participant directory '${1}'. Make sure the specified directory matches the participant you used for the calculations." | ||
exit 1 | ||
fi | ||
|
||
gnuplot -p << EOF | ||
set grid | ||
set title 'Voltage and current' | ||
set xlabel 'time [s]' | ||
set ylabel 'Voltage / Current' | ||
plot "$1/precice-Capacitor-watchpoint-VoltageCurrent.log" using 1:4 with linespoints title "Voltage", "$1/precice-Capacitor-watchpoint-VoltageCurrent.log" using 1:5 with linespoints title "Current" | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters