Skip to content

Commit 4f3302b

Browse files
[iss-323] [iss-324]
Squashed commit of the following: commit 2765785 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Thu Jul 2 13:58:13 2026 -0300 Updated mmoc tests. commit 1475964 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Thu Jul 2 13:57:51 2026 -0300 Updated engine tests. commit 7415043 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Thu Jul 2 13:57:20 2026 -0300 Filter tearing and jac code generation. commit c6e0262 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Thu Jul 2 13:56:07 2026 -0300 Switch Sparse-Dense Jac generation order. commit 648cf23 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Thu Jul 2 11:49:16 2026 -0300 [iss-323] Squashed commit of the following: commit c20b07c Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Tue Jun 30 15:15:22 2026 -0300 Update classic solvers dat files in gt tests. commit dc938ff Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:46:21 2026 -0300 Modify print for classic solvers for python script printer. commit ee8a1f3 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 11:10:23 2026 -0300 Adjust Jacobian generation for integrators. commit 2b5bc4c Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 11:10:00 2026 -0300 Generate marix code only if needed. commit 0d8ad64 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 11:09:21 2026 -0300 Added jac generation annotation method. commit 93d2e58 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:44:11 2026 -0300 Generate code for tearing variables and solve loops. commit ae2020a Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:42:31 2026 -0300 Added tearing writer sections. commit 9433325 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:42:12 2026 -0300 Adapt equation and expressions to check for tearing variables. commit 3f24e87 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:40:57 2026 -0300 Added tearing variables code generation interface. commit 0d49f52 Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:40:08 2026 -0300 Modify tearing vars to parameters instead of states. commit ffcb29e Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com> Date: Mon Jun 29 10:39:15 2026 -0300 Added generic solve loop interface.
1 parent e891886 commit 4f3302b

224 files changed

Lines changed: 111521 additions & 118275 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/engine/classic/classic_cvode_integrator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void CVODE_integrate(SIM_simulator simulate)
218218
flag = CVodeRootInit(cvode_mem, clcData->events, CVODE_events);
219219
if (check_flag(&flag, "CVodeRootInit", 1, simulator)) return;
220220

