matutil.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /* MatUtil.c
  2. Utility routines for matrix handling in Dymola.
  3. Author: Hilding Elmqvist, Dynasim AB, 1995-10-01
  4. Version: 1.6, 1998-06-08
  5. 1.7, 1998-10-05: modified for realtime
  6. 1.8, 1998-12-01: allowed several files for matrices
  7. 1.9, 1999-02-01: fixed test of value returned by SearchMatrix (HO)
  8. */
  9. /*
  10. * Copyright (C) 1995-2001 Dynasim AB.
  11. * All rights reserved.
  12. *
  13. */
  14. /* DLL-status: This file is has static data which is sometimes included with
  15. the model. In addition it does not deallocate files */
  16. #include <math.h>
  17. /* #include <malloc.h> */
  18. #if !defined(NO_FILE)
  19. #include <stdio.h>
  20. #endif
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "dymutil.h"
  24. #include "matutil.h"
  25. #include "amat.h"
  26. typedef Amatrix* AmatrixP;
  27. #define MaxMatrices 1000
  28. static AmatrixP* Matrices;
  29. static char * MatrixFiles[MaxMatrices];
  30. static int nMatrices;
  31. typedef struct table {
  32. int Used;
  33. Amatrix *dim, *grid, *table;
  34. } table;
  35. static table Tables[MaxMatrices];
  36. #ifndef NO_FILE
  37. static int ReadMatrices (char *fileName) {
  38. /* Read matrices from a file. */
  39. AmatGetFile file;
  40. int status;
  41. char mess[1000];
  42. if ( amatGetOpen(fileName, "NoClass", "", &file ) != 0 ) return 1;
  43. while (1) {
  44. Matrices[nMatrices] = amatNew();
  45. if (Matrices[nMatrices] == 0) {
  46. DymosimError("Simulator could not allocate space for matrix.");
  47. return 1;
  48. }
  49. status = amatGetMatrix(&file, Matrices[nMatrices]);
  50. if (status == 3)
  51. break;
  52. if (status > 0) {
  53. sprintf(mess, "Error reading matrix:\n%s\n", amatError);
  54. DymosimError(mess);
  55. return 1;
  56. }
  57. MatrixFiles[nMatrices] = malloc(strlen(fileName)+1);
  58. strcpy(MatrixFiles[nMatrices], fileName);
  59. nMatrices = nMatrices + 1;
  60. if (nMatrices >= MaxMatrices) {
  61. DymosimError("Simulator is not able to read all matrices from data file.");
  62. amatGetClose(&file);
  63. return 2;
  64. }
  65. }
  66. amatGetClose(&file);
  67. return 0;
  68. }
  69. static void InitMatrices() {
  70. static int INIT=1;
  71. int i;
  72. if (INIT) {
  73. Matrices = (AmatrixP *) malloc(sizeof(AmatrixP) * MaxMatrices);
  74. INIT = 0;
  75. for (i=0;i<MaxMatrices;i++) {
  76. Tables[i].Used = 0;
  77. }
  78. nMatrices = 0;
  79. }
  80. }
  81. /*
  82. static void FreeMatrices() {
  83. int i;
  84. for (i=0;i<nMatrices;i++) {
  85. free(Matrices[i]->data.d);
  86. }
  87. free(Matrices);
  88. for (i=0;i<nMatrices;i++) {
  89. if(Tables[i].Used) {
  90. free(Tables[i].dim->data.d);
  91. free(Tables[i].grid->data.d);
  92. free(Tables[i].table->data.d);
  93. free(Tables[i].dim);
  94. free(Tables[i].grid);
  95. free(Tables[i].table);
  96. }
  97. }
  98. }
  99. */
  100. static int SearchMatrix(char *fileName, char *MatrixName,
  101. AmatType type, Amatrix **matrix) {
  102. int i;
  103. char mess[1000];
  104. for (i=0;i<nMatrices;i++) {
  105. if (strcmp(Matrices[i]->name, MatrixName) == 0 &&
  106. strcmp(MatrixFiles[i], fileName) == 0) {
  107. *matrix = Matrices[i];
  108. if (Matrices[i]->type != type) {
  109. sprintf(mess, "Matrix %s has wrong type in file %s.", MatrixName, fileName);
  110. DymosimError(mess);
  111. return -1;
  112. }
  113. return i;
  114. }
  115. }
  116. return -2;
  117. }
  118. static int LocateMatrix(char *MatrixName, AmatType type, Amatrix **matrix) {
  119. int status;
  120. char filename[300];
  121. char * name;
  122. char * delim;
  123. char mess[1000];
  124. /* Split name into file name and matrix name. */
  125. strcpy(filename, MatrixName);
  126. delim = strrchr(filename, '/');
  127. if (delim == 0) {
  128. name = MatrixName;
  129. strcpy(filename, "dsdata.mat");
  130. } else {
  131. * delim = '\0';
  132. name = delim + 1;
  133. }
  134. status = SearchMatrix(filename, name, type, matrix);
  135. if (status >= 0) return status;
  136. if (ReadMatrices(filename) == 1) {
  137. sprintf(mess, "Simulator could not find the data file %s.", filename);
  138. DymosimError(mess);
  139. return -1;
  140. }
  141. status = SearchMatrix(filename, name, type, matrix);
  142. if (status >= 0) return status;
  143. sprintf(mess, "Simulator could not find matrix %s in file %s.", name, filename);
  144. DymosimError(mess);
  145. return -1;
  146. }
  147. static AmatrixP CreateDoubleMatrix(int nx, int ny) {
  148. AmatrixP m;
  149. m = amatNew();
  150. if (m == 0) {
  151. DymosimError("Simulator is unfortunately out of memory.");
  152. return 0;
  153. }
  154. m->nrow = nx;
  155. m->ncol = ny;
  156. m->data.d = malloc(nx*ny*sizeof(double));
  157. if (m->data.d == 0) {
  158. DymosimError("Simulator is unfortunately out of memory.");
  159. return 0;
  160. }
  161. return m;
  162. }
  163. double ReadMatrix(char name[], double M[], int n1, int n2) {
  164. /* Reads an external matrix named in name into matrix M. */
  165. Amatrix * mat=0;
  166. int i, j, matind;
  167. char mess[1000];
  168. InitMatrices();
  169. matind = LocateMatrix(name, doubleMatrix, &mat);
  170. if (matind < 0 || !mat) {
  171. return 0.0;
  172. }
  173. if (mat->nrow != n1 || mat->ncol != n2) {
  174. sprintf(mess, "Incompatible dimension of matrix: %s\n", mat->name);
  175. return DymosimError(mess);
  176. }
  177. for (i = 0; i<n1; i++)
  178. for (j = 0; j<n2; j++)
  179. M[i*n2 + j] = mat->data.d[j*n1 + i];
  180. free(mat->data.d);
  181. free(mat);
  182. return 1.0;
  183. }
  184. double WriteMatrix(char name[], double M[], int n1, int n2) {
  185. /* Writes matrix M into an matrix named name in file named name.mat. */
  186. Amatrix mat;
  187. int i, j, status;
  188. char filename[80];
  189. char mess[1000];
  190. amatInit(&mat);
  191. mat.name = malloc(strlen(name)+1);
  192. strcpy(mat.name, name);
  193. mat.type = doubleMatrix;
  194. mat.nrow = n1;
  195. mat.ncol = n2;
  196. mat.data.d = (doublereal *) malloc( n1*n2*sizeof(doublereal) );
  197. if ( mat.data.d == NULL ) {
  198. sprintf(mess, "Simulator is unfortunately out of memory.\n");
  199. return DymosimError(mess);
  200. }
  201. for (i = 0; i<n1; i++)
  202. for (j = 0; j<n2; j++)
  203. mat.data.d[i*n2 + j] = M[j*n1 + i];
  204. strcpy(filename, mat.name);
  205. strcat(filename, ".mat");
  206. status = amatWrite(filename, amatBinary, mat);
  207. if (status != 0) {
  208. sprintf(mess, "Error writing matrix: %s\n%s\n", mat.name, amatError);
  209. return DymosimError(mess);
  210. }
  211. free(mat.data.d);
  212. return 1.0;
  213. }
  214. #else
  215. DYMOLA_STATIC double ReadMatrix(char name[], double M[], int n1, int n2) {
  216. return 0.0;
  217. }
  218. DYMOLA_STATIC double WriteMatrix(char name[], double M[], int n1, int n2) {
  219. return 0.0;
  220. }
  221. #endif
  222. DYMOLA_STATIC int SearchGrid(double x, const double xval[], int Mxval) {
  223. /* Calculates index in xval according to:
  224. below = 0;
  225. while (x > xval[below+1])
  226. below = below + 1;
  227. return below;
  228. A more efficient search is done by calculating a better start
  229. value based on the assumption of approximately equidistant
  230. grid.
  231. */
  232. int below;
  233. double xlow, xhigh;
  234. xlow = xval[0];
  235. xhigh = xval[Mxval-1];
  236. /* Calculate start approximation for below. */
  237. below = (int) ( (double)(Mxval-2) * (x-xlow) / (xhigh-xlow) );
  238. /* Check bounds to take care of roundoff errors. */
  239. if (below < 0)
  240. below = 0;
  241. else if (below > Mxval-2)
  242. below = Mxval-2; /* Not the last value. */
  243. /* Modify below if too low. */
  244. while (x > xval[below+1])
  245. below = below + 1;
  246. /* Modify below if too high. */
  247. while (x < xval[below])
  248. below = below - 1;
  249. return below;
  250. }
  251. DYMOLA_STATIC double Interpolate1(double x, const double xval[], int Mxval, int Nxval,
  252. const double yval[], int Myval, int Nyval) {
  253. /* Linear interpolation in one dimension: y=f(x).
  254. F is defined by points (xval[i], yval[i]). */
  255. int below;
  256. int Kxval, Kyval;
  257. if ((Mxval != 1 && Nxval != 1) || (Myval != 1 && Nyval != 1)) {
  258. return DymosimError("Arguments to Interpolate1 must be vectors.");
  259. }
  260. Kxval = Mxval > 1 ? Mxval : Nxval;
  261. Kyval = Myval > 1 ? Myval : Nyval;
  262. if (Kxval != Kyval) {
  263. return DymosimError("The x and y vectors must have the same length in Interpolate1.");
  264. }
  265. else if (x <= xval[0])
  266. return (yval[0]);
  267. else if (x >= xval[Kxval-1])
  268. return (yval[Kxval-1]);
  269. else {
  270. below = SearchGrid(x, xval, Kxval);
  271. return ( yval[below] + (yval[below+1]-yval[below]) * (x-xval[below]) /
  272. (xval[below+1]-xval[below]));
  273. }
  274. }
  275. DYMOLA_STATIC void Interpolate1DerS(double*res,double*res_d,double x, const double xval[], int Mxval, int Nxval,
  276. const double yval[], int Myval, int Nyval,double dx) {
  277. /* Linear interpolation in one dimension: y=f(x).
  278. F is defined by points (xval[i], yval[i]). */
  279. int below;
  280. int Kxval, Kyval;
  281. *res=0;
  282. *res_d=0;
  283. if ((Mxval != 1 && Nxval != 1) || (Myval != 1 && Nyval != 1)) {
  284. DymosimError("Arguments to Interpolate1 must be vectors.");
  285. return;
  286. }
  287. Kxval = Mxval > 1 ? Mxval : Nxval;
  288. Kyval = Myval > 1 ? Myval : Nyval;
  289. if (Kxval != Kyval) {
  290. DymosimError("The x and y vectors must have the same length in Interpolate1.");
  291. return;
  292. }
  293. else if (x <= xval[0]) {
  294. *res=(yval[0]);
  295. *res_d=0;
  296. } else if (x >= xval[Kxval-1]) {
  297. *res=(yval[Kxval-1]);
  298. *res_d=0;
  299. } else {
  300. below = SearchGrid(x, xval, Kxval);
  301. *res=( yval[below] + (yval[below+1]-yval[below]) * (x-xval[below]) /
  302. (xval[below+1]-xval[below]));
  303. *res_d=((yval[below+1]-yval[below])*dx/(xval[below+1]-xval[below]));
  304. }
  305. }
  306. DYMOLA_STATIC double Interpolate(const double dim[], int Mdim, int Ndim,
  307. const double xval[], int Mxval, int Nxval,
  308. const double yval[], int Myval, int Nyval,
  309. const double x[], int Mx, int Nx) {
  310. /* Linear interpolation in n dimensions: y=f(x1, x2, ..., xn).
  311. The dim vector gives the number of grid points for each argument.
  312. The grid points are stored in xval:
  313. [x1[1], x1[2], ... x1[dim[1]],
  314. [x2[1], x2[2], ... x2[dim[2]],
  315. [xn[1], xn[2], ... xn[dim[n]].
  316. The corresponding function values are stored in yval:
  317. f[1,1, ..., 1], f[1,1, ..., 2], ..., f[1,1, ..., dim[n]],
  318. ...
  319. f[1,2, ..., 1], f[1,2, ..., 2], ..., f[1,2, ..., dim[n]],
  320. f[dim[1],dim[2], ..., 1], f[dim[1],dim[2], ..., 2], ..., f[dim[1],dim[2], ..., dim[n]]
  321. Interpolate can be used as follows in Modelica:
  322. model m
  323. Real x[3, 1]=Modelica.Math.sin([3*time; 5*time; 7*time]) "Input";
  324. parameter Real dim[3, 1]=[3; 2; 2] "Sizes for the dimensions";
  325. parameter Real xval[3 + 2 + 2, 1]=[-1;0;1; -1;1; -1;1]
  326. "Grid values in the dimensions";
  327. parameter Real yval[3*2*2, 1]=[1;2; 3;4; 11;12; 13;14; 15;16; 17;18]
  328. "Table values";
  329. output Real y;
  330. equation
  331. y = Interpolate(dim, xval, yval, x);
  332. end m;
  333. The interpolation is done recursively:
  334. - search among grid points for this argument
  335. - interpolate the remaining arguments for grid points above and below
  336. - interpolate in result from the two interpolations
  337. */
  338. int below;
  339. double ybelow, yabove;
  340. int i, dimsum, dimprod;
  341. int Kx, Kdim, Kxval, Kyval;
  342. if ((Mx != 1 && Nx != 1) || (Mdim != 1 && Ndim != 1)) {
  343. return DymosimError("Arguments to Interpolate must be vectors.");
  344. }
  345. Kx = Mx > 1 ? Mx : Nx;
  346. Kdim = Mdim > 1 ? Mdim : Ndim;
  347. Kxval = Mxval * Nxval;
  348. Kyval = Myval * Nyval;
  349. if (Kx != Kdim) {
  350. return DymosimError("The size of the argument vector is not the same as\n"
  351. " the dimension vector of a table.");
  352. }
  353. /* Calculate the size of a table for the rest of the arguments. */
  354. dimsum = 0;
  355. dimprod = 1;
  356. for (i=0;i<Kdim;i++) {
  357. if (dim[i] != floor(dim[i])) {
  358. return DymosimError("The dimension vector of a table must only contain "
  359. "integers.");
  360. }
  361. dimsum = dimsum + (int) dim[i];
  362. dimprod = dimprod * (int) dim[i];
  363. }
  364. if (Kxval != dimsum) {
  365. return DymosimError("The size of the grid vector is not consistent with\n"
  366. " information in the dimension vector of a table.");
  367. }
  368. if (Kyval != dimprod) {
  369. return DymosimError("The size of the table value vector is not\n"
  370. " consistent with information in the dimension vector.");
  371. }
  372. if (Kdim <= 1)
  373. return Interpolate1(x[0], xval, Kxval, 1, yval, Kyval, 1);
  374. else if (x[0] <= xval[0]) {
  375. below = 0; /* Use lowest data set. */
  376. return Interpolate(&dim[1], Kdim-1, 1,
  377. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  378. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  379. &x[1], Kx-1, 1);
  380. }
  381. else if (x[0] >= xval[(int)dim[0]-1]) {
  382. below = (int)dim[0]-1;
  383. return Interpolate(&dim[1], Kdim-1, 1,
  384. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  385. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  386. &x[1], Kx-1, 1);
  387. }
  388. else {
  389. below = SearchGrid(x[0], xval, (int)dim[0]);
  390. /* Interpolate the rest of the arguments for the grid points below and above. */
  391. ybelow = Interpolate(&dim[1], Kdim-1, 1,
  392. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  393. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  394. &x[1], Kx-1, 1);
  395. yabove = Interpolate(&dim[1], Kdim-1, 1,
  396. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  397. &yval[(below+1)*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  398. &x[1], Kx-1, 1);
  399. /* Interpolate this argument. */
  400. return ( ybelow + (yabove-ybelow) * (x[0]-xval[below]) /
  401. (xval[below+1]-xval[below]));
  402. }
  403. }
  404. DYMOLA_STATIC void InterpolateDerS(double*res,double*d_res,const double dim[], int Mdim, int Ndim,
  405. const double xval[], int Mxval, int Nxval,
  406. const double yval[], int Myval, int Nyval,
  407. const double x[], int Mx, int Nx, const double dx[]) {
  408. /* Derivative of linear interpolation in n dimensions: y=f(x1, x2, ..., xn).
  409. The dim vector gives the number of grid points for each argument.
  410. The grid points are stored in xval:
  411. [x1[1], x1[2], ... x1[dim[1]],
  412. [x2[1], x2[2], ... x2[dim[2]],
  413. [xn[1], xn[2], ... xn[dim[n]].
  414. The corresponding function values are stored in yval:
  415. f[1,1, ..., 1], f[1,1, ..., 2], ..., f[1,1, ..., dim[n]],
  416. ...
  417. f[1,2, ..., 1], f[1,2, ..., 2], ..., f[1,2, ..., dim[n]],
  418. f[dim[1],dim[2], ..., 1], f[dim[1],dim[2], ..., 2], ..., f[dim[1],dim[2], ..., dim[n]]
  419. Interpolate can be used as follows in Modelica:
  420. model m
  421. Real x[3, 1]=Modelica.Math.sin([3*time; 5*time; 7*time]) "Input";
  422. parameter Real dim[3, 1]=[3; 2; 2] "Sizes for the dimensions";
  423. parameter Real xval[3 + 2 + 2, 1]=[-1;0;1; -1;1; -1;1]
  424. "Grid values in the dimensions";
  425. parameter Real yval[3*2*2, 1]=[1;2; 3;4; 11;12; 13;14; 15;16; 17;18]
  426. "Table values";
  427. output Real y;
  428. equation
  429. y = Interpolate(dim, xval, yval, x);
  430. end m;
  431. The interpolation is done recursively:
  432. - search among grid points for this argument
  433. - interpolate the remaining arguments for grid points above and below
  434. - interpolate in result from the two interpolations
  435. */
  436. int below;
  437. int i, dimsum, dimprod;
  438. int Kx, Kdim, Kxval, Kyval;
  439. *res=0;
  440. *d_res=0;
  441. if ((Mx != 1 && Nx != 1) || (Mdim != 1 && Ndim != 1)) {
  442. DymosimError("Arguments to Interpolate must be vectors.");
  443. return ;
  444. }
  445. Kx = Mx > 1 ? Mx : Nx;
  446. Kdim = Mdim > 1 ? Mdim : Ndim;
  447. Kxval = Mxval * Nxval;
  448. Kyval = Myval * Nyval;
  449. if (Kx != Kdim) {
  450. DymosimError("The size of the argument vector is not the same as\n"
  451. " the dimension vector of a table.");
  452. return;
  453. }
  454. /* Calculate the size of a table for the rest of the arguments. */
  455. dimsum = 0;
  456. dimprod = 1;
  457. for (i=0;i<Kdim;i++) {
  458. if (dim[i] != floor(dim[i])) {
  459. DymosimError("The dimension vector of a table must only contain "
  460. "integers.");
  461. return;
  462. }
  463. dimsum = dimsum + (int) dim[i];
  464. dimprod = dimprod * (int) dim[i];
  465. }
  466. if (Kxval != dimsum) {
  467. DymosimError("The size of the grid vector is not consistent with\n"
  468. " information in the dimension vector of a table.");
  469. return;
  470. }
  471. if (Kyval != dimprod) {
  472. DymosimError("The size of the table value vector is not\n"
  473. " consistent with information in the dimension vector.");
  474. return;
  475. }
  476. if (Kdim <= 1) {
  477. Interpolate1DerS(res,d_res,x[0], xval, Kxval, 1, yval, Kyval, 1, dx[0]);
  478. return;
  479. } else if (x[0] <= xval[0]) {
  480. below = 0; /* Use lowest data set. */
  481. InterpolateDerS(res,d_res,&dim[1], Kdim-1, 1,
  482. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  483. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  484. &x[1], Kx-1, 1,&dx[1]);
  485. return;
  486. } else if (x[0] >= xval[(int)dim[0]-1]) {
  487. below = (int)dim[0]-1;
  488. InterpolateDerS(res,d_res,&dim[1], Kdim-1, 1,
  489. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  490. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  491. &x[1], Kx-1, 1,&dx[1]);
  492. return;
  493. }
  494. else {
  495. double ybelow, yabove;
  496. double ybelow_d, yabove_d;
  497. below = SearchGrid(x[0], xval, (int)dim[0]);
  498. /* Interpolate the rest of the arguments for the grid points below and above. */
  499. InterpolateDerS(&ybelow,&ybelow_d,&dim[1], Kdim-1, 1,
  500. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  501. &yval[below*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  502. &x[1], Kx-1, 1,&dx[1]);
  503. InterpolateDerS(&yabove,&yabove_d,&dim[1], Kdim-1, 1,
  504. &xval[(int)dim[0]], Kxval-(int)dim[0], 1,
  505. &yval[(below+1)*dimprod/(int)dim[0]], Kyval/(int)dim[0], 1,
  506. &x[1], Kx-1, 1,&dx[1]);
  507. /* Interpolate this argument. */
  508. *res=( ybelow + (yabove-ybelow) * (x[0]-xval[below]) /
  509. (xval[below+1]-xval[below]));
  510. *d_res=((yabove-ybelow)*dx[0]/(xval[below+1]-xval[below]))+
  511. (ybelow_d+(yabove_d-ybelow_d)*(x[0]-xval[below]) / (xval[below+1]-xval[below]));
  512. return;
  513. }
  514. }
  515. DYMOLA_STATIC double InterpolateDer(const double dim[], int Mdim, int Ndim,
  516. const double xval[], int Mxval, int Nxval,
  517. const double yval[], int Myval, int Nyval,
  518. const double x[], int Mx, int Nx, const double dx[]) {
  519. double res,d_res;
  520. InterpolateDerS(&res,&d_res,dim,Mdim,Ndim,xval,Mxval,Nxval,yval,Myval,Nyval,x,Mx,Nx,dx);
  521. return d_res;
  522. }
  523. #ifndef NO_FILE
  524. double ExternalTable(char *dimname, char *gridname, char *tablename,
  525. double x[], int Mx, int Nx) {
  526. /* Interpolation in n dimensions utilizing external matrices dimname,
  527. gridname and tablename. */
  528. static Amatrix *dim=0, *grid=0, *table=0;
  529. int matind;
  530. double retur;
  531. InitMatrices();
  532. matind = LocateMatrix(dimname, doubleMatrix, &dim);
  533. if (matind < 0 || !dim) {
  534. return 0.0;
  535. }
  536. matind = LocateMatrix(gridname, doubleMatrix, &grid);
  537. if (matind < 0 || !grid) {
  538. return 0.0;
  539. }
  540. matind = LocateMatrix(tablename, doubleMatrix, &table);
  541. if (matind < 0 || !table) {
  542. return 0.0;
  543. }
  544. retur = Interpolate(dim->data.d, dim->nrow, dim->ncol,
  545. grid->data.d, grid->nrow, grid->ncol,
  546. table->data.d, table->nrow, table->ncol,
  547. x, Mx, Nx);
  548. return retur;
  549. }
  550. double ExternalTable1(char *gridname, char *tablename, double x) {
  551. /* Interpolation in one dimension utilizing external matrices
  552. gridname and tablename. */
  553. static Amatrix *dim=0, *grid=0, *table=0;
  554. int matind;
  555. double retur;
  556. double arg[1];
  557. InitMatrices();
  558. arg[0] = x;
  559. matind = LocateMatrix(gridname, doubleMatrix, &grid);
  560. if (matind < 0 || !grid) {
  561. return 0.0;
  562. }
  563. matind = LocateMatrix(tablename, doubleMatrix, &table);
  564. if (matind < 0 || !table) {
  565. return 0.0;
  566. }
  567. dim = CreateDoubleMatrix(1, 1);
  568. if (dim == 0)
  569. return 0.0;
  570. dim->data.d[0] = grid->nrow*grid->ncol;
  571. retur = Interpolate(dim->data.d, dim->nrow, dim->ncol,
  572. grid->data.d, grid->nrow, grid->ncol,
  573. table->data.d, table->nrow, table->ncol,
  574. arg, 1, 1);
  575. free(dim->data.d);
  576. free(dim);
  577. return retur;
  578. }
  579. double ExternalTable2(char *MatrixName, double x, double y) {
  580. /* Interpolation in 2 dimensions utilizing external matrix MatrixName.
  581. First column is grid for x and first row is grid for y. */
  582. static Amatrix *mat=0, *dim=0, *grid=0, *table=0;
  583. int matind;
  584. double retur;
  585. int nx, ny, i, j;
  586. double arg[2];
  587. InitMatrices();
  588. matind = LocateMatrix(MatrixName, doubleMatrix, &mat);
  589. if (matind < 0 || !mat) {
  590. return 0.0;
  591. }
  592. arg[0] = x;
  593. arg[1] = y;
  594. if (! Tables[matind].Used) {
  595. Tables[matind].Used = 1;
  596. Tables[matind].dim = CreateDoubleMatrix(2, 1);
  597. if (Tables[matind].dim == 0)
  598. return 0.0;
  599. nx = mat->nrow - 1;
  600. ny = mat->ncol - 1;
  601. Tables[matind].dim->data.d[0] = nx;
  602. Tables[matind].dim->data.d[1] = ny;
  603. Tables[matind].grid = CreateDoubleMatrix(nx + ny, 1);
  604. if (Tables[matind].grid == 0)
  605. return 0.0;
  606. for (i=0; i<nx; i++)
  607. Tables[matind].grid->data.d[i] = mat->data.d[i+1];
  608. /* First column except first element. */
  609. for (i=0; i<ny; i++)
  610. Tables[matind].grid->data.d[nx + i] = mat->data.d[(i+1)*mat->nrow];
  611. /* First row except first element. */
  612. Tables[matind].table = CreateDoubleMatrix(nx*ny, 1);
  613. if (Tables[matind].table == 0)
  614. return 0.0;
  615. for (i=0; i<nx; i++)
  616. for (j=0; j<ny; j++)
  617. Tables[matind].table->data.d[i*ny+j] = mat->data.d[(i+1) + (j+1)*mat->nrow];
  618. }
  619. dim = Tables[matind].dim;
  620. grid = Tables[matind].grid;
  621. table = Tables[matind].table;
  622. if (!dim || !grid || !table) return 0.0; /* Null-pointer check */
  623. retur = Interpolate(dim->data.d, dim->nrow, dim->ncol,
  624. grid->data.d, grid->nrow, grid->ncol,
  625. table->data.d, table->nrow, table->ncol,
  626. arg, 2, 1);
  627. /*
  628. free(dim->data.d);
  629. free(grid->data.d);
  630. free(table->data.d);
  631. free(dim);
  632. free(grid);
  633. free(table);
  634. */
  635. return retur;
  636. }
  637. #else
  638. DYMOLA_STATIC double ExternalTable(char *dimname, char *gridname, char *tablename,
  639. double x[], int Mx, int Nx) {
  640. return 0.0;
  641. }
  642. DYMOLA_STATIC double ExternalTable1(char *gridname, char *tablename, double x) {
  643. return 0.0;
  644. }
  645. DYMOLA_STATIC double ExternalTable2(char *MatrixName, double x, double y) {
  646. return 0.0;
  647. }
  648. #endif