-
-
Notifications
You must be signed in to change notification settings - Fork 122
Add python variant of resonant circuit #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BenjaminRodenberg
merged 22 commits into
precice:develop
from
BenjaminRodenberg:add-lc-circuit-python
Aug 30, 2024
Merged
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
feb899c
Add python version of resonator.
BenjaminRodenberg 18737c7
Add an error estimate.
BenjaminRodenberg eef2da1
Use black-box time stepper, subcycling, and time interpolation.
BenjaminRodenberg 44eda69
Fix black-box adaptive time stepping
BenjaminRodenberg 234f911
Remove unneeded.
BenjaminRodenberg 19dd2c3
Fix format.
BenjaminRodenberg 42480c4
Merge branch 'develop' into add-lc-circuit-python
BenjaminRodenberg 47c6c6a
Merge branch 'precice:develop' into add-lc-circuit-python
NiklasVin d47c5c3
Merge branch 'develop' into add-lc-circuit-python
BenjaminRodenberg 0b52557
Merge branch 'add-lc-circuit-python' of github.com:BenjaminRodenberg/…
BenjaminRodenberg 88733c5
Fix labels, use time interpolation, measure convergence.
BenjaminRodenberg dfffd5e
Update README.md
BenjaminRodenberg f6bcd44
Merge branch 'develop' into add-lc-circuit-python
MakisH dfca8d6
Merge branch 'develop' into add-lc-circuit-python
BenjaminRodenberg f1ecc77
Fix autopep8.
BenjaminRodenberg 4f1fe64
Update resonant-circuit/README.md
MakisH 033a2c1
Add requirement.txt and modify run file to use venv
NiklasVin 60c2b92
Add description how to start the python case
NiklasVin 1b7f320
Revert changes in README
NiklasVin 28d8010
Fix postprocessing.
BenjaminRodenberg edc9701
Merge branch 'add-lc-circuit-python' of github.com:BenjaminRodenberg/…
BenjaminRodenberg 64fdfa1
Update resonant-circuit/README.md
BenjaminRodenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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-ParticipantU-watchpoint-UI.log" | ||
|
||
if [ ! -f "$FILE" ]; then | ||
echo "Unable to locate the watchpoint file (precice-ParticipantU-watchpoint-UI.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-ParticipantU-watchpoint-UI.log" using 1:4 with linespoints title "Voltage", "$1/precice-ParticipantU-watchpoint-UI.log" using 1:5 with linespoints title "Current" | ||
EOF |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.