amat.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. #ifndef amat_h
  2. #define amat_h
  3. /*
  4. * Copyright (C) 1997-2010 Dynasim AB.
  5. * All rights reserved.
  6. *
  7. */
  8. /* To make functions and variabled static to allow several S-function blocks
  9. for real-time targets */
  10. #if !defined(DYMOLA_STATIC)
  11. #if defined(__cplusplus)
  12. #define DYMOLA_STATIC extern
  13. #elif defined(RT) && !defined(DYM2DS)
  14. #define DYMOLA_STATIC static
  15. #else
  16. #define DYMOLA_STATIC
  17. #endif
  18. #endif
  19. #if !defined(NO_FILE)
  20. /* Realtime compilation. DB 1998-10-05 */
  21. #include <stdio.h>
  22. #endif
  23. #include "f2c.h"
  24. #include "amach.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /*
  29. Read/write numeric and/or character matrices from/to file.
  30. The numeric matrices may be stored in real or in double precision.
  31. The data may be stored in ASCII format or in Matlab binary format
  32. on file.
  33. A numeric matrix may be mapped to a desired type when reading
  34. the data (e.g. a matrix stored in double precision may be returned
  35. in real precision; in this case only storage for one real matrix
  36. is allocated).
  37. When numeric matrices are generated row-wise (e.g. during a simulation),
  38. every row of the matrix may be stored on file immediately after it is
  39. available. The row dimension of the matrix needs not to be known
  40. in advance. This feature requires a special opening of the output file
  41. (in this case, ALL matrices are stored row-wise, instead of the usual
  42. column-wise format, when the data is stored in binary format).
  43. Functions for reading matrices:
  44. amatRead : Read one matrix from a file.
  45. amatReadAll : Read all matrices from a file.
  46. amatReadAll2 : Read all matrices from a file
  47. (amatGetOpen must be called before).
  48. amatGetOpen : Open data file and return file identificator "file".
  49. amatGetClose : Close file "file" and release allocated storage.
  50. amatGetError : Inquire which type of error occured.
  51. amatGetDescr : Get description text of file.
  52. amatGetVersion: Get version string of file.
  53. amatGetMatrix : Read next matrix from file "file".
  54. amatGetMatrixP: Read next matrix from file "file". If matrix
  55. is of type "real" or "doublereal" map type
  56. to a desired one.
  57. amatGetHeaderP: Read only matrix header from file "file".
  58. The matrix data is read by a subsequent amatGetMatrix
  59. Functions for writing matrices:
  60. amatWrite : Write one matrix on file (open, put and close).
  61. amatWriteAll : Write all matrices on file.
  62. amatPutOpen : Open data file and return file identificator "file".
  63. amatPutClose : Close file "file" and release allocated storage.
  64. amatPutMatrix : Write matrix on file "file".
  65. amatPutMatrixD: Write matrix on file "file" including a description text
  66. for the matrix and optionally for all of its elements,
  67. if data is stored in ASCII format.
  68. amatPutRinit : Initialize row-wise writing of numeric matrix.
  69. amatPutRclose : Close row-wise writing of numeric matrix.
  70. amatPutRreal : Write one row of a real matrix onto file.
  71. Utility functions:
  72. amatNew : Allocate storage for a new Amatrix object and
  73. initialize it.
  74. amatInit : Initialize an Amatrix object.
  75. amatInitValue : Initialize an Amatrix object with specific values.
  76. amatDel : Delete the storage allocated from an Amatrix object.
  77. amatTextGen : Generate default text.
  78. amatTextResize: Resize text.
  79. amatTextDel : Delete storage of text.
  80. amatTextFind : Find string in text.
  81. amatTextIndex : Build index from two text objects.
  82. Example 1 for reading (most simple usage):
  83. // File has three matrices "mat1, mat2, mat3". "mat1" has fixed
  84. // dimensions, whereas "mat2" has variable dimensions and
  85. // "mat3" must have the same dimensions as "mat2".
  86. const int mat1 = 0;
  87. const int mat2 = 1;
  88. const int mat3 = 2;
  89. static real vec[5*5];
  90. Aclass c[3] = { {{"mat1", 5, 5, realMatrix, vec }, 0, 0, 0},
  91. {{"mat2", 0, 0, realMatrix, NULL}, 0, 0, 0},
  92. {{"mat3", 0, 0, realMatrix, NULL}, 0, 1, 1}
  93. };
  94. // Define, that "mat3" has the same dimensions as "mat2"
  95. c[mat3].rowIndex = mat2;
  96. c[mat3].colIndex = mat2;
  97. // Read the matrices from file
  98. if ( amatReadAll("temp.mat", "TestClass", "1.0", c, 3) != 0 ) {
  99. fprintf("stderr,"Error in program XXX:\n%s",amatError);
  100. exit(1):
  101. }
  102. // Use matrices
  103. // "mat1" = c[mat1].matrix
  104. // "mat2" = c[mat2].matrix
  105. // "mat3" = c[mat3].matrix
  106. Example 2 for reading (usage of basic routines):
  107. AmatGetFile file;
  108. static real vec[5*5];
  109. Amatrix mat1 = { "mat1", 5, 5, realMatrix, vec }
  110. Amatrix mat2;
  111. char *descr;
  112. // Initialize storage of matrix mat2
  113. amatInit (mat2);
  114. // The first matrix must have a specific name, type and dimension
  115. // as defined in the declaration above. No storage will be allocated
  116. // by amatGetMatrix, because the whole storage is already provided by mat1.
  117. // Only data is copied from file into "vec".
  118. // The second matrix must have just a specific name and type.
  119. // The matrix may be optionally empty.
  120. mat2.name = "mat2";
  121. mat2.type = realMatrix;
  122. // Read the two matrices mat1(5,5) and mat2(*,*) from file
  123. if ( amatGetOpen("temp.mat", "TestClass", "1.0", &file ) != 0 ) goto ERROR;
  124. if ( amatGetMatrix(&file, mat1) != 0 ) goto ERROR;
  125. if ( amatGetMatrix(&file, mat2) > 1 ) goto ERROR;
  126. descr= amatGetDescr(&file);
  127. printf(">>> read file temp.mat:\n%s\n", descr);
  128. free(descr);
  129. amatGetClose(&file);
  130. // Use matrices mat1, mat2
  131. ...
  132. // Free allocated storage of matrices (note: mat1.name, mat1.data and mat2.name
  133. // is static storage, whereas mat2.data is dynamic storage)
  134. free(mat2.data);
  135. return 0;
  136. // Error handling (amatError is a global variable, defined in "amat.h")
  137. ERROR: fprintf(stderr,"Error in program XXX:\n%s",amatError);
  138. amatGetClose(&file);
  139. free(mat2.data);
  140. return 1;
  141. Example for writing:
  142. AmatPutFile file;
  143. Amatrix mat1;
  144. const int ncol=10;
  145. real row[ncol];
  146. integer i;
  147. char *descr;
  148. // Build object mat1
  149. ...
  150. // Open file and write object onto file
  151. if ( amatPutOpen("temp.mat", amatBinary, binTrans,
  152. "TestClass", "1.0", "This is a test", &file) != 0) goto ERROR;
  153. if ( amatPutMatrix(&file, mat1) != 0 ) goto ERROR;
  154. // Write matrix row-wise onto file in real precision
  155. if ( amatPutRinit(&file, "mat2", ncol, realMatrix) != 0 ) goto ERROR;
  156. while(1) {
  157. // Compute row i of matrix matr2 and store it in "row".
  158. ...
  159. // Write row to file
  160. if ( amatPutRreal(&file, row) != 0 ) goto ERROR;
  161. // If computation ended, break
  162. ...
  163. }
  164. amatPutRclose(&file);
  165. // Close file
  166. amatPutClose(&file);
  167. return 0;
  168. // Error handling (amatError is pointer to thread-global variable, defined in "amat.h")
  169. ERROR: fprintf(stderr,"Error in program XXX:\n%s",amatError);
  170. amatPutClose(&file);
  171. return 1;
  172. */
  173. /*---------- Error message --------------------------------------------------*/
  174. #define NAMATERROR 1500
  175. #include "libdssetup.h"
  176. LIBDS_API char* amatErrorFunction(void);
  177. #ifdef LIBDS_DLL
  178. #define amatError (amatErrorFunction())
  179. #else
  180. DYMOLA_STATIC char amatError[NAMATERROR];
  181. #endif
  182. /* Pointer to error message of last error */
  183. /* (message contains '\n') */
  184. /* amatError */
  185. /* Note: Without dll it is a plain char */
  186. /* array, but for dll it is a macro */
  187. /* multiple threads have different amatError*/
  188. /*---------- Matrix data structure in program -------------------------------*/
  189. /*
  190. A numeric matrix is stored in column-dense format, i.e.,
  191. the columns of a matrix are stored in consecutive order one after
  192. the other in a vector (= Fortran convention).
  193. A character matrix is stored as a vector of character pointers
  194. which is called "text".
  195. */
  196. typedef enum {
  197. voidMatrix , /* matrix type is not known or not needed */
  198. /* (only used as required type option) */
  199. integerMatrix, /* matrix is of type integer */
  200. realMatrix , /* matrix is of type "real" */
  201. doubleMatrix , /* matrix is of type "doublereal" */
  202. charMatrix, /* vector of character pointers */
  203. realRowMatrix, /* matrix is of type "real" and stored row-wise */
  204. doubleRowMatrix /* matrix is of type "doublereal" and stored row-wise */
  205. } AmatType;
  206. typedef union {
  207. void *v; /* generic pointer */
  208. integer *i; /* pointer to integer matrix */
  209. real *r; /* pointer to real matrix */
  210. doublereal *d; /* pointer to doublereal matrix */
  211. char **c; /* pointer to char matrix */
  212. /* (= vector of character pointers) */
  213. real **rrow; /* pointer to pointers of real */
  214. doublereal **drow; /* pointer to pointers of doublereal */
  215. } AmatData;
  216. typedef struct Amatrix {
  217. char *name; /* matrix name */
  218. integer nrow; /* number of rows (nrow=0 means empty matrix) */
  219. integer ncol; /* number of columns */
  220. /* (if type=charMatrix then */
  221. /* ncol > 0: maximum number of characters */
  222. /* in a string */
  223. /* ncol = 0: maximum number not known */
  224. /* endif) */
  225. AmatType type; /* matrix type */
  226. AmatData data; /* matrix data */
  227. integer nrowallocated;
  228. /* only used for doubleRowMatrix, used to avoid reallocation*/
  229. integer nrowread; /* only used for doubleRowMatrix, and as internal variable for charMatrix */
  230. } Amatrix;
  231. /*---------- File structure -------------------------------------------------*/
  232. typedef enum {
  233. amatASCII , /* ASCII format */
  234. amatBinary /* Matlab binary format */
  235. } AmatFileType;
  236. typedef enum {
  237. binNormal, /* normal storage of matrix */
  238. binTrans /* if binary file format, matrix is stored in */
  239. /* transposed format */
  240. } AmatBinStruct;
  241. /*---------- Matlab matrix file structure -----------------------------------*/
  242. #define MATTYPE (0L)
  243. #ifdef MSDOS_ONLY
  244. # undef MATTYPE
  245. # define MATTYPE (0L)
  246. #endif
  247. #ifdef WIN32_ONLY
  248. # undef MATTYPE
  249. # define MATTYPE (0L)
  250. #endif
  251. #if defined(UNIX) && defined(linux)
  252. # undef MATTYPE
  253. # define MATTYPE (0L)
  254. #endif
  255. #if defined(UNIX) && !defined(linux)
  256. # undef MATTYPE
  257. # define MATTYPE (1000L)
  258. #endif
  259. #ifdef VAXD
  260. # undef MATTYPE
  261. # define MATTYPE (2000L)
  262. #endif
  263. #ifdef VAXG
  264. # undef MATTYPE
  265. # define MATTYPE (3000L)
  266. #endif
  267. typedef struct {
  268. int type; /* type = MOPT
  269. M = 0 for PC's and other machines with Intel byte order,
  270. 1 for Sun's and other machines with Motorola byte order,
  271. 2 for VAX D-float,
  272. 3 for VAX G-float,
  273. 4 for Cray.
  274. O = 0 reserved for future use
  275. P = 0 64 bit floating point numbers
  276. = 1 32 bit floating point numbers
  277. = 2 32 bit signed integers
  278. = 3 16 bit signed integers
  279. = 4 16 bit signed integers
  280. = 5 8-bit unsigned integers
  281. T = 0 numeric matrix
  282. = 1 text matrix
  283. */
  284. int mrows; /* row dimension */
  285. int ncols; /* column dimension */
  286. int imagf; /* flag indicating imag part
  287. = 0 matrix is real,
  288. = 1 matrix is not real.
  289. */
  290. int namlen; /* name length (including NULL) */
  291. } Fmatrix;
  292. /*---------- Functions to read matrices from file -------------------------- */
  293. LIBDS_API int amatRead (const char *fileName, Amatrix *matrix);
  294. /* Read single matrix from a file (there is just one matrix on the file
  295. and now class description)
  296. -> fileName: File name.
  297. -> matrix : All components of matrix must be initialized on input (use amatInit).
  298. Checks are applied in the following way:
  299. matrix.name != NULL : Required name of matrix
  300. matrix.nrow > 0 : Required number of rows.
  301. = 0 : Number of rows is arbitrary (>= 0)
  302. < 0 : Number of rows must be less than or
  303. equal to abs(matrix.nrow).
  304. matrix.ncol > 0 : Required number of columns.
  305. = 0 : Number of columns is arbitrary (>= 0)
  306. < 0 : Number of columns must be less than or
  307. equal to abs(matrix.ncol).
  308. matrix.type != voidMatrix: Required matrix type
  309. matrix.data != NULL : Storage provided for matrix. This requires
  310. that nrow!=0, ncol!=0, type!=voidMatrix.
  311. For type=charMatrix, only storage for a
  312. vector of pointers is provided.
  313. <- matrix : Matrix stored on file.
  314. <- RETURN : = 0 : no error occured.
  315. else: an error occured; the error message is stored
  316. as (static) string in global variable "amatError".
  317. */
  318. typedef struct {
  319. Amatrix *matrix; /* matrix on file */
  320. int req; /* = 0: matrix may or may not be present on file */
  321. /* = 1: matrix must be present on file */
  322. int rowDim; /* = 0: rowDim and rowIndex are ignored */
  323. /* = 1: element rowIndex of Aclass vector must */
  324. /* have the same row dimension. */
  325. /* = 2: element rowIndex of Aclass vector must */
  326. /* have the same column dimension. */
  327. int colDim; /* = 0: colDim and colIndex are ignored */
  328. /* = 1: element colIndex of Aclass vector must */
  329. /* have the same row dimension. */
  330. /* = 2: element colIndex of Aclass vector must */
  331. /* have the same column dimension. */
  332. integer rowIndex; /* Aclass index, if rowDim = 1 or 2 */
  333. integer colIndex; /* Aclass index, if colDim = 1 or 2 */
  334. int onFile; /* = 0: matrix is NOT present on file */
  335. /* = 1: matrix is present on file */
  336. } AclassRead;
  337. LIBDS_API int amatReadAll (const char *fileName, const char *classReq, const char *versionReq,
  338. AclassRead c[], integer dim_c);
  339. /* Read all matrices from a file.
  340. -> fileName : Name of file.
  341. -> classReq : = non-empty string: Required class name.
  342. = NULL : Class name is not checked.
  343. -> versionReq: = non-empty string: Required version number.
  344. = NULL : Version number is not checked.
  345. -> c : c[i].matrix.name != NULL: Required name of matrix
  346. c[i].matrix.nrow > 0 : Required number of rows.
  347. = 0 : Number of rows is arbitrary (>= 0)
  348. < 0 : Number of rows must be less than or
  349. equal to abs(c[i].matrix.nrow).
  350. c[i].matrix.ncol > 0 : Required number of columns.
  351. = 0 : Number of columns is arbitrary (>= 0)
  352. < 0 : Number of columns must be less than or
  353. equal to abs(c[i].matrix.ncol).
  354. c[i].matrix.type != voidMatrix: Required matrix type
  355. c[i].matrix.data != NULL: Storage provided for matrix. This requires
  356. that nrow!=0, ncol!=0, type!=voidMatrix.
  357. For type=charMatrix, only storage for a
  358. vector of pointers is provided.
  359. c[i].rowDim > 0 : Row dimension depends on dimension of
  360. another matrix (see Aclass).
  361. c[i].colDim > 0 : Column dimension depends on dimension of
  362. another matrix (see Aclass).
  363. <- c : The matrix-data read from file
  364. c[i].onFile = 0: c[i] was not stored on file (or dimension was zero)
  365. = 1: c[i] was read from file.
  366. -> dim_c : Dimension of vector c, i.e., c[dim_c]
  367. (= number of matrices to be read from file).
  368. <- RETURN : = 0: no error occured.
  369. > 0: an error occured; the error message is stored
  370. as (static) string in global variable "amatError".
  371. = 1: The first matrix on file is not a class description object.
  372. = 2: Class-name is not equal to required one.
  373. = 3: Version number is not equal to required one.
  374. = 4: EOF reached.
  375. = 5: Other error.
  376. */
  377. typedef struct {
  378. char *name; /* name of file (used in error message) */
  379. FILE *fp; /* stream pointer to file */
  380. AmatFileType format; /* file format (amatASCII/amatBinary) */
  381. AmatBinStruct bstruct; /* required matrix reading format */
  382. Amatrix classInfo; /* class information */
  383. int header; /* 0/1 header not/already read */
  384. int prec; /* precision flag of Matlab matrix */
  385. Amatrix matrix; /* auxiliary storage for last matrix */
  386. } AmatGetFile;
  387. LIBDS_API int amatGetOpen (const char *fileName, const char *classReq, const char *versionReq, AmatGetFile *file);
  388. /* Open file for reading of matrices
  389. -> fileName : Name of file.
  390. -> classReq : = non-empty string: Required name of class.
  391. = "NoClass" : No class description is stored on file,
  392. just open file for further reading.
  393. = NULL : Class name is not checked.
  394. -> versionReq: = non-empty string: Required version number.
  395. = NULL : Version number is not checked.
  396. <- file : File identificator. Storage for the struct has to
  397. be provided in the calling routine.
  398. <- RETURN : = 0: no error occured.
  399. > 0: an error occured; the error message is stored
  400. as (static) string in global variable "amatError".
  401. = 1: The first matrix on file is not a class description object.
  402. = 2: Class-name is not equal to required one.
  403. = 3: Version number is not equal to required one.
  404. = 4: EOF reached.
  405. = 5: Other error.
  406. */
  407. LIBDS_API int amatReadAll2 (AmatGetFile *file, AclassRead c[], integer dim_c);
  408. /* Read all matrices from file defined by amatGetOpen and close file
  409. afterwards by a call to amatGetClose. This allows opening with
  410. amatGetOpen and check of class- or/and version number.
  411. -> file : file identificator from amatGetOpen.
  412. -> c : c[i].matrix.name != NULL: Required name of matrix
  413. c[i].matrix.nrow > 0 : Required number of rows.
  414. = 0 : Number of rows is arbitrary (>= 0)
  415. < 0 : Number of rows must be less than or
  416. equal to abs(c[i].matrix.nrow).
  417. c[i].matrix.ncol > 0 : Required number of columns.
  418. = 0 : Number of columns is arbitrary (>= 0)
  419. < 0 : Number of columns must be less than or
  420. equal to abs(c[i].matrix.ncol).
  421. c[i].matrix.type != voidMatrix: Required matrix type
  422. c[i].matrix.data != NULL: Storage provided for matrix. This requires
  423. that nrow!=0, ncol!=0, type!=voidMatrix.
  424. For type=charMatrix, only storage for a
  425. vector of pointers is provided.
  426. c[i].rowDim > 0 : Row dimension depends on dimension of
  427. another matrix (see Aclass).
  428. c[i].colDim > 0 : Column dimension depends on dimension of
  429. another matrix (see Aclass).
  430. <- c : The matrix-data read from file
  431. c[i].onFile = 0: c[i] was not stored on file (or dimension was zero).
  432. = 1: c[i] was read from file.
  433. -> dim_c : Dimension of vector c, i.e., c[dim_c]
  434. (= number of matrices to be read from file).
  435. <- RETURN : = 0: no error occured.
  436. > 0: an error occured; the error message is stored
  437. as (static) string in global variable "amatError".
  438. = 1: The first matrix on file is not a class description object.
  439. = 2: Class-name is not equal to required one.
  440. = 3: Version number is not equal to required one.
  441. = 4: EOF reached.
  442. = 5: Other error.
  443. */
  444. LIBDS_API void amatGetClose (AmatGetFile *file);
  445. /* Close file and release allocated storage
  446. -> file: File identificator.
  447. */
  448. LIBDS_API char *amatGetClass (AmatGetFile *file);
  449. /* Get class name stored on file (the string is only
  450. returned for the first call. For all subsequent calls on the same
  451. file, "" is returned). The storage of the string must be freed
  452. with "free", when no longer needed.
  453. */
  454. LIBDS_API char *amatGetDescr (AmatGetFile *file);
  455. /* Get description string stored on file (the string is only
  456. returned for the first call. For all subsequent calls on the same
  457. file, "" is returned). The storage of the string must be freed
  458. with "free", when no longer needed.
  459. */
  460. LIBDS_API char *amatGetVersion (AmatGetFile *file);
  461. /* Get version string stored on file (the string is only
  462. returned for the first call. For all subsequent calls on the same
  463. file, "" is returned). The storage of the string must be freed
  464. with "free", when no longer needed.
  465. */
  466. LIBDS_API int amatGetMatrix (AmatGetFile *file, Amatrix *matrix);
  467. LIBDS_API int amatGetMatrixP (AmatGetFile *file, Amatrix *matrix, AmatType typeReq);
  468. LIBDS_API int amatGetMatrixP2 (AmatGetFile *file, Amatrix *matrixReq, AmatType typeReq, const fpos_t*dataCont);
  469. LIBDS_API int amatGetMatrixP3 (AmatGetFile *file, Amatrix *matrixReq, AmatType typeReq, const fpos_t*dataCont,int removeTrailingSpace);
  470. /* Read next matrix from file.
  471. -> file : File identificator.
  472. -> matrix : All components of matrix must be initialized on input (use amatInit).
  473. Checks are applied in the following way:
  474. matrix.name != NULL : Required name of matrix
  475. (matrix.name will not be changed).
  476. matrix.nrow != 0 : Required number of rows
  477. (matrix.nrow will not be changed).
  478. matrix.ncol != 0 : Required number of columns
  479. (matrix.ncol will not be changed).
  480. matrix.type != voidMatrix: Required matrix type
  481. (matrix.type will not be changed).
  482. matrix.data != NULL : Storage provided for matrix. This requires
  483. that nrow!=0, ncol!=0, type!=voidMatrix.
  484. For type=charMatrix, only storage for a
  485. vector of pointers is provided.
  486. <- matrix : Next matrix from file.
  487. -> typeReq: If matrix.type == voidMatrix, read a "real" or "doublereal"
  488. matrix in precision typeReq. Otherwise ignore this argument.
  489. (typeReq = realMatrix/doubleMatrix).
  490. <- RETURN : = 0: No error occured.
  491. > 0: An error occured. An error message is stored
  492. as (static) string in global variable "amatError".
  493. = 1: The matrix on file has zero dimensions (nrow*ncol=0)
  494. (required matrix name and type already have been checked).
  495. = 2: The matrix on file does not have the required attributes.
  496. = 3: End-of-File reached (file was already at EOF when calling this
  497. function).
  498. = 4: Another error occured.
  499. */
  500. LIBDS_API char *amatGetName (AmatGetFile *file, int *eof);
  501. /* Read name of next matrix from file. By a subsequent call of amatGetMatrix or
  502. amatGetMatrixP the rest of the next matrix is read. Hereby it is assumed
  503. that matrix.name points to the name given by amatGetName.
  504. -> file : File identificator.
  505. <- eof : = 0: No END-OF-FILE encountered.
  506. = 1: END-OF-FILE encountered (file was already positioned at EOF
  507. when calling this function).
  508. <- RETURN: Pointer to string containing the name of the next matrix.
  509. The storage for the string is allocated in amatGetName.
  510. An error occured when a NULL pointer is returned. An error message
  511. is stored as (static) string in global variable "amatError".
  512. */
  513. /*---------- Functions to write matrices to file --------------------------- */
  514. LIBDS_API int amatWrite (char *fileName, AmatFileType ftype, Amatrix matrix);
  515. /* Write matrix on file
  516. -> fileName: Name of file.
  517. -> ftype : Storage type on file (amatASCII or amatBinary).
  518. -> matrix : Matrix to be stored on file.
  519. <- RETURN : = 0 : no error occurs.
  520. else: an error occured; the error message is stored
  521. as (static) string in global variable "amatError".
  522. */
  523. typedef struct {
  524. Amatrix *matrix; /* matrix to be written to file */
  525. int nblank; /* Number of leading blanks (including comment sign '#') */
  526. /* used for elemDescr. If nblank=0, no blanks and no */
  527. /* comment sign is written (the comment sign has to be */
  528. /* present in elemDescr). */
  529. const char *const*elemDescr; /* Vector of strings. Every string is written as comment */
  530. /* after the corresponding element. Row dimension of */
  531. /* elemDescr = matrix.nrow. */
  532. /* If elemDescr=NULL, no text is written. */
  533. const char *matDescr; /* Text written as comment before declaration of matrix */
  534. } AclassWrite;
  535. LIBDS_API int amatWriteAll (const char *fileName, AmatFileType ftype, char *className,
  536. char *version, char *descr, AclassWrite c[], integer dim_c);
  537. /* Write all matrices on file.
  538. -> fileName : Name of file.
  539. -> ftype : Storage type on file (amatASCII or amatBinary).
  540. -> className: Class name. If className="", no class description is stored
  541. on file.
  542. -> version : Version number.
  543. -> descr : Description text.
  544. -> c : Matrices "c[i].matrix" are written on file.
  545. -> dim_c : Dimension of vector c, i.e., c[dim_c]
  546. (= number of matrices to be written to file).
  547. <- RETURN : = 0 : no error occurs.
  548. else: an error occured; the error message is stored
  549. as (static) string in global variable "amatError".
  550. */
  551. typedef struct {
  552. /* Defined by amatPutOpen (always needed) */
  553. char *name; /* Name of file (used in error message) */
  554. FILE *fp; /* Pointer to stream */
  555. AmatFileType format; /* Desired file format */
  556. AmatBinStruct bstruct; /* Required matrix writing format */
  557. int noRow; /* = 0/1 not/allowed to used amatPutRxxx */
  558. /* Defined by amatPutRinit (row-wise writing of matrix) */
  559. Fmatrix x; /* Info about matrix needed from Matlab */
  560. char *matName; /* Name of matrix (used in error message) */
  561. fpos_t posHead; /* Position in stream, where header is placed */
  562. AmatType type; /* Matrix type */
  563. integer ncol; /* Number of output columns */
  564. integer nrowAct; /* Actual number of rows written to file */
  565. } AmatPutFile;
  566. LIBDS_API int amatPutOpen (const char *fileName, AmatFileType ftype, AmatBinStruct bstruct,
  567. char *className, char *version, char *descr, AmatPutFile *file);
  568. /* Open file for writing of matrices (amatPutOpen opens the file and writes
  569. the text matrix "Aclass" containing the classname, version-number and
  570. description text)
  571. -> fileName: Name of file to be opened. An existing file is destroyed.
  572. If fileName="", output is done on standard output
  573. (in this case functions amatputRxxx for row-wise writing
  574. of matrices cannot be used).
  575. -> ftype : Storage type on file (amatASCII or amatBinary).
  576. -> bstruct : = binNormal: Normal storage of matrices.
  577. = binTrans : Matrices are stored in transposed format.
  578. This is a prerequisite in order to use amatPutRxxx
  579. in binary storage mode.
  580. -> className: Class name. If className="", no class description is stored
  581. on file.
  582. -> version : Version number.
  583. -> descr : Description text.
  584. <- file : File identificator.
  585. <- RETURN : = 0 : no error occurs.
  586. else: an error occured; the error message is stored
  587. as (static) string in global variable "amatError".
  588. */
  589. LIBDS_API void amatPutClose (AmatPutFile *file);
  590. /* Close file and free allocated storage */
  591. LIBDS_API void amatPutConnect(FILE *fp, char *fileName, AmatPutFile *file);
  592. /* Connect an open ASCII-stream to an AmatPutFile structure.
  593. After connecting, the other functions of this package can be
  594. used via "file". It is not allowed to use "amatPutClose".
  595. -> fp : Open ASCII output stream.
  596. -> fileName: Name of output file.
  597. <- file : Initialized matPutFile structure
  598. (fp and fileName are referenced within file but not copied)
  599. */
  600. LIBDS_API int amatPutMatrix (AmatPutFile *file, Amatrix matrix);
  601. /* Write matrix to file
  602. -> file : File identificator
  603. -> matrix: Matrix to be written to file
  604. <- RETURN : = 0 : no error occurs.
  605. else: an error occured; the error message is stored
  606. as (static) string in global variable "amatError".
  607. */
  608. LIBDS_API int amatPutMatrixPadding(AmatPutFile *file, Amatrix matrix,char padForText);
  609. /* Write matrix to file
  610. -> file : File identificator
  611. -> matrix: Matrix to be written to file
  612. -> padForText: In binary mode for text: use this character for padding.
  613. <- RETURN : = 0 : no error occurs.
  614. else: an error occured; the error message is stored
  615. as (static) string in global variable "amatError".
  616. */
  617. LIBDS_API int amatPutMatrixD (AmatPutFile *file, Amatrix matrix, const char *matDescr,
  618. const char *const elemDescr[], int nblank);
  619. /* Write matrix to file. If data is written in ASCII format, include
  620. a verbal description of the matrix and of its elements. The element description
  621. texts (elemDescr) are ignored for character matrices (matrix.type = charMatrix).
  622. -> file : File identificator
  623. -> matrix : Matrix to be written to file
  624. -> matDescr : Text written as comment before declaration of matrix
  625. (if matDescr=NULL or matDescr="\0", no comment is written).
  626. -> elemDescr: Vector of strings. Every string is written as comment after the
  627. corresponding element. Row dimension of elemDescr = matrix.nrow.
  628. If elemDescr=NULL, no text is written.
  629. -> nblank : Number of leading blanks (including comment sign '#') used
  630. for elemDescr. If nblank=0, no blanks and no comment sign is
  631. written (the comment sign has to be present in elemDescr).
  632. <- RETURN : = 0 : no error occurs.
  633. else: an error occured; the error message is stored
  634. as (static) string in global variable "amatError".
  635. */
  636. LIBDS_API int amatPutRinit (AmatPutFile *file, const char *name, integer ncol, AmatType type);
  637. /* Initialize writing of numeric matrix row-wise to file
  638. -> file : File identificator.
  639. -> name : Name of matrix.
  640. -> ncol : Column-dimension of matrix.
  641. -> type : Type of matrix on file (realMatrix, doubleMatrix).
  642. <- RETURN: = 0 : no error occurs.
  643. else: an error occured; the error message is stored
  644. as (static) string in global variable "amatError".
  645. */
  646. LIBDS_API int amatPutRclose (AmatPutFile *file);
  647. /* Close row-wise writing of matrix
  648. -> file : File identificator.
  649. <- RETURN: = 0 : no error occurs.
  650. else: an error occured; the error message is stored
  651. as (static) string in global variable "amatError".
  652. */
  653. LIBDS_API int amatPutRreal (AmatPutFile *file, const real row[]);
  654. LIBDS_API int amatPutRdouble (AmatPutFile *file, const doublereal row[]);
  655. /* Write row to file
  656. -> file : File identificator.
  657. -> row : Row to be written to file. The dimension of the vector was
  658. defined by argument ncol of amatPutRinit.
  659. <- RETURN: = 0 : no error occurs.
  660. else: an error occured; the error message is stored
  661. as (static) string in global variable "amatError".
  662. */
  663. /*---------- Utility functions --------------------------------------------- */
  664. DYMOLA_STATIC LIBDS_API_AFTER Amatrix *amatNew(void);
  665. /* Generate a new Amatrix object */
  666. DYMOLA_STATIC LIBDS_API_AFTER void amatInit(Amatrix *mat);
  667. /* Initialize an Amatrix object */
  668. DYMOLA_STATIC LIBDS_API_AFTER void amatInitValue(char *name, integer nrow, integer ncol,
  669. AmatType type, AmatData data, Amatrix *mat);
  670. /* Initialize an Amatrix object with the specified values.
  671. -> name: stored in mat.name.
  672. -> nrow: stored in mat.nrow.
  673. -> ncol: stored in mat.ncol.
  674. -> type: stored in mat.type.
  675. -> data: stored in mat.data
  676. <- mat : initialized object
  677. */
  678. DYMOLA_STATIC LIBDS_API_AFTER void amatDel (Amatrix *mat);
  679. /* Delete storage of the components of matrix mat */
  680. DYMOLA_STATIC LIBDS_API_AFTER char **amatTextGen (char *name, integer nrow);
  681. /* Generate default text
  682. -> name : Name used to build strings.
  683. -> nrow : Required number of rows of text.
  684. <- RETURN: Vector of string pointers.
  685. Element i of text gets the value "name<i+1>".
  686. E.g. if name="col_", text[3]="col_4".
  687. */
  688. DYMOLA_STATIC LIBDS_API_AFTER char **amatTextResize (char *text[], integer nrowIn, integer nrowOut,
  689. char *name);
  690. /* Resize text
  691. -> text : Text to be resized.
  692. -> nrowIn : Row dimension of text on entry.
  693. -> nrowOut: Desired row dimension of text on return;
  694. -> name : Name used to build default names of new elements
  695. (if text vector is enlarged).
  696. <- RETURN : Pointer to resized vector.
  697. */
  698. DYMOLA_STATIC LIBDS_API_AFTER void amatTextDel (char *text[], integer nrow);
  699. /* Delete text
  700. -> text: Pointer to vector of string pointers.
  701. On return, the storage for the vector and for all of the
  702. strings is freed.
  703. -> nrow: Row dimension of vector.
  704. */
  705. DYMOLA_STATIC LIBDS_API_AFTER int amatTextFind (Amatrix tmat, const char *str);
  706. /* Find C-index of string "str" within text field of "tmat".
  707. -> tmat : Textmatrix.
  708. -> str : String to be searched.
  709. <- RETURN: Returns index i >= 0, if tmat.data.c[i] = str, or
  710. i=-1 if "str" is not contained in tmat.
  711. */
  712. DYMOLA_STATIC LIBDS_API_AFTER integer *amatTextIndex(Amatrix tmat1, Amatrix tmat2, int message);
  713. /* Build index from two text objects.
  714. -> tmat1 : Reference text object
  715. (row dimension >= row dimension of tmat2).
  716. -> tmat2 : Text object which contains a subset of the names stored
  717. in "tmat1".
  718. -> message: = 0: if name tmat2[i] is not present in tmat1, set vec[i]=-1.
  719. = 1: if name tmat2[i] is not present in tmat1 store an
  720. error message in amatError and return NULL.
  721. <- RETURN : Pointer to integer vector (named "vec" in the following)
  722. with the same row dimension as tmat2.
  723. vec[i] is the row number of "tmat1" which has the
  724. same name as "tmat2" at row "i".
  725. If an error occurred, NULL is returned.
  726. */
  727. #ifdef __cplusplus
  728. }
  729. #endif
  730. #endif