221-
if (simulator->data->params->jacobian == 0) {
221+
if (simulator->data->params->jacobian == 1) {
222222
nnz = 0;
223223
for (i = 0; i < size; i++) {
224224
nnz += clcData->nSD[i];

src/engine/classic/classic_ida_integrator.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
/* Header files with a description of contents used */
2121

22-
//#define USE_JACOBIAN
23-
//#ifdef USE_JACOBIAN
22+
// #define USE_JACOBIAN
23+
// #ifdef USE_JACOBIAN
2424
#include <ida/ida_klu.h> /* prototype for CVSUPERLUMT */
2525
#include <sundials/sundials_sparse.h> /* definitions SlsMat */
26-
//#endif
26+
// #endif
2727

2828
#include <ida/ida.h>
2929
#include <ida/ida_dense.h>
@@ -52,7 +52,7 @@ static CLC_model clcModel = NULL;
5252

5353
static SD_output simOutput = NULL;
5454

55-
//#ifdef USE_JACOBIAN
55+
// #ifdef USE_JACOBIAN
5656
/* Test jacobian */
5757
static int IDA_Jac(realtype t, realtype cj, N_Vector y, N_Vector fy, N_Vector resvec, SlsMat JacMat, void *user_data, N_Vector tmp1,
5858
N_Vector tmp2, N_Vector tmp3)
@@ -81,7 +81,7 @@ static int IDA_Jac(realtype t, realtype cj, N_Vector y, N_Vector fy, N_Vector re
8181

8282
return 0;
8383
}
84-
//#endif
84+
// #endif
8585

8686
/* Test jacobian */
8787
static int check_flag(void *flagvalue, const char *funcname, int opt, CLC_simulator simulator)
@@ -200,11 +200,7 @@ void IDA_integrate(SIM_simulator simulate)
200200
flag = IDARootInit(mem, clcData->events, IDA_events);
201201
if (check_flag(&flag, "IDARootInit", 1, simulator)) return;
202202

203-
//#ifndef USE_JACOBIAN
204203
if (simulator->data->params->jacobian == 1) {
205-
flag = IDADense(mem, size);
206-
if (check_flag(&flag, "IDADense", 1, simulator)) return;
207-
} else {
208204
nnz = 0;
209205
for (i = 0; i < size; i++) {
210206
nnz += clcData->nSD[i];
@@ -217,9 +213,11 @@ void IDA_integrate(SIM_simulator simulate)
217213

218214
flag = IDASlsSetSparseJacFn(mem, IDA_Jac);
219215
if (check_flag(&flag, "IDASlsSetSparseJacFn", 1, simulator)) return;
216+
} else {
217+
flag = IDADense(mem, size);
218+
if (check_flag(&flag, "IDADense", 1, simulator)) return;
220219
}
221220

222-
//#endif
223221
double *res = malloc(sizeof(double) * size);
224222
IDASetUserData(mem, res);
225223
getTime(simulator->stats->sTime);

src/engine/classic/classic_integrator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void CLC_write_output(SD_output simOutput, double **solution, double *solution_t
8383
sprintf(name, "%s.dat", simOutput->variable[i].name);
8484
FILE *out = fopen(name, "w");
8585
for (j = 0; j < totalOutputSteps; j++) {
86-
fprintf(out, "%13.13g\t\t%13.13g\n", solution_time[j], solution[i][j]);
86+
fprintf(out, "%lf\t%lf\n", solution_time[j], solution[i][j]);
8787
}
8888
fclose(out);
8989
}

src/engine/common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ target_sources(
88
random.c
99
settings.c
1010
simulator.c
11+
solve_loop.c
1112
tree.c
1213
utils.c
1314
)

src/engine/common/solve_loop.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*****************************************************************************
2+
3+
This file is part of QSS Solver.
4+
5+
QSS Solver is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
QSS Solver is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17+
18+
******************************************************************************/
19+
20+
#include <gsl/gsl_multiroots.h>
21+
22+
#include "solve_loop.h"
23+
24+
// Eval and argument function callbacks.
25+
SVL_eval _eval_callback;
26+
double *_x = NULL;
27+
double *_d = NULL;
28+
double *_a = NULL;
29+
double *_t = NULL;
30+
double *_dx = NULL;
31+
int _tearing_vars;
32+
double *_tearing_var_values = NULL;
33+
34+
int SVL_SolveEval(const gsl_vector *__x, void *__p, gsl_vector *__f)
35+
{
36+
_eval_callback(_x, _d, _a, _t, _dx, _tearing_var_values);
37+
for (int i = 0; i < _tearing_vars; i++) {
38+
gsl_vector_set(__f, i, _tearing_var_values[i]);
39+
}
40+
return GSL_SUCCESS;
41+
}
42+
43+
void SVL_AssignCallback(double *x, double *d, double *a, double *t, double *dx, int tearing_vars, SVL_eval eval_tearing_vars)
44+
{
45+
if (_tearing_var_values == NULL) {
46+
_eval_callback = eval_tearing_vars;
47+
_x = x;
48+
_d = d;
49+
_a = a;
50+
_t = t;
51+
_dx = dx;
52+
_tearing_vars = tearing_vars;
53+
_tearing_var_values = (double *)malloc(sizeof(double) * _tearing_vars);
54+
}
55+
}
56+
57+
void SVL_SolveLoop(double *x, double *d, double *a, double *t, double *dx, int tearing_vars, SVL_iter assign_loop_vars,
58+
SVL_eval eval_tearing_vars)
59+
{
60+
size_t __iter = 0;
61+
int __status;
62+
63+
SVL_AssignCallback(x, d, a, t, dx, tearing_vars, eval_tearing_vars);
64+
65+
const gsl_multiroot_fsolver_type *__T = gsl_multiroot_fsolver_hybrid;
66+
gsl_multiroot_fsolver *__s = gsl_multiroot_fsolver_alloc(__T, tearing_vars);
67+
gsl_multiroot_function __F;
68+
static gsl_vector *__x = NULL;
69+
70+
if (__x == NULL) {
71+
__x = gsl_vector_alloc(tearing_vars);
72+
for (int i = 0; i < tearing_vars; i++) {
73+
gsl_vector_set(__x, i, 0);
74+
}
75+
}
76+
__F.n = tearing_vars;
77+
__F.f = SVL_SolveEval;
78+
gsl_vector *__f = gsl_vector_alloc(tearing_vars);
79+
// Try if we are already in the solution from the start (useful for discrete dependendt loops)
80+
SVL_SolveEval(__x, NULL, __f);
81+
if (gsl_multiroot_test_residual(__f, 1e-7) == GSL_SUCCESS) {
82+
gsl_vector_free(__f);
83+
gsl_multiroot_fsolver_free(__s);
84+
return;
85+
}
86+
gsl_vector_free(__f);
87+
gsl_multiroot_fsolver_set(__s, &__F, __x);
88+
89+
do {
90+
__iter++;
91+
__status = gsl_multiroot_fsolver_iterate(__s);
92+
if (__status) { /* check if solver is stuck */
93+
break;
94+
}
95+
__status = gsl_multiroot_test_residual(__s->f, 1e-7);
96+
assign_loop_vars(x, d, a, t, dx);
97+
} while (__status == GSL_CONTINUE && __iter < 100);
98+
99+
if (__iter == 100) {
100+
printf("Warning: GSL could not solve an algebraic loop after %d iterations\n", (int)__iter);
101+
}
102+
103+
for (int i = 0; i < tearing_vars; i++) {
104+
double ret = gsl_vector_get(__s->x, i);
105+
gsl_vector_set(__x, i, ret);
106+
}
107+
108+
gsl_multiroot_fsolver_free(__s);
109+
}

src/engine/common/solve_loop.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*****************************************************************************
2+
3+
This file is part of QSS Solver.
4+
5+
QSS Solver is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
QSS Solver is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17+
18+
******************************************************************************/
19+
20+
#ifndef SOLVE_LOOP_H_
21+
#define SOLVE_LOOP_H_
22+
23+
#include "data.h"
24+
25+
typedef void (*SVL_iter)(double *, double *, double *, double *, double *);
26+
typedef void (*SVL_eval)(double *, double *, double *, double *, double *, double *);
27+
28+
/**
29+
*
30+
*/
31+
void SVL_solve_loop(double *x, double *d, double *a, double *t, double *dx, int tearing_vars, SVL_iter assign_loop_vars,
32+
SVL_eval eval_tearing_vars);
33+
34+
#endif /* SOLVE_LOOP_H_ */

src/engine/qss/qss_hyb_integrator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void QSS_HYB_integrate(SIM_simulator simulate)
423423
flag = CVodeSVtolerances(cvode_mem, reltol, abstol);
424424
if (check_flag(&flag, "CVodeInit", 1, simulator)) return;
425425

426-
if (simulator->data->params->jacobian == 0) {
426+
if (simulator->data->params->jacobian == 1) {
427427
int nnz = 0;
428428
for (i = 0; i < nBDF; i++) {
429429
int row = BDFMap[i];

0 commit comments

Comments
 (0)