Skip to content

Commit

Permalink
Reduce MATLAB boilerplate: PART II
Browse files Browse the repository at this point in the history
More about #2

Twin commit: beddalumia/QcmP-LAB@00c06be
  • Loading branch information
beddalumia committed Jan 12, 2022
1 parent 6dee035 commit 48b72c1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 223 deletions.
124 changes: 3 additions & 121 deletions KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
% at convergence!)
% >>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<

[SOI_list, SOI_names] = get_list('SOI');
[SOI_list, SOI_names] = postDMFT.get_list('SOI');
Nlines = length(SOI_list);
totalEnergies = cell(Nlines,1);
for iSOI = 1:Nlines
Expand All @@ -29,9 +29,9 @@
U_list = [];
end
fprintf('> E_kin.');
[kins,U_list] = kinetic_line(U_list); fprintf('..DONE\n');
[kins,U_list] = postDMFT.kinetic_line(U_list); fprintf('..DONE\n');
fprintf('> E_pot.');
[ids,ens,U_list] = energy_line(U_list); fprintf('..DONE\n');
[ids,ens,U_list] = postDMFT.energy_line(U_list); fprintf('..DONE\n');
pots = ens{1}; % The total potential energy is always the 1st value!
totalEnergies{iSOI} = kins + pots;
save('energy_line.mat','ids','ens','kins','U_list');
Expand Down Expand Up @@ -63,121 +63,3 @@
fig.Renderer='Painters';
%set(gca, 'ZScale', 'log');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Subroutines
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [flist, strlist] = get_list(VARNAME)
%% Getting a list of variable values, from directories.
% VARNAME: a string, identifying the listed variable (e.g. 'U')
% flist: an array of float_values (e.g. U=[:] )
% strlist: an array of dir_name strings (e.g. ['U=%f'] )
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subthings = dir('.'); % Retrieves every subdir and file inside pwd
subfolders = subthings([subthings(:).isdir]); % Keeps only subfolders
subfolders = subfolders(~ismember({subfolders(:).name},{'.','..'}));
N = length(subfolders); flist = zeros(N,1); strlist = strings(N,1);
for i = 1:N
DIR = subfolders(i).name; % Let's get the indexed string...
flist(i) = sscanf(DIR, [VARNAME,'=%f']); %...and extract the value!
strlist(i) = DIR;
end
% We need to sort the lists by floats (not strings, as it is now)
[flist, sortedIDX] = sort(flist); strlist = strlist(sortedIDX);
end

function [kins,U_list] = kinetic_line(U_LIST)
%% Getting a list of energy values, from directories.
% U_LIST: an array of values for Hubbard interaction U (could be empty!)
% kins: an array of values for Kinetic energies, forall U
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global ignUlist
if isempty(U_LIST) || ignUlist == true
[U_LIST, ~] = get_list('U');
else
U_LIST = sort(U_LIST);
end
% Then we can proceed spanning all the U-values
Nu = length(U_LIST);
kins = zeros(Nu,1);
for iU = 1:length(U_LIST)
U = U_LIST(iU);
UDIR= sprintf('U=%f',U);
if ~isfolder(UDIR)
errstr = 'U_list file appears to be inconsistent: ';
errstr = [errstr,UDIR];
errstr = [errstr,' folder has not been found.'];
error(errstr);
end
cd(UDIR);

% The dmft_kinetic_energy.dat file has a weird structure...
strCell = readcell('dmft_kinetic_energy.dat'); % cell of strings
tempStr = strCell{1}; % single string
tempVec = sscanf(tempStr,'%f'); % extract all the %f
kins(iU) = tempVec(1); % For sure the 1st value is the total K.E.

cd('..');
end

U_list = U_LIST;
end

function [ids,ens,U_list] = energy_line(U_LIST)
%% Getting a list of energy values, from directories.
% U_LIST: an array of values for Hubbard interaction U (could be empty!)
% ids: a cell of strings, the QcmPlab names for the pot-energy terms
% ens: a cell of float-arrays, corresponding to the names above
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global ignUlist
if isempty(U_LIST) | ignUlist == true
[U_LIST, ~] = get_list('U');
else
U_LIST = sort(U_LIST);
end
% Then we can proceed spanning all the U-values
Nu = length(U_LIST);
cellEn = cell(Nu,1);
for iU = 1:length(U_LIST)
U = U_LIST(iU);
UDIR= sprintf('U=%f',U);
if ~isfolder(UDIR)
errstr = 'U_list file appears to be inconsistent: ';
errstr = [errstr,UDIR];
errstr = [errstr,' folder has not been found.'];
error(errstr);
end
cd(UDIR);
[ids, cellEn{iU}] = get_energies();
cd('..');
end
% We need some proper reshaping
Nids = length(ids);
ens = cell(1,Nids);
for jEn = 1:Nids
ens{jEn} = zeros(Nu,1);
for iU = 1:Nu
ens{jEn}(iU) = cellEn{iU}(jEn);
end
end
U_list = U_LIST;
end

