Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 0 additions & 157 deletions core/startup.m

This file was deleted.

163 changes: 149 additions & 14 deletions startup.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,162 @@
function startup()
run_startup();
dirs = {'autodiff', 'model-io', 'multiscale', 'modules', 'solvers', ...
'visualization','co2lab'};
base_path = fileparts(mfilename('fullpath')); %#ok
clear fn
for i = 1:numel(dirs)
mrstPath('addroot', fullfile(base_path, dirs{i}));
end
run_local();
end

function d = rootdir()
%Amend MATLAB PATH to handle MRST implementation.

%{
Copyright 2009-2024 SINTEF Digital, Mathematics & Cybernetics.
This file is part of The MATLAB Reservoir Simulation Toolbox (MRST).
MRST is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MRST is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MRST. If not, see <http://www.gnu.org/licenses/>.
%}

build_mrst_path_tree();

% Register known third-party modules
mod_3rdparty = { 'matlab_bgl' };
activate_3rdparty_modules(mod_3rdparty);

% If there exists a startup_user.m file in the root dir of MRST, we
% execute this file.
run_local();

% Run platform (Octave/Matlab) specific startup routines
run_platform_specific();

% Automatically load selected modules for backwards compatibility.
autoload = {};
load_compat_modules(autoload);
Comment on lines +36 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set of compatibility modules used to be the "incomp"/"mimetic" modules at some point, but by now it's been empty for a long time. I think we should use the opportunity when fusing these startup functions into one to just remove the compatibility modules altogether.


% Display welcome message
mrstStartupMessage();

% Attempt to load settings - and perform first-time setup if required.
settings = mrstSettings(); %#ok
end

%--------------------------------------------------------------------------

function d = rootdir
d = fileparts(mfilename('fullpath'));
end

function run_startup()
run(fullfile(rootdir(), 'core', 'startup.m'));
%--------------------------------------------------------------------------

function build_mrst_path_tree
d = rootdir();

m = fullfile(d, 'modules');
p = split_path(genpath(d));

% Remove octave_only folders from path
oct_only = ~cellfun(@isempty, regexp(p, 'octave_only'));

i = strncmp(m, p, length(m));
i = i | ~cellfun(@isempty, regexp(p, '\.(git|hg|svn)'));
i = i | ~cellfun(@isempty, regexp(p, '3rdparty'));
i = i | ~cellfun(@isempty, regexp(p, 'output'));
i = i | cellfun(@isempty, p);
i = i | oct_only;

addpath(p{~i});

if mrstPlatform('octave')
% If we are on Octave, we add in the octave_only paths at the end to
% ensure that they can overwrite any of the core files where needed.
% We first turn off the shadowing warning since we may have
% compatibility fixes included in this folder.
os = 'Octave:shadowed-function';
warnstate = warning('query', os);
warning('off', os);
addpath(p{oct_only});
warning(warnstate);
end

% Add modules as root directories
dirs = {'autodiff', 'model-io', 'multiscale', 'modules', 'solvers', ...
'visualization','co2lab'};
base_path = fileparts(mfilename('fullpath')); %#ok
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm reading it incorrectly, this is just rootdir(), i.e., d.

clear fn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we use/assign fn anywhere, so we could just remove this statement I think.

for i = 1:numel(dirs)
mrstPath('addroot', fullfile(base_path, dirs{i}));
end
Comment on lines +90 to +92
Copy link
Contributor

@bska bska May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need the index i here. We might as well just write this as

for rdir = dirs
   mrstPath('addroot', fullfile(d, rdir{1}));
end

instead.

If we wish to be very careful/future proof we might even consider writing

for rdir = reshape(dirs, 1, [])
   mrstPath('addroot', fullfile(d, rdir{1}));
end

although that is getting close to going overboard.


end

%--------------------------------------------------------------------------

function p = split_path(p)
try
p = regexp(p, pathsep, 'split');
catch %#ok
% Octave compatibility. It is an error to get here in an M run.
p = strsplit(p, pathsep);
end
end

%--------------------------------------------------------------------------

function activate_3rdparty_modules(mod_3rdparty)
d = rootdir();
thirdparty = @(m) fullfile(d, 'core', 'utils', '3rdparty', m);

for mod = reshape(mod_3rdparty, 1, [])
mrstPath('add', mod{1}, thirdparty(mod{1}));
end
end

%--------------------------------------------------------------------------

function load_compat_modules(mlist)
if isempty(mlist), return, end

p = mrstPath('search', mlist{:});

if isempty(p)
mlist = {};
elseif iscellstr(p)
mlist = mlist(~ cellfun(@isempty, p));
end

if ~isempty(mlist)
pl = 's'; if numel(mlist) == 1, pl = ''; end

fprintf(['Note: Automatically loading selected ', ...
'module%s for backwards compatibility:\n'], pl);

fprintf(' * %s\n', mlist{:});

mrstModule('add', mlist{:})
end
end

%--------------------------------------------------------------------------

function run_local()
do_run_local(fullfile(rootdir(), 'startup_user.m'));
end

%--------------------------------------------------------------------------

function run_platform_specific()
if mrstPlatform('octave')
do_run_local(fullfile(rootdir(), 'utils', ...
'octave_only', 'startup_octave.m'));
end
end

%--------------------------------------------------------------------------

function do_run_local(local)
if exist(local, 'file') == 2
run(local);
Expand Down