Added live plotting functionality for the Variational solver.

This commit is contained in:
Karthik 2024-11-18 11:14:56 +01:00
parent 7bdfb6af51
commit 8b2732cad5
6 changed files with 99 additions and 5 deletions

View File

@ -0,0 +1,67 @@
function plotLive2D(psi, Params, Transf, Observ)
set(0,'defaulttextInterpreter','latex')
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
format long
% Axes scaling and coordinates in micrometers
x = Transf.x * Params.l0 * 1e6;
y = Transf.y * Params.l0 * 1e6;
% Compute probability density |psi|^2
nxy = abs(psi).^2;
nxyScaled = nxy*(Params.add*10^6)^2;
% Plotting
figure(1);
set(gcf,'Position', [100, 100, 1600, 900])
clf
% Plot |psi(x,y)|^2 (density in x and y plane)
ax1 = subplot('Position', [0.05, 0.55, 0.28, 0.4]);
plotxy = pcolor(x,y,nxyScaled');
set(plotxy, 'EdgeColor', 'none');
% shading interp; % Smooth shading
clim(ax1);
cbar1 = colorbar;
cbar1.Label.Interpreter = 'latex';
ylabel(cbar1,'$na_{dd}^2$','FontSize',16,'Rotation',270)
xlabel('$x$ ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14)
ylabel('$y$ ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14)
title('$|\Psi(x,y)|^2$', 'Interpreter', 'latex', 'FontSize', 14)
% Plot real part of psi in the x-y plane
subplot('Position', [0.36, 0.55, 0.28, 0.4])
pcolor(x, y, real(psi)');
shading interp;
colorbar;
xlabel('$x$ [$\mu$m]', 'FontSize', 14); ylabel('$y$ [$\mu$m]', 'FontSize', 14);
title('Re$\{\Psi_{xy}\}$', 'FontSize', 14);
% Plot imaginary part of psi in the x-y plane
subplot('Position', [0.67, 0.55, 0.28, 0.4])
pcolor(x, y, imag(psi)');
shading interp;
colorbar;
xlabel('$x$ [$\mu$m]', 'FontSize', 14); ylabel('$y$ [$\mu$m]', 'FontSize', 14);
title('Im$\{\Psi_{xy}\}$', 'FontSize', 14);
% Plot residual (time steps vs -log10(residual))
subplot('Position', [0.05, 0.05, 0.26, 0.4]);
plot(-log10(Observ.residual), '-b')
ylabel('$-\mathrm{log}_{10}(r)$', 'FontSize', 14); xlabel('Time steps', 'FontSize', 14);
title('Residual', 'FontSize', 14);
% Plot total energy over time
subplot('Position', [0.36, 0.05, 0.26, 0.4]);
plot(Observ.EVec, '-b')
ylabel('$E_{tot}$', 'FontSize', 14); xlabel('Time steps', 'FontSize', 14);
title('Total Energy', 'FontSize', 14);
% Plot chemical potential over time
subplot('Position', [0.67, 0.05, 0.26, 0.4]);
plot(Observ.mucVec, '-b')
ylabel('$\mu$', 'FontSize', 14); xlabel('Time steps', 'FontSize', 14);
title('Chemical Potential', 'FontSize', 14);
end

View File

@ -73,6 +73,7 @@ OptionsStruct.WidthLowerBound = 5;
OptionsStruct.WidthUpperBound = 8;
OptionsStruct.WidthCutoff = 1e-3;
OptionsStruct.PlotLive = true;
OptionsStruct.JobNumber = 1;
OptionsStruct.RunOnGPU = false;
OptionsStruct.SaveData = true;

View File

@ -175,7 +175,7 @@ function [psi] = propagateWavefunction(this,psi,Params,Transf,VDk,V,t_idx,Observ
Observ.tVecPlot = [Observ.tVecPlot tVal];
Observ.res_idx = Observ.res_idx + 1;
pb.run(' - Job Completed!');
pb.run(' - Run Completed!');
disp('Saving data...');
save(sprintf('./Data/Run_%03i/TimeEvolution/psi_%i.mat',Params.njob,Observ.res_idx),'psi','muchem','Observ','t_idx');
disp('Save complete!');

View File

@ -26,7 +26,7 @@ classdef DipolarGas < handle & matlab.mixin.Copyable
Calculator;
SimulationParameters;
PlotLive;
JobNumber;
RunOnGPU;
DebugMode;
@ -78,6 +78,8 @@ classdef DipolarGas < handle & matlab.mixin.Copyable
@(x) assert(isnumeric(x) && isscalar(x) && (x > 0)));
addParameter(p, 'JobNumber', 1,...
@(x) assert(isnumeric(x) && isscalar(x) && (x > 0)));
addParameter(p, 'PlotLive', false,...
@islogical);
addParameter(p, 'RunOnGPU', false,...
@islogical);
addParameter(p, 'DebugMode', false,...
@ -109,6 +111,7 @@ classdef DipolarGas < handle & matlab.mixin.Copyable
this.WidthUpperBound = p.Results.WidthUpperBound;
this.WidthCutoff = p.Results.WidthCutoff;
this.PlotLive = p.Results.PlotLive;
this.JobNumber = p.Results.JobNumber;
this.RunOnGPU = p.Results.RunOnGPU;
this.DebugMode = p.Results.DebugMode;

View File

@ -22,6 +22,11 @@ function [psi, Observ] = propagateWavefunction(this, psi, Params, VParams, Trans
res = this.Calculator.calculateNormalizedResiduals(psi,Params,VParams,Transf,VDk,V,muchem);
Observ.residual = [Observ.residual res];
if this.PlotLive
Plotter.plotLive2D(psi,Params,Transf,Observ)
drawnow
end
g_eff = Params.gs * (1/(sqrt(2*pi)*VParams.ell));
gamma_eff = Params.gammaQF * (sqrt(2/5)/(pi^(3/4)*VParams.ell^(3/2)));
Ez = (0.25*VParams.ell^2) + (0.25*Params.gz*VParams.ell^2);
@ -55,7 +60,8 @@ function [psi, Observ] = propagateWavefunction(this, psi, Params, VParams, Trans
muchem = this.Calculator.calculateChemicalPotential(psi,Params,VParams,Transf,VDk,V);
if mod(t_idx,1000) == 0
%Plotting loop
if mod(t_idx,500) == 0
% Change in Energy
E = this.Calculator.calculateTotalEnergy(psi,Params,VParams,Transf,VDk,V);
@ -70,6 +76,13 @@ function [psi, Observ] = propagateWavefunction(this, psi, Params, VParams, Trans
Observ.residual = [Observ.residual res];
Observ.res_idx = Observ.res_idx + 1;
% Plotting
if this.PlotLive
Plotter.plotLive2D(psi,Params,Transf,Observ)
drawnow
end
%
%Adaptive time step -- Careful, this can quickly get out of control
relres = abs(Observ.residual(Observ.res_idx)-Observ.residual(Observ.res_idx-1))/Observ.residual(Observ.res_idx);
if relres <1e-5
@ -109,6 +122,6 @@ function [psi, Observ] = propagateWavefunction(this, psi, Params, VParams, Trans
% Chemical potential
Observ.mucVec = [Observ.mucVec muchem];
pb.run(' - Job Completed!');
pb.run(' - Run Completed!');
clear pb.run
end

View File

@ -62,6 +62,16 @@ function [Params, Transf, psi, V, VDk] = run(this)
save(sprintf('./Data/Run_%03i/psi_gs_%i.mat',Params.njob),'psi','Observ','Transf','Params','VDk','V','VParams');
%Plotting
if this.PlotLive
figure(2);
plot(ells,E_vs_iter,'LineStyle','none','Marker','o','MarkerSize',3,'Color','b','MarkerFaceColor','b')
xlabel('$\ell$', 'FontSize', 14)
ylabel('$E_{var}$', 'FontSize', 14)
title('Variational Energy', 'FontSize', 14);
drawnow
end
if relelldiff < Params.ellcutoff
break
end