OMCompiler/SimulationRuntime/c/openmodelica.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 openmodelica.h | ||
| 29 | * Description: This is the C header file for the C code generated from | ||
| 30 | * Modelica. It includes e.g. the C object representation of the builtin types | ||
| 31 | * and arrays, etc. | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef OPENMODELICA_H_ | ||
| 35 | #define OPENMODELICA_H_ | ||
| 36 | |||
| 37 | #if defined(__cplusplus) | ||
| 38 | extern "C" { | ||
| 39 | #endif | ||
| 40 | |||
| 41 | #include "omc_simulation_settings.h" | ||
| 42 | |||
| 43 | #include <stdlib.h> | ||
| 44 | #include <stdio.h> | ||
| 45 | #include <limits.h> | ||
| 46 | #include <assert.h> | ||
| 47 | #include <float.h> | ||
| 48 | |||
| 49 | #include "omc_dll.h" | ||
| 50 | |||
| 51 | #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L | ||
| 52 | #define HAVE_VA_MACROS 1 | ||
| 53 | #endif | ||
| 54 | |||
| 55 | #include "openmodelica_types.h" | ||
| 56 | #if defined(__MINGW32__) || defined(_MSC_VER) | ||
| 57 | #if !defined(WIN32_LEAN_AND_MEAN) | ||
| 58 | #define WIN32_LEAN_AND_MEAN | ||
| 59 | #endif | ||
| 60 | #if !defined(NOMINMAX) | ||
| 61 | #define NOMINMAX | ||
| 62 | #endif | ||
| 63 | #endif | ||
| 64 | |||
| 65 | /* BEFORE: compat.h */ | ||
| 66 | #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__AVR__) | ||
| 67 | #define EXIT(code) exit(code) | ||
| 68 | #elif defined(OMC_GCOV_COVERAGE) | ||
| 69 | /* Same _exit() as below, but gcov writes its counters from an atexit handler, | ||
| 70 | * which _exit() skips - omc and every simulation would record nothing at all. | ||
| 71 | * Dump them explicitly first rather than switching to exit(), so a coverage | ||
| 72 | * build keeps the exit-code behaviour the comment below describes. | ||
| 73 | * Defined by -DOM_ENABLE_COVERAGE=ON; see cmake/modules/OpenModelicaCoverage.cmake. */ | ||
| 74 | #include <unistd.h> | ||
| 75 | extern void __gcov_dump(void); | ||
| 76 | #define EXIT(code) {fflush(NULL); __gcov_dump(); _exit(code);} | ||
| 77 | #else | ||
| 78 | /* We need to patch exit() on Unix systems | ||
| 79 | * It does not change the exit code of simulations for some reason! */ | ||
| 80 | #include <unistd.h> | ||
| 81 | #define EXIT(code) {fflush(NULL); _exit(code);} | ||
| 82 | #endif | ||
| 83 | |||
| 84 | #include "omc_inline.h" | ||
| 85 | |||
| 86 | /* BEFORE: fortran_types */ | ||
| 87 | #if defined(__alpha__) || defined(__sparc64__) || defined(__x86_64__) || defined(__ia64__) | ||
| 88 | typedef int fortran_integer; | ||
| 89 | typedef unsigned int fortran_uinteger; | ||
| 90 | #else | ||
| 91 | typedef long int fortran_integer; | ||
| 92 | typedef unsigned long int fortran_uinteger; | ||
| 93 | #endif | ||
| 94 | |||
| 95 | |||
| 96 | /* BEFORE: read_write */ | ||
| 97 | typedef struct type_desc_s type_description; | ||
| 98 | |||
| 99 | enum type_desc_e { | ||
| 100 | TYPE_DESC_NONE, | ||
| 101 | TYPE_DESC_REAL, | ||
| 102 | TYPE_DESC_REAL_ARRAY, | ||
| 103 | TYPE_DESC_INT, | ||
| 104 | TYPE_DESC_INT_ARRAY, | ||
| 105 | TYPE_DESC_BOOL, | ||
| 106 | TYPE_DESC_BOOL_ARRAY, | ||
| 107 | TYPE_DESC_STRING, | ||
| 108 | TYPE_DESC_STRING_ARRAY, | ||
| 109 | TYPE_DESC_TUPLE, | ||
| 110 | TYPE_DESC_COMPLEX, | ||
| 111 | TYPE_DESC_RECORD, | ||
| 112 | /* function pointer - added by stefan */ | ||
| 113 | TYPE_DESC_FUNCTION, | ||
| 114 | TYPE_DESC_MMC, | ||
| 115 | TYPE_DESC_NORETCALL | ||
| 116 | }; | ||
| 117 | |||
| 118 | /* As type_desc_s.data.string: elements cross as char*, not modelica_string. */ | ||
| 119 | struct _str_array { | ||
| 120 | int ndims; | ||
| 121 | _index_t *dim_size; | ||
| 122 | const char **data; | ||
| 123 | }; | ||
| 124 | |||
| 125 | static inline size_t str_array_nr_of_elements(struct _str_array a) | ||
| 126 | { | ||
| 127 | size_t i, n = 1; | ||
| 128 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | for (i = 0; i < (size_t) a.ndims; ++i) { |
| 129 | 2 | n *= (size_t) a.dim_size[i]; | |
| 130 | } | ||
| 131 | return n; | ||
| 132 | } | ||
| 133 | |||
| 134 | struct type_desc_s { | ||
| 135 | enum type_desc_e type; | ||
| 136 | int retval : 1; | ||
| 137 | union _data { | ||
| 138 | modelica_real real; | ||
| 139 | real_array r_array; | ||
| 140 | modelica_integer integer; | ||
| 141 | integer_array int_array; | ||
| 142 | modelica_boolean boolean; | ||
| 143 | boolean_array bool_array; | ||
| 144 | const char *string; /* neutral: omc and a dlopened library differ */ | ||
| 145 | struct _str_array str_array; | ||
| 146 | struct _tuple { | ||
| 147 | size_t elements; | ||
| 148 | struct type_desc_s *element; | ||
| 149 | } tuple; | ||
| 150 | modelica_complex om_complex; | ||
| 151 | struct _record { | ||
| 152 | const char *record_name; | ||
| 153 | size_t elements; | ||
| 154 | char **name; | ||
| 155 | struct type_desc_s *element; | ||
| 156 | } record; | ||
| 157 | /* function pointer - stefan */ | ||
| 158 | modelica_fnptr function; | ||
| 159 | void* mmc; | ||
| 160 | } data; | ||
| 161 | }; | ||
| 162 | |||
| 163 | /* math functions (-lm)*/ | ||
| 164 | |||
| 165 | /* Special Modelica builtin functions*/ | ||
| 166 | #define smooth(P,EXP) (EXP) | ||
| 167 | static inline modelica_real semiLinear(const modelica_real x,const modelica_real positiveSlope, const modelica_real negativeSlope){return x*((x>=0)? positiveSlope : negativeSlope);} | ||
| 168 | /* sign function */ | ||
| 169 | static inline int sign(double v) | ||
| 170 | { | ||
| 171 |
19/56✗ Branch 0 not taken.
✓ Branch 0 taken 1958669 times.
✗ Branch 1 not taken.
✓ Branch 1 taken 2152485 times.
✗ Branch 2 not taken.
✓ Branch 2 taken 351138 times.
✗ Branch 3 not taken.
✓ Branch 3 taken 1607531 times.
✗ Branch 4 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
✓ Branch 12 taken 3 times.
✗ Branch 13 not taken.
✓ Branch 13 taken 3 times.
✗ Branch 14 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 15 taken 3 times.
✗ Branch 16 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 17 taken 1 time.
✗ Branch 18 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✓ Branch 23 taken 1 time.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 time.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✓ Branch 31 taken 1 time.
|
4111164 | return v > 0 ? 1 : v < 0 ? -1 : 0; |
| 172 | } | ||
| 173 | |||
| 174 | #include "util/omc_msvc.h" | ||
| 175 | |||
| 176 | /* initial and terminal function calls */ | ||
| 177 | #define initial() data->simulationInfo->initial | ||
| 178 | #define terminal() data->simulationInfo->terminal | ||
| 179 | |||
| 180 | #define homotopy(actual, simplified) ((simplified) * (1.0-data->simulationInfo->lambda) + (actual) * data->simulationInfo->lambda) | ||
| 181 | #define homotopyParameter() data->simulationInfo->lambda | ||
| 182 | |||
| 183 | #include "gc/omc_gc.h" | ||
| 184 | |||
| 185 | /* g++ does not allow putting attributes next to labels | ||
| 186 | * clang++ does allow it however... | ||
| 187 | */ | ||
| 188 | #if defined(__cplusplus) && defined(__GNUC__) | ||
| 189 | #define OMC_LABEL_UNUSED | ||
| 190 | #else | ||
| 191 | #define OMC_LABEL_UNUSED __attribute__((unused)) | ||
| 192 | #endif | ||
| 193 | |||
| 194 | #if !defined(OMC_DISABLE_OPT) | ||
| 195 | #if defined(__clang__) | ||
| 196 | #define OMC_DISABLE_OPT __attribute__((optnone)) | ||
| 197 | #elif defined(__GNUC__) | ||
| 198 | #define OMC_DISABLE_OPT __attribute__((optimize(0))) | ||
| 199 | #elif defined(_MSC_VER) | ||
| 200 | #define OMC_DISABLE_OPT /* nothing */ | ||
| 201 | #endif | ||
| 202 | #endif | ||
| 203 | |||
| 204 | #if defined(__GNUC__) || defined(__clang__) | ||
| 205 | #define OMC_LIKELY(x) __builtin_expect(!!(x), 1) | ||
| 206 | #define OMC_COLD __attribute__((cold, noinline)) | ||
| 207 | #else | ||
| 208 | #define OMC_LIKELY(x) (x) | ||
| 209 | #define OMC_COLD | ||
| 210 | #endif | ||
| 211 | |||
| 212 | #if defined(__cplusplus) | ||
| 213 | } /* end extern "C" */ | ||
| 214 | #endif | ||
| 215 | |||
| 216 | #endif | ||
| 217 |