From f1b781f8b805ca38b4bc21e0ef78e11cda5716b7 Mon Sep 17 00:00:00 2001 From: Karthik Date: Tue, 1 Apr 2025 01:09:34 +0200 Subject: [PATCH] Renamed script of gradient descent. --- Dipolar-Gas-Simulator/+Scripts/run_locally.m | 2 +- Dipolar-Gas-Simulator/+Simulator/@DipolarGas/run.m | 2 +- .../{minimizeEnergyFunctional.m => runGradientDescent.m} | 9 +++++---- 3 files changed, 7 insertions(+), 6 deletions(-) rename Dipolar-Gas-Simulator/+Simulator/@DipolarGas/{minimizeEnergyFunctional.m => runGradientDescent.m} (92%) diff --git a/Dipolar-Gas-Simulator/+Scripts/run_locally.m b/Dipolar-Gas-Simulator/+Scripts/run_locally.m index c4bd733..a54dbee 100644 --- a/Dipolar-Gas-Simulator/+Scripts/run_locally.m +++ b/Dipolar-Gas-Simulator/+Scripts/run_locally.m @@ -23,7 +23,7 @@ OptionsStruct.UseApproximationForLHY = true; OptionsStruct.IncludeDDICutOff = true; OptionsStruct.CutoffType = 'Cylindrical'; OptionsStruct.SimulationMode = 'EnergyMinimization'; % 'ImaginaryTimeEvolution' | 'RealTimeEvolution' | 'EnergyMinimization' -OptionsStruct.MaxIterationsForCGD = 1E3; +OptionsStruct.MaxIterationsForGD = 1E5; OptionsStruct.TimeStepSize = 1E-4; % in s OptionsStruct.MinimumTimeStepSize = 2E-10; % in s OptionsStruct.TimeCutOff = 2E6; % in s diff --git a/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/run.m b/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/run.m index 0806bab..0393059 100644 --- a/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/run.m +++ b/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/run.m @@ -20,7 +20,7 @@ function [Params, Transf, psi,V,VDk] = run(this) mkdir(sprintf(this.SaveDirectory)) mkdir(sprintf(strcat(this.SaveDirectory, '/Run_%03i'),Params.njob)) if strcmp(this.SimulationMode, 'EnergyMinimization') - [psi] = this.minimizeEnergyFunctional(psi,Params,Transf,VDk,V,Observ); + [psi] = this.runGradientDescent(psi,Params,Transf,VDk,V,Observ); else [psi] = this.propagateWavefunction(psi,Params,Transf,VDk,V,t_idx,Observ); end diff --git a/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/minimizeEnergyFunctional.m b/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/runGradientDescent.m similarity index 92% rename from Dipolar-Gas-Simulator/+Simulator/@DipolarGas/minimizeEnergyFunctional.m rename to Dipolar-Gas-Simulator/+Simulator/@DipolarGas/runGradientDescent.m index 0cb0aee..69e36f5 100644 --- a/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/minimizeEnergyFunctional.m +++ b/Dipolar-Gas-Simulator/+Simulator/@DipolarGas/runGradientDescent.m @@ -8,6 +8,7 @@ function [psi] = runGradientDescent(this,psi,Params,Transf,VDk,V,Observ) beta = 0.9; Observ.residual = 1; + Observ.res = 1; psi_old = psi; % Previous psi value (for heavy-ball method) if this.PlotLive @@ -22,10 +23,10 @@ function [psi] = runGradientDescent(this,psi,Params,Transf,VDk,V,Observ) J = compute_gradient(psi, Params, Transf, VDk, V); % Calculate chemical potential and norm - muchem = sum(real(conj(psi) .* J)) / sum(abs(psi).^2); + muchem = sum(real(conj(psi(:)) .* J(:))) / sum(abs(psi(:)).^2); % Calculate residual and check convergence - residual = sum(abs(J - muchem * psi).^2) * Transf.dx * Transf.dy * Transf.dz; + residual = sum(abs(J(:) - muchem * psi(:)).^2) * Transf.dx * Transf.dy * Transf.dz; if residual < epsilon fprintf('Convergence reached at iteration %d\n', idx); break; @@ -39,7 +40,7 @@ function [psi] = runGradientDescent(this,psi,Params,Transf,VDk,V,Observ) Norm = sum(abs(psi_new(:)).^2) * Transf.dx * Transf.dy * Transf.dz; psi = sqrt(Params.N) * psi_new / sqrt(Norm); - if mod(idx,500) == 0 + if mod(idx,100) == 0 % Collect change in energy E = this.Calculator.calculateTotalEnergy(psi,Params,Transf,VDk,V); @@ -50,7 +51,7 @@ function [psi] = runGradientDescent(this,psi,Params,Transf,VDk,V,Observ) Observ.mucVec = [Observ.mucVec muchem]; % Collect residuals - Observ.residual = [Observ.residual res]; + Observ.residual = [Observ.residual residual]; Observ.res_idx = Observ.res_idx + 1; if this.PlotLive