# AceGen: Equation solving fails (because of missing pivoting?)

I am looking to solve some nonlinear equations in a code generated by AceGen. To this end, i implemented a Newton-Raphson scheme making use of the SMSLUFactor[] and SMSLUSolve[] methods.

This approach did not reliably solve my system of equations - sometimes the LU factorisation fails (even though the matrices are regular, and can be factorised if i export them to matlab). After some trial and error, and consulting the documentation, i now think this is caused by the fact that no pivoting takes place in the AceGen routine.

Since i am not sure if this is intended behaviour or user error on my side, i am looking for some feedback on how to deal with this problem. Are there any best practices? For now, i can work around the problem with manual pivoting of my equation system, but this requires knowledge about the structure of the matrix at every time, and it is hard to catch individual entries that may be zero in certain cases.

I would be fine with "non-optimised" equation solving at runtime as a trade-off for reliability.

A minimal working example for the AceGen code i use to produce a Matlab function looks like this:

<< AceGen;

n = 2;

SMSInitialize["equationSolving", "Language" -> "Matlab"];
SMSModule["equationSolving", Real[A$$[n, n], x$$[n], b$$[n]], "Input" ->{A, b}, "Output" -> {x$$}];

A ⊢ SMSReal[Table[A$$[i, j], {i, n}, {j, n}]]; b ⊢ SMSReal[Table[b$$[i], {i, n}]];

LUA ⊨ SMSLUFactor[A];
x ⊨ SMSLUSolve[LUA, b];
SMSExport[x, Table[x[i], {i, n}]];

SMSWrite[];


At this time i test/use these codes in Matlab since i am most familiar with it, but i would like to recycle the same code to produce user materials and possibly elements for Abaqus later. I use the following Matlab code to reproduce the error:

% prescribe A and x
A = [1 0; 0 1];
x = [1 2]';

% compute b
b = A*x;

% call the AceGen routine to solve Ax=b (this is working)
x1 = equationSolving(A,b)

% prescribe C (permutation of lines 1 and 2 compared to A)
C = [0 1; 1 0];

% compute d with the same x as above (permutation of lines 1 and 2 compared to b)
d = C*x;

% call the AceGen routine to solve Cx=d (this is NOT working)
x2 = equationSolving(C,d)
`