Using custom linear solver

14 views
Skip to first unread message

Oscar Molina

unread,
May 7, 2026, 9:53:11 AM (9 days ago) May 7
to MRST-users: The Matlab Reservoir Simulation Toolbox User Group
Hi,

I'm trying to implement my own linear solver with MRST. However,  I'm getting the following error when launching the simulation: "Solver Failure (...) Failure reason: Linear solver failure: MATLAB:TooManyInputs: Too many input arguments." 

Based on the example below, can you tell if I'm doing something wrong?

Thanks in advance for your help.

Oscar

%%%%%%%%%%%%%%%%%%%%%%%%%%
% Example solver function
%%%%%%%%%%%%%%%%%%%%%%%%%%
function x = gpuGMRESwithILU(A, b)
% ILU on CPU, solve on GPU
setup.type = 'ilutp';
setup.droptol = 1e-4;
[L, U] = ilu(A, setup);
M1_gpu = gpuArray(full(L));
M2_gpu = gpuArray(full(U));
A_gpu = gpuArray(A);
b_gpu = gpuArray(b);
x_gpu = gmres(A_gpu, b_gpu, 50, 1e-8, 10, M1_gpu, M2_gpu);
x = gather(x_gpu);
end

% Define function handle
ls = @(A, b) gpuGMRESwithILU(A, b);
linsolver = HandleLinearSolverAD(ls);

% Incorporate linear solver into schedule simulator
[wellSols, states, report] = simulateScheduleAD(initState, model, schedule, 'linearsolver', linsolver);

Bård Skaflestad

unread,
May 7, 2026, 10:08:07 AM (9 days ago) May 7
to Oscar Molina, MRST-users: The Matlab Reservoir Simulation Toolbox User Group
Hi,

Would you be able to give a little more context for the diagnostic, please? In particular, I think it would be useful to have a full backtrace for the "Solver Failure...Too many input arguments" message as that might identify the location that triggers the problem.


Best Regards,

Bård Skaflestad

SINTEF Digital, Mathematics & Cybernetics
Applied Computational Science group


From: sinte...@googlegroups.com <sinte...@googlegroups.com> on behalf of Oscar Molina <ommoli...@gmail.com>
Sent: Thursday, May 07, 2026 15:53
To: MRST-users: The Matlab Reservoir Simulation Toolbox User Group <sinte...@googlegroups.com>
Subject: [MRST Users] Using custom linear solver
--
You received this message because you are subscribed to the Google Groups "MRST-users: The Matlab Reservoir Simulation Toolbox User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sintef-mrst...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sintef-mrst/ffa2eb23-6307-4dc9-8dfa-7f5d376a92f0n%40googlegroups.com.

Oscar Molina

unread,
May 11, 2026, 5:35:53 PM (5 days ago) May 11
to MRST-users: The Matlab Reservoir Simulation Toolbox User Group
Hi Bard,

Apologies for the delayed response. It seems like the HandleLinearSolverAD class either cannot be used directly with the schedule simulator or need additional set-up work (superclass initialization, parametrization, etc.) Is my observation correct?

Below is an example custom solver class that I put together for testing purposes; my goal is to extend this approach to GPU-based solvers.

classdef CustomSolver < LinearSolverAD
properties
options = struct("type","ilutp","droptol",1e-6);
end
methods
function solver = CustomSolver(varargin)
solver@LinearSolverAD();
solver = merge_options(solver, varargin{:});
end
function [result, report] = solveLinearSystem(solver, A, b, varargin)
timer = tic();
try
setup.type = 'ilutp';
setup.droptol = max(1e-3, solver.options.droptol); % larger drop tolerance -> cheaper
setup.udiag = 1; % keep unit diagonal if appropriate
[L,U] = ilu(sparse(A), setup);
catch
d = diag(A);
d(abs(d) < eps) = 1; % avoid division by zero
Dinv = spdiags(1./d, 0, size(A,1), size(A,2));
L = speye(size(A,1)); % treat as M1 = I
U = Dinv; % treat as M2 = D^{-1} (applied on right in bicgstab/gmres calls)
end
result = bicgstab(A,b,1e-4,100,L,U);
t_solve = toc(timer);
report = solver.getSolveReport('LinearSolutionTime', t_solve);
end
function [d, sn] = getDescription(solver)
sn = 'mldivide';
sn = [sn, solver.id];
d = 'Matlab \ operator (mldivide direct solver)';
end
end
end

Thanks,
Oscar

Reply all
Reply to author
Forward
0 new messages