Skip to content

Commit

Permalink
Clean up of BMI code, add save/load run mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
BSchilperoort committed Nov 28, 2023
1 parent 62a2105 commit f23ad62
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
Binary file modified run_model_on_snellius/exe/STEMMUS_SCOPE
Binary file not shown.
45 changes: 21 additions & 24 deletions src/STEMMUS_SCOPE.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,20 @@
P_gg = InitialValues.P_gg; % will be updated!
P_g = InitialValues.P_g; % will be updated!

disp('Finished initialization!');
end

% Save the workspace after initialization if running in the BMI-mode.
if strcmp(runMode, 'initialize')
save([Output_dir, 'STEMMUS_SCOPE_state.mat'], "-v7.3", "-nocompression");
%% soil layer information
SoilLayer.thickness = ModelSettings.DeltZ_R;
SoilLayer.depth = Ztot'; % used in Initial_root_biomass

% NOTE: workspace will be saved, this code block is after the update step.
% (this is to not repeat the save-workspace code).
disp('Finished model initialization');
end

% If the runMode is update, retrieve the (possibly) updated state.
% The state can be modified by the STEMMUS_SCOPE BMI. See PyStemmusScope.
if strcmp(runMode, 'update')
[InputPath, OutputPath, InitialConditionPath] = io.read_config(CFG);
actualRunMode = runMode; % Will be overridden by load...
load([OutputPath, 'STEMMUS_SCOPE_state.mat']); % Load the workspace to be able to (continue) running the model
runMode = actualRunMode;
clear actualRunMode % clear actual run mode so it won't be in the workspace on saving.
if KT >= TimeProperties.Dur_tot
if KT + 1 >= TimeProperties.Dur_tot
runMode = 'finished';
disp("Finished running the model. Updating won't do anything!");
else
Expand All @@ -309,7 +308,7 @@
end

% Actually run the model
if strcmp(runMode, 'update') | strcmp(runMode, 'full')
if strcmp(runMode, 'update') || strcmp(runMode, 'full')
% Will do one timestep in "update mode", and run until the end if in "full run" mode.
while KT < endTime
KT = KT + 1; % Counting Number of timesteps
Expand Down Expand Up @@ -709,28 +708,26 @@
n_col = io.output_data_binary(file_ids, k, xyt, rad, canopy, ScopeParameters, vi, vmax, options, fluxes, meteo, iter, thermal, spectral, gap, profiles, Sim_Theta_U, Sim_Temp, Trap, Evap);
fclose("all");
end
if strcmp(runMode, 'update')
save([Output_dir, 'STEMMUS_SCOPE_state.mat'], "-v7.3", "-nocompression");
end
end

if strcmp(runMode, 'initialize') || strcmp(runMode, 'update')
% Save the required variables to the model state file.
% NOTE: bmiVarNames are defined in STEMMUS_SCOPE_exe.m
save([Output_dir, 'STEMMUS_SCOPE_state.mat'], bmiVarNames{:}, "-v7.3", "-nocompression");
end


if strcmp(runMode, 'finalize')
[InputPath, OutputPath, InitialConditionPath] = io.read_config(CFG);
actualRunMode = runMode; % Will be overridden by load...
load([OutputPath, 'STEMMUS_SCOPE_state.mat']); % Load the workspace to be able to (continue) running the model
runMode = actualRunMode;
% Load the workspace to be able to finalize the model.
load([OutputPath, 'STEMMUS_SCOPE_state.mat']);
end

if strcmp(runMode, 'finalize') | strcmp(runMode, 'full')
if strcmp(runMode, 'finalize') || strcmp(runMode, 'full')
disp('Finalizing STEMMUS_SCOPE');
if options.verify
io.output_verification(Output_dir);
end

%% soil layer information
SoilLayer.thickness = ModelSettings.DeltZ_R;
SoilLayer.depth = Ztot'; % used in Initial_root_biomass

io.bin_to_csv(fnames, n_col, k, options, SoilLayer);
save([Output_dir, 'output.mat']);
end
57 changes: 38 additions & 19 deletions src/STEMMUS_SCOPE_exe.m
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
%%%%%%% A function to run STEMMUS_SCOPE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function STEMMUS_SCOPE_exe(config_file, run_mode, debug)
function STEMMUS_SCOPE_exe(config_file, run_mode)
if ~exist('run_mode','var')
runMode = "full";
else
runMode = run_mode;
end

if exist('debug','var')
debugMode = true;
else
debugMode = false;
end

% disable all warnings
warning('off', 'all');

% set the variable CFG
CFG = config_file;
% set the variable runMode

% If the runMode is "full" or was not provided, the model will run as normal
if strcmp(runMode, "full")
run STEMMUS_SCOPE;

% In interactive mode MATLAB stays open and waits for a new command...
if strcmp(runMode, "interactive")
elseif strcmp(runMode, "interactive")
% Define BMI required variable names:
bmiVarNames = {...
'ModelSettings', ... % Model settings struct
'TimeStep', ... % Time step size (in seconds)
'KT', ... % Index of current time step
'SiteProperties', ... % Site properties (e.g. lat, lon)
'fluxes', ... % Atmospheric fluxes
'TT', ... % Soil temperature over depth
}; %#ok

% ...until finalize has been run, at which point it quits.
while ~strcmp(runMode, "finalize")
runMode = input("\nFinished command. Select run mode: ", "s");
if strcmp(runMode, "initialize") | strcmp(runMode, "update") | strcmp(runMode, "finalize")
if debugMode
disp(["Running in mode: ", runMode]);
end

if strcmp(runMode, "initialize") || strcmp(runMode, "update") || strcmp(runMode, "finalize")
% The 'initialize', 'update' and 'finalize' run modes are dispatched to the model.
run STEMMUS_SCOPE;
else
if debugMode
disp(["Run mode '", runMode, "' not recognised. Try again"]);

elseif strcmp(runMode, "save")
% Save entire model state to file. This can be retrieved with the
% model run mode "load", where this file is opened again.
save([Output_dir, 'STEMMUS_SCOPE_full_state.mat'], "-v7.3", "-nocompression");

elseif strcmp(runMode, "load")
% Load the full workspace file
[~, OutputPath, ~] = io.read_config(CFG);
stateFile = [OutputPath, 'STEMMUS_SCOPE_full_state.mat'];
if isfile(stateFile)
load(stateFile); %#ok
else
disp(["No file found at ", stateFile, ". Could not load file."]);
end

else
% If the runMode is not one of the above, promt user again.
disp(["Run mode '", runMode, "' not recognised. Try again"]);
end
end
disp("Finished clean up. Quitting...");

else
% run STEMMUS_SCOPE main code, in the selected run mode, and then quit.
run STEMMUS_SCOPE;
disp(["Run mode '", runMode, "' not recognised. The only valid modes are 'full' or 'interactive'"]);
end

0 comments on commit f23ad62

Please sign in to comment.