From 48b72c1e0628e302d21a79f3998b1ce4458b10ed Mon Sep 17 00:00:00 2001 From: Gabriele Bellomia Date: Wed, 12 Jan 2022 23:12:32 +0100 Subject: [PATCH] Reduce MATLAB boilerplate: PART II More about #2 Twin commit: https://github.com/bellomia/DMFT-LAB/commit/00c06bedeb0d325969e4213cda7591db4d61f9f0 --- KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m | 124 +----------------- KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m | 26 +--- KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m | 80 +---------- 3 files changed, 7 insertions(+), 223 deletions(-) diff --git a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m index e8904ae..6d3eff4 100644 --- a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m +++ b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/DMFTenergies.m @@ -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 @@ -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'); @@ -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 \ No newline at end of file diff --git a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m index 2130fb7..08eff9a 100644 --- a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m +++ b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PlotDMFT.m @@ -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); @@ -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); @@ -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 diff --git a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m index 2d84aad..ea0967e 100644 --- a/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m +++ b/KMH-DMFT/KMH-DMFT_mat/PostDMFT/PostDMFT.m @@ -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); @@ -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