From 1d524f1f0b35dae8d1545d141a0d30001c36dd09 Mon Sep 17 00:00:00 2001 From: Karthik Chandrashekara Date: Wed, 12 Jun 2024 20:05:27 +0200 Subject: [PATCH] Latest commit --- Dipolar Gas Simulator/+Scripts/test.m | 9 ++ .../+Simulator/@Calculator/Calculator.m | 54 +++++++ .../@Calculator/calculateVDCutoff.m | 4 +- .../+Simulator/@DipolarGas/DipolarGas.m | 143 ++++++++++++++++++ .../+Simulator/@DipolarGas/initialize.m | 12 +- .../+Simulator/@DipolarGas/runSimulation.m | 53 +++---- .../+Simulator/@DipolarGas/setupParameters.m | 2 +- .../+Simulator/@DipolarGas/setupSpace.m | 2 +- .../+Simulator/@DipolarGas/setupSpaceRadial.m | 2 +- .../@DipolarGas/setupWavefunction.m | 2 +- .../solver.m} | 2 +- .../+Simulator/@Potentials/Potentials.m | 54 +++++++ 12 files changed, 294 insertions(+), 45 deletions(-) create mode 100644 Dipolar Gas Simulator/+Scripts/test.m create mode 100644 Dipolar Gas Simulator/+Simulator/@Calculator/Calculator.m create mode 100644 Dipolar Gas Simulator/+Simulator/@DipolarGas/DipolarGas.m rename Dipolar Gas Simulator/+Simulator/{@ImaginaryTimeEvolution/splitStepFourier.m => @DipolarGas/solver.m} (97%) create mode 100644 Dipolar Gas Simulator/+Simulator/@Potentials/Potentials.m diff --git a/Dipolar Gas Simulator/+Scripts/test.m b/Dipolar Gas Simulator/+Scripts/test.m new file mode 100644 index 0000000..ba15e8e --- /dev/null +++ b/Dipolar Gas Simulator/+Scripts/test.m @@ -0,0 +1,9 @@ +%-% Run Simulation %-% +clearvars + +sim = DipolarGas(); +calc = Calculator(); +[Params, Transf, psi, V, VDk] = sim.runSimulation(calc); + +%-% Plot %-% +Plotter.visualizeSpace(Transf) diff --git a/Dipolar Gas Simulator/+Simulator/@Calculator/Calculator.m b/Dipolar Gas Simulator/+Simulator/@Calculator/Calculator.m new file mode 100644 index 0000000..53c2eda --- /dev/null +++ b/Dipolar Gas Simulator/+Simulator/@Calculator/Calculator.m @@ -0,0 +1,54 @@ +classdef Calculator < handle & matlab.mixin.Copyable + + properties (Access = public) + + end + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods + function this = Calculator(varargin) + + end + end + + methods + + end % - setters and getters + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods(Access = protected) + function cp = copyElement(this) + % Shallow copy object + cp = copyElement@matlab.mixin.Copyable(this); + + % Forces the setter to redefine the function handles to the new copied object + + pl = properties(this); + for k = 1:length(pl) + sc = superclasses(this.(pl{k})); + if any(contains(sc,{'matlab.mixin.Copyable'})) + cp.(pl{k}) = this.(pl{k}).copy(); + end + end + end + end + + methods (Static) + + % Creates an Instance of Class, ensures singleton behaviour (that there + % can only be one Instance of this class + function singleObj = getInstance(varargin) + % Creates an Instance of Class, ensures singleton behaviour + persistent localObj; + if isempty(localObj) || ~isvalid(localObj) + localObj = Simulator.Calculator(varargin{:}); + end + singleObj = localObj; + end + end + +end diff --git a/Dipolar Gas Simulator/+Simulator/@Calculator/calculateVDCutoff.m b/Dipolar Gas Simulator/+Simulator/@Calculator/calculateVDCutoff.m index d5310c1..aac3e4b 100644 --- a/Dipolar Gas Simulator/+Simulator/@Calculator/calculateVDCutoff.m +++ b/Dipolar Gas Simulator/+Simulator/@Calculator/calculateVDCutoff.m @@ -1,4 +1,4 @@ -function VDk = calculateVDCutoff(Params, Transf) +function VDk = calculateVDCutoff(this, Params, Transf) % makes the dipolar interaction matrix, size numel(Params.kr) * numel(Params.kz) % Rmax and Zmax are the interaction cutoffs % VDk needs to be multiplied by Cdd @@ -19,7 +19,7 @@ VDk = VDk + exp(-Zcutoff*sqrt(Transf.KX.^2+Transf.KY.^2)).*( sinsq .* cos(Zcut Params.Lr = 0.5*min(Params.Lx,Params.Ly); Params.Nr = max(Params.Nx,Params.Ny); [TransfRad] = SetupSpaceRadial(Params); -VDkNon = NumericalHankelTransform(TransfRad.kr, TransfRad.kz, TransfRad.Rmax, Zcutoff); +VDkNon = this.calculateNumericalHankelTransform(TransfRad.kr, TransfRad.kz, TransfRad.Rmax, Zcutoff); % Interpolating the nonanalytic part onto 3D grid fullkr = [-flip(TransfRad.kr)',TransfRad.kr']; diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/DipolarGas.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/DipolarGas.m new file mode 100644 index 0000000..ea3ca51 --- /dev/null +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/DipolarGas.m @@ -0,0 +1,143 @@ +classdef DipolarGas < handle & matlab.mixin.Copyable + + properties (Access = public) + SimulationMode; + TimeStep; + SimulationTime; + NumberOfAtoms; + ErrorEstimationMethod; + + %Flags + + + DebugMode; + DoSave; + SaveDirectory; + end + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods + function this = DipolarGas(varargin) + + p = inputParser; + p.KeepUnmatched = true; + addParameter(p, 'SimulationMode', 'ImaginaryTimeEvolution',... + @(x) any(strcmpi(x,{'ImaginaryTimeEvolution','RealTimeEvolution'}))); + addParameter(p, 'ErrorEstimationMethod', 'bootstrap',... + @(x) any(strcmpi(x,{'jackknife','bootstrap'}))); + addParameter(p, 'NumberOfAtoms', 5000,... + @(x) assert(isnumeric(x) && isscalar(x) && (x > 0))); + addParameter(p, 'TimeStep', 10e-06,... + @(x) assert(isnumeric(x) && isscalar(x) && (x > 0))); + addParameter(p, 'SimulationTime', 3e-03,... + @(x) assert(isnumeric(x) && isscalar(x) && (x > 0))); + addParameter(p, 'DebugMode', false,... + @islogical); + addParameter(p, 'SaveData', false,... + @islogical); + addParameter(p, 'SaveDirectory', './Data',... + @ischar); + + p.parse(varargin{:}); + + this.SimulationMode = p.Results.SimulationMode; + this.ErrorEstimationMethod= p.Results.ErrorEstimationMethod; + + this.NumberOfAtoms = p.Results.NumberOfAtoms; + this.TimeStep = p.Results.TimeStep; + this.SimulationTime = p.Results.SimulationTime; + + this.DebugMode = p.Results.DebugMode; + this.DoSave = p.Results.SaveData; + this.SaveDirectory = p.Results.SaveDirectory; + + switch this.SimulationMode + case "ImaginaryTimeEvolution" + % Development In progress + case "RealTimeEvolution" + % Development In progress + end + end + end + + methods + function set.TimeStep(this, val) + assert(val > 1e-06, 'Not time efficient to compute for time steps smaller than 1 microsecond!'); + this.TimeStep = val; + end + function ret = get.TimeStep(this) + ret = this.TimeStep; + end + function set.SimulationTime(this, val) +% assert(val <= 5e-03, 'Not time efficient to compute for time spans longer than 5 milliseconds!'); + this.SimulationTime = val; + end + function ret = get.SimulationTime(this) + ret = this.SimulationTime; + end + function set.NumberOfAtoms(this, val) + assert(val <= 50000, '!!Not time efficient to compute for atom numbers larger than 50,000!!'); + this.NumberOfAtoms = val; + end + function ret = get.NumberOfAtoms(this) + ret = this.NumberOfAtoms; + end + + function set.DebugMode(this, val) + this.DebugMode = val; + end + function ret = get.DebugMode(this) + ret = this.DebugMode; + end + function set.DoSave(this, val) + this.DoSave = val; + end + function ret = get.DoSave(this) + ret = this.DoSave; + end + function set.SaveDirectory(this, val) + this.SaveDirectory = val; + end + function ret = get.SaveDirectory(this) + ret = this.SaveDirectory; + end + + end % - setters and getters + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods(Access = protected) + function cp = copyElement(this) + % Shallow copy object + cp = copyElement@matlab.mixin.Copyable(this); + + % Forces the setter to redefine the function handles to the new copied object + + pl = properties(this); + for k = 1:length(pl) + sc = superclasses(this.(pl{k})); + if any(contains(sc,{'matlab.mixin.Copyable'})) + cp.(pl{k}) = this.(pl{k}).copy(); + end + end + end + end + + methods (Static) + + % Creates an Instance of Class, ensures singleton behaviour (that there + % can only be one Instance of this class + function singleObj = getInstance(varargin) + % Creates an Instance of Class, ensures singleton behaviour + persistent localObj; + if isempty(localObj) || ~isvalid(localObj) + localObj = Simulator.DipolarGas(varargin{:}); + end + singleObj = localObj; + end + end + +end diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/initialize.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/initialize.m index 2e4c9c2..7ee30c5 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/initialize.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/initialize.m @@ -1,4 +1,4 @@ -function [psi,V,VDk] = initialize(Params,Transf) +function [psi,V,VDk] = initialize(this,calcObj,Params,Transf) format long X = Transf.X; Y = Transf.Y; Z = Transf.Z; @@ -7,16 +7,16 @@ X = Transf.X; Y = Transf.Y; Z = Transf.Z; V = 0.5*(Params.gx.*X.^2+Params.gy.*Y.^2+Params.gz*Z.^2); % == Calculating the DDIs == % -if isfile('./Data/VDk_M.mat') - VDk = load(sprintf('./Data/VDk_M.mat')); +if isfile(strcat(this.SaveDirectory, '/VDk_M.mat')) + VDk = load(sprintf(strcat(this.SaveDirectory, '/VDk_M.mat'))); VDk = VDk.VDk; else - VDk = VDCutoff(Params, Transf); - save(sprintf('./Data/VDk_M.mat'),'VDk'); + VDk = calcObj.calculateVDCutoff(Params, Transf); + save(sprintf(strcat(this.SaveDirectory, '/VDk_M.mat')),'VDk'); end disp('Finished DDI') % == Setting up the initial wavefunction == % -psi = SetupWavefunction(); +psi = this.SetupWavefunction(); end \ No newline at end of file diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/runSimulation.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/runSimulation.m index 57d966c..9e23767 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/runSimulation.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/runSimulation.m @@ -1,34 +1,23 @@ -%-% Run Simulation %-% -clearvars +function [Params, Transf, psi,V,VDk] = runSimulation(this,calcObj) + % --- Obtain simulation parameters --- + [Params] = this.setupParameters(); -% --- Obtain simulation parameters --- -[Params] = setupParameters(); + % --- Set up spatial grids and transforms --- + [Transf] = this.setupSpace(Params); + + % --- Initialize --- + mkdir(sprintf(this.SaveDirectory)) + + [psi,V,VDk] = this.initialize(calcObj,Params,Transf); + + Observ.EVec = []; Observ.NormVec = []; Observ.PCVec = []; Observ.tVecPlot = []; Observ.mucVec = []; + t_idx = 1; %Start at t = 0; + Observ.res_idx = 1; -% --- Set up spatial grids and transforms --- -[Transf] = setupSpace(Params); - -% Plotter.visualizeSpace(Transf) - - -%% -% --- Initialize --- - -mkdir(sprintf('./Data')) - -[psi,V,VDk] = initialize(Params,Transf); - -Observ.EVec = []; Observ.NormVec = []; Observ.PCVec = []; Observ.tVecPlot = []; Observ.mucVec = []; -t_idx = 1; %Start at t = 0; -Observ.res_idx = 1; - -%% -% --- Job Settings --- - -njob = 6; - -mkdir(sprintf('./Data/Run_%03i',njob)) - -% --- Run Simulation --- - -% Imaginary Time Evolution -[psi] = SplitStepFourierImaginaryTime(psi,Params,Transf,VDk,V,njob,t_idx,Observ); \ No newline at end of file + % --- Job Settings --- + % njob = 6; + % mkdir(sprintf('./Data/Run_%03i',njob)) + + % --- Run Simulation --- + % [psi] = this.solver(psi,Params,Transf,VDk,V,njob,t_idx,Observ); +end diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupParameters.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupParameters.m index feac996..678372d 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupParameters.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupParameters.m @@ -1,4 +1,4 @@ -function [Params] = setupParameters() +function [Params] = setupParameters(this) %%--%% Parameters %%--%% diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpace.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpace.m index ea98ba4..56f7f7c 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpace.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpace.m @@ -1,4 +1,4 @@ -function [Transf] = setupSpace(Params) +function [Transf] = setupSpace(this,Params) Transf.Xmax = 0.5*Params.Lx; Transf.Ymax = 0.5*Params.Ly; Transf.Zmax = 0.5*Params.Lz; diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpaceRadial.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpaceRadial.m index ce2c96d..2000863 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpaceRadial.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupSpaceRadial.m @@ -1,4 +1,4 @@ -function [Transf] = setupSpaceRadial(Params,morder) +function [Transf] = setupSpaceRadial(this,Params,morder) Zmax = 0.5*Params.Lz; Rmax = Params.Lr; Nz = Params.Nz; diff --git a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupWavefunction.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupWavefunction.m index d1d90d3..756a553 100644 --- a/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupWavefunction.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/setupWavefunction.m @@ -1,4 +1,4 @@ -function [psi] = setupWavefunction() +function [psi] = setupWavefunction(this) ellx = sqrt(Params.hbar/(Params.m*Params.wx))/Params.l0; elly = sqrt(Params.hbar/(Params.m*Params.wy))/Params.l0; diff --git a/Dipolar Gas Simulator/+Simulator/@ImaginaryTimeEvolution/splitStepFourier.m b/Dipolar Gas Simulator/+Simulator/@DipolarGas/solver.m similarity index 97% rename from Dipolar Gas Simulator/+Simulator/@ImaginaryTimeEvolution/splitStepFourier.m rename to Dipolar Gas Simulator/+Simulator/@DipolarGas/solver.m index 1397739..fa150b1 100644 --- a/Dipolar Gas Simulator/+Simulator/@ImaginaryTimeEvolution/splitStepFourier.m +++ b/Dipolar Gas Simulator/+Simulator/@DipolarGas/solver.m @@ -1,4 +1,4 @@ -function [psi] = splitStepFourier(psi,Params,Transf,VDk,V,njob,t_idx,Observ) +function [psi] = solver(this,psi,Params,Transf,VDk,V,njob,t_idx,Observ) set(0,'defaulttextInterpreter','latex') set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex'); diff --git a/Dipolar Gas Simulator/+Simulator/@Potentials/Potentials.m b/Dipolar Gas Simulator/+Simulator/@Potentials/Potentials.m new file mode 100644 index 0000000..0df5607 --- /dev/null +++ b/Dipolar Gas Simulator/+Simulator/@Potentials/Potentials.m @@ -0,0 +1,54 @@ +classdef Potentials < handle & matlab.mixin.Copyable + + properties (Access = public) + + end + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods + function this = Potentials(varargin) + + end + end + + methods + + end % - setters and getters + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %- Methods + + methods(Access = protected) + function cp = copyElement(this) + % Shallow copy object + cp = copyElement@matlab.mixin.Copyable(this); + + % Forces the setter to redefine the function handles to the new copied object + + pl = properties(this); + for k = 1:length(pl) + sc = superclasses(this.(pl{k})); + if any(contains(sc,{'matlab.mixin.Copyable'})) + cp.(pl{k}) = this.(pl{k}).copy(); + end + end + end + end + + methods (Static) + + % Creates an Instance of Class, ensures singleton behaviour (that there + % can only be one Instance of this class + function singleObj = getInstance(varargin) + % Creates an Instance of Class, ensures singleton behaviour + persistent localObj; + if isempty(localObj) || ~isvalid(localObj) + localObj = Simulator.Potentials(varargin{:}); + end + singleObj = localObj; + end + end + +end