function [names, energies] = get_energies()
%% Getting all information from energy_last.ed and energy_info.ed
% names: a cell of strings, the QcmPlab names for the pot-energy terms
% energies: an array of float values, corresponding to the names above
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
names = readcell('energy_info.ed','FileType','fixedwidth');
names(strcmp(names,'#'))=[];
for i = 1:length(names)
tempstr = names{i}; % Temporary string variable
head = sscanf(tempstr,'%d'); % Extracts the initial integer
head = int2str(head); % Int to Str conversion
names{i} = erase(tempstr,head); % Proper beheading ;D
end
energies = load('energy_last.ed');
end
26 changes: 2 additions & 24 deletions KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function phase_line(varID)
% Select observable
iOBS = varID; % (iOBS==0 means everything)
% Get SOI value list
[SOI_list, SOI_names] = get_list('SOI');
[SOI_list, SOI_names] = postDMFT.get_list('SOI');
Nlines = length(SOI_list);
for iSOI = 1:Nlines
lineID = SOI_names(iSOI);
Expand Down Expand Up @@ -73,7 +73,7 @@ function phase_map(varID,doTransLine)
if varID == 0
error('All observables option not allowed for phase maps!')
end
[SOI_list, SOI_names] = get_list('SOI');
[SOI_list, SOI_names] = postDMFT.get_list('SOI');
Nlines = length(SOI_list);
phaseVAR = cell(Nlines,1);
transLine = zeros(2,Nlines);
Expand Down Expand Up @@ -121,27 +121,5 @@ function phase_map(varID,doTransLine)
%set(gca, 'ZScale', 'log');
clc
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Subroutines
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function [flist, strlist] = get_list(VARNAME)
%% Getting a list of variable values, from directories.
% VARNAME: a string, identifying the listed variable (e.g. 'U')
% flist: an array of float_values (e.g. U=[:] )
% strlist: an array of dir_name strings (e.g. ['U=%f'] )
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subthings = dir('.'); % Retrieves every subdir and file inside pwd
subfolders = subthings([subthings(:).isdir]); % Keeps only subfolders
subfolders = subfolders(~ismember({subfolders(:).name},{'.','..'}));
N = length(subfolders); flist = zeros(N,1); strlist = strings(N,1);
for i = 1:N
DIR = subfolders(i).name; % Let's get the indexed string...
flist(i) = sscanf(DIR, [VARNAME,'=%f']); %...and extract the value!
strlist(i) = DIR;
end
% We need to sort the lists by floats (not strings, as it is now)
[flist, sortedIDX] = sort(flist); strlist = strlist(sortedIDX);
end
80 changes: 2 additions & 78 deletions KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

% We don't have a SOI-values list, but we can obtain that by just
% inspecting the subdirectories...
[SOI_list, SOI_names] = get_list('SOI');
[SOI_list, SOI_names] = postDMFT.get_list('SOI');
Nlines = length(SOI_list);
for iSOI = 1:Nlines
SOIDIR = SOI_names(iSOI);
Expand All @@ -20,87 +20,11 @@
else
U_list = [];
end
[ids,obs,U_list] = extract_line(U_list); fprintf('..DONE\n');
[ids,obs,U_list] = postDMFT.observables_line(U_list); fprintf('..DONE\n');
save('observables_line.mat','ids','obs','U_list');
cd('..');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [flist, strlist] = get_list(VARNAME)
%% Getting a list of variable values, from directories.
% VARNAME: a string, identifying the listed variable (e.g. 'U')
% flist: an array of float_values (e.g. U=[:] )
% strlist: an array of dir_name strings (e.g. ['U=%f'] )
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subthings = dir('.'); % Retrieves every subdir and file inside pwd
subfolders = subthings([subthings(:).isdir]); % Keeps only subfolders
subfolders = subfolders(~ismember({subfolders(:).name},{'.','..'}));
N = length(subfolders); flist = zeros(N,1); strlist = strings(N,1);
for i = 1:N
DIR = subfolders(i).name; % Let's get the indexed string...
flist(i) = sscanf(DIR, [VARNAME,'=%f']); %...and extract the value!
strlist(i) = DIR;
end
% We need to sort the lists by floats (not strings, as it is now)
[flist, sortedIDX] = sort(flist); strlist = strlist(sortedIDX);
end

function [ids,obs,U_list] = extract_line(U_LIST)
%% Getting a list of variable values, from directories.
% U_LIST: an array of values for Hubbard interaction U (could be empty!)
% ids: a cell of strings, the QcmPlab names of the observables
% obs: a cell of float-arrays, corresponding to the names above, forall U
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
global ignUlist
if isempty(U_LIST) | ignUlist == true
[U_LIST, ~] = get_list('U');
else
U_LIST = sort(U_LIST);
end
% Then we can proceed spanning all the U-values
Nu = length(U_LIST);
cellobs = cell(Nu,1);
for iU = 1:length(U_LIST)
U = U_LIST(iU);
UDIR= sprintf('U=%f',U);
if ~isfolder(UDIR)
errstr = 'U_list file appears to be inconsistent: ';
errstr = [errstr,UDIR];
errstr = [errstr,' folder has not been found.'];
error(errstr);
end
cd(UDIR);
[ids, cellobs{iU}] = get_observables();
cd('..');
end
% We need some proper reshaping
Nobs = length(ids);
obs = cell(1,Nobs);
for jOBS = 1:Nobs
obs{jOBS} = zeros(Nu,1);
for iU = 1:Nu
obs{jOBS}(iU) = cellobs{iU}(jOBS);
end
end
U_list = U_LIST;
end

function [names, observables] = get_observables()
%% Getting all information from observables_last.ed and observables_info.ed
% names: a cell of strings, the QcmPlab names of the observables
% observables: an array of float values, corresponding to the names above
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
names = readcell('observables_info.ed','FileType','fixedwidth');
names(strcmp(names,'#'))=[];
for i = 1:length(names)
tempstr = names{i}; % Temporary string variable
head = sscanf(tempstr,'%d'); % Extracts the initial integer
head = int2str(head); % Int to Str conversion
names{i} = erase(tempstr,head); % Proper beheading ;D
end
observables = load('observables_last.ed');
end

0 comments on commit 48b72c1

Please sign in to comment.