OMCompiler/SimulationRuntime/c/simulation/solver/jacobian_analysis.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | /*! \file jacobian_analysis.h | ||
| 29 | */ | ||
| 30 | |||
| 31 | #ifndef _JACOBIAN_ANALYSIS_H | ||
| 32 | #define _JACOBIAN_ANALYSIS_H | ||
| 33 | |||
| 34 | #include <stdio.h> | ||
| 35 | #include <stdlib.h> | ||
| 36 | |||
| 37 | #include "../../simulation_data.h" | ||
| 38 | #include "../simulation_info_json.h" | ||
| 39 | #include "../jacobian_util.h" | ||
| 40 | #include "../options.h" | ||
| 41 | #include "sundials_util.h" | ||
| 42 | #include "nonlinearSystem.h" | ||
| 43 | #include "simulation_data.h" | ||
| 44 | #include "sundials_error.h" | ||
| 45 | #include "simulation_data.h" | ||
| 46 | #include "util/simulation_options.h" | ||
| 47 | |||
| 48 | #include <kinsol/kinsol.h> | ||
| 49 | #include <nvector/nvector_serial.h> | ||
| 50 | |||
| 51 | #ifdef __cplusplus | ||
| 52 | extern "C" { | ||
| 53 | #endif | ||
| 54 | |||
| 55 | // TODO: add the derivative test here, so we only have one code with all the analysis methods | ||
| 56 | // unify all the debug messages of them | ||
| 57 | |||
| 58 | typedef enum SolverCaller { | ||
| 59 | KINSOL_JAC_EVAL, | ||
| 60 | KINSOL_ENTRY_POINT, | ||
| 61 | KINSOL_B_JAC_EVAL, | ||
| 62 | KINSOL_B_ENTRY_POINT | ||
| 63 | } SolverCaller; | ||
| 64 | |||
| 65 | static inline const char* SolverCaller_toString(SolverCaller caller) { | ||
| 66 | ✗ | switch (caller) { | |
| 67 | case KINSOL_JAC_EVAL: return "kinsol: Jacobian eval"; | ||
| 68 | ✗ | case KINSOL_ENTRY_POINT: return "kinsol: Kinsol entry point"; | |
| 69 | ✗ | case KINSOL_B_JAC_EVAL: return "experimental-kinsol: Jacobian eval"; | |
| 70 | ✗ | case KINSOL_B_ENTRY_POINT:return "experimental-kinsol: Kinsol entry point"; | |
| 71 | ✗ | default: return "UNKNOWN_SOLVER_CALLER"; | |
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | static inline const char* SolverCaller_callerString(SolverCaller caller) { | ||
| 76 | ✗ | switch (caller) { | |
| 77 | case KINSOL_JAC_EVAL: return "kinsol"; | ||
| 78 | case KINSOL_ENTRY_POINT: return "kinsol"; | ||
| 79 | ✗ | case KINSOL_B_JAC_EVAL: return "experimental-kinsol"; | |
| 80 | ✗ | case KINSOL_B_ENTRY_POINT:return "experimental-kinsol"; | |
| 81 | ✗ | default: return "UNKNOWN_SOLVER"; | |
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | // One column of U or V (NOT TRANSPOSE) | ||
| 86 | typedef struct SVD_Component { | ||
| 87 | int index; | ||
| 88 | modelica_real value; | ||
| 89 | } SVD_Component; | ||
| 90 | |||
| 91 | typedef struct SVD_DATA { | ||
| 92 | DATA *data; | ||
| 93 | NONLINEAR_SYSTEM_DATA *nls_data; | ||
| 94 | SolverCaller caller; | ||
| 95 | |||
| 96 | modelica_boolean scaled; // NLS is scaled | ||
| 97 | |||
| 98 | int rows; // #rows | ||
| 99 | int cols; // #columns | ||
| 100 | int min_rows_cols; // min(#rows, #cols) | ||
| 101 | modelica_real *A_dense; // dense column-major matrix (m × n) | ||
| 102 | modelica_real *S; // singular values (min(m, n)) | ||
| 103 | modelica_real *U; // left singular vectors (m × m if full) | ||
| 104 | modelica_real *VT; // right singular vectors transpose (n × n if full) | ||
| 105 | SPARSE_PATTERN *sparse_pattern; // sparse pattern | ||
| 106 | modelica_real *sp_values; // CSC values (same order as sp->index) | ||
| 107 | |||
| 108 | // statistics | ||
| 109 | modelica_real sigma_max; // largest singular value | ||
| 110 | modelica_real sigma_min; // smallest singular value | ||
| 111 | modelica_real cond; // condition = largest singular value / smallest singular value | ||
| 112 | modelica_real rank_est_tol; // tolerance for rank estimation, ranks count if sigma_i > rank_est_tol | ||
| 113 | int estimated_rank; // estimated rank | ||
| 114 | int least_one_percent; // index for first singular value < 1% of largest | ||
| 115 | } SVD_DATA; | ||
| 116 | |||
| 117 | static inline modelica_real _svd_max2(modelica_real a, modelica_real b) | ||
| 118 | { | ||
| 119 | ✗ | return (a > b ? a : b); | |
| 120 | } | ||
| 121 | |||
| 122 | // entry point for svd analysis | ||
| 123 | int svd_compute(DATA *data, NONLINEAR_SYSTEM_DATA *nls_data, modelica_real *values, modelica_boolean scaled, SolverCaller caller); | ||
| 124 | |||
| 125 | // entry point for jacobian sums of abs rows and cols | ||
| 126 | void nlsJacobianRowColSums(DATA *data, NONLINEAR_SYSTEM_DATA *nlsData, SUNMatrix J, | ||
| 127 | SolverCaller caller, modelica_boolean scaled); | ||
| 128 | |||
| 129 | #ifdef __cplusplus | ||
| 130 | }; | ||
| 131 | #endif | ||
| 132 | |||
| 133 | #endif // _JACOBIAN_ANALYSIS_H | ||
| 134 |