From a5f9532cecb9f5421984327c06709470af5e5f5e Mon Sep 17 00:00:00 2001 From: marinlauber Date: Fri, 25 Jul 2025 13:41:56 +0200 Subject: [PATCH 01/12] add julia resonant circuit example --- resonant-circuit/capacitor-julia/Project.toml | 3 + resonant-circuit/capacitor-julia/capacitor.jl | 90 +++++++++++++++ resonant-circuit/capacitor-julia/run.sh | 3 + resonant-circuit/coil-julia/Project.toml | 3 + resonant-circuit/coil-julia/coil.jl | 107 ++++++++++++++++++ resonant-circuit/coil-julia/run.sh | 3 + 6 files changed, 209 insertions(+) create mode 100644 resonant-circuit/capacitor-julia/Project.toml create mode 100644 resonant-circuit/capacitor-julia/capacitor.jl create mode 100755 resonant-circuit/capacitor-julia/run.sh create mode 100644 resonant-circuit/coil-julia/Project.toml create mode 100644 resonant-circuit/coil-julia/coil.jl create mode 100755 resonant-circuit/coil-julia/run.sh diff --git a/resonant-circuit/capacitor-julia/Project.toml b/resonant-circuit/capacitor-julia/Project.toml new file mode 100644 index 000000000..8a303cd38 --- /dev/null +++ b/resonant-circuit/capacitor-julia/Project.toml @@ -0,0 +1,3 @@ +[deps] +OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +PreCICE = "57fbd4af-5cc3-4712-aac0-6930e7658184" diff --git a/resonant-circuit/capacitor-julia/capacitor.jl b/resonant-circuit/capacitor-julia/capacitor.jl new file mode 100644 index 000000000..3e97adbf2 --- /dev/null +++ b/resonant-circuit/capacitor-julia/capacitor.jl @@ -0,0 +1,90 @@ +using PreCICE +using OrdinaryDiffEq +using JLD2 + +# Initialize and configure preCICE +participant = PreCICE.createParticipant("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 = PreCICE.getMeshDimensions(mesh_name) + +vertex_ids = PreCICE.setMeshVertices(mesh_name, [0. 0.]) + +let + # 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 = 1 # Initial current + phi = 0 # Phase of the signal + + w0 = 1 / sqrt(L * C) # Resonant frequency + U0 = -w0 * L * Io * sin(phi) # Initial condition for U + + function f_U(u, p, t) + (dt, C, mesh_name, read_data_name, vertex_ids) = p + I = PreCICE.readData(mesh_name, read_data_name, vertex_ids, dt) + return -I[1] / C # Time derivative of U + end + + # Initialize simulation + if PreCICE.requiresInitialData() + @show I0 + PreCICE.writeData(mesh_name, write_data_name, vertex_ids, I0) + end + PreCICE.initialize() + + solver_dt = PreCICE.getMaxTimeStepSize() + + # Start simulation + t = t0 + U0_checkpoint = U0 + t_checkpoint = t + U_store = [[t, U0]] + while PreCICE.isCouplingOngoing() + + + # Record checkpoint if necessary + if PreCICE.requiresWritingCheckpoint() + U0_checkpoint = U0 + t_checkpoint = t + end + + # Make Simulation Step + precice_dt = PreCICE.getMaxTimeStepSize() + dt = min(precice_dt, solver_dt) + t_span = (t, t + dt) + params = (dt, C, mesh_name, read_data_name, vertex_ids) + prob = ODEProblem(f_U, U0, t_span, params) + # https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts + sol = solve(prob, Tsit5(), reltol=1e-12, abstol=1e-12) + + # Exchange data + evals = max(length(sol.t), 3) # at least do 3 substeps to allow cubic interpolation + for i in range(1,evals) + U0 = sol(t_span[1] + i * dt / evals) + PreCICE.writeData(mesh_name, write_data_name, vertex_ids, fill(U0, (1,1))) + PreCICE.advance(dt / evals) + end + t = t + dt + + # Recover checkpoint if not converged + if PreCICE.requiresReadingCheckpoint() + U0 = U0_checkpoint + t = t_checkpoint + end + if PreCICE.isTimeWindowComplete() + push!(U_store, [t, U0]) + end + end + jldsave("capacitor.jld2", U=U_store) + # Stop coupling + PreCICE.finalize() +end \ No newline at end of file diff --git a/resonant-circuit/capacitor-julia/run.sh b/resonant-circuit/capacitor-julia/run.sh new file mode 100755 index 000000000..b8fad8705 --- /dev/null +++ b/resonant-circuit/capacitor-julia/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +julia --project=. capacitor.jl \ No newline at end of file diff --git a/resonant-circuit/coil-julia/Project.toml b/resonant-circuit/coil-julia/Project.toml new file mode 100644 index 000000000..8a303cd38 --- /dev/null +++ b/resonant-circuit/coil-julia/Project.toml @@ -0,0 +1,3 @@ +[deps] +OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" +PreCICE = "57fbd4af-5cc3-4712-aac0-6930e7658184" diff --git a/resonant-circuit/coil-julia/coil.jl b/resonant-circuit/coil-julia/coil.jl new file mode 100644 index 000000000..00c192ec0 --- /dev/null +++ b/resonant-circuit/coil-julia/coil.jl @@ -0,0 +1,107 @@ +using PreCICE +using OrdinaryDiffEq +using Plots +using JLD2 + +# Initialize and configure preCICE +participant = PreCICE.createParticipant("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 = PreCICE.getMeshDimensions(mesh_name) + +vertex_ids = PreCICE.setMeshVertices(mesh_name, [0. 0.]) + +let + # 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 = 1 # Initial current + phi = 0 # Phase of the signal + + w0 = 1 / sqrt(L * C) # Resonant frequency + I0 = Io * cos(phi) # Initial condition for I + + # to estimate cost + f_evals = 0 + + function f_I(u, p, t) + f_evals += 1 + (dt, L, mesh_name, read_data_name, vertex_ids) = p + U = PreCICE.readData(mesh_name, read_data_name, vertex_ids, dt) + return U[1] / L # Time derivative of I; ODE determining capacitor + end + + # Initialize simulation + if PreCICE.requiresInitialData() + @show I0 + PreCICE.writeData(mesh_name, write_data_name, vertex_ids, I0) + end + PreCICE.initialize() + + solver_dt = PreCICE.getMaxTimeStepSize() + + # Start simulation + t = t0 + I0_checkpoint = I0 + t_checkpoint = t + I_store = [[t, I0]] + while PreCICE.isCouplingOngoing() + + # Record checkpoint if necessary + if PreCICE.requiresWritingCheckpoint() + I0_checkpoint = I0 + t_checkpoint = t + end + + # Make Simulation Step + precice_dt = PreCICE.getMaxTimeStepSize() + dt = min(precice_dt, solver_dt) + t_span = (t, t + dt) + params = (dt, L, mesh_name, read_data_name, vertex_ids) + prob = ODEProblem(f_I, I0, t_span, params) + # https://docs.sciml.ai/DiffEqDocs/stable/basics/common_solver_opts + sol = solve(prob, Tsit5(), reltol=1e-12, abstol=1e-12) + + # Exchange data + evals = max(length(sol.t), 3) # at least do 3 substeps to allow cubic interpolation + for i in range(1,evals) + I0 = sol(t_span[1] + i * dt / evals) + PreCICE.writeData(mesh_name, write_data_name, vertex_ids, fill(I0, (1,1))) + PreCICE.advance(dt / evals) + end + t = t + dt + + # Recover checkpoint if not converged + if PreCICE.requiresReadingCheckpoint() + I0 = I0_checkpoint + t = t_checkpoint + end + if PreCICE.isTimeWindowComplete() + push!(I_store, [t, I0]) + end + end + jldsave("coil.jld2", U=I_store) + # Stop coupling + PreCICE.finalize() + + # solutions + I_analytical(t) = Io * cos(t * w0 + phi) + U_analytical(t) = -w0 * L * Io * sin(w0 * t + phi); + + # plot + time = getindex.(I_store,1); I_num = getindex.(I_store,2) + plot(time, [I_num, I_analytical.(time), U_analytical.(time)], + label=["Simulated Current" "Analytical Current" "Analytical Voltage"], + xlabel="Time", title="Resonant Circuit Simulation") + U_store = jldopen(joinpath(@__DIR__,"../capacitor-julia/capacitor.jld2")) + time = getindex.(U_store["U"],1); U_num = getindex.(U_store["U"],2) + plot!(time, U_num, label="Simulated Voltage") + savefig("resonant_circuit.png") +end \ No newline at end of file diff --git a/resonant-circuit/coil-julia/run.sh b/resonant-circuit/coil-julia/run.sh new file mode 100755 index 000000000..7d19bd1e8 --- /dev/null +++ b/resonant-circuit/coil-julia/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +julia --project=. coil.jl \ No newline at end of file From 114d8115532f453c8fd3c0e34bb2eab54198c9aa Mon Sep 17 00:00:00 2001 From: marinlauber Date: Fri, 25 Jul 2025 14:37:31 +0200 Subject: [PATCH 02/12] update README.md --- resonant-circuit/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/resonant-circuit/README.md b/resonant-circuit/README.md index fbbcbc4e9..ca2cb21fd 100644 --- a/resonant-circuit/README.md +++ b/resonant-circuit/README.md @@ -1,7 +1,7 @@ --- title: Resonant Circuit permalink: tutorials-resonant-circuit.html -keywords: MATLAB, Python +keywords: MATLAB, Python, Julia summary: We simulate a two-element LC circuit (one inductor and one capacitor). --- @@ -33,6 +33,7 @@ preCICE configuration (image generated using the [precice-config-visualizer](htt * *MATLAB* A solver using the [MATLAB bindings](https://precice.org/installation-bindings-matlab.html). Before running this tutorial, follow the [instructions](https://precice.org/installation-bindings-matlab.html) to correctly install the MATLAB bindings. * *Python* A solver using the preCICE [Python bindings](https://precice.org/installation-bindings-python.html). +* *Julia* A solver using the preCICE [Julia bindings](https://precice.org/installation-bindings-julia.html). ## Running the simulation From 64889d92ce3426f6537b5e5a41f849c070292d29 Mon Sep 17 00:00:00 2001 From: marinlauber Date: Wed, 6 Aug 2025 09:47:12 +0200 Subject: [PATCH 03/12] tidy scripts and remove plotting from there, fix run.sh --- resonant-circuit/capacitor-julia/capacitor.jl | 10 +------- resonant-circuit/capacitor-julia/run.sh | 11 +++++++-- resonant-circuit/coil-julia/coil.jl | 24 +------------------ resonant-circuit/coil-julia/run.sh | 11 +++++++-- 4 files changed, 20 insertions(+), 36 deletions(-) diff --git a/resonant-circuit/capacitor-julia/capacitor.jl b/resonant-circuit/capacitor-julia/capacitor.jl index 3e97adbf2..175efe88e 100644 --- a/resonant-circuit/capacitor-julia/capacitor.jl +++ b/resonant-circuit/capacitor-julia/capacitor.jl @@ -1,6 +1,5 @@ using PreCICE using OrdinaryDiffEq -using JLD2 # Initialize and configure preCICE participant = PreCICE.createParticipant("Capacitor", "../precice-config.xml", 0, 1) @@ -10,7 +9,7 @@ mesh_name = "Capacitor-Mesh" dimensions = PreCICE.getMeshDimensions(mesh_name) -vertex_ids = PreCICE.setMeshVertices(mesh_name, [0. 0.]) +vertex_ids = PreCICE.setMeshVertices(mesh_name, zeros((1,dimensions))) let # Data IDs @@ -36,7 +35,6 @@ let # Initialize simulation if PreCICE.requiresInitialData() - @show I0 PreCICE.writeData(mesh_name, write_data_name, vertex_ids, I0) end PreCICE.initialize() @@ -47,10 +45,8 @@ let t = t0 U0_checkpoint = U0 t_checkpoint = t - U_store = [[t, U0]] while PreCICE.isCouplingOngoing() - # Record checkpoint if necessary if PreCICE.requiresWritingCheckpoint() U0_checkpoint = U0 @@ -80,11 +76,7 @@ let U0 = U0_checkpoint t = t_checkpoint end - if PreCICE.isTimeWindowComplete() - push!(U_store, [t, U0]) - end end - jldsave("capacitor.jld2", U=U_store) # Stop coupling PreCICE.finalize() end \ No newline at end of file diff --git a/resonant-circuit/capacitor-julia/run.sh b/resonant-circuit/capacitor-julia/run.sh index b8fad8705..4e6b56f7b 100755 --- a/resonant-circuit/capacitor-julia/run.sh +++ b/resonant-circuit/capacitor-julia/run.sh @@ -1,3 +1,10 @@ -#!/bin/sh +#!/usr/bin/env bash +set -e -u -julia --project=. capacitor.jl \ No newline at end of file +. ../../tools/log.sh +exec > >(tee --append "$LOGFILE") 2>&1 + +julia --project=Project.toml -e "using Pkg; Pkg.instantiate();" +julia --project=Project.toml capacitor.jl + +close_log \ No newline at end of file diff --git a/resonant-circuit/coil-julia/coil.jl b/resonant-circuit/coil-julia/coil.jl index 00c192ec0..38549a719 100644 --- a/resonant-circuit/coil-julia/coil.jl +++ b/resonant-circuit/coil-julia/coil.jl @@ -1,7 +1,5 @@ using PreCICE using OrdinaryDiffEq -using Plots -using JLD2 # Initialize and configure preCICE participant = PreCICE.createParticipant("Coil", "../precice-config.xml", 0, 1) @@ -11,7 +9,7 @@ mesh_name = "Coil-Mesh" dimensions = PreCICE.getMeshDimensions(mesh_name) -vertex_ids = PreCICE.setMeshVertices(mesh_name, [0. 0.]) +vertex_ids = PreCICE.setMeshVertices(mesh_name, zeros((1,dimensions))) let # Data IDs @@ -40,7 +38,6 @@ let # Initialize simulation if PreCICE.requiresInitialData() - @show I0 PreCICE.writeData(mesh_name, write_data_name, vertex_ids, I0) end PreCICE.initialize() @@ -51,7 +48,6 @@ let t = t0 I0_checkpoint = I0 t_checkpoint = t - I_store = [[t, I0]] while PreCICE.isCouplingOngoing() # Record checkpoint if necessary @@ -83,25 +79,7 @@ let I0 = I0_checkpoint t = t_checkpoint end - if PreCICE.isTimeWindowComplete() - push!(I_store, [t, I0]) - end end - jldsave("coil.jld2", U=I_store) # Stop coupling PreCICE.finalize() - - # solutions - I_analytical(t) = Io * cos(t * w0 + phi) - U_analytical(t) = -w0 * L * Io * sin(w0 * t + phi); - - # plot - time = getindex.(I_store,1); I_num = getindex.(I_store,2) - plot(time, [I_num, I_analytical.(time), U_analytical.(time)], - label=["Simulated Current" "Analytical Current" "Analytical Voltage"], - xlabel="Time", title="Resonant Circuit Simulation") - U_store = jldopen(joinpath(@__DIR__,"../capacitor-julia/capacitor.jld2")) - time = getindex.(U_store["U"],1); U_num = getindex.(U_store["U"],2) - plot!(time, U_num, label="Simulated Voltage") - savefig("resonant_circuit.png") end \ No newline at end of file diff --git a/resonant-circuit/coil-julia/run.sh b/resonant-circuit/coil-julia/run.sh index 7d19bd1e8..08a42b289 100755 --- a/resonant-circuit/coil-julia/run.sh +++ b/resonant-circuit/coil-julia/run.sh @@ -1,3 +1,10 @@ -#!/bin/sh +#!/usr/bin/env bash +set -e -u -julia --project=. coil.jl \ No newline at end of file +. ../../tools/log.sh +exec > >(tee --append "$LOGFILE") 2>&1 + +julia --project=Project.toml -e "using Pkg; Pkg.instantiate();" +julia --project=Project.toml coil.jl + +close_log \ No newline at end of file From 6d4bae9fcfb42a67e3a90e181fd1e5c02ad298a1 Mon Sep 17 00:00:00 2001 From: marinlauber Date: Wed, 6 Aug 2025 10:36:40 +0200 Subject: [PATCH 04/12] add package install in run and switch OrdinaryDiffEq for DifferentialEquations --- resonant-circuit/capacitor-julia/capacitor.jl | 2 +- resonant-circuit/capacitor-julia/run.sh | 2 +- resonant-circuit/coil-julia/coil.jl | 2 +- resonant-circuit/coil-julia/run.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/resonant-circuit/capacitor-julia/capacitor.jl b/resonant-circuit/capacitor-julia/capacitor.jl index 175efe88e..9219c5cae 100644 --- a/resonant-circuit/capacitor-julia/capacitor.jl +++ b/resonant-circuit/capacitor-julia/capacitor.jl @@ -1,5 +1,5 @@ using PreCICE -using OrdinaryDiffEq +using DifferentialEquations # Initialize and configure preCICE participant = PreCICE.createParticipant("Capacitor", "../precice-config.xml", 0, 1) diff --git a/resonant-circuit/capacitor-julia/run.sh b/resonant-circuit/capacitor-julia/run.sh index 4e6b56f7b..a61d1fa67 100755 --- a/resonant-circuit/capacitor-julia/run.sh +++ b/resonant-circuit/capacitor-julia/run.sh @@ -4,7 +4,7 @@ set -e -u . ../../tools/log.sh exec > >(tee --append "$LOGFILE") 2>&1 -julia --project=Project.toml -e "using Pkg; Pkg.instantiate();" +julia --project=Project.toml -e 'using Pkg; Pkg.add("DifferentialEquations"); Pkg.add("PreCICE"); Pkg.instantiate();' julia --project=Project.toml capacitor.jl close_log \ No newline at end of file diff --git a/resonant-circuit/coil-julia/coil.jl b/resonant-circuit/coil-julia/coil.jl index 38549a719..6cd949ebb 100644 --- a/resonant-circuit/coil-julia/coil.jl +++ b/resonant-circuit/coil-julia/coil.jl @@ -1,5 +1,5 @@ using PreCICE -using OrdinaryDiffEq +using DifferentialEquations # Initialize and configure preCICE participant = PreCICE.createParticipant("Coil", "../precice-config.xml", 0, 1) diff --git a/resonant-circuit/coil-julia/run.sh b/resonant-circuit/coil-julia/run.sh index 08a42b289..6340410ae 100755 --- a/resonant-circuit/coil-julia/run.sh +++ b/resonant-circuit/coil-julia/run.sh @@ -4,7 +4,7 @@ set -e -u . ../../tools/log.sh exec > >(tee --append "$LOGFILE") 2>&1 -julia --project=Project.toml -e "using Pkg; Pkg.instantiate();" +julia --project=Project.toml -e 'using Pkg; Pkg.add("DifferentialEquations"); Pkg.add("PreCICE"); Pkg.instantiate();' julia --project=Project.toml coil.jl close_log \ No newline at end of file From cc4dedf866d04e0117368781c4f8121a6700e226 Mon Sep 17 00:00:00 2001 From: marinlauber Date: Wed, 6 Aug 2025 10:39:49 +0200 Subject: [PATCH 05/12] rm old Project files --- resonant-circuit/capacitor-julia/Project.toml | 3 --- resonant-circuit/coil-julia/Project.toml | 3 --- 2 files changed, 6 deletions(-) delete mode 100644 resonant-circuit/capacitor-julia/Project.toml delete mode 100644 resonant-circuit/coil-julia/Project.toml diff --git a/resonant-circuit/capacitor-julia/Project.toml b/resonant-circuit/capacitor-julia/Project.toml deleted file mode 100644 index 8a303cd38..000000000 --- a/resonant-circuit/capacitor-julia/Project.toml +++ /dev/null @@ -1,3 +0,0 @@ -[deps] -OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" -PreCICE = "57fbd4af-5cc3-4712-aac0-6930e7658184" diff --git a/resonant-circuit/coil-julia/Project.toml b/resonant-circuit/coil-julia/Project.toml deleted file mode 100644 index 8a303cd38..000000000 --- a/resonant-circuit/coil-julia/Project.toml +++ /dev/null @@ -1,3 +0,0 @@ -[deps] -OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" -PreCICE = "57fbd4af-5cc3-4712-aac0-6930e7658184" From d807eb4b9bf08e1275704439cce8de3330561957 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 21:46:43 +0200 Subject: [PATCH 06/12] Add changelog entry --- changelog-entries/658.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog-entries/658.md diff --git a/changelog-entries/658.md b/changelog-entries/658.md new file mode 100644 index 000000000..cf9e728c7 --- /dev/null +++ b/changelog-entries/658.md @@ -0,0 +1 @@ +- Added Julia-based participants to the resonant-circuit tutorial [#658](https://github.com/precice/tutorials/pull/658) \ No newline at end of file From 75d133c02b619a969228b9d558691e960a572b9d Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 21:50:56 +0200 Subject: [PATCH 07/12] Add Manifest.toml and Project.toml to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 7f2055b46..4b7dafe69 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,10 @@ __pycache__/ Cargo.lock target/ +# Julia +Manifest.toml +Project.toml + # OpenFOAM 0.*/ [1-9]*/ From 36d77e1390d26b1ecb66901dc79cd59e47576b46 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 22:00:49 +0200 Subject: [PATCH 08/12] Add cleaning scripts --- resonant-circuit/capacitor-julia/clean.sh | 7 +++++++ resonant-circuit/coil-julia/clean.sh | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100755 resonant-circuit/capacitor-julia/clean.sh create mode 100755 resonant-circuit/coil-julia/clean.sh diff --git a/resonant-circuit/capacitor-julia/clean.sh b/resonant-circuit/capacitor-julia/clean.sh new file mode 100755 index 000000000..8c3dba08a --- /dev/null +++ b/resonant-circuit/capacitor-julia/clean.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e -u + +. ../../tools/cleaning-tools.sh + +clean_precice_logs . +clean_case_logs . \ No newline at end of file diff --git a/resonant-circuit/coil-julia/clean.sh b/resonant-circuit/coil-julia/clean.sh new file mode 100755 index 000000000..8c3dba08a --- /dev/null +++ b/resonant-circuit/coil-julia/clean.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e -u + +. ../../tools/cleaning-tools.sh + +clean_precice_logs . +clean_case_logs . \ No newline at end of file From dac37d6b96992d8f299194ff1e750b98d4906a98 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 22:02:17 +0200 Subject: [PATCH 09/12] Add missing clean.sh scripts for Python participants --- resonant-circuit/capacitor-python/clean.sh | 7 +++++++ resonant-circuit/coil-python/clean.sh | 7 +++++++ 2 files changed, 14 insertions(+) create mode 100755 resonant-circuit/capacitor-python/clean.sh create mode 100755 resonant-circuit/coil-python/clean.sh diff --git a/resonant-circuit/capacitor-python/clean.sh b/resonant-circuit/capacitor-python/clean.sh new file mode 100755 index 000000000..8c3dba08a --- /dev/null +++ b/resonant-circuit/capacitor-python/clean.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e -u + +. ../../tools/cleaning-tools.sh + +clean_precice_logs . +clean_case_logs . \ No newline at end of file diff --git a/resonant-circuit/coil-python/clean.sh b/resonant-circuit/coil-python/clean.sh new file mode 100755 index 000000000..8c3dba08a --- /dev/null +++ b/resonant-circuit/coil-python/clean.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -e -u + +. ../../tools/cleaning-tools.sh + +clean_precice_logs . +clean_case_logs . \ No newline at end of file From b89534f7600e22f6e722eddaf172723bef0e41d4 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 22:04:50 +0200 Subject: [PATCH 10/12] Update clean.sh for capacitor-matlab --- resonant-circuit/capacitor-matlab/clean.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resonant-circuit/capacitor-matlab/clean.sh b/resonant-circuit/capacitor-matlab/clean.sh index 6a77c6d6c..8b1311d03 100755 --- a/resonant-circuit/capacitor-matlab/clean.sh +++ b/resonant-circuit/capacitor-matlab/clean.sh @@ -1,8 +1,7 @@ #!/bin/sh set -e -u -rm ./*.png ./*.mat - . ../../tools/cleaning-tools.sh clean_matlab . +rm -f ./*.png ./*.mat From fcd8a86f76a2468eb0dc259803a181f5cbbb9956 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 22:06:08 +0200 Subject: [PATCH 11/12] Add missing logger in precice-config.xml --- resonant-circuit/precice-config.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/resonant-circuit/precice-config.xml b/resonant-circuit/precice-config.xml index cda70ee19..fb72e5772 100644 --- a/resonant-circuit/precice-config.xml +++ b/resonant-circuit/precice-config.xml @@ -1,5 +1,12 @@ + + + + From e1f14770bbd797a20170d60377dfae9edb965d10 Mon Sep 17 00:00:00 2001 From: Gerasimos Chourdakis Date: Mon, 25 May 2026 22:17:20 +0200 Subject: [PATCH 12/12] Rewrite README.md to accommodate all solver options --- resonant-circuit/README.md | 40 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/resonant-circuit/README.md b/resonant-circuit/README.md index ca2cb21fd..4f81fa45b 100644 --- a/resonant-circuit/README.md +++ b/resonant-circuit/README.md @@ -1,5 +1,5 @@ --- -title: Resonant Circuit +title: Resonant circuit permalink: tutorials-resonant-circuit.html keywords: MATLAB, Python, Julia summary: We simulate a two-element LC circuit (one inductor and one capacitor). @@ -8,7 +8,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 of a very simple system with one inductor and one capacitor: +Two different solvers are coupled to simulate a two-element LC circuit. This type of circuit consists of a simple system with one inductor and one capacitor: ![LC circuit diagram [1]](images/tutorials-resonant-circuit-diagram.svg) @@ -20,7 +20,7 @@ $I(t) = -C \frac{\text{d}U}{\text{d}t}$ 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 solved by a different solver. Note that, as only one scalar is solved per equation, this is a 0+1 dimensional problem. ## Configuration @@ -31,44 +31,42 @@ preCICE configuration (image generated using the [precice-config-visualizer](htt ## Available solvers * *MATLAB* A solver using the [MATLAB bindings](https://precice.org/installation-bindings-matlab.html). - Before running this tutorial, follow the [instructions](https://precice.org/installation-bindings-matlab.html) to correctly install the MATLAB bindings. * *Python* A solver using the preCICE [Python bindings](https://precice.org/installation-bindings-python.html). * *Julia* A solver using the preCICE [Julia bindings](https://precice.org/installation-bindings-julia.html). ## Running the simulation -### MATLAB +All listed solvers can be used to run the simulation. Open two separate terminals and start the desired capacitor and coil participants by calling the respective run script. For example: -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: - -```MATLAB -coil +```bash +cd capacitor-python +./run.sh ``` -And in the second MATLAB instance, the solver for the voltage: +and -```MATLAB -capacitor +```bash +cd coil-julia +./run.sh ``` -The preCICE configuration file is hard-coded as `precice-config.xml` in the solvers. +### Running the MATLAB participants + +For running this example in the MATLAB GUI, first get into each of the solver folders and open a MATLAB instance for each. +After adding the MATLAB bindings to the MATLAB path (in both instances), run the `coil` and `capacitor` commands in the two windows. -#### Running from terminal +The path to the preCICE configuration file is hard-coded as `precice-config.xml` in the solvers. -If you prefer to not open the MATLAB GUIs, you can alternatively use two shells instead. +If you prefer not to use the MATLAB GUI, you can alternatively use two shells instead. For that, modify the path in the file `matlab-bindings-path.sh` found in the base directory of this tutorial to the path to your MATLAB bindings. By doing that, you can now open two shells and switch into the directories `capacitor-matlab` and `coil-matlab` and execute the `run.sh` scripts. ## Post-processing -As we defined a watchpoint on the 'Capacitor' participant (see `precice-config.xml`), we can plot it with gnuplot using the script `plot-solution.sh.` You need to specify the directory of the selected solid participant as a command line argument, so that the script can pick-up the desired watchpoint file, e.g. `./plot-solution.sh capacitor-python`. The resulting graph shows the voltage and current exchanged between the two participants. +As we defined a watchpoint on the 'Capacitor' participant (see `precice-config.xml`), we can plot it with gnuplot using the script `plot-solution.sh`. You need to specify the directory of the selected solid participant as a command line argument, so that the script can pick-up the desired watchpoint file, e.g., `./plot-solution.sh capacitor-python`. The resulting graph shows the voltage and current exchanged between the two participants. -Additionally, the MATLAB participant `capacitor-matlab` records the current and voltage over time. At the end of the simulation it creates a plot with the computed waveforms of current and voltage, as well as the analytical solution. +Additionally, the `capacitor-matlab` case records the current and voltage over time. At the end of the simulation, it creates a plot with the computed waveforms of current and voltage, as well as the analytical solution. After successfully running the coupling, one can find the curves in the folder `capacitor-matlab` as `Curves.png`.