ModelicaIO.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* ModelicaIO.c - Array I/O functions
  2. Copyright (C) 2016, Modelica Association
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /* The functions in this file are non-portable. The following #define's are used
  23. to define the system calls of the operating system
  24. __GNUC__ : GNU C compiler
  25. NO_FILE_SYSTEM : A file system is not present (e.g. on dSPACE or xPC).
  26. MODELICA_EXPORT: Prefix used for function calls. If not defined, blank is used
  27. Useful definitions:
  28. - "static" that is all functions become static
  29. (useful if file is included with other C-sources for an
  30. embedded system)
  31. - "__declspec(dllexport)" if included in a DLL and the
  32. functions shall be visible outside of the DLL
  33. Release Notes:
  34. Mar. 03, 2016: by Thomas Beutlich, ITI GmbH and Martin Otter, DLR
  35. Implemented a first version (ticket #1856)
  36. */
  37. #if !defined(MODELICA_EXPORT)
  38. #define MODELICA_EXPORT DYMOLA_STATIC
  39. #endif
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "ModelicaUtilities.h"
  43. #ifdef NO_FILE_SYSTEM
  44. static void ModelicaNotExistError(const char* name) {
  45. /* Print error message if a function is not implemented */
  46. ModelicaFormatError("C-Function \"%s\" is called "
  47. "but is not implemented for the actual environment "
  48. "(e.g., because there is no file system available on the machine "
  49. "as for dSPACE or xPC systems)\n", name);
  50. }
  51. MODELICA_EXPORT void ModelicaIO_readMatrixSizes(const char* fileName,
  52. const char* varName, int* dim) {
  53. ModelicaNotExistError("ModelicaIO_readMatrixSizes"); }
  54. MODELICA_EXPORT void ModelicaIO_readRealMatrix(const char* fileName,
  55. const char* varName, double* a, size_t m, size_t n, int verbose) {
  56. ModelicaNotExistError("ModelicaIO_readRealMatrix"); }
  57. MODELICA_EXPORT int ModelicaIO_writeRealMatrix(const char* fileName,
  58. const char* varName, double* a, size_t m, size_t n, int append, const char* version) {
  59. ModelicaNotExistError("ModelicaIO_writeRealMatrix"); return 0; }
  60. #else
  61. #include <stdio.h>
  62. #include "ModelicaIO.h"
  63. #include "ModelicaMatIO.h"
  64. static void ModelicaIO_transpose(double* table, size_t nRow, size_t nCol) {
  65. /* Reference:
  66. Cycle-based in-place array transposition
  67. (http://en.wikipedia.org/wiki/In-place_matrix_transposition#Non-square_matrices:_Following_the_cycles)
  68. */
  69. size_t i;
  70. for (i = 1; i < nRow*nCol - 1; i++) {
  71. size_t x = nRow*(i % nCol) + i/nCol; /* predecessor of i in the cycle */
  72. /* Continue if cycle is of length one or predecessor already was visited */
  73. if (x <= i) {
  74. continue;
  75. }
  76. /* Continue if cycle already was visited */
  77. while (x > i) {
  78. x = nRow*(x % nCol) + x/nCol;
  79. }
  80. if (x < i) {
  81. continue;
  82. }
  83. {
  84. double tmp = table[i];
  85. size_t s = i; /* start index in the cycle */
  86. x = nRow*(i % nCol) + i/nCol; /* predecessor of i in the cycle */
  87. while (x != i) {
  88. table[s] = table[x];
  89. s = x;
  90. x = nRow*(x % nCol) + x/nCol;
  91. }
  92. table[s] = tmp;
  93. }
  94. }
  95. }
  96. MODELICA_EXPORT void ModelicaIO_readMatrixSizes(const char* fileName,
  97. const char* varName, int* dim) {
  98. mat_t* mat;
  99. matvar_t* matvar;
  100. matvar_t* matvarRoot;
  101. char* varNameCopy;
  102. char* token;
  103. varNameCopy = (char*)malloc((strlen(varName) + 1)*sizeof(char));
  104. if (varNameCopy != NULL) {
  105. strcpy(varNameCopy, varName);
  106. }
  107. else {
  108. dim[0] = 0;
  109. dim[1] = 0;
  110. ModelicaError("Memory allocation error\n");
  111. return;
  112. }
  113. mat = Mat_Open(fileName, (int)MAT_ACC_RDONLY);
  114. if (mat == NULL) {
  115. dim[0] = 0;
  116. dim[1] = 0;
  117. free(varNameCopy);
  118. ModelicaFormatError("Not possible to open file \"%s\": "
  119. "No such file or directory\n", fileName);
  120. return;
  121. }
  122. token = strtok(varNameCopy, ".");
  123. matvarRoot = Mat_VarReadInfo(mat, token == NULL ? varName : token);
  124. if (matvarRoot == NULL) {
  125. dim[0] = 0;
  126. dim[1] = 0;
  127. free(varNameCopy);
  128. (void)Mat_Close(mat);
  129. ModelicaFormatError(
  130. "Variable \"%s\" not found on file \"%s\".\n",
  131. token == NULL ? varName : token, fileName);
  132. return;
  133. }
  134. matvar = matvarRoot;
  135. token = strtok(NULL, ".");
  136. /* Get field while matvar is of struct class and of 1x1 size */
  137. while (token != NULL && matvar != NULL &&
  138. matvar->class_type == MAT_C_STRUCT && matvar->rank == 2 &&
  139. matvar->dims[0] == 1 && matvar->dims[1] == 1) {
  140. matvar = Mat_VarGetStructField(matvar, (void*)token, MAT_BY_NAME, 0);
  141. token = strtok(NULL, ".");
  142. }
  143. free(varNameCopy);
  144. if (matvar == NULL) {
  145. Mat_VarFree(matvarRoot);
  146. (void)Mat_Close(mat);
  147. ModelicaFormatError(
  148. "Matrix \"%s\" not found on file \"%s\".\n", varName, fileName);
  149. return;
  150. }
  151. /* Check if matvar is a matrix */
  152. if (matvar->rank != 2) {
  153. dim[0] = 0;
  154. dim[1] = 0;
  155. Mat_VarFree(matvarRoot);
  156. (void)Mat_Close(mat);
  157. ModelicaFormatError(
  158. "Array \"%s\" has not the required rank 2.\n", varName);
  159. return;
  160. }
  161. dim[0] = (int)matvar->dims[0];
  162. dim[1] = (int)matvar->dims[1];
  163. Mat_VarFree(matvarRoot);
  164. (void)Mat_Close(mat);
  165. }
  166. MODELICA_EXPORT void ModelicaIO_readRealMatrix(const char* fileName,
  167. const char* varName, double* a, size_t m, size_t n, int verbose) {
  168. mat_t* mat;
  169. matvar_t* matvar;
  170. matvar_t* matvarRoot;
  171. size_t nRow, nCol;
  172. int tableReadError = 0;
  173. char* varNameCopy;
  174. char* token;
  175. if (verbose == 1) {
  176. /* Print info message, that matrix / file is loading */
  177. ModelicaFormatMessage("... loading \"%s\" from \"%s\"\n",
  178. varName, fileName);
  179. }
  180. varNameCopy = (char*)malloc((strlen(varName) + 1)*sizeof(char));
  181. if (varNameCopy != NULL) {
  182. strcpy(varNameCopy, varName);
  183. }
  184. else {
  185. ModelicaError("Memory allocation error\n");
  186. return;
  187. }
  188. mat = Mat_Open(fileName, (int)MAT_ACC_RDONLY);
  189. if (mat == NULL) {
  190. free(varNameCopy);
  191. ModelicaFormatError("Not possible to open file \"%s\": "
  192. "No such file or directory\n", fileName);
  193. return;
  194. }
  195. token = strtok(varNameCopy, ".");
  196. matvarRoot = Mat_VarReadInfo(mat, token == NULL ? varName : token);
  197. if (matvarRoot == NULL) {
  198. free(varNameCopy);
  199. (void)Mat_Close(mat);
  200. ModelicaFormatError(
  201. "Variable \"%s\" not found on file \"%s\".\n",
  202. token == NULL ? varName : token, fileName);
  203. return;
  204. }
  205. matvar = matvarRoot;
  206. token = strtok(NULL, ".");
  207. /* Get field while matvar is of struct class and of 1x1 size */
  208. while (token != NULL && matvar != NULL &&
  209. matvar->class_type == MAT_C_STRUCT && matvar->rank == 2 &&
  210. matvar->dims[0] == 1 && matvar->dims[1] == 1) {
  211. matvar = Mat_VarGetStructField(matvar, (void*)token, MAT_BY_NAME, 0);
  212. token = strtok(NULL, ".");
  213. }
  214. free(varNameCopy);
  215. if (matvar == NULL) {
  216. Mat_VarFree(matvarRoot);
  217. (void)Mat_Close(mat);
  218. ModelicaFormatError(
  219. "Matrix \"%s\" not found on file \"%s\".\n", varName, fileName);
  220. return;
  221. }
  222. /* Check if matvar is a matrix */
  223. if (matvar->rank != 2) {
  224. Mat_VarFree(matvarRoot);
  225. (void)Mat_Close(mat);
  226. ModelicaFormatError(
  227. "Array \"%s\" has not the required rank 2.\n", varName);
  228. return;
  229. }
  230. /* Check if matvar is of double precision class (and thus non-sparse) */
  231. if (matvar->class_type != MAT_C_DOUBLE) {
  232. Mat_VarFree(matvarRoot);
  233. (void)Mat_Close(mat);
  234. ModelicaFormatError("2D array \"%s\" has not the required "
  235. "double precision class.\n", varName);
  236. return;
  237. }
  238. /* Check if matvar is purely real-valued */
  239. if (matvar->isComplex) {
  240. Mat_VarFree(matvarRoot);
  241. (void)Mat_Close(mat);
  242. ModelicaFormatError("2D array \"%s\" must not be complex.\n",
  243. varName);
  244. return;
  245. }
  246. nRow = matvar->dims[0];
  247. nCol = matvar->dims[1];
  248. /* Check if number of rows matches */
  249. if (m != nRow) {
  250. Mat_VarFree(matvarRoot);
  251. (void)Mat_Close(mat);
  252. ModelicaFormatError(
  253. "Cannot read %lu rows of array \"%s(%lu,%lu)\" "
  254. "from file \"%s\"\n", (unsigned long)m, varName,
  255. (unsigned long)nRow, (unsigned long)nCol, fileName);
  256. return;
  257. }
  258. /* Check if number of columns matches */
  259. if (n != nCol) {
  260. Mat_VarFree(matvarRoot);
  261. (void)Mat_Close(mat);
  262. ModelicaFormatError(
  263. "Cannot read %lu columns of array \"%s(%lu,%lu)\" "
  264. "from file \"%s\"\n", (unsigned long)n, varName,
  265. (unsigned long)nRow, (unsigned long)nCol, fileName);
  266. return;
  267. }
  268. {
  269. int start[2] = {0, 0};
  270. int stride[2] = {1, 1};
  271. int edge[2];
  272. edge[0] = (int)nRow;
  273. edge[1] = (int)nCol;
  274. tableReadError = Mat_VarReadData(mat, matvar, a, start, stride, edge);
  275. }
  276. Mat_VarFree(matvarRoot);
  277. (void)Mat_Close(mat);
  278. if (tableReadError == 0) {
  279. /* Array is stored column-wise -> need to transpose */
  280. ModelicaIO_transpose(a, nRow, nCol);
  281. }
  282. else {
  283. ModelicaFormatError(
  284. "Error when reading numeric data of matrix \"%s(%lu,%lu)\" "
  285. "from file \"%s\"\n", varName, (unsigned long)nRow,
  286. (unsigned long)nCol, fileName);
  287. return;
  288. }
  289. }
  290. MODELICA_EXPORT int ModelicaIO_writeRealMatrix(const char* fileName,
  291. const char* varName, double* a, size_t m, size_t n, int append, const char* version) {
  292. int status;
  293. mat_t* mat;
  294. matvar_t* matvar;
  295. size_t dims[2];
  296. double* aT;
  297. enum mat_ft matv;
  298. enum matio_compression matc;
  299. if ((0 != strcmp(version, "4")) && (0 != strcmp(version, "6")) && (0 != strcmp(version, "7")) && (0 != strcmp(version, "7.3"))) {
  300. ModelicaFormatError("Invalid version %s for file \"%s\"\n", version, fileName);
  301. return 0;
  302. }
  303. if (0 == strcmp(version, "4")) {
  304. matv = MAT_FT_MAT4;
  305. matc = MAT_COMPRESSION_NONE;
  306. }
  307. else if (0 == strcmp(version, "7.3")) {
  308. matv = MAT_FT_MAT73;
  309. matc = MAT_COMPRESSION_ZLIB;
  310. }
  311. else if (0 == strcmp(version, "7")) {
  312. matv = MAT_FT_MAT5;
  313. matc = MAT_COMPRESSION_ZLIB;
  314. }
  315. else {
  316. matv = MAT_FT_MAT5;
  317. matc = MAT_COMPRESSION_NONE;
  318. }
  319. if ( append == 0 ) {
  320. mat = Mat_CreateVer(fileName, NULL, matv);
  321. if (mat == NULL) {
  322. ModelicaFormatError("Not possible to newly create file \"%s\"\n(maybe version 7.3 not supported)\n", fileName);
  323. return 0;
  324. }
  325. } else {
  326. mat = Mat_Open(fileName, (int)MAT_ACC_RDWR | matv);
  327. if (mat == NULL) {
  328. ModelicaFormatError("Not possible to open file \"%s\"\n", fileName);
  329. return 0;
  330. }
  331. }
  332. /* MAT file array is stored column-wise -> need to transpose */
  333. aT = (double*)malloc(m*n*sizeof(double));
  334. if (aT == NULL) {
  335. (void)Mat_Close(mat);
  336. ModelicaError("Memory allocation error\n");
  337. return 0;
  338. }
  339. memcpy(aT, a, m*n*sizeof(double));
  340. ModelicaIO_transpose(aT, n, m);
  341. if (append != 0) {
  342. (void)Mat_VarDelete(mat, varName);
  343. }
  344. dims[0] = m;
  345. dims[1] = n;
  346. matvar = Mat_VarCreate(varName, MAT_C_DOUBLE, MAT_T_DOUBLE, 2, dims, aT, MAT_F_DONT_COPY_DATA);
  347. status = Mat_VarWrite(mat, matvar, matc);
  348. Mat_VarFree(matvar);
  349. (void)Mat_Close(mat);
  350. free(aT);
  351. if (status != 0) {
  352. ModelicaFormatError("Cannot write variable \"%s\" to \"%s\"\n", varName, fileName);
  353. return 0;
  354. }
  355. return 1;
  356. }
  357. #endif