OMCompiler/SimulationRuntime/c/util/division.c
| 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 <stdio.h> | ||
| 29 | #include <stdlib.h> | ||
| 30 | #include <math.h> | ||
| 31 | #include <float.h> | ||
| 32 | |||
| 33 | #include "division.h" | ||
| 34 | #include "omc_msvc.h" | ||
| 35 | #include "omc_error.h" | ||
| 36 | |||
| 37 | 37 | int valid_number(double a) | |
| 38 | { | ||
| 39 |
4/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 10 times.
|
37 | return !isnan(a) && !isinf(a); |
| 40 | } | ||
| 41 | |||
| 42 | modelica_real __OMC_DIV_SIM_SLOW(threadData_t *threadData, const modelica_real a, const modelica_real b, const char *msg, const int *equationIndexes, modelica_boolean noThrowDivZero, const modelica_real time_, const modelica_boolean initial_) | ||
| 43 | { | ||
| 44 | modelica_real res; | ||
| 45 | if(b != 0.0) | ||
| 46 | res = a/b; | ||
| 47 | else if(initial_ && a == 0.0) | ||
| 48 | res = 0.0; | ||
| 49 | else { | ||
| 50 | res = a / division_error_equation_time(threadData, a, b, msg, equationIndexes, time_, noThrowDivZero); | ||
| 51 | if(!noThrowDivZero) | ||
| 52 | return res; | ||
| 53 | } | ||
| 54 | |||
| 55 | if(!valid_number(res)){ | ||
| 56 | if(noThrowDivZero) { | ||
| 57 | warningStreamPrintWithEquationIndexes(OMC_LOG_DIVISION, omc_dummyFileInfo, 0, equationIndexes, "division leads to inf or nan at time %g, (a=%g) / (b=%g), where divisor b is: %s", time_, a, b, msg); | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | raiseStreamPrintWithEquationIndexes(threadData, omc_dummyFileInfo, equationIndexes, "division leads to inf or nan at time %g, (a=%g) / (b=%g), where divisor b is: %s", time_, a, b, msg); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | return res; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | 1 | modelica_real division_error_equation_time(threadData_t *threadData, modelica_real a, modelica_real b, const char *msg, const int *indexes, modelica_real time, modelica_boolean noThrow) | |
| 68 | { | ||
| 69 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | if(noThrow){ |
| 70 | ✗ | warningStreamPrintWithEquationIndexes(OMC_LOG_DIVISION, omc_dummyFileInfo, 0, indexes, "solver will try to handle division by zero at time %.16g: %s", time, msg); | |
| 71 | } else { | ||
| 72 | 1 | raiseStreamPrintWithEquationIndexes(threadData, omc_dummyFileInfo, indexes, "division by zero at time %.16g, (a=%.16g) / (b=%.16g), where divisor b expression is: %s", time, a, b, msg); | |
| 73 | } | ||
| 74 | 1 | return b; | |
| 75 | } | ||
| 76 | |||
| 77 | 2 | modelica_real division_error_time(threadData_t *threadData, modelica_real b, const char* division_str, modelica_real time, const char* file, long line, modelica_boolean noThrow) | |
| 78 | { | ||
| 79 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if(noThrow){ |
| 80 | 2 | warningStreamPrint(OMC_LOG_DIVISION, 0, | |
| 81 | "division by zero in partial equation: %s\n" | ||
| 82 | "at Time=%f\n" | ||
| 83 | "solver will try to handle that.", division_str, time); | ||
| 84 | } else { | ||
| 85 | ✗ | warningStreamPrint(OMC_LOG_STDOUT, 0, | |
| 86 | "division by zero in partial equation: %s\n" | ||
| 87 | "at Time=%f\n" | ||
| 88 | "[line] %ld | [file] %s", division_str, time, line, file); | ||
| 89 | #ifndef __APPLE_CC__ | ||
| 90 | ✗ | raiseStreamPrint(threadData,"division by zero"); | |
| 91 | #endif | ||
| 92 | } | ||
| 93 | 2 | return b; | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | modelica_real division_error(threadData_t *threadData, modelica_real b, const char* division_str, const char* file, long line) | |
| 97 | { | ||
| 98 | ✗ | warningStreamPrint(OMC_LOG_STDOUT, 0, "division by zero in partial equation: %s\n" | |
| 99 | "[line] %ld | [file] %s", division_str, line, file); | ||
| 100 | #ifndef __APPLE_CC__ | ||
| 101 | ✗ | raiseStreamPrint(threadData,"division by zero"); | |
| 102 | #endif | ||
| 103 | ✗ | return b; | |
| 104 | } | ||
| 105 | |||
| 106 | ✗ | modelica_real isnan_error(threadData_t *threadData,modelica_real b, const char* division_str, const char* file, long line) | |
| 107 | { | ||
| 108 | ✗ | if(isnan(b)) | |
| 109 | { | ||
| 110 | ✗ | warningStreamPrint(OMC_LOG_STDOUT, 0, "division result in NAN in partial equation: %s\n" | |
| 111 | "[line] %ld | [file] %s", division_str, line, file); | ||
| 112 | #ifndef __APPLE_CC__ | ||
| 113 | ✗ | raiseStreamPrint(threadData,"division by zero"); | |
| 114 | #endif | ||
| 115 | } | ||
| 116 | ✗ | return b; | |
| 117 | } | ||
| 118 |