Skip to content

Add new tutorial: resonant circuit tutorial#480

Merged
BenjaminRodenberg merged 25 commits into
precice:developfrom
BenjaminRodenberg:add-lc-circuit
Jun 17, 2024
Merged

Add new tutorial: resonant circuit tutorial#480
BenjaminRodenberg merged 25 commits into
precice:developfrom
BenjaminRodenberg:add-lc-circuit

Conversation

@BenjaminRodenberg

@BenjaminRodenberg BenjaminRodenberg commented Mar 14, 2024

Copy link
Copy Markdown
Contributor

@MichaelWiesheu

MichaelWiesheu commented Mar 15, 2024

Copy link
Copy Markdown

Meine Änderungen in Solver_I.m
Im wesentlichen wurde
dt = min(solverdt, precicedt);
und das Speichern und Laden von "t" hinzugefügt.

clear; close all; clc;

% Initialize and configure preCICE
interface = precice.Participant("ParticipantI", "precice-config.xml", 0, 1);

% Geometry IDs. As it is a 0-D simulation, only one vertex is necessary.
meshName ="MeshI";
vertex_ID = interface.setMeshVertex(meshName, [0 0]);

% Data IDs
dataNameI = "I";
dataNameU = "U";

% Simulation parameters and initial condition
C = 2;                      % Capacitance
L = 1;                      % Inductance
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
I0 = Io*cos(phi);           % Initial condition for I
U0 = -w0*L*Io*sin(phi);     % Initial condition for U

f_I = @(t, I, U) U/L;       % Time derivative of I

% Initialize simulation
I = I0;                     % Vector of I through time
U = U0;                     % Vector of U through time
t_vec = t0;                 % Vector of time
if interface.requiresInitialData()
    interface.writeData(meshName, DataNameI, vertex_ID, I0);
end
interface.initialize();
solverdt = 0.01;
precicedt = interface.getMaxTimeStepSize();
dt = min(solverdt, precicedt);

% Start simulation
t = t0 + dt;
while interface.isCouplingOngoing()

    % Record checkpoint if necessary
    if interface.requiresWritingCheckpoint()
        I0_checkpoint = I0;
        U0_checkpoint = U0;
        t_checkpoint = t;
    end
    precicedt = interface.getMaxTimeStepSize();
    dt = min(solverdt, precicedt);
    disp(['t: ' num2str(t) 9 ' solverdt: ' num2str(solverdt) 9 'precicedt: ' num2str(precicedt) 9 ' dt: ' num2str(dt)]);

    % Make Simulation Step
    [t_ode, I_ode] = ode45(@(t, y) f_I(t, y, U0), [t0 t], I0);
    I0 = I_ode(end);

    % Exchange data
    interface.writeData(meshName, dataNameI, vertex_ID, I0);
    interface.advance(dt);

    % Recover checkpoint if not converged, else finish time step
    if interface.requiresReadingCheckpoint()
        I0 = I0_checkpoint;
        U0 = U0_checkpoint;
        t = t_checkpoint;
    else
        U0 = interface.readData(meshName, dataNameU, vertex_ID, dt);
        U = [U U0];
        I = [I I0];
        t_vec = [t_vec, t];
        t0 = t;
        t = t0 + dt;
    end
end

% Stop coupling
interface.finalize();

% Analytical solution for comparison
I_an = Io*cos(w0*t_vec+phi);
U_an = -w0*L*Io*sin(w0*t_vec+phi);

% Make and save plot
figure(1)
subplot(2,1,1)
plot(t_vec, I_an)
hold on;
plot(t_vec, U_an)
ylim([-1,1])
legend('I', 'U')
title('Analytical')

subplot(2,1,2)
plot(t_vec, I)
hold on;
plot(t_vec, U)
ylim([-1,1])
title('Numerical')
legend('I', 'U')

save('outputs.mat', 'I', 'U')
saveas(gcf, 'Curves.png')

Comment thread resonant-circuit/capacitor-matlab/Solver_I.m
Comment thread resonant-circuit/precice-config.xml Outdated
Comment thread resonant-circuit/capacitor-matlab/Solver_I.m
Comment thread resonant-circuit/precice-config.xml Outdated
@BenjaminRodenberg BenjaminRodenberg changed the title WIP: Add resonant circuit tutorial Add resonant circuit tutorial Apr 16, 2024

@BenjaminRodenberg BenjaminRodenberg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. @NiklasVin please also remove the case https://github.com/precice/matlab-bindings/tree/develop/tutorial from the matlab-bindings repo now that we moved it here.

I think it would be nice to get a review from somebody else, if possible. The installation process of the MATLAB bindings is not the best and adding the bindings to the path is not so easy, but let's try to solve this in the repository of the bindings. Not here.

Comment thread resonant-circuit/matlab-bindings-path.sh Outdated
Comment thread resonant-circuit/coil-matlab/run.sh Outdated
Comment thread resonant-circuit/capacitor-matlab/run.sh Outdated

@MakisH MakisH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Structure-wise, this looks ready.
I have not tried to run it, but I would be interested, since we don't currently have any Matlab-based tutorial.

If everything is camera-ready today, we could squeeze it into this release, if you feel confident about it. But I would not wait for it, because we are already quite late.

Comment thread resonant-circuit/README.md
Comment thread resonant-circuit/capacitor-matlab/run.sh Outdated
Comment thread resonant-circuit/coil-matlab/clean.sh Outdated
Comment thread resonant-circuit/coil-matlab/run.sh Outdated

@BenjaminRodenberg BenjaminRodenberg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found another minor simplification. @NiklasVin if you think this is reasonable, please commit these suggestions to the branch.

Comment thread resonant-circuit/capacitor-matlab/run.sh Outdated
Comment thread resonant-circuit/coil-matlab/run.sh Outdated
Comment thread resonant-circuit/precice-config.xml Outdated

@BenjaminRodenberg BenjaminRodenberg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are converging here! Can you please go once more through all the comments, briefly write an answer (if necessary) and resolve the conversations. @MakisH Do you want to do another quick check, approve, and merge (I cannot approve, because I created this PR originally).

Comment thread resonant-circuit/capacitor-matlab/run.sh Outdated
Comment thread resonant-circuit/coil-matlab/run.sh Outdated
@BenjaminRodenberg BenjaminRodenberg changed the title Add resonant circuit tutorial Add new tutorial: resonant circuit tutorial Apr 19, 2024

@MakisH MakisH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 🚀

I still have not tested it, but that would take much longer, as I first have to finish other tasks. Feel free to merge after fixing a few minor issues in the README.

Comment thread resonant-circuit/README.md
Comment thread resonant-circuit/README.md Outdated
Comment thread resonant-circuit/README.md Outdated
Comment thread resonant-circuit/README.md Outdated
Comment thread resonant-circuit/README.md Outdated
Comment thread resonant-circuit/README.md
Comment thread resonant-circuit/README.md Outdated
Comment thread changelog-entries/480.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants