OMCompiler/SimulationRuntime/c/moo/debug.cpp
| 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 | #include <sstream> | ||
| 29 | #include <iomanip> | ||
| 30 | |||
| 31 | #include <base/util.h> | ||
| 32 | |||
| 33 | #include "debug.h" | ||
| 34 | |||
| 35 | namespace OpenModelica { | ||
| 36 | |||
| 37 | ✗ | void print_real_var_names(DATA* data) { | |
| 38 | ✗ | for (long idx = 0; idx < data->modelData->nVariablesReal; idx++) | |
| 39 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", data->modelData->realVarsData[idx].info.name); | |
| 40 | ✗ | } | |
| 41 | |||
| 42 | ✗ | void print_parameters(DATA* data) { printParameters(data, 1); } | |
| 43 | |||
| 44 | ✗ | void print_real_var_names_values(DATA* data) { | |
| 45 | long maxNameLen = 2; | ||
| 46 | |||
| 47 | ✗ | for (long idx = 0; idx < data->modelData->nVariablesReal; idx++) { | |
| 48 | ✗ | long nameLen = strlen(data->modelData->realVarsData[idx].info.name); | |
| 49 | if (nameLen > maxNameLen) | ||
| 50 | maxNameLen = nameLen; | ||
| 51 | } | ||
| 52 | |||
| 53 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "Time :: %.17g", data->localData[0]->timeValue); | |
| 54 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "--------------------------------------------------"); | |
| 55 | |||
| 56 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%-8s :: %-*s :: %s", "Index", static_cast<int>(maxNameLen), "Name", "Value"); | |
| 57 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "--------------------------------------------------"); | |
| 58 | |||
| 59 | ✗ | for (long idx = 0; idx < data->modelData->nVariablesReal; idx++) { | |
| 60 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%-8ld :: %-*s :: %+.17e", | |
| 61 | idx, | ||
| 62 | ✗ | static_cast<int>(maxNameLen), data->modelData->realVarsData[idx].info.name, | |
| 63 | ✗ | data->localData[0]->realVars[idx]); | |
| 64 | } | ||
| 65 | ✗ | } | |
| 66 | ✗ | void print_jacobian_sparsity(const JACOBIAN* jac, bool print_pattern, const char* name = nullptr) { | |
| 67 | ✗ | if (!jac || !jac->sparsePattern) { | |
| 68 | ✗ | errorStreamPrint(OMC_LOG_MOO, 0, "Invalid JACOBIAN or missing SPARSE_PATTERN"); | |
| 69 | ✗ | return; | |
| 70 | } | ||
| 71 | |||
| 72 | const SPARSE_PATTERN* sp = jac->sparsePattern; | ||
| 73 | ✗ | const unsigned int nRows = jac->sizeRows; | |
| 74 | ✗ | const unsigned int nCols = jac->sizeCols; | |
| 75 | |||
| 76 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "\n=== JACOBIAN SPARSITY INFO ==="); | |
| 77 | ✗ | if (name) { | |
| 78 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "Name: %s", name); | |
| 79 | } | ||
| 80 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "Jacobian: %u rows x %u cols", nRows, nCols); | |
| 81 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "numberOfNonZeros: %u", sp->nnz); | |
| 82 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "maxColors: %u", sp->maxColors); | |
| 83 | |||
| 84 | // leadindex | ||
| 85 | { | ||
| 86 | ✗ | std::ostringstream oss; | |
| 87 | ✗ | oss << "leadindex: "; | |
| 88 | ✗ | for (unsigned int i = 0; i <= nCols; ++i) { | |
| 89 | ✗ | oss << (sp->leadindex ? sp->leadindex[i] : 0) << " "; | |
| 90 | } | ||
| 91 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", oss.str().c_str()); | |
| 92 | ✗ | } | |
| 93 | |||
| 94 | // index | ||
| 95 | { | ||
| 96 | ✗ | std::ostringstream oss; | |
| 97 | ✗ | oss << "index: "; | |
| 98 | ✗ | for (unsigned int i = 0; i < sp->nnz; ++i) { | |
| 99 | ✗ | oss << (sp->index ? sp->index[i] : 0) << " "; | |
| 100 | } | ||
| 101 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", oss.str().c_str()); | |
| 102 | ✗ | } | |
| 103 | |||
| 104 | // colorCols | ||
| 105 | { | ||
| 106 | ✗ | std::ostringstream oss; | |
| 107 | ✗ | oss << "colorCols: "; | |
| 108 | ✗ | for (unsigned int i = 0; i < sp->maxColors; ++i) { | |
| 109 | ✗ | oss << (sp->colorCols ? sp->colorCols[i] : 0) << " "; | |
| 110 | } | ||
| 111 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", oss.str().c_str()); | |
| 112 | ✗ | } | |
| 113 | |||
| 114 | ✗ | if (!print_pattern) { | |
| 115 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "==============================="); | |
| 116 | ✗ | return; | |
| 117 | } | ||
| 118 | |||
| 119 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "\n=== JACOBIAN SPARSITY PLOT ==="); | |
| 120 | |||
| 121 | // column numbers | ||
| 122 | { | ||
| 123 | ✗ | std::ostringstream oss; | |
| 124 | ✗ | oss << " "; | |
| 125 | ✗ | for (unsigned int col = 0; col < nCols; col++) | |
| 126 | ✗ | oss << (col % 10); | |
| 127 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", oss.str().c_str()); | |
| 128 | ✗ | } | |
| 129 | |||
| 130 | // rows | ||
| 131 | ✗ | for (unsigned int row = 0; row < nRows; row++) { | |
| 132 | ✗ | std::ostringstream oss; | |
| 133 | ✗ | oss << std::setw(4) << row << ": "; | |
| 134 | ✗ | for (unsigned int col = 0; col < nCols; col++) { | |
| 135 | bool found = false; | ||
| 136 | ✗ | for (unsigned int nz = sp->leadindex[col]; nz < sp->leadindex[col + 1]; nz++) { | |
| 137 | ✗ | if (sp->index[nz] == row) { | |
| 138 | found = true; | ||
| 139 | break; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | ✗ | oss << (found ? '*' : ' '); | |
| 143 | } | ||
| 144 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "%s", oss.str().c_str()); | |
| 145 | ✗ | } | |
| 146 | |||
| 147 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "================================"); | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | void print_bounds_fixed_vector(FixedVector<Bounds>& vec) { | |
| 151 | int i = 0; | ||
| 152 | ✗ | for (const auto& b : vec) { | |
| 153 | ✗ | infoStreamPrint(OMC_LOG_MOO, 0, "[%d] lb: %.17g, ub: %.17g", i++, b.lb, b.ub); | |
| 154 | } | ||
| 155 | ✗ | } | |
| 156 | |||
| 157 | |||
| 158 | ✗ | void disable_omc_logs() { | |
| 159 | memset(omc_useStream, 0, OMC_SIM_LOG_MAX * sizeof(int)); | ||
| 160 | ✗ | } | |
| 161 | |||
| 162 | } // namespace OpenModelica | ||
| 163 |