dymtable.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  1. /*
  2. Functions to define and interpolate in tables by a Dymola model.
  3. Author : Martin Otter, DLR.
  4. Release: 1997-09-07: implemented and tested
  5. 1997-11-26:
  6. 1998-06-08: includes stdlib.h instead of malloc.h
  7. 1998-10-05: modified for realtime
  8. 2000-11-18: time function interpolation:
  9. - startTime introduced
  10. - optionally hold last/first table point outside
  11. of time range.
  12. - optionally periodic interpolation
  13. 2001-03-31: bug fixed for time interpolation with respect to time,
  14. when table is read from file and start time of integration
  15. is not within the first interval of the table.
  16. */
  17. /*
  18. * Copyright (C) 1997-2001 DLR and Dynasim AB.
  19. * All rights reserved.
  20. *
  21. */
  22. #include <string.h>
  23. #include <math.h>
  24. #ifndef NO_FILE
  25. # include <stdio.h>
  26. # include <ctype.h>
  27. # include <stdlib.h>
  28. # include "amat.h"
  29. #endif
  30. #include "dymutil.h"
  31. #include "dymtable.h"
  32. /* DLL-status: This file is has static data which is sometimes included with
  33. the model. */
  34. /* Internal memory of the table class ------------------------------------- */
  35. #define MAX_TABLE_DIMENSIONS 3
  36. #define MAX_TABLE 800
  37. typedef struct {
  38. const char *tableName; /* name of table */
  39. const char *fileName; /* name of file */
  40. /* (names used for print output) */
  41. double *table; /* table values */
  42. double startTime; /* only for nipo=1: */
  43. /* start time of interpolation */
  44. double t_offset; /* only for nipo=1, expoType=2: */
  45. /* time - t_offset is ipo-time */
  46. int colWise; /* = 0: row-wise storage */
  47. /* = 1: column-wise storage */
  48. int nipo; /* = 0: Time-interpolation */
  49. /* = 1: 1D-interpolation */
  50. /* = 2: 2D-interpolation */
  51. /* = 3: 3D-interpolation */
  52. int expoType; /* extrapolation for nipo=0: */
  53. /* = 0: last/first point */
  54. /* = 1: last/first 2 points ipo. */
  55. /* = 2: repeat table periodicaly */
  56. int ipoType; /* only for nipo=0,nipo=1,nipo=2 */
  57. /* = 0 linear */
  58. /* = 1 Spline */
  59. int ipoPoint; /* only for nipo=0 */
  60. /* = 0: within table range */
  61. /* = 1: time < startTime */
  62. /* = 2: time < tmin */
  63. /* = 3: time > tmax */
  64. int dim [MAX_TABLE_DIMENSIONS]; /* dimensions of table */
  65. int last [MAX_TABLE_DIMENSIONS]; /* last used lower grid index */
  66. } DymTable;
  67. static DymTable dymTable[MAX_TABLE]; /* All table definitions */
  68. static int lastTableID = -1; /* table is filled upto this index */
  69. typedef struct DymMatrixListStruct {
  70. struct DymMatrixListStruct *next; /* next matrix on same file */
  71. Amatrix matrix; /* matrix of file */
  72. } DymMatrixList;
  73. typedef struct DymFileListStruct {
  74. char *name; /* name of file */
  75. struct DymFileListStruct *next; /* pointer to next list element */
  76. DymMatrixList *firstMatrix; /* pointer to first matrix on file */
  77. } DymFileList;
  78. static DymFileList *fileList = NULL; /* pointer to all the tables stored */
  79. /* on all files */
  80. #ifndef NO_FILE
  81. static DymFileList* dymTableLocateFile (const char *fileName, int *ierr);
  82. static Amatrix* dymTableLocateMatrix(const DymFileList *file, const char *uName,
  83. AmatType matrixType);
  84. static int dymTableTranspose( Amatrix *matrix, char *fileName );
  85. #endif
  86. /* definitions internal to file ------------------------------------------- */
  87. typedef enum {
  88. TABLE_ERROR , /* error */
  89. TABLE_FROM_FIELD , /* table is defined by explicit field */
  90. TABLE_FROM_FUNCTION, /* table is defined in a function */
  91. TABLE_FROM_FILE /* table is defined on file */
  92. } TableType;
  93. static int dymTableCheckName(const char *name);
  94. static int dymTableSearch (double const *table, int nt1, int nt2, int colWise, int last, double u);
  95. static TableType dymTableType (const char *tableName, const char *fileName);
  96. #define round(x) (x >= 0.0 ? (int) (x+0.5) : (int) (x-0.5));
  97. /* externally accessible functions ---------------------------------------- */
  98. DYMOLA_STATIC void dymTableClearMini(void) {
  99. lastTableID = -1;
  100. }
  101. DYMOLA_STATIC void dymTableClear(void) {
  102. /* Clear the complete table datastructure and remove all
  103. allocated memory
  104. */
  105. #ifndef NO_FILE
  106. DymFileList *nextFile , *lastFile;
  107. DymMatrixList *nextMatrix, *lastMatrix;
  108. nextFile = fileList;
  109. while ( nextFile ) {
  110. nextMatrix = nextFile->firstMatrix;
  111. while ( nextMatrix ) {
  112. amatDel(&(nextMatrix->matrix));
  113. lastMatrix = nextMatrix;
  114. nextMatrix = nextMatrix->next;
  115. free( lastMatrix );
  116. }
  117. lastFile = nextFile;
  118. nextFile = nextFile->next;
  119. free( lastFile );
  120. }
  121. #endif
  122. lastTableID = -1;
  123. fileList = NULL;
  124. }
  125. /* Access table element (row- or column-wise storage).
  126. It is assumed that the variables "nt1,nt2" (table dimensions)
  127. and "colWise" are defined. The access indices are 1..nt1, 1..nt2,
  128. i.e., an engineering type of matrix access where the first index
  129. is 1 and not 0 as in C.
  130. */
  131. #define tableGet(i,j) (colWise ? (table[(i-1) + (j-1)*nt1]) : (table[(j-1) + (i-1)*nt2]))
  132. DYMOLA_STATIC double dymTableInit(double nipoIn, double ipoTypeIn, const char *tableName,
  133. const char *fileName, double const *tableIn, long nt1In, long nt2In,
  134. double colWiseIn) {
  135. /* Initialize table interpolation */
  136. #ifndef NO_FILE
  137. DymFileList *file;
  138. Amatrix *tableMatrix;
  139. int ierr;
  140. #endif
  141. TableType tType;
  142. double *table;
  143. double u1, u2;
  144. int nipo, nipotype, nt1, nt2, colWise;
  145. int dim[MAX_TABLE_DIMENSIONS];
  146. int tableID;
  147. int i, ibeg;
  148. /* Check nipo and whether the maximum number of table entries is reached */
  149. nipo = round(nipoIn);
  150. nipotype = round(ipoTypeIn);
  151. if ( nipo < 0 || nipo > 2 ) {
  152. sprintf(amatError,"Argument 1 (nipo=%d) of function \"dymTableInit\"\n"
  153. "has to be 0 (time-interpolation), 1 (1D-interpolation)\n"
  154. "or 2 (2D-interpolation).\n",
  155. nipo);
  156. DymosimError(amatError);
  157. goto ERROR_;
  158. } else if (nipotype<0 || nipotype>1) {
  159. sprintf(amatError,"Argument 1 (nipoType=%d) of function \"dymTableInit\"\n"
  160. "has to be 0 (linear), or 1 (spline)\n",
  161. nipotype);
  162. DymosimError(amatError);
  163. goto ERROR_;
  164. }else if ( lastTableID >= MAX_TABLE-1 ) {
  165. sprintf(amatError,"The maximum number of tables (= %d) is reached.\n"
  166. "No more table memory is available\n", (int) MAX_TABLE);
  167. DymosimError(amatError);
  168. goto ERROR_;
  169. }
  170. tableID = lastTableID + 1;
  171. /* Determine, in which way the table values are defined */
  172. tType = dymTableType(tableName, fileName);
  173. if ( tType == TABLE_ERROR ) goto ERROR_;
  174. /* Action according to table type */
  175. if ( tType == TABLE_FROM_FIELD ) {
  176. table = (double *) tableIn;
  177. nt1 = (int) nt1In;
  178. nt2 = (int) nt2In;
  179. colWise = round(colWiseIn);
  180. } else if ( tType == TABLE_FROM_FUNCTION ) {
  181. if ( usertab((char *) tableName, nipo, dim, &colWise, &table) != 0 )
  182. goto ERROR_;
  183. nt1 = dim[0];
  184. nt2 = dim[1];
  185. } else {
  186. #ifdef NO_FILE
  187. /* tType is already appropriately set. If the code is correct,
  188. it is not possible to arrive at this place
  189. */
  190. DymosimError("unexpected error in function \"dymTableInit1a\".");
  191. goto ERROR_;
  192. #else
  193. /* Tables are stored on file; read matrices from file and
  194. find the desired ones
  195. */
  196. file = dymTableLocateFile(fileName, &ierr);
  197. if ( ierr != 0 ) goto ERROR_;
  198. tableMatrix = dymTableLocateMatrix(file, tableName, doubleMatrix);
  199. if ( tableMatrix == NULL ) goto ERROR_;
  200. table = tableMatrix->data.d;
  201. nt1 = tableMatrix->nrow;
  202. nt2 = tableMatrix->ncol;
  203. colWise = 1;
  204. #endif
  205. }
  206. /* Check dimensions */
  207. if ( nipo == 0 && (nt1 < 1 || nt2 < 2) ) {
  208. sprintf(amatError, "Table matrix \"%s(%d,%d)\" does not have appropriate\n"
  209. "dimensions for time interpolation.\n",
  210. tableName, nt1, nt2);
  211. DymosimError(amatError);
  212. goto ERROR_;
  213. } else if ( nipo == 1 && (nt1 < 1 || nt2 < 2) ) {
  214. sprintf(amatError, "Table matrix \"%s(%d,%d)\" does not have appropriate\n"
  215. "dimensions for 1D interpolation.\n",
  216. tableName, nt1, nt2);
  217. DymosimError(amatError);
  218. goto ERROR_;
  219. } else if ( nipo == 2 && (nt1 < 2 || nt2 < 2) ) {
  220. sprintf(amatError, "Table matrix \"%s(%d,%d)\" does not have appropriate\n"
  221. "dimensions for 2D interpolation.\n",
  222. tableName, nt1, nt2);
  223. DymosimError(amatError);
  224. goto ERROR_;
  225. }
  226. /* Check, whether first column values are monotonically (nipo=1) or
  227. strict monotonically (nipo=1,2) increasing
  228. */
  229. if ( nipo == 0 ) {
  230. for (i=1; i<nt1; i++) {
  231. u1 = tableGet(i ,1);
  232. u2 = tableGet(i+1,1);
  233. if ( u1 > u2 ) {
  234. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  235. "NOT monotonically increasing because\n"
  236. " %s(%d,1) (=%23.15e) >\n"
  237. " %s(%d,1) (=%23.15e).\n",
  238. tableName, nt1, nt2, tableName, i , u1,
  239. tableName, i+1, u2 );
  240. DymosimError(amatError);
  241. goto ERROR_;
  242. }
  243. }
  244. } else {
  245. ibeg = (nipo==1) ? 1 : 2;
  246. for (i=ibeg; i<nt1; i++) {
  247. u1 = tableGet(i ,1);
  248. u2 = tableGet(i+1,1);
  249. if ( u1 >= u2 ) {
  250. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  251. "NOT STRICT monotonically increasing because\n"
  252. " %s(%d,1) (=%23.15e) >=\n"
  253. " %s(%d,1) (=%23.15e).\n",
  254. tableName, nt1, nt2, tableName, i , u1,
  255. tableName, i+1, u2 );
  256. DymosimError(amatError);
  257. goto ERROR_;
  258. }
  259. }
  260. }
  261. /* Check, whether first row values are strict monotonically increasing */
  262. if ( nipo == 2 ) {
  263. for (i=2; i<nt2; i++) {
  264. u1 = tableGet(1,i );
  265. u2 = tableGet(1,i+1);
  266. if ( u1 >= u2 ) {
  267. sprintf(amatError,"The values of the first row of table \"%s(%d,%d)\" are\n"
  268. "NOT STRICT monotonically increasing because\n"
  269. " %s(1,%d) (=%23.15e) >=\n"
  270. " %s(1,%d) (=%23.15e).\n",
  271. tableName, nt1, nt2, tableName, i , u1,
  272. tableName, i+1, u2 );
  273. DymosimError(amatError);
  274. goto ERROR_;
  275. }
  276. }
  277. }
  278. /* Store data in internal datastructure */
  279. for (i=0; i<MAX_TABLE_DIMENSIONS; i++) {
  280. dymTable[tableID].dim [i] = 0;
  281. if ( nipo == 2 ) {
  282. dymTable[tableID].last[i] = 2;
  283. } else {
  284. dymTable[tableID].last[i] = 1;
  285. }
  286. }
  287. dymTable[tableID].tableName = tableName;
  288. dymTable[tableID].fileName = fileName;
  289. dymTable[tableID].table = (double *) table;
  290. dymTable[tableID].colWise = colWise;
  291. dymTable[tableID].nipo = nipo;
  292. dymTable[tableID].dim [0] = nt1;
  293. dymTable[tableID].dim [1] = nt2;
  294. dymTable[tableID].expoType = 1;
  295. dymTable[tableID].startTime = 0.0;
  296. dymTable[tableID].t_offset = 0.0;
  297. dymTable[tableID].ipoType = nipotype;
  298. lastTableID = tableID;
  299. return tableID;
  300. /* Error handling */
  301. ERROR_: return (-2.0); /* tableID < lastTableID required and the */
  302. /* default for lastTableID is -1 */
  303. }
  304. /* Macro for linear interpolation */
  305. #define linearIpo(x,x1,x2,y1,y2) ( y1 + (y2-y1) * (x - x1) / (x2 - x1 ) )
  306. DYMOLA_STATIC double dymTableSpline(double q[4],double*table,int iu,int icol,int colWise,int nt1,int nt2) {
  307. int last=nt1;
  308. int j;
  309. double m1,m2,m3,m4,m5;
  310. double t3,t4;
  311. double a2,a3,a4;
  312. double wleft,wright,sw;
  313. double p0;
  314. j = (iu<2) ? 2 : (iu > last) ? last : iu;
  315. a3 = tableGet(j, 1) - tableGet(j-1, 1);
  316. m3 = (tableGet(j, icol)-tableGet(j-1,icol))/a3;
  317. p0 = tableGet(j-1, 1);
  318. q[0] = tableGet(j-1, icol);
  319. if (last != 2) {
  320. if (j != 2) {
  321. a2 = tableGet(j-1, 1)-tableGet(j-2, 1);
  322. m2 = (tableGet(j-1, icol)-tableGet(j-2, icol))/a2;
  323. if (j != last) {
  324. a4 = tableGet(j+1, 1)-tableGet(j,1);
  325. m4 = (tableGet(j+1, icol)-tableGet(j,icol))/a4;
  326. } else {
  327. a4 = 1;
  328. m4 = m3 + m3 - m2;
  329. }
  330. } else {
  331. a2 = 1;
  332. /* To work around a potential bug in the original algorithm (no hard cases found)*/
  333. a4 = tableGet(j+1,1)-tableGet(j,1);
  334. m4 = (tableGet(j+1, icol)-tableGet(j,icol))/a4;
  335. m2 = m3 + m3 - m4;
  336. }
  337. } else {
  338. a2 = 1;
  339. /* To work around a bug in the original algorithm for interval=3, last=2. */
  340. a4 = 1;
  341. /* To work around a bug in the original algorithm for interval=1, last=2 */
  342. m2 = m3;
  343. m4 = m3;
  344. }
  345. if (j > 3) {
  346. m1 = (tableGet(j-2,icol)-tableGet(j-3,icol))/(tableGet(j-2,1)-tableGet(j-3,1));
  347. } else {
  348. m1 = m2 + m2 - m3;
  349. }
  350. if (j < last - 1) {
  351. m5 = (tableGet(j+2,icol)-tableGet(j+1,icol))/(tableGet(j+2,1)-tableGet(j+1,1));
  352. } else {
  353. m5 = m4 + m4 - m3;
  354. }
  355. if (iu < last + 1) {
  356. wleft = fabs(m4 - m3);
  357. wright = fabs(m2 - m1);
  358. sw = wleft + wright;
  359. if (sw == 0) {
  360. wleft = 0.5;
  361. wright = 0.5;
  362. sw = 1.0;
  363. }
  364. t3 = (wleft*m2 + wright*m3)/sw;
  365. if (iu > 1) {
  366. wleft = fabs(m5 - m4);
  367. wright = fabs(m3 - m2);
  368. sw = wleft + wright;
  369. if (sw == 0) {
  370. wleft = 0.5;
  371. wright = 0.5;
  372. sw = 1.0;
  373. }
  374. t4 = (wleft*m3 + wright*m4)/sw;
  375. } else {
  376. t4 = t3;
  377. t3 = 0.5*(m1 + m2 - a4*(a3 - a4)*(m3 - m4)/((a3 + a4)*(a3+a4)));
  378. p0 = tableGet(j-1,1) - a4;
  379. q[0] = tableGet(j-1,icol) - m2*a4;
  380. a3 = a4;
  381. m3 = m2;
  382. }
  383. } else {
  384. wleft = fabs(m5 - m4);
  385. wright = fabs(m3 - m2);
  386. sw = wleft + wright;
  387. if (sw == 0) {
  388. wleft = 0.5;
  389. wright = 0.5;
  390. sw = 1.0;
  391. }
  392. t4 = (wleft*m3 + wright*m4)/sw;
  393. t3 = t4;
  394. t4 = 0.5*(m4 + m5 - a2*(a2 - a3)*(m2 - m3)/((a2 + a3)*(a2+a3)));
  395. p0 = tableGet(j,1);
  396. q[0] = tableGet(j,icol);
  397. m3 = m4;
  398. a3 = a2;
  399. }
  400. q[1] = t3;
  401. q[2] = (2.0*(m3 - t3) + m3 - t4)/a3;
  402. q[3] = (-m3 - m3 + t3 + t4)/(a3*a3);
  403. return p0;
  404. }
  405. DYMOLA_STATIC double dymTableIpo1DerDerDer(double tableID, double icolIn, double u, double ud, double udd, double uddd) {
  406. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  407. int ID, nt1, nt2, icol, nipo, colWise, iu, iuMax;
  408. double *table, u1, u2, y1, y2;
  409. /* Check tableID */
  410. icol = round(icolIn);
  411. ID = round(tableID);
  412. if ( ID < 0 || ID > lastTableID ) {
  413. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  414. ID, (int) Dymola_max(0,lastTableID));
  415. goto ERROR_;
  416. }
  417. /* Check table */
  418. colWise = dymTable[ID].colWise;
  419. nipo = dymTable[ID].nipo;
  420. nt1 = dymTable[ID].dim[0];
  421. nt2 = dymTable[ID].dim[1];
  422. table = dymTable[ID].table;
  423. icol = round(icolIn);
  424. if ( nipo != 1 ) {
  425. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 1D-interpolation\n"
  426. "(table defined for %dD-interpolation).\n",
  427. dymTable[ID].tableName, nt1, nt2, nipo);
  428. goto ERROR_;
  429. } else if ( icol < 2 || icol > nt2 ) {
  430. sprintf(amatError, "DerDerDer: Table \"%s(%d,%d)\" is used for 1D-interpolation\n"
  431. "in column %d, which is out of range.\n",
  432. dymTable[ID].tableName, nt1, nt2, icol);
  433. goto ERROR_;
  434. }
  435. /* If table has only one value, return ZERO */
  436. if ( nt1 <= 1 ) return ( 0 );
  437. /* Search interval */
  438. iu = dymTable[ID].last[0];
  439. if (dymTable[ID].ipoType==1) {
  440. iuMax = nt1 +1;
  441. while ( iu < iuMax && u > tableGet(iu,1) ) iu++;
  442. while ( iu > 1 && u < tableGet(iu-1 ,1) ) iu--;
  443. dymTable[ID].last[0] = iu;
  444. {
  445. /* Spline interpolation */
  446. double q[4];
  447. double p0=dymTableSpline(q,table,iu,icol,colWise,nt1,nt2);
  448. double dx = u - p0;
  449. return ( 6*q[3]*ud*ud*ud + (2*q[2]+6*dx*q[3])*2*ud*udd + uddd*(q[1]+dx*(2*q[2]+3*dx*q[3])) + udd*ud*(2*q[2]+6*dx*q[3]));
  450. }
  451. }
  452. iuMax = nt1 - 1;
  453. while ( iu < iuMax && u > tableGet(iu+1,1) ) iu++;
  454. while ( iu > 1 && u < tableGet(iu ,1) ) iu--;
  455. /* Store lower grid index in datastructure */
  456. dymTable[ID].last[0] = iu;
  457. /* Get interpolation data */
  458. u1 = tableGet(iu , 1);
  459. u2 = tableGet(iu+1, 1);
  460. y1 = tableGet(iu ,icol);
  461. y2 = tableGet(iu+1,icol);
  462. /* Check for zero division (should not be possible, because already checked,
  463. whether abszissa values are strict monotonically increasing)
  464. */
  465. if ( u1 >= u2 ) {
  466. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  467. "NOT STRICT monotonically increasing because\n"
  468. " %s(%d,1) (=%e) >=\n"
  469. " %s(%d,1) (=%e).\n",
  470. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  471. iu, u1, dymTable[ID].tableName, iu+1, u2 );
  472. goto ERROR_;
  473. }
  474. /* Interpolate and return */
  475. return 0;
  476. /* An error occured */
  477. ERROR_: DymosimError(amatError);
  478. return 0.0;
  479. }
  480. DYMOLA_STATIC double dymTableIpo1DerDer(double tableID, double icolIn, double u, double ud, double udd) {
  481. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  482. int ID, nt1, nt2, icol, nipo, colWise, iu, iuMax;
  483. double *table, u1, u2, y1, y2;
  484. /* Check tableID */
  485. icol = round(icolIn);
  486. ID = round(tableID);
  487. if ( ID < 0 || ID > lastTableID ) {
  488. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  489. ID, (int) Dymola_max(0,lastTableID));
  490. goto ERROR_;
  491. }
  492. /* Check table */
  493. colWise = dymTable[ID].colWise;
  494. nipo = dymTable[ID].nipo;
  495. nt1 = dymTable[ID].dim[0];
  496. nt2 = dymTable[ID].dim[1];
  497. table = dymTable[ID].table;
  498. icol = round(icolIn);
  499. if ( nipo != 1 ) {
  500. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 1D-interpolation\n"
  501. "(table defined for %dD-interpolation).\n",
  502. dymTable[ID].tableName, nt1, nt2, nipo);
  503. goto ERROR_;
  504. } else if ( icol < 2 || icol > nt2 ) {
  505. sprintf(amatError, "DerDer: Table \"%s(%d,%d)\" is used for 1D-interpolation\n"
  506. "in column %d, which is out of range.\n",
  507. dymTable[ID].tableName, nt1, nt2, icol);
  508. goto ERROR_;
  509. }
  510. /* If table has only one value, return ZERO */
  511. if ( nt1 <= 1 ) return ( 0 );
  512. /* Search interval */
  513. iu = dymTable[ID].last[0];
  514. if (dymTable[ID].ipoType==1) {
  515. iuMax = nt1 +1;
  516. while ( iu < iuMax && u > tableGet(iu,1) ) iu++;
  517. while ( iu > 1 && u < tableGet(iu-1 ,1) ) iu--;
  518. dymTable[ID].last[0] = iu;
  519. {
  520. /* Spline interpolation */
  521. double q[4];
  522. double p0=dymTableSpline(q,table,iu,icol,colWise,nt1,nt2);
  523. double dx = u - p0;
  524. return ( udd*(q[1]+dx*(2*q[2]+3*dx*q[3])) + ud*ud*(2*q[2]+6*dx*q[3]) );
  525. }
  526. }
  527. iuMax = nt1 - 1;
  528. while ( iu < iuMax && u > tableGet(iu+1,1) ) iu++;
  529. while ( iu > 1 && u < tableGet(iu ,1) ) iu--;
  530. /* Store lower grid index in datastructure */
  531. dymTable[ID].last[0] = iu;
  532. /* Get interpolation data */
  533. u1 = tableGet(iu , 1);
  534. u2 = tableGet(iu+1, 1);
  535. y1 = tableGet(iu ,icol);
  536. y2 = tableGet(iu+1,icol);
  537. /* Check for zero division (should not be possible, because already checked,
  538. whether abszissa values are strict monotonically increasing)
  539. */
  540. if ( u1 >= u2 ) {
  541. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  542. "NOT STRICT monotonically increasing because\n"
  543. " %s(%d,1) (=%e) >=\n"
  544. " %s(%d,1) (=%e).\n",
  545. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  546. iu, u1, dymTable[ID].tableName, iu+1, u2 );
  547. goto ERROR_;
  548. }
  549. /* Interpolate and return */
  550. return 0;
  551. /* An error occured */
  552. ERROR_: DymosimError(amatError);
  553. return 0.0;
  554. }
  555. DYMOLA_STATIC double dymTableIpo1Der(double tableID, double icolIn, double u,double ud) {
  556. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  557. int ID, nt1, nt2, icol, nipo, colWise, iu, iuMax;
  558. double *table, u1, u2, y1, y2;
  559. /* Check tableID */
  560. icol = round(icolIn);
  561. ID = round(tableID);
  562. if ( ID < 0 || ID > lastTableID ) {
  563. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  564. ID, (int) Dymola_max(0,lastTableID));
  565. goto ERROR_;
  566. }
  567. /* Check table */
  568. colWise = dymTable[ID].colWise;
  569. nipo = dymTable[ID].nipo;
  570. nt1 = dymTable[ID].dim[0];
  571. nt2 = dymTable[ID].dim[1];
  572. table = dymTable[ID].table;
  573. if ( nipo != 1 ) {
  574. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 1D-interpolation\n"
  575. "(table defined for %dD-interpolation).\n",
  576. dymTable[ID].tableName, nt1, nt2, nipo);
  577. goto ERROR_;
  578. } else if ( icol < 2 || icol > nt2 ) {
  579. sprintf(amatError, "Table \"%s(%d,%d)\" is used for 1D-interpolation\n"
  580. "in column %d, which is out of range.\n",
  581. dymTable[ID].tableName, nt1, nt2, icol);
  582. goto ERROR_;
  583. }
  584. /* If table has only one value, return ZERO */
  585. if ( nt1 <= 1 ) return ( 0 );
  586. /* Search interval */
  587. iu = dymTable[ID].last[0];
  588. if (dymTable[ID].ipoType==1) {
  589. iuMax = nt1 +1;
  590. while ( iu < iuMax && u > tableGet(iu,1) ) iu++;
  591. while ( iu > 1 && u < tableGet(iu-1 ,1) ) iu--;
  592. dymTable[ID].last[0] = iu;
  593. {
  594. /* Spline interpolation */
  595. double q[4];
  596. double p0=dymTableSpline(q,table,iu,icol,colWise,nt1,nt2);
  597. double dx = u - p0;
  598. return ud*(q[1]+dx*(2*q[2]+3*dx*q[3]));
  599. }
  600. }
  601. iuMax = nt1 - 1;
  602. while ( iu < iuMax && u > tableGet(iu+1,1) ) iu++;
  603. while ( iu > 1 && u < tableGet(iu ,1) ) iu--;
  604. /* Store lower grid index in datastructure */
  605. dymTable[ID].last[0] = iu;
  606. /* Get interpolation data */
  607. u1 = tableGet(iu , 1);
  608. u2 = tableGet(iu+1, 1);
  609. y1 = tableGet(iu ,icol);
  610. y2 = tableGet(iu+1,icol);
  611. /* Check for zero division (should not be possible, because already checked,
  612. whether abszissa values are strict monotonically increasing)
  613. */
  614. if ( u1 >= u2 ) {
  615. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  616. "NOT STRICT monotonically increasing because\n"
  617. " %s(%d,1) (=%e) >=\n"
  618. " %s(%d,1) (=%e).\n",
  619. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  620. iu, u1, dymTable[ID].tableName, iu+1, u2 );
  621. goto ERROR_;
  622. }
  623. /* Interpolate and return */
  624. return (y1-y2)/(u1-u2)*ud;
  625. /* An error occured */
  626. ERROR_: DymosimError(amatError);
  627. return 0.0;
  628. }
  629. DYMOLA_STATIC double dymTableIpo1(double tableID, double icolIn, double u) {
  630. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  631. int ID, nt1, nt2, icol, nipo, colWise, iu, iuMax;
  632. double *table, u1, u2, y1, y2;
  633. /* Check tableID */
  634. icol = round(icolIn);
  635. ID = round(tableID);
  636. if ( ID < 0 || ID > lastTableID ) {
  637. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  638. ID, (int) Dymola_max(0,lastTableID));
  639. goto ERROR_;
  640. }
  641. /* Check table */
  642. colWise = dymTable[ID].colWise;
  643. nipo = dymTable[ID].nipo;
  644. nt1 = dymTable[ID].dim[0];
  645. nt2 = dymTable[ID].dim[1];
  646. table = dymTable[ID].table;
  647. if ( nipo != 1 ) {
  648. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 1D-interpolation\n"
  649. "(table defined for %dD-interpolation).\n",
  650. dymTable[ID].tableName, nt1, nt2, nipo);
  651. goto ERROR_;
  652. } else if ( icol < 2 || icol > nt2 ) {
  653. sprintf(amatError, "Table \"%s(%d,%d)\" is used for 1D-interpolation\n"
  654. "in column %d, which is out of range.\n",
  655. dymTable[ID].tableName, nt1, nt2, icol);
  656. goto ERROR_;
  657. }
  658. /* If table has only one value, return this value */
  659. if ( nt1 <= 1 ) return ( tableGet(1,icol) );
  660. /* Search interval */
  661. iu = dymTable[ID].last[0];
  662. if (dymTable[ID].ipoType==1) {
  663. iuMax = nt1 +1;
  664. while ( iu < iuMax && u > tableGet(iu,1) ) iu++;
  665. while ( iu > 1 && u < tableGet(iu-1 ,1) ) iu--;
  666. dymTable[ID].last[0] = iu;
  667. {
  668. /* Spline interpolation */
  669. double q[4];
  670. double p0=dymTableSpline(q,table,iu,icol,colWise,nt1,nt2);
  671. double dx = u - p0;
  672. return q[0]+dx*(q[1]+dx*(q[2]+dx*q[3]));
  673. }
  674. }
  675. iuMax = nt1 - 1;
  676. while ( iu < iuMax && u > tableGet(iu+1,1) ) iu++;
  677. while ( iu > 1 && u < tableGet(iu ,1) ) iu--;
  678. /* Store lower grid index in datastructure */
  679. dymTable[ID].last[0] = iu;
  680. if (dymTable[ID].ipoType==1) {
  681. {
  682. /* Spline interpolation */
  683. double q[4];
  684. double p0=dymTableSpline(q,table,iu+1,icol,colWise,nt1,nt2);
  685. double dx = u - p0;
  686. return q[0]+dx*(q[1]+dx*(q[2]+dx*q[3]));
  687. }
  688. }
  689. /* Get interpolation data */
  690. u1 = tableGet(iu , 1);
  691. u2 = tableGet(iu+1, 1);
  692. y1 = tableGet(iu ,icol);
  693. y2 = tableGet(iu+1,icol);
  694. /* Check for zero division (should not be possible, because already checked,
  695. whether abszissa values are strict monotonically increasing)
  696. */
  697. if ( u1 >= u2 ) {
  698. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  699. "NOT STRICT monotonically increasing because\n"
  700. " %s(%d,1) (=%e) >=\n"
  701. " %s(%d,1) (=%e).\n",
  702. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  703. iu, u1, dymTable[ID].tableName, iu+1, u2 );
  704. goto ERROR_;
  705. }
  706. /* Interpolate and return */
  707. return linearIpo(u,u1,u2,y1,y2);
  708. /* An error occured */
  709. ERROR_: DymosimError(amatError);
  710. return 0.0;
  711. }
  712. #ifndef NDEBUG
  713. #define tableGetX(i) (!(i>=1&&i<nt1)&&DymosimError("X"),tableGet(i+1,1))
  714. #define tableGetY(i) (!(i>=1&&i<nt2)&&DymosimError("Y"),tableGet(1,i+1))
  715. #define tableGetZ(i,j) (!(i>=1&&i<nt1)&&DymosimError("Z(x)"),!(j>=1&&j<nt2)&&DymosimError("Z(y)"),tableGet(i+1,j+1))
  716. #else
  717. #define tableGetX(i) tableGet(i+1,1)
  718. #define tableGetY(i) tableGet(1,i+1)
  719. #define tableGetZ(i,j) tableGet(i+1,j+1)
  720. #endif
  721. /* Subroutine */ DYMOLA_STATIC int itplbv_(integer ixin,double uk,integer iyin,double vk,
  722. double*table,int nt1,int nt2,int colWise,double*result,int derOrder,double ukD,double vkD)
  723. {
  724. /* System generated locals */
  725. double r__1=0;
  726. double equiv_9[10]={0,0,0,0,0,0,0,0,0,0}, equiv_19[10]={0,0,0,0,0,0,0,0,0,0},
  727. equiv_28[9]={0,0,0,0,0,0,0,0,0}, equiv_41[1]={1};
  728. integer jx, jy, jx1, jy1, jxm2, jym2;
  729. double dx, dy, equiv_73[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, equiv_78[16]={
  730. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
  731. equiv_85[1]={0}, equiv_86[1]={0}, equiv_88[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  732. /* Local variables */
  733. integer lx0,lxm1,lxm2,lxp1;
  734. integer ly0,lym1,lym2,lyp1;
  735. integer ix,iy;
  736. #define a (equiv_73 + 1)
  737. #define b (equiv_73 + 4)
  738. #define c__ (equiv_73 + 7)
  739. #define d__ (equiv_78 + 1)
  740. #define e (equiv_78 + 13)
  741. #define a1 (equiv_73 + 1)
  742. #define b1 (equiv_73 + 1)
  743. #define a2 (equiv_73 + 4)
  744. #define a4 (equiv_73 + 7)
  745. #define a5 (equiv_73 + 1)
  746. #define b5 (equiv_73 + 1)
  747. #define b2 (equiv_78 + 1)
  748. #define b4 (equiv_78 + 13)
  749. double a3=0, b3=0;
  750. #define q0 (equiv_73 + 1)
  751. #define q1 (equiv_73 + 4)
  752. #define q2 (equiv_73 + 7)
  753. #define q3 (equiv_78 + 1)
  754. #define w1 (equiv_86)
  755. #define w2 (equiv_85)
  756. #define x2 (equiv_73 + 2)
  757. #define y2 (equiv_73 + 13)
  758. #define x4 (equiv_73 + 8)
  759. #define x5 (equiv_73 + 11)
  760. #define y4 (equiv_78 + 2)
  761. #define y5 (equiv_73 + 14)
  762. #define w4 (equiv_85)
  763. #define w3 (equiv_86)
  764. #define w5 (equiv_86)
  765. double x3=0, y3=0;
  766. #define p00 (equiv_41)
  767. #define p01 (equiv_78 + 5)
  768. #define p10 (equiv_73 + 5)
  769. #define p11 (equiv_88 + 5)
  770. #define p02 (equiv_73 + 14)
  771. #define p03 (equiv_78 + 4)
  772. #define p12 (equiv_78 + 7)
  773. #define p13 (equiv_78 + 8)
  774. #define p20 (equiv_78 + 11)
  775. #define p21 (equiv_78 + 14)
  776. #define p22 (equiv_88 + 1)
  777. #define za (equiv_9)
  778. #define zb (equiv_19)
  779. #define z33 (equiv_41)
  780. #define z23 (equiv_78 + 4)
  781. #define z24 (equiv_78 + 7)
  782. #define z32 (equiv_78 + 8)
  783. #define z34 (equiv_78 + 11)
  784. #define z35 (equiv_78 + 14)
  785. #define z42 (equiv_88 + 1)
  786. #define z43 (equiv_88 + 4)
  787. #define p23 (equiv_88 + 4)
  788. #define z44 (equiv_88 + 2)
  789. #define zx (equiv_73)
  790. #define zy (equiv_78)
  791. #define p30 (equiv_88 + 2)
  792. #define z45 (equiv_88 + 7)
  793. #define p31 (equiv_88 + 7)
  794. #define z53 (equiv_88 + 8)
  795. #define p32 (equiv_88 + 8)
  796. #define z54 (equiv_88 + 11)
  797. #define p33 (equiv_88 + 11)
  798. double sw=0;
  799. #define wx2 (equiv_88 + 13)
  800. #define wy2 (equiv_85)
  801. #define wy3 (equiv_86)
  802. #define wx3 (equiv_88 + 14)
  803. #define zab (equiv_28)
  804. #define z3a1 (equiv_9)
  805. #define z3a2 (equiv_9 + 1)
  806. #define z3a3 (equiv_9 + 2)
  807. #define z3a4 (equiv_9 + 3)
  808. #define z3a5 (equiv_9 + 4)
  809. #define z4a1 (equiv_9 + 5)
  810. #define z4a2 (equiv_9 + 6)
  811. #define z4a3 (equiv_9 + 7)
  812. #define z4a4 (equiv_9 + 8)
  813. #define z4a5 (equiv_9 + 9)
  814. #define z3b1 (equiv_19)
  815. #define z3b2 (equiv_19 + 2)
  816. #define z3b3 (equiv_19 + 4)
  817. #define z3b4 (equiv_19 + 6)
  818. #define z3b5 (equiv_19 + 8)
  819. #define z4b1 (equiv_19 + 1)
  820. #define z4b2 (equiv_19 + 3)
  821. #define z4b3 (equiv_19 + 5)
  822. #define z4b4 (equiv_19 + 7)
  823. #define z4b5 (equiv_19 + 9)
  824. #define zx33 (equiv_73 + 5)
  825. #define zx43 (equiv_73 + 6)
  826. #define zx34 (equiv_73 + 9)
  827. #define zx44 (equiv_73 + 10)
  828. #define zy33 (equiv_78 + 5)
  829. #define zy43 (equiv_78 + 6)
  830. #define zy34 (equiv_78 + 9)
  831. #define zy44 (equiv_78 + 10)
  832. #define zxy (equiv_88)
  833. #define a3sq (equiv_73 + 2)
  834. #define b3sq (equiv_78 + 2)
  835. #define za2b2 (equiv_28)
  836. #define za3b2 (equiv_28 + 1)
  837. #define za4b2 (equiv_28 + 2)
  838. #define za2b3 (equiv_28 + 3)
  839. #define za3b3 (equiv_28 + 4)
  840. #define za4b3 (equiv_28 + 5)
  841. #define za2b4 (equiv_28 + 6)
  842. #define za3b4 (equiv_28 + 7)
  843. #define za4b4 (equiv_28 + 8)
  844. double zx3b3=0;
  845. integer jxml, jyml;
  846. double zx4b3=0, zy3a3=0, zy4a3=0;
  847. #define zxy33 (equiv_88 + 5)
  848. #define zxy43 (equiv_88 + 6)
  849. #define zxy34 (equiv_88 + 9)
  850. #define zxy44 (equiv_88 + 10)
  851. /* BIVARIATE INTERPOLATION */
  852. /* THIS SUBROUTINE INTERPOLATES, FROM VALUES OF THE FUNCTION */
  853. /* GIVEN AT INPUT GRID POINTS IN AN X-Y PLANE AND FOR A GIVEN */
  854. /* SET OF POINTS IN THE PLANE, THE VALUES OF A SINGLE-VALUED */
  855. /* BIVARIATE FUNCTION Z = Z(X,Y). */
  856. /* THE METHOD IS BASED ON A PIECE-WISE FUNCTION COMPOSED OF */
  857. /* A SET OF BICUBIC POLYNOMIALS IN X AND Y. EACH POLYNOMIAL */
  858. /* IS APPLICABLE TO A RECTANGLE OF THE INPUT GRID IN THE X-Y */
  859. /* PLANE. EACH POLYNOMIAL IS DETERMINED LOCALLY. */
  860. /* THE INPUT PARAMETERS ARE */
  861. /* LX = NUMBER OF INPUT GRID POINTS IN THE X COORDINATE */
  862. /* (MUST BE 2 OR GREATER) */
  863. /* LY = NUMBER OF INPUT GRID POINTS IN THE Y COORDINATE */
  864. /* (MUST BE 2 OR GREATER) */
  865. /* X = ARRAY OF DIMENSION LX STORING THE X COORDINATES */
  866. /* OF INPUT GRID POINTS (IN ASCENDING ORDER) */
  867. /* Y = ARRAY OF DIMENSION LY STORING THE Y COORDINATES */
  868. /* OF INPUT GRID POINTS (IN ASCENDING ORDER) */
  869. /* Z = DOUBLY-DIMENSIONED ARRAY OF DIMENSION (LX,LY) */
  870. /* STORING THE VALUES OF THE FUNCTION (Z VALUES) */
  871. /* AT INPUT GRID POINTS */
  872. /* N = NUMBER OF POINTS AT WHICH INTERPOLATION OF THE */
  873. /* Z VALUE IS DESIRED (MUST BE 1 OR GREATER) */
  874. /* U = ARRAY OF DIMENSION N STORING THE X COORDINATES */
  875. /* OF DESIRED POINTS */
  876. /* V = ARRAY OF DIMENSION N STORING THE Y COORDINATES */
  877. /* OF DESIRED POINTS */
  878. /* THE OUTPUT PARAMETER IS */
  879. /* W = ARRAY OF DIMENSION N WHERE THE INTERPOLATED Z */
  880. /* VALUES AT DESIRED POINTS ARE TO BE DISPLAYED */
  881. /* SOME VARIABLES INTERNALLY USED ARE */
  882. /* ZA = DIVIDED DIFFERENCE OF Z WITH RESPECT TO X */
  883. /* ZB = DIVIDED DIFFERENCE OF Z WITH RESPECT TO Y */
  884. /* ZAB = SECOND ORDER DIVIDED DIFFERENCE OF Z WITH */
  885. /* RESPECT TO X AND Y */
  886. /* ZX = PARTIAL DERIVATIVE OF Z WITH RESPECT TO X */
  887. /* ZY = PARTIAL DERIVATIVE OF Z WITH RESPECT TO Y */
  888. /* ZXY = SECOND ORDER PARTIAL DERIVATIVE OF Z WITH */
  889. /* RESPECT TO X AND Y */
  890. /* DECLARATION STATEMENTS */
  891. /* PRELIMINARY PROCESSING */
  892. /* SETTING OF SOME INPUT PARAMETERS TO LOCAL VARIABLES */
  893. /* Function Body */
  894. lx0 = nt1-1;
  895. lxm1 = lx0 - 1;
  896. lxm2 = lxm1 - 1;
  897. lxp1 = lx0 + 1;
  898. ly0 = nt2-1;
  899. lym1 = ly0 - 1;
  900. lym2 = lym1 - 1;
  901. lyp1 = ly0 + 1;
  902. /* ERROR CHECK */
  903. if (lxm2 < 0) {
  904. goto L710;
  905. }
  906. if (lym2 < 0) {
  907. goto L720;
  908. }
  909. /* MAIN DO-LOOP */
  910. ix = ixin-1;
  911. iy = iyin-1;
  912. /* ROUTINES TO PICK UP NECESSARY X, Y, AND Z VALUES, TO */
  913. /* COMPUTE THE ZA, ZB, AND ZAB VALUES, AND TO ESTIMATE THEM */
  914. /* WHEN NECESSARY */
  915. jx = ix;
  916. if (jx == 1) {
  917. jx = 2;
  918. }
  919. if (jx == lxp1) {
  920. jx = lx0;
  921. }
  922. jy = iy;
  923. if (jy == 1) {
  924. jy = 2;
  925. }
  926. if (jy == lyp1) {
  927. jy = ly0;
  928. }
  929. jxm2 = jx - 2;
  930. jxml = jx - lx0;
  931. jym2 = jy - 2;
  932. jyml = jy - ly0;
  933. /* IN THE CORE AREA, I.E., IN THE RECTANGLE THAT CONTAINS */
  934. /* THE DESIRED POINT */
  935. x3 = tableGetX(jx-1);
  936. *x4 = tableGetX(jx);
  937. a3 = 1.f / (*x4 - x3);
  938. y3 = tableGetY(jy - 1);
  939. *y4 = tableGetY(jy);
  940. b3 = 1.f / (*y4 - y3);
  941. *z33 = tableGetZ(jx-1,jy-1);
  942. *z43 = tableGetZ(jx,jy-1);
  943. *z34 = tableGetZ(jx - 1,jy);
  944. *z44 = tableGetZ(jx,jy);
  945. *z3a3 = (*z43 - *z33) * a3;
  946. *z4a3 = (*z44 - *z34) * a3;
  947. *z3b3 = (*z34 - *z33) * b3;
  948. *z4b3 = (*z44 - *z43) * b3;
  949. *za3b3 = (*z4b3 - *z3b3) * a3;
  950. /* IN THE X DIRECTION */
  951. if (lxm2 == 0) {
  952. goto L230;
  953. }
  954. if (jxm2 == 0) {
  955. goto L170;
  956. }
  957. *x2 = tableGetX(jx - 2);
  958. *a2 = 1.f / (x3 - *x2);
  959. *z23 = tableGetZ(jx - 2,jy - 1);
  960. *z24 = tableGetZ(jx - 2,jy);
  961. *z3a2 = (*z33 - *z23) * *a2;
  962. *z4a2 = (*z34 - *z24) * *a2;
  963. if (jxml == 0) {
  964. goto L180;
  965. }
  966. L170:
  967. *x5 = tableGetX(jx + 1);
  968. *a4 = 1.f / (*x5 - *x4);
  969. *z53 = tableGetZ(jx + 1,jy - 1);
  970. *z54 = tableGetZ(jx + 1,jy);
  971. *z3a4 = (*z53 - *z43) * *a4;
  972. *z4a4 = (*z54 - *z44) * *a4;
  973. if (jxm2 != 0) {
  974. goto L190;
  975. }
  976. *z3a2 = *z3a3 + *z3a3 - *z3a4;
  977. *z4a2 = *z4a3 + *z4a3 - *z4a4;
  978. goto L190;
  979. L180:
  980. *z3a4 = *z3a3 + *z3a3 - *z3a2;
  981. *z4a4 = *z4a3 + *z4a3 - *z4a2;
  982. L190:
  983. *za2b3 = (*z4a2 - *z3a2) * b3;
  984. *za4b3 = (*z4a4 - *z3a4) * b3;
  985. if (jx <= 3) {
  986. goto L200;
  987. }
  988. *a1 = 1.f / (*x2 - tableGetX(jx - 3));
  989. *z3a1 = (*z23 - tableGetZ(jx - 3,jy - 1)) * *a1;
  990. *z4a1 = (*z24 - tableGetZ(jx - 3,jy)) * *a1;
  991. goto L210;
  992. L200:
  993. *z3a1 = *z3a2 + *z3a2 - *z3a3;
  994. *z4a1 = *z4a2 + *z4a2 - *z4a3;
  995. L210:
  996. if (jx >= lxm1) {
  997. goto L220;
  998. }
  999. *a5 = 1.f / (tableGetX(jx + 2) - *x5);
  1000. *z3a5 = (tableGetZ(jx + 2, jy - 1) - *z53) * *a5;
  1001. *z4a5 = (tableGetZ(jx + 2, jy) - *z54) * *a5;
  1002. goto L240;
  1003. L220:
  1004. *z3a5 = *z3a4 + *z3a4 - *z3a3;
  1005. *z4a5 = *z4a4 + *z4a4 - *z4a3;
  1006. goto L240;
  1007. L230:
  1008. *z3a2 = *z3a3;
  1009. *z4a2 = *z4a3;
  1010. goto L180;
  1011. /* IN THE Y DIRECTION */
  1012. L240:
  1013. if (lym2 == 0) {
  1014. goto L310;
  1015. }
  1016. if (jym2 == 0) {
  1017. goto L250;
  1018. }
  1019. *y2 = tableGetY(jy - 2);
  1020. *b2 = 1.f / (y3 - *y2);
  1021. *z32 = tableGetZ(jx - 1,jy - 2);
  1022. *z42 = tableGetZ(jx,jy - 2);
  1023. *z3b2 = (*z33 - *z32) * *b2;
  1024. *z4b2 = (*z43 - *z42) * *b2;
  1025. if (jyml == 0) {
  1026. goto L260;
  1027. }
  1028. L250:
  1029. *y5 = tableGetY(jy + 1);
  1030. *b4 = 1.f / (*y5 - *y4);
  1031. *z35 = tableGetZ(jx - 1, jy + 1);
  1032. *z45 = tableGetZ(jx ,jy + 1);
  1033. *z3b4 = (*z35 - *z34) * *b4;
  1034. *z4b4 = (*z45 - *z44) * *b4;
  1035. if (jym2 != 0) {
  1036. goto L270;
  1037. }
  1038. *z3b2 = *z3b3 + *z3b3 - *z3b4;
  1039. *z4b2 = *z4b3 + *z4b3 - *z4b4;
  1040. goto L270;
  1041. L260:
  1042. *z3b4 = *z3b3 + *z3b3 - *z3b2;
  1043. *z4b4 = *z4b3 + *z4b3 - *z4b2;
  1044. L270:
  1045. *za3b2 = (*z4b2 - *z3b2) * a3;
  1046. *za3b4 = (*z4b4 - *z3b4) * a3;
  1047. if (jy <= 3) {
  1048. goto L280;
  1049. }
  1050. *b1 = 1.f / (*y2 - tableGetY(jy - 3));
  1051. *z3b1 = (*z32 - tableGetZ(jx - 1,jy - 3)) * *b1;
  1052. *z4b1 = (*z42 - tableGetZ(jx, jy - 3)) * *b1;
  1053. goto L290;
  1054. L280:
  1055. *z3b1 = *z3b2 + *z3b2 - *z3b3;
  1056. *z4b1 = *z4b2 + *z4b2 - *z4b3;
  1057. L290:
  1058. if (jy >= lym1) {
  1059. goto L300;
  1060. }
  1061. *b5 = 1.f / (tableGetY(jy + 2) - *y5);
  1062. *z3b5 = (tableGetZ(jx - 1,jy + 2) - *z35) * *b5;
  1063. *z4b5 = (tableGetZ(jx, jy + 2) - *z45) * *b5;
  1064. goto L320;
  1065. L300:
  1066. *z3b5 = *z3b4 + *z3b4 - *z3b3;
  1067. *z4b5 = *z4b4 + *z4b4 - *z4b3;
  1068. goto L320;
  1069. L310:
  1070. *z3b2 = *z3b3;
  1071. *z4b2 = *z4b3;
  1072. goto L260;
  1073. /* IN THE DIAGONAL DIRECTIONS */
  1074. L320:
  1075. if (lxm2 == 0) {
  1076. goto L400;
  1077. }
  1078. if (lym2 == 0) {
  1079. goto L410;
  1080. }
  1081. if (jxml == 0) {
  1082. goto L350;
  1083. }
  1084. if (jym2 == 0) {
  1085. goto L330;
  1086. }
  1087. *za4b2 = ((*z53 - tableGetZ(jx + 1,jy - 2)) * *b2 - *z4b2) *
  1088. *a4;
  1089. if (jyml == 0) {
  1090. goto L340;
  1091. }
  1092. L330:
  1093. *za4b4 = ((tableGetZ(jx + 1, jy + 1) - *z54) * *b4 - *z4b4) *
  1094. *a4;
  1095. if (jym2 != 0) {
  1096. goto L380;
  1097. }
  1098. *za4b2 = *za4b3 + *za4b3 - *za4b4;
  1099. goto L380;
  1100. L340:
  1101. *za4b4 = *za4b3 + *za4b3 - *za4b2;
  1102. goto L380;
  1103. L350:
  1104. if (jym2 == 0) {
  1105. goto L360;
  1106. }
  1107. *za2b2 = (*z3b2 - (*z23 - tableGetZ(jx - 2, jy - 2)) * *b2) *
  1108. *a2;
  1109. if (jyml == 0) {
  1110. goto L370;
  1111. }
  1112. L360:
  1113. *za2b4 = (*z3b4 - (tableGetZ(jx - 2, jy + 1) - *z24) * *b4) *
  1114. *a2;
  1115. if (jym2 != 0) {
  1116. goto L390;
  1117. }
  1118. *za2b2 = *za2b3 + *za2b3 - *za2b4;
  1119. goto L390;
  1120. L370:
  1121. *za2b4 = *za2b3 + *za2b3 - *za2b2;
  1122. goto L390;
  1123. L380:
  1124. if (jxm2 != 0) {
  1125. goto L350;
  1126. }
  1127. *za2b2 = *za3b2 + *za3b2 - *za4b2;
  1128. *za2b4 = *za3b4 + *za3b4 - *za4b4;
  1129. goto L420;
  1130. L390:
  1131. if (jxml != 0) {
  1132. goto L420;
  1133. }
  1134. *za4b2 = *za3b2 + *za3b2 - *za2b2;
  1135. *za4b4 = *za3b4 + *za3b4 - *za2b4;
  1136. goto L420;
  1137. L400:
  1138. *za2b2 = *za3b2;
  1139. *za4b2 = *za3b2;
  1140. *za2b4 = *za3b4;
  1141. *za4b4 = *za3b4;
  1142. goto L420;
  1143. L410:
  1144. *za2b2 = *za2b3;
  1145. *za2b4 = *za2b3;
  1146. *za4b2 = *za4b3;
  1147. *za4b4 = *za4b3;
  1148. /* NUMERICAL DIFFERENTIATION --- TO DETERMINE PARTIAL */
  1149. /* DERIVATIVES ZX, ZY, AND ZXY AS WEIGHTED MEANS OF DIVIDED */
  1150. /* DIFFERENCES ZA, ZB, AND ZAB, RESPECTIVELY */
  1151. L420:
  1152. for (jy = 2; jy <= 3; ++(jy)) {
  1153. for (jx = 2; jx <= 3; ++(jx)) {
  1154. *w2 = (r__1 = za[jx + 2 + (jy - 1) * 5 - 6] - za[jx + 1 + (
  1155. jy - 1) * 5 - 6], dabs(r__1));
  1156. *w3 = (r__1 = za[jx + (jy - 1) * 5 - 6] - za[jx - 1 + (jy
  1157. - 1) * 5 - 6], dabs(r__1));
  1158. sw = *w2 + *w3;
  1159. if (sw == 0.f) {
  1160. *wx2 = .5f;
  1161. *wx3 = .5f;
  1162. } else {
  1163. *wx2 = *w2 / sw;
  1164. *wx3 = *w3 / sw;
  1165. }
  1166. zx[jx + (jy << 2) - 5] = *wx2 * za[jx + (jy - 1) * 5 - 6]
  1167. + *wx3 * za[jx + 1 + (jy - 1) * 5 - 6];
  1168. *w2 = (r__1 = zb[jx - 1 + (jy + 2 << 1) - 3] - zb[jx - 1 +
  1169. (jy + 1 << 1) - 3], dabs(r__1));
  1170. *w3 = (r__1 = zb[jx - 1 + (jy << 1) - 3] - zb[jx - 1 + (
  1171. jy - 1 << 1) - 3], dabs(r__1));
  1172. sw = *w2 + *w3;
  1173. if (sw == 0.f) {
  1174. *wy2 = .5f;
  1175. *wy3 = .5f;
  1176. } else {
  1177. *wy2 = *w2 / sw;
  1178. *wy3 = *w3 / sw;
  1179. }
  1180. zy[jx + (jy << 2) - 5] = *wy2 * zb[jx - 1 + (jy << 1) - 3]
  1181. + *wy3 * zb[jx - 1 + (jy + 1 << 1) - 3];
  1182. zxy[jx + (jy << 2) - 5] = *wy2 * (*wx2 * zab[jx - 1 + (jy
  1183. - 1) * 3 - 4] + *wx3 * zab[jx + (jy - 1) * 3 - 4])
  1184. + *wy3 * (*wx2 * zab[jx - 1 + jy * 3 - 4] + *wx3 *
  1185. zab[jx + jy * 3 - 4]);
  1186. /* L470: */
  1187. }
  1188. /* L480: */
  1189. }
  1190. /* WHEN (U(K).LT.X(1)).OR.(U(K).GT.X(LX)) */
  1191. if (ix == lxp1) {
  1192. goto L530;
  1193. }
  1194. if (ix != 1) {
  1195. goto L590;
  1196. }
  1197. *w2 = *a4 * (a3 * 3.f + *a4);
  1198. *w1 = a3 * 2.f * (a3 - *a4) + *w2;
  1199. for (jy = 2; jy <= 3; ++(jy)) {
  1200. zx[(jy << 2) - 4] = (*w1 * za[(jy - 1) * 5 - 5] + *w2 * za[(jy
  1201. - 1) * 5 - 4]) / (*w1 + *w2);
  1202. zy[(jy << 2) - 4] = zy[(jy << 2) - 3] + zy[(jy << 2) - 3] - zy[
  1203. (jy << 2) - 2];
  1204. zxy[(jy << 2) - 4] = zxy[(jy << 2) - 3] + zxy[(jy << 2) - 3] -
  1205. zxy[(jy << 2) - 2];
  1206. for (jx1 = 2; jx1 <= 3; ++(jx1)) {
  1207. jx = 5 - jx1;
  1208. zx[jx + (jy << 2) - 5] = zx[jx - 1 + (jy << 2) - 5];
  1209. zy[jx + (jy << 2) - 5] = zy[jx - 1 + (jy << 2) - 5];
  1210. zxy[jx + (jy << 2) - 5] = zxy[jx - 1 + (jy << 2) - 5];
  1211. /* L490: */
  1212. }
  1213. /* L500: */
  1214. }
  1215. x3 -= 1.f / *a4;
  1216. *z33 -= *z3a2 / *a4;
  1217. for (jy = 1; jy <= 5; ++(jy)) {
  1218. zb[(jy << 1) - 1] = zb[(jy << 1) - 2];
  1219. /* L510: */
  1220. }
  1221. for (jy = 2; jy <= 4; ++(jy)) {
  1222. zb[(jy << 1) - 2] -= zab[(jy - 1) * 3 - 3] / *a4;
  1223. /* L520: */
  1224. }
  1225. a3 = *a4;
  1226. jx = 1;
  1227. goto L570;
  1228. L530:
  1229. *w4 = *a2 * (a3 * 3.f + *a2);
  1230. *w5 = a3 * 2.f * (a3 - *a2) + *w4;
  1231. for (jy = 2; jy <= 3; ++(jy)) {
  1232. zx[(jy << 2) - 1] = (*w4 * za[(jy - 1) * 5 - 2] + *w5 * za[(jy
  1233. - 1) * 5 - 1]) / (*w4 + *w5);
  1234. zy[(jy << 2) - 1] = zy[(jy << 2) - 2] + zy[(jy << 2) - 2] - zy[
  1235. (jy << 2) - 3];
  1236. zxy[(jy << 2) - 1] = zxy[(jy << 2) - 2] + zxy[(jy << 2) - 2] -
  1237. zxy[(jy << 2) - 3];
  1238. for (jx = 2; jx <= 3; ++(jx)) {
  1239. zx[jx + (jy << 2) - 5] = zx[jx + 1 + (jy << 2) - 5];
  1240. zy[jx + (jy << 2) - 5] = zy[jx + 1 + (jy << 2) - 5];
  1241. zxy[jx + (jy << 2) - 5] = zxy[jx + 1 + (jy << 2) - 5];
  1242. /* L540: */
  1243. }
  1244. /* L550: */
  1245. }
  1246. x3 = *x4;
  1247. *z33 = *z43;
  1248. for (jy = 1; jy <= 5; ++(jy)) {
  1249. zb[(jy << 1) - 2] = zb[(jy << 1) - 1];
  1250. /* L560: */
  1251. }
  1252. a3 = *a2;
  1253. jx = 3;
  1254. L570:
  1255. za[2] = za[jx];
  1256. for (jy = 1; jy <= 3; ++(jy)) {
  1257. zab[jy * 3 - 2] = zab[jx + jy * 3 - 4];
  1258. /* L580: */
  1259. }
  1260. /* WHEN (V(K).LT.Y(1)).OR.(V(K).GT.Y(LY)) */
  1261. L590:
  1262. if (iy == lyp1) {
  1263. goto L630;
  1264. }
  1265. if (iy != 1) {
  1266. goto L680;
  1267. }
  1268. *w2 = *b4 * (b3 * 3.f + *b4);
  1269. *w1 = b3 * 2.f * (b3 - *b4) + *w2;
  1270. for (jx = 2; jx <= 3; ++(jx)) {
  1271. if (jx == 3 && ix == lxp1) {
  1272. goto L600;
  1273. }
  1274. if (jx == 2 && ix == 1) {
  1275. goto L600;
  1276. }
  1277. zy[jx - 1] = (*w1 * zb[jx - 2] + *w2 * zb[jx]) / (*w1 + *w2);
  1278. zx[jx - 1] = zx[jx + 3] + zx[jx + 3] - zx[jx + 7];
  1279. zxy[jx - 1] = zxy[jx + 3] + zxy[jx + 3] - zxy[jx + 7];
  1280. L600:
  1281. for (jy1 = 2; jy1 <= 3; ++(jy1)) {
  1282. jy = 5 - jy1;
  1283. zy[jx + (jy << 2) - 5] = zy[jx + (jy - 1 << 2) - 5];
  1284. zx[jx + (jy << 2) - 5] = zx[jx + (jy - 1 << 2) - 5];
  1285. zxy[jx + (jy << 2) - 5] = zxy[jx + (jy - 1 << 2) - 5];
  1286. /* L610: */
  1287. }
  1288. /* L620: */
  1289. }
  1290. y3 -= 1.f / *b4;
  1291. *z33 -= *z3b2 / *b4;
  1292. *z3a3 -= *za3b2 / *b4;
  1293. *z3b3 = *z3b2;
  1294. *za3b3 = *za3b2;
  1295. b3 = *b4;
  1296. goto L670;
  1297. L630:
  1298. *w4 = *b2 * (b3 * 3.f + *b2);
  1299. *w5 = b3 * 2.f * (b3 - *b2) + *w4;
  1300. for (jx = 2; jx <= 3; ++(jx)) {
  1301. if (jx == 3 && ix == lxp1) {
  1302. goto L640;
  1303. }
  1304. if (jx == 2 && ix == 1) {
  1305. goto L640;
  1306. }
  1307. zy[jx + 11] = (*w4 * zb[jx + 4] + *w5 * zb[jx + 6]) / (*w4 + *
  1308. w5);
  1309. zx[jx + 11] = zx[jx + 7] + zx[jx + 7] - zx[jx + 3];
  1310. zxy[jx + 11] = zxy[jx + 7] + zxy[jx + 7] - zxy[jx + 3];
  1311. L640:
  1312. for (jy = 2; jy <= 3; ++(jy)) {
  1313. zy[jx + (jy << 2) - 5] = zy[jx + (jy + 1 << 2) - 5];
  1314. zx[jx + (jy << 2) - 5] = zx[jx + (jy + 1 << 2) - 5];
  1315. zxy[jx + (jy << 2) - 5] = zxy[jx + (jy + 1 << 2) - 5];
  1316. /* L650: */
  1317. }
  1318. /* L660: */
  1319. }
  1320. y3 = *y4;
  1321. *z33 += *z3b3 / b3;
  1322. *z3a3 += *za3b3 / b3;
  1323. *z3b3 = *z3b4;
  1324. *za3b3 = *za3b4;
  1325. b3 = *b2;
  1326. L670:
  1327. if (ix != 1 && ix != lxp1) {
  1328. goto L680;
  1329. }
  1330. jx = ix / lxp1 + 2;
  1331. jx1 = 5 - jx;
  1332. jy = iy / lyp1 + 2;
  1333. jy1 = 5 - jy;
  1334. zx[jx + (jy << 2) - 5] = zx[jx1 + (jy << 2) - 5] + zx[jx + (jy1
  1335. << 2) - 5] - zx[jx1 + (jy1 << 2) - 5];
  1336. zy[jx + (jy << 2) - 5] = zy[jx1 + (jy << 2) - 5] + zy[jx + (jy1
  1337. << 2) - 5] - zy[jx1 + (jy1 << 2) - 5];
  1338. zxy[jx + (jy << 2) - 5] = zxy[jx1 + (jy << 2) - 5] + zxy[jx + (
  1339. jy1 << 2) - 5] - zxy[jx1 + (jy1 << 2) - 5];
  1340. /* DETERMINATION OF THE COEFFICIENTS OF THE POLYNOMIAL */
  1341. L680:
  1342. zx3b3 = (*zx34 - *zx33) * b3;
  1343. zx4b3 = (*zx44 - *zx43) * b3;
  1344. zy3a3 = (*zy43 - *zy33) * a3;
  1345. zy4a3 = (*zy44 - *zy34) * a3;
  1346. *a = *za3b3 - zx3b3 - zy3a3 + *zxy33;
  1347. *b = zx4b3 - zx3b3 - *zxy43 + *zxy33;
  1348. *c__ = zy4a3 - zy3a3 - *zxy34 + *zxy33;
  1349. *d__ = *zxy44 - *zxy43 - *zxy34 + *zxy33;
  1350. *e = *a + *a - *b - *c__;
  1351. *a3sq = a3 * a3;
  1352. *b3sq = b3 * b3;
  1353. *p02 = ((*z3b3 - *zy33) * 2.f + *z3b3 - *zy34) * b3;
  1354. *p03 = (*z3b3 * -2.f + *zy34 + *zy33) * *b3sq;
  1355. *p12 = ((zx3b3 - *zxy33) * 2.f + zx3b3 - *zxy34) * b3;
  1356. *p13 = (zx3b3 * -2.f + *zxy34 + *zxy33) * *b3sq;
  1357. *p20 = ((*z3a3 - *zx33) * 2.f + *z3a3 - *zx43) * a3;
  1358. *p21 = ((zy3a3 - *zxy33) * 2.f + zy3a3 - *zxy43) * a3;
  1359. *p22 = ((*a + *e) * 3.f + *d__) * a3 * b3;
  1360. *p23 = (*e * -3.f - *b - *d__) * a3 * *b3sq;
  1361. *p30 = (*z3a3 * -2.f + *zx43 + *zx33) * *a3sq;
  1362. *p31 = (zy3a3 * -2.f + *zxy43 + *zxy33) * *a3sq;
  1363. *p32 = (*e * -3.f - *c__ - *d__) * b3 * *a3sq;
  1364. *p33 = (*d__ + *e + *e) * *a3sq * *b3sq;
  1365. /* COMPUTATION OF THE POLYNOMIAL */
  1366. L690:
  1367. dy = vk - y3;
  1368. *q0 = *p00 + dy * (*p01 + dy * (*p02 + dy * *p03));
  1369. *q1 = *p10 + dy * (*p11 + dy * (*p12 + dy * *p13));
  1370. *q2 = *p20 + dy * (*p21 + dy * (*p22 + dy * *p23));
  1371. *q3 = *p30 + dy * (*p31 + dy * (*p32 + dy * *p33));
  1372. dx = uk - x3;
  1373. if (derOrder==1) {
  1374. *result = ukD*( *q1 + dx * (2* *q2+3*dx* *q3));
  1375. *q0 = vkD*(*p01+dy*(2* *p02 + 3*dy* *p03));
  1376. *q1 = vkD*(*p11+dy*(2* *p12 + 3*dy* *p13));
  1377. *q2 = vkD*(*p21+dy*(2* *p22 + 3*dy* *p23));
  1378. *q3 = vkD*(*p31+dy*(2* *p32 + 3*dy* *p33));
  1379. *result += *q0 + dx * (*q1 + dx * (*q2 + dx * *q3));
  1380. return 0;
  1381. }
  1382. *result = *q0 + dx * (*q1 + dx * (*q2 + dx * *q3));
  1383. /* L700: */
  1384. /* NORMAL EXIT */
  1385. return 0;
  1386. /* ERROR EXIT */
  1387. L710:
  1388. DymosimError("Number of x values <= 1");
  1389. return 1;
  1390. L720:
  1391. DymosimError("Number of y values <= 1");
  1392. return 1;
  1393. /* FORMAT STATEMENTS */
  1394. } /* itplbv_ */
  1395. #undef zxy44
  1396. #undef zxy34
  1397. #undef zxy43
  1398. #undef zxy33
  1399. #undef za4b4
  1400. #undef za3b4
  1401. #undef za2b4
  1402. #undef za4b3
  1403. #undef za3b3
  1404. #undef za2b3
  1405. #undef za4b2
  1406. #undef za3b2
  1407. #undef za2b2
  1408. #undef lyp1
  1409. #undef lym2
  1410. #undef lxp1
  1411. #undef lym1
  1412. #undef lxm2
  1413. #undef lxm1
  1414. #undef b3sq
  1415. #undef a3sq
  1416. #undef zxy
  1417. #undef zy44
  1418. #undef zy34
  1419. #undef zy43
  1420. #undef zy33
  1421. #undef zx44
  1422. #undef zx34
  1423. #undef zx43
  1424. #undef zx33
  1425. #undef z4b5
  1426. #undef z4b4
  1427. #undef z4b3
  1428. #undef z4b2
  1429. #undef z4b1
  1430. #undef z3b5
  1431. #undef z3b4
  1432. #undef z3b3
  1433. #undef z3b2
  1434. #undef z3b1
  1435. #undef z4a5
  1436. #undef z4a4
  1437. #undef z4a3
  1438. #undef z4a2
  1439. #undef z4a1
  1440. #undef z3a5
  1441. #undef z3a4
  1442. #undef z3a3
  1443. #undef z3a2
  1444. #undef z3a1
  1445. #undef zab
  1446. #undef wx3
  1447. #undef wy3
  1448. #undef wy2
  1449. #undef wx2
  1450. #undef p33
  1451. #undef z54
  1452. #undef p32
  1453. #undef z53
  1454. #undef p31
  1455. #undef z45
  1456. #undef p30
  1457. #undef zy
  1458. #undef zx
  1459. #undef z44
  1460. #undef p23
  1461. #undef z43
  1462. #undef z42
  1463. #undef z35
  1464. #undef z34
  1465. #undef z32
  1466. #undef z24
  1467. #undef z23
  1468. #undef vk
  1469. #undef jy
  1470. #undef jx
  1471. #undef iy
  1472. #undef ix
  1473. #undef z33
  1474. #undef zb
  1475. #undef za
  1476. #undef p22
  1477. #undef p21
  1478. #undef p20
  1479. #undef p13
  1480. #undef p12
  1481. #undef p03
  1482. #undef p02
  1483. #undef p11
  1484. #undef p10
  1485. #undef p01
  1486. #undef p00
  1487. #undef w5
  1488. #undef w3
  1489. #undef w4
  1490. #undef y5
  1491. #undef y4
  1492. #undef x5
  1493. #undef x4
  1494. #undef y2
  1495. #undef x2
  1496. #undef w2
  1497. #undef w1
  1498. #undef q3
  1499. #undef q2
  1500. #undef q1
  1501. #undef q0
  1502. #undef b4
  1503. #undef b2
  1504. #undef b5
  1505. #undef a5
  1506. #undef a4
  1507. #undef a2
  1508. #undef b1
  1509. #undef a1
  1510. #undef e
  1511. #undef d__
  1512. #undef c__
  1513. #undef b
  1514. #undef a
  1515. DYMOLA_STATIC double dymTableIpo2DerDer(double tableID, double u1, double u2,double ud1,double ud2,double udd1,double udd2) {
  1516. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  1517. int ID, nt1, nt2, nipo, colWise, iu1, iu2, iuMax;
  1518. double *table, u1a, u1b, u2a, u2b;
  1519. double y1a, y1b, y1c, y1d, y2a, y2b, yd2a, yd2b, ydd2a, ydd2b, ydd;
  1520. /* Check tableID */
  1521. ID = round(tableID);
  1522. if ( ID < 0 || ID > lastTableID ) {
  1523. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  1524. ID, (int) Dymola_max(0,lastTableID));
  1525. goto ERROR_;
  1526. }
  1527. /* Check table */
  1528. colWise = dymTable[ID].colWise;
  1529. nipo = dymTable[ID].nipo;
  1530. nt1 = dymTable[ID].dim[0];
  1531. nt2 = dymTable[ID].dim[1];
  1532. table = dymTable[ID].table;
  1533. if ( nipo != 2 ) {
  1534. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 2D-interpolation\n"
  1535. "(table defined for %dD-interpolation).\n",
  1536. dymTable[ID].tableName, nt1, nt2, nipo);
  1537. goto ERROR_;
  1538. }
  1539. /* If table has only one value, return ZERO */
  1540. if ( nt1 <= 2 && nt2 <= 2 ) return ( 0 );
  1541. if (dymTable[ID].ipoType==1) {
  1542. iu1 = dymTable[ID].last[0];
  1543. iuMax = nt1 +1;
  1544. while ( iu1 < iuMax && u1 > tableGet(iu1,1) ) iu1++;
  1545. while ( iu1 > 2 && u1 < tableGet(iu1-1 ,1) ) iu1--;
  1546. dymTable[ID].last[0] = iu1;
  1547. iu2 = dymTable[ID].last[1];
  1548. iuMax = nt2 +1;
  1549. while ( iu2 < iuMax && u2 > tableGet(1, iu2) ) iu2++;
  1550. while ( iu2 > 2 && u2 < tableGet(1, iu2-1) ) iu2--;
  1551. dymTable[ID].last[1] = iu2;
  1552. {
  1553. double res=0;
  1554. itplbv_(iu1,u1,iu2,u2,table,nt1,nt2,colWise,&res,1,ud1,ud2);
  1555. return res;
  1556. }
  1557. }
  1558. /* Search u1 interval */
  1559. iu1 = dymTable[ID].last[0];
  1560. iuMax = nt1 - 1;
  1561. while ( iu1 < iuMax && u1 > tableGet(iu1+1,1) ) iu1++;
  1562. while ( iu1 > 2 && u1 < tableGet(iu1 ,1) ) iu1--;
  1563. /* Search u2 interval */
  1564. iu2 = dymTable[ID].last[1];
  1565. iuMax = nt2 - 1;
  1566. while ( iu2 < iuMax && u2 > tableGet(1,iu2+1) ) iu2++;
  1567. while ( iu2 > 2 && u2 < tableGet(1,iu2 ) ) iu2--;
  1568. /* Get interpolation data */
  1569. u1a = tableGet(iu1 , 1 );
  1570. u1b = tableGet(iu1+1, 1 );
  1571. u2a = tableGet( 1 , iu2 );
  1572. u2b = tableGet( 1 , iu2+1);
  1573. y1a = tableGet(iu1 , iu2 );
  1574. y1b = tableGet(iu1+1, iu2 );
  1575. y1c = tableGet(iu1 , iu2+1);
  1576. y1d = tableGet(iu1+1, iu2+1);
  1577. /* Store lower grid indices in datastructure */
  1578. dymTable[ID].last[0] = iu1;
  1579. dymTable[ID].last[1] = iu2;
  1580. /* Check for zero division (should not be possible, because already checked,
  1581. whether abszissa values are strict monotonically increasing)
  1582. */
  1583. if ( u1a >= u1b ) {
  1584. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  1585. "NOT STRICT monotonically increasing because\n"
  1586. " %s(%d,1) (=%e) >=\n"
  1587. " %s(%d,1) (=%e).\n",
  1588. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1589. iu1, u1a, dymTable[ID].tableName, iu1+1, u1b );
  1590. goto ERROR_;
  1591. } else if ( u2a >= u2b ) {
  1592. sprintf(amatError,"The values of the first row of table \"%s(%d,%d)\" are\n"
  1593. "NOT STRICT monotonically increasing because\n"
  1594. " %s(1,%d) (=%e) >=\n"
  1595. " %s(1,%d) (=%e).\n",
  1596. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1597. iu2, u2a, dymTable[ID].tableName, iu2+1, u2b );
  1598. goto ERROR_;
  1599. }
  1600. /* Interpolate */
  1601. y2a = linearIpo(u1, u1a,u1b,y1a,y1b);
  1602. yd2a = ud1*(y1b-y1a)/(u1b-u1a);
  1603. ydd2a = udd1*(y1b-y1a)/(u1b-u1a);
  1604. y2b = linearIpo(u1, u1a,u1b,y1c,y1d);
  1605. yd2b = ud1*(y1d-y1c)/(u1b-u1a);
  1606. ydd2b = udd2*(y1d-y1c)/(u1b-u1a);
  1607. ydd = udd2*(y2b-y2a)/(u2b-u2a)+2*ud2*(yd2a-yd2b)/(u2b-u2a)+linearIpo(u2, u2a ,u2b, ydd2a, ydd2b);
  1608. /* Interpolate and return */
  1609. return ydd;
  1610. /* An error occured */
  1611. ERROR_: DymosimError(amatError);
  1612. return 0.0;
  1613. }
  1614. DYMOLA_STATIC double dymTableIpo2Der(double tableID, double u1, double u2,double ud1,double ud2) {
  1615. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  1616. int ID, nt1, nt2, nipo, colWise, iu1, iu2, iuMax;
  1617. double *table, u1a, u1b, u2a, u2b;
  1618. double y1a, y1b, y1c, y1d, y2a, y2b, yd2a, yd2b, yd;
  1619. /* Check tableID */
  1620. ID = round(tableID);
  1621. if ( ID < 0 || ID > lastTableID ) {
  1622. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  1623. ID, (int) Dymola_max(0,lastTableID));
  1624. goto ERROR_;
  1625. }
  1626. /* Check table */
  1627. colWise = dymTable[ID].colWise;
  1628. nipo = dymTable[ID].nipo;
  1629. nt1 = dymTable[ID].dim[0];
  1630. nt2 = dymTable[ID].dim[1];
  1631. table = dymTable[ID].table;
  1632. if ( nipo != 2 ) {
  1633. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 2D-interpolation\n"
  1634. "(table defined for %dD-interpolation).\n",
  1635. dymTable[ID].tableName, nt1, nt2, nipo);
  1636. goto ERROR_;
  1637. }
  1638. /* If table has only one value, return ZERO */
  1639. if ( nt1 <= 2 && nt2 <= 2 ) return ( 0 );
  1640. if (dymTable[ID].ipoType==1) {
  1641. iu1 = dymTable[ID].last[0];
  1642. iuMax = nt1 +1;
  1643. while ( iu1 < iuMax && u1 > tableGet(iu1,1) ) iu1++;
  1644. while ( iu1 > 2 && u1 < tableGet(iu1-1 ,1) ) iu1--;
  1645. dymTable[ID].last[0] = iu1;
  1646. iu2 = dymTable[ID].last[1];
  1647. iuMax = nt2 +1;
  1648. while ( iu2 < iuMax && u2 > tableGet(1, iu2) ) iu2++;
  1649. while ( iu2 > 2 && u2 < tableGet(1, iu2-1) ) iu2--;
  1650. dymTable[ID].last[1] = iu2;
  1651. {
  1652. double res=0;
  1653. itplbv_(iu1,u1,iu2,u2,table,nt1,nt2,colWise,&res,1,ud1,ud2);
  1654. return res;
  1655. }
  1656. }
  1657. /* Search u1 interval */
  1658. iu1 = dymTable[ID].last[0];
  1659. iuMax = nt1 - 1;
  1660. while ( iu1 < iuMax && u1 > tableGet(iu1+1,1) ) iu1++;
  1661. while ( iu1 > 2 && u1 < tableGet(iu1 ,1) ) iu1--;
  1662. /* Search u2 interval */
  1663. iu2 = dymTable[ID].last[1];
  1664. iuMax = nt2 - 1;
  1665. while ( iu2 < iuMax && u2 > tableGet(1,iu2+1) ) iu2++;
  1666. while ( iu2 > 2 && u2 < tableGet(1,iu2 ) ) iu2--;
  1667. /* Get interpolation data */
  1668. u1a = tableGet(iu1 , 1 );
  1669. u1b = tableGet(iu1+1, 1 );
  1670. u2a = tableGet( 1 , iu2 );
  1671. u2b = tableGet( 1 , iu2+1);
  1672. y1a = tableGet(iu1 , iu2 );
  1673. y1b = tableGet(iu1+1, iu2 );
  1674. y1c = tableGet(iu1 , iu2+1);
  1675. y1d = tableGet(iu1+1, iu2+1);
  1676. /* Store lower grid indices in datastructure */
  1677. dymTable[ID].last[0] = iu1;
  1678. dymTable[ID].last[1] = iu2;
  1679. /* Check for zero division (should not be possible, because already checked,
  1680. whether abszissa values are strict monotonically increasing)
  1681. */
  1682. if ( u1a >= u1b ) {
  1683. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  1684. "NOT STRICT monotonically increasing because\n"
  1685. " %s(%d,1) (=%e) >=\n"
  1686. " %s(%d,1) (=%e).\n",
  1687. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1688. iu1, u1a, dymTable[ID].tableName, iu1+1, u1b );
  1689. goto ERROR_;
  1690. } else if ( u2a >= u2b ) {
  1691. sprintf(amatError,"The values of the first row of table \"%s(%d,%d)\" are\n"
  1692. "NOT STRICT monotonically increasing because\n"
  1693. " %s(1,%d) (=%e) >=\n"
  1694. " %s(1,%d) (=%e).\n",
  1695. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1696. iu2, u2a, dymTable[ID].tableName, iu2+1, u2b );
  1697. goto ERROR_;
  1698. }
  1699. /* Interpolate */
  1700. y2a = linearIpo(u1, u1a,u1b,y1a,y1b);
  1701. yd2a = ud1*(y1b-y1a)/(u1b-u1a);
  1702. y2b = linearIpo(u1, u1a,u1b,y1c,y1d);
  1703. yd2b = ud1*(y1d-y1c)/(u1b-u1a);
  1704. yd = (y2b-y2a)*ud2/(u2b-u2a)+linearIpo(u2, u2a,u2b,yd2a,yd2b);
  1705. /* Interpolate and return */
  1706. return yd;
  1707. /* An error occured */
  1708. ERROR_: DymosimError(amatError);
  1709. return 0.0;
  1710. }
  1711. DYMOLA_STATIC double dymTableIpo2(double tableID, double u1, double u2) {
  1712. /* Interpolation in the table "tableID" defined via "dymTableInit" */
  1713. int ID, nt1, nt2, nipo, colWise, iu1, iu2, iuMax;
  1714. double *table, u1a, u1b, u2a, u2b;
  1715. double y1a, y1b, y1c, y1d, y2a, y2b, y;
  1716. /* Check tableID */
  1717. ID = round(tableID);
  1718. if ( ID < 0 || ID > lastTableID ) {
  1719. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  1720. ID, (int) Dymola_max(0,lastTableID));
  1721. goto ERROR_;
  1722. }
  1723. /* Check table */
  1724. colWise = dymTable[ID].colWise;
  1725. nipo = dymTable[ID].nipo;
  1726. nt1 = dymTable[ID].dim[0];
  1727. nt2 = dymTable[ID].dim[1];
  1728. table = dymTable[ID].table;
  1729. if ( nipo != 2 ) {
  1730. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for 2D-interpolation\n"
  1731. "(table defined for %dD-interpolation).\n",
  1732. dymTable[ID].tableName, nt1, nt2, nipo);
  1733. goto ERROR_;
  1734. }
  1735. /* If table has only one value, return this value */
  1736. if ( nt1 <= 2 && nt2 <= 2 ) return ( tableGet(2,2) );
  1737. if (dymTable[ID].ipoType==1) {
  1738. iu1 = dymTable[ID].last[0];
  1739. iuMax = nt1 +1;
  1740. while ( iu1 < iuMax && u1 > tableGet(iu1,1) ) iu1++;
  1741. while ( iu1 > 2 && u1 < tableGet(iu1-1 ,1) ) iu1--;
  1742. dymTable[ID].last[0] = iu1;
  1743. iu2 = dymTable[ID].last[1];
  1744. iuMax = nt2 +1;
  1745. while ( iu2 < iuMax && u2 > tableGet(1, iu2) ) iu2++;
  1746. while ( iu2 > 2 && u2 < tableGet(1, iu2-1) ) iu2--;
  1747. dymTable[ID].last[1] = iu2;
  1748. {
  1749. double res=0;
  1750. itplbv_(iu1,u1,iu2,u2,table,nt1,nt2,colWise,&res,0,0,0);
  1751. return res;
  1752. }
  1753. }
  1754. /* Search u1 interval */
  1755. iu1 = dymTable[ID].last[0];
  1756. iuMax = nt1 - 1;
  1757. while ( iu1 < iuMax && u1 > tableGet(iu1+1,1) ) iu1++;
  1758. while ( iu1 > 2 && u1 < tableGet(iu1 ,1) ) iu1--;
  1759. /* Search u2 interval */
  1760. iu2 = dymTable[ID].last[1];
  1761. iuMax = nt2 - 1;
  1762. while ( iu2 < iuMax && u2 > tableGet(1,iu2+1) ) iu2++;
  1763. while ( iu2 > 2 && u2 < tableGet(1,iu2 ) ) iu2--;
  1764. /* Get interpolation data */
  1765. u1a = tableGet(iu1 , 1 );
  1766. u1b = tableGet(iu1+1, 1 );
  1767. u2a = tableGet( 1 , iu2 );
  1768. u2b = tableGet( 1 , iu2+1);
  1769. y1a = tableGet(iu1 , iu2 );
  1770. y1b = tableGet(iu1+1, iu2 );
  1771. y1c = tableGet(iu1 , iu2+1);
  1772. y1d = tableGet(iu1+1, iu2+1);
  1773. /* Store lower grid indices in datastructure */
  1774. dymTable[ID].last[0] = iu1;
  1775. dymTable[ID].last[1] = iu2;
  1776. /* Check for zero division (should not be possible, because already checked,
  1777. whether abszissa values are strict monotonically increasing)
  1778. */
  1779. if ( u1a >= u1b ) {
  1780. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  1781. "NOT STRICT monotonically increasing because\n"
  1782. " %s(%d,1) (=%e) >=\n"
  1783. " %s(%d,1) (=%e).\n",
  1784. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1785. iu1, u1a, dymTable[ID].tableName, iu1+1, u1b );
  1786. goto ERROR_;
  1787. } else if ( u2a >= u2b ) {
  1788. sprintf(amatError,"The values of the first row of table \"%s(%d,%d)\" are\n"
  1789. "NOT STRICT monotonically increasing because\n"
  1790. " %s(1,%d) (=%e) >=\n"
  1791. " %s(1,%d) (=%e).\n",
  1792. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1793. iu2, u2a, dymTable[ID].tableName, iu2+1, u2b );
  1794. goto ERROR_;
  1795. }
  1796. /* Interpolate */
  1797. y2a = linearIpo(u1, u1a,u1b,y1a,y1b);
  1798. y2b = linearIpo(u1, u1a,u1b,y1c,y1d);
  1799. y = linearIpo(u2, u2a,u2b,y2a,y2b);
  1800. /* Interpolate and return */
  1801. return y;
  1802. /* An error occured */
  1803. ERROR_: DymosimError(amatError);
  1804. return 0.0;
  1805. }
  1806. DYMOLA_STATIC void registerTimeEvent(const double);
  1807. DYMOLA_STATIC double dymTableTimeIni(double time, double ipoType,
  1808. const char *tableName, const char *fileName,
  1809. double const *tableIn, long nt1In, long nt2In,
  1810. double colWiseIn) {
  1811. /* Initialize table for time interpolation */
  1812. double *table;
  1813. double tableID, nextTime;
  1814. int ID, iu, iuMax, nt1, nt2;
  1815. int colWise = round(colWiseIn);
  1816. /* General Initialization of table */
  1817. tableID = dymTableInit(0.0, ipoType, tableName, fileName, tableIn, nt1In, nt2In,
  1818. colWiseIn);
  1819. ID = round(tableID);
  1820. if ( ID < -1 ) return tableID;
  1821. /* Get table information */
  1822. table = dymTable[ID].table;
  1823. colWise = dymTable[ID].colWise;
  1824. nt1 = dymTable[ID].dim[0];
  1825. nt2 = dymTable[ID].dim[1];
  1826. /* Find correct interval of time function */
  1827. iu = 1;
  1828. iuMax = nt1 - 1;
  1829. while ( iu < iuMax && time >= tableGet(iu+1,1) ) iu++;
  1830. while ( iu > 1 && time < tableGet(iu ,1) ) iu--;
  1831. /* Store index of interval */
  1832. dymTable[ID].last[0] = iu;
  1833. /* Determine next time event */
  1834. if ( iu < iuMax ) {
  1835. nextTime = tableGet(iu+1,1);
  1836. registerTimeEvent(nextTime);
  1837. }
  1838. return tableID;
  1839. }
  1840. DYMOLA_STATIC double dymTableTimeIpol(double tableID, double icolIn, double time, int event) {
  1841. /* Time interpolation in the table "tableID" defined via "dymTableTimeInit" */
  1842. int ID, nt1, nt2, nipo, icol, colWise, iu, iuMax;
  1843. double *table;
  1844. double nextTime, u1, u2, y1, y2;
  1845. const double TIME_EPS = 1.E-6;
  1846. /* Check tableID */
  1847. icol = round(icolIn);
  1848. ID = round(tableID);
  1849. if ( ID < 0 || ID > lastTableID ) {
  1850. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  1851. ID, (int) Dymola_max(0,lastTableID));
  1852. goto ERROR_;
  1853. }
  1854. /* Get table data and check it */
  1855. colWise = dymTable[ID].colWise;
  1856. nipo = dymTable[ID].nipo;
  1857. nt1 = dymTable[ID].dim[0];
  1858. nt2 = dymTable[ID].dim[1];
  1859. table = dymTable[ID].table;
  1860. if ( nipo != 0 ) {
  1861. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for time-interpolation\n"
  1862. "(table defined for %dD-interpolation).\n",
  1863. dymTable[ID].tableName, nt1, nt2, nipo);
  1864. goto ERROR_;
  1865. } else if ( icol < 2 || icol > nt2 ) {
  1866. sprintf(amatError, "Table \"%s(%d,%d)\" is used for time-interpolation\n"
  1867. "in column %d, which is out of range.\n",
  1868. dymTable[ID].tableName, nt1, nt2, icol);
  1869. goto ERROR_;
  1870. }
  1871. /* If table has only one value, return this value */
  1872. if ( nt1 <= 1 ) return ( tableGet(1,icol) );
  1873. /* At an event instant, set next time event */
  1874. iu = dymTable[ID].last[0];
  1875. iuMax = nt1 - 1;
  1876. if ( event && iu < iuMax ) {
  1877. nextTime = tableGet(iu+1,1);
  1878. if ( time >= (nextTime - TIME_EPS*fabs(nextTime)) ) {
  1879. /* Set new time event (remember, that two consecutive time instants
  1880. in the table may be identical due to a discontinuous point)
  1881. */
  1882. iu++;
  1883. while ( iu < iuMax && time >= tableGet(iu+1,1)- TIME_EPS*fabs(tableGet(iu+1,1)) ) iu++;
  1884. dymTable[ID].last[0] = iu;
  1885. if ( iu < iuMax ) {
  1886. nextTime = tableGet(iu+1,1);
  1887. registerTimeEvent(nextTime);
  1888. }
  1889. } else {
  1890. /* Set current time event again */
  1891. registerTimeEvent(nextTime);
  1892. }
  1893. }
  1894. /* Get interpolation data */
  1895. iu = dymTable[ID].last[0];
  1896. if (dymTable[ID].ipoType==1) {
  1897. {
  1898. /* Spline interpolation */
  1899. double q[4];
  1900. double p0=dymTableSpline(q,table,iu+1,icol,colWise,nt1,nt2);
  1901. double dx = time - p0;
  1902. return q[0]+dx*(q[1]+dx*(q[2]+dx*q[3]));
  1903. }
  1904. }
  1905. u1 = tableGet(iu , 1);
  1906. u2 = tableGet(iu+1, 1);
  1907. y1 = tableGet(iu ,icol);
  1908. y2 = tableGet(iu+1,icol);
  1909. /* Check for zero division (should not be possible, because already checked,
  1910. whether abszissa values are strict monotonically increasing)
  1911. */
  1912. if ( u1 >= u2 ) {
  1913. sprintf(amatError,"The values of the first column of table \"%s(%d,%d)\" are\n"
  1914. "NOT STRICT monotonically increasing because\n"
  1915. " %s(%d,1) (=%23.15e) >=\n"
  1916. " %s(%d,1) (=%23.15e).\n",
  1917. dymTable[ID].tableName, nt1, nt2, dymTable[ID].tableName,
  1918. iu, u1, dymTable[ID].tableName, iu+1, u2 );
  1919. goto ERROR_;
  1920. }
  1921. /* Interpolate and return */
  1922. return linearIpo(time,u1,u2,y1,y2);
  1923. /* An error occured */
  1924. ERROR_: DymosimError(amatError);
  1925. return 0.0;
  1926. }
  1927. #define timeLimit(nextTime) ( nextTime - 1.E-6*fabs(nextTime) )
  1928. DYMOLA_STATIC double initialTime(void);
  1929. DYMOLA_STATIC int dymTableTimeIni2(double time, double startTime, int ipoType,
  1930. int expoType, const char *tableName, const char *fileName,
  1931. double const *tableIn, int nt1In, int nt2In, int colWiseIn) {
  1932. /* Initialize table for time interpolation */
  1933. double *table;
  1934. double tableID, nextTime, tmin, tmax, t_offset, table_time;
  1935. int ID, iu, colWise, nt1, nt2;
  1936. double T, iv;
  1937. time=initialTime();
  1938. /* General Initialization of table */
  1939. tableID = dymTableInit(0.0, ipoType, tableName, fileName, tableIn, nt1In, nt2In,
  1940. colWiseIn);
  1941. ID = round(tableID);
  1942. if ( ID < -1 ) return ID;
  1943. /* Check expoType argument */
  1944. if ( expoType < 0 || expoType > 2 ) {
  1945. sprintf(amatError,"Extrapolation argument for table \"%s(%d,%d)\"\n"
  1946. "is %d, which is wrong (should be 0, 1 or 2).\n",
  1947. dymTable[ID].tableName, nt1In, nt2In, expoType);
  1948. DymosimError(amatError);
  1949. return (-2);
  1950. }
  1951. dymTable[ID].expoType = expoType;
  1952. dymTable[ID].startTime = startTime;
  1953. dymTable[ID].last[0] = 1;
  1954. dymTable[ID].ipoPoint = 0;
  1955. table = dymTable[ID].table;
  1956. colWise = dymTable[ID].colWise;
  1957. nt1 = dymTable[ID].dim[0];
  1958. nt2 = dymTable[ID].dim[1];
  1959. /* Determine table time */
  1960. tmin = tableGet(1,1);
  1961. tmax = tableGet(nt1,1);
  1962. if ( tmax > tmin && expoType == 2 ) {
  1963. /* periodic signal */
  1964. T = tmax - tmin;
  1965. iv = (time - startTime - tmin) / T;
  1966. t_offset = startTime + floor(iv)*T;
  1967. } else {
  1968. t_offset = startTime;
  1969. }
  1970. dymTable[ID].t_offset = t_offset;
  1971. table_time = time - t_offset;
  1972. /* Handle startTime */
  1973. if ( time < timeLimit(startTime) ) {
  1974. dymTable[ID].ipoPoint = 1;
  1975. registerTimeEvent(startTime);
  1976. return ID;
  1977. }
  1978. /* Handle case, when table has only one entry */
  1979. if ( nt1 <= 1 ) {
  1980. dymTable[ID].last[0] = 1;
  1981. return ID;
  1982. }
  1983. /* Find correct interval of time function, sucht that iu is at most nt1-1 */
  1984. iu = 1;
  1985. while ( iu < nt1-1 && table_time >= timeLimit(tableGet(iu+1,1))-((expoType==2) ? 1e-6*(tmax-tmin):0)) iu++;
  1986. /* Store index of interval */
  1987. dymTable[ID].last[0] = iu;
  1988. /* Determine next time event */
  1989. if ( expoType == 0 ) {
  1990. if ( table_time < timeLimit(tmin) ) {
  1991. nextTime = t_offset + tmin;
  1992. registerTimeEvent(nextTime);
  1993. dymTable[ID].ipoPoint = 2;
  1994. } else if ( table_time > timeLimit(tmax) ) {
  1995. dymTable[ID].ipoPoint = 3;
  1996. } else {
  1997. nextTime = t_offset + tableGet(iu+1,1);
  1998. registerTimeEvent(nextTime);
  1999. dymTable[ID].ipoPoint = 0;
  2000. }
  2001. } else if ( expoType == 1 ) {
  2002. if ( table_time <= timeLimit(tmax) ) {
  2003. nextTime = t_offset + tableGet(iu+1,1);
  2004. registerTimeEvent(nextTime);
  2005. }
  2006. } else { /* expoType == 2 (periodic)*/
  2007. nextTime = t_offset + tableGet(iu+1,1);
  2008. registerTimeEvent(nextTime);
  2009. }
  2010. return ID;
  2011. }
  2012. DYMOLA_STATIC double dymTableTimeTmin(int ID) {
  2013. /* Return minimum time value in table */
  2014. double *table, tmin;
  2015. int colWise, nt1, nt2;
  2016. /* Check table ID */
  2017. if ( ID < 0 || ID > lastTableID ) {
  2018. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  2019. ID, (int) Dymola_max(0,lastTableID));
  2020. return 0.0;
  2021. }
  2022. /* Get tmin */
  2023. table = dymTable[ID].table;
  2024. colWise = dymTable[ID].colWise;
  2025. nt1 = dymTable[ID].dim[0];
  2026. nt2 = dymTable[ID].dim[1];
  2027. tmin = tableGet(1,1);
  2028. return tmin;
  2029. }
  2030. DYMOLA_STATIC double dymTableTimeTmax(int ID) {
  2031. /* Return maximum time value in table */
  2032. double *table, tmax;
  2033. int colWise, nt1, nt2;
  2034. /* Check table ID */
  2035. if ( ID < 0 || ID > lastTableID ) {
  2036. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  2037. ID, (int) Dymola_max(0,lastTableID));
  2038. return 0.0;
  2039. }
  2040. /* Get tmin */
  2041. table = dymTable[ID].table;
  2042. colWise = dymTable[ID].colWise;
  2043. nt1 = dymTable[ID].dim[0];
  2044. nt2 = dymTable[ID].dim[1];
  2045. tmax = tableGet(nt1,1);
  2046. return tmax;
  2047. }
  2048. DYMOLA_STATIC int isModelicaEvent(void);
  2049. DYMOLA_STATIC double dymTableTimeIpo2(int ID, int icol, double time) {
  2050. /* Time interpolation in the table "tableID" defined via "dymTableTimeIni2" */
  2051. int nt1, nt2, nipo, colWise, iu, expoType, ipoPoint;
  2052. long event = isModelicaEvent();
  2053. double *table;
  2054. double nextTime, u1, u2, y1, y2, t_offset, table_time, tmin, tmax;
  2055. double startTime, iv, T;
  2056. /* Check table ID */
  2057. if ( ID < 0 || ID > lastTableID ) {
  2058. sprintf(amatError, "ID (= %d) of a table is not in the range 0 .. %d\n",
  2059. ID, (int) Dymola_max(0,lastTableID));
  2060. goto ERROR_;
  2061. }
  2062. /* Get table data and check it */
  2063. colWise = dymTable[ID].colWise;
  2064. nipo = dymTable[ID].nipo;
  2065. nt1 = dymTable[ID].dim[0];
  2066. nt2 = dymTable[ID].dim[1];
  2067. table = dymTable[ID].table;
  2068. expoType = dymTable[ID].expoType;
  2069. t_offset = dymTable[ID].t_offset;
  2070. startTime = dymTable[ID].startTime;
  2071. table_time = time - t_offset;
  2072. if ( nipo != 0 ) {
  2073. sprintf(amatError, "Table \"%s(%d,%d)\" is not defined for time-interpolation\n"
  2074. "(table defined for %dD-interpolation).\n",
  2075. dymTable[ID].tableName, nt1, nt2, nipo);
  2076. goto ERROR_;
  2077. } else if ( icol < 2 || icol > nt2 ) {
  2078. sprintf(amatError, "Table \"%s(%d,%d)\" is used for time-interpolation\n"
  2079. "in column %d, which is out of range.\n",
  2080. dymTable[ID].tableName, nt1, nt2, icol);
  2081. goto ERROR_;
  2082. }
  2083. /* Determine minimum and maximum table values */
  2084. tmin = tableGet(1,1);
  2085. tmax = tableGet(nt1,1);
  2086. /* At an event instant, set next time event */
  2087. iu = dymTable[ID].last[0];
  2088. if ( event ) {
  2089. /* Handle startTime */
  2090. if ( time < timeLimit(startTime) ) {
  2091. registerTimeEvent(startTime);
  2092. return 0.0;
  2093. }
  2094. /* Handle case, when table has only one entry */
  2095. if ( nt1 <= 1 ) {
  2096. y1 = tableGet(1,icol);
  2097. return y1;
  2098. }
  2099. dymTable[ID].ipoPoint = 0;
  2100. /* Find next interval for periodic functions */
  2101. if ( (expoType == 2) && (table_time >= timeLimit(tmax)) ) {
  2102. T = tmax - tmin;
  2103. iv = (time - startTime - tmin) / T;
  2104. t_offset = startTime + (floor(iv)-1)*T;
  2105. table_time = time - t_offset;
  2106. while (table_time>=timeLimit(tmax)) {
  2107. t_offset +=T;
  2108. table_time = time - t_offset;
  2109. dymTable[ID].t_offset = t_offset;
  2110. }
  2111. dymTable[ID].t_offset = t_offset;
  2112. iu = 1;
  2113. dymTable[ID].last[0] = iu;
  2114. }
  2115. /* Find correct interval of time function, sucht that iu is at most nt1-1 */
  2116. while ( iu < nt1-1 && table_time >= timeLimit(tableGet(iu+1,1))-((expoType==2) ? 1e-6*(tmax-tmin):0)) iu++;
  2117. /* Store index of interval */
  2118. dymTable[ID].last[0] = iu;
  2119. /* Determine next time event */
  2120. if ( expoType == 0 ) {
  2121. if ( table_time < timeLimit(tmin) ) {
  2122. nextTime = t_offset + tmin;
  2123. registerTimeEvent(nextTime);
  2124. dymTable[ID].ipoPoint = 2;
  2125. } else if ( table_time > timeLimit(tmax) ) {
  2126. dymTable[ID].ipoPoint = 3;
  2127. } else {
  2128. nextTime = t_offset + tableGet(iu+1,1);
  2129. registerTimeEvent(nextTime);
  2130. dymTable[ID].ipoPoint = 0;
  2131. }
  2132. } else if ( expoType == 1 ) {
  2133. if ( table_time <= timeLimit(tmax) ) {
  2134. nextTime = t_offset + tableGet(iu+1,1);
  2135. registerTimeEvent(nextTime);
  2136. }
  2137. } else { /* expoType == 2 (periodic) */
  2138. nextTime = t_offset + tableGet(iu+1,1);
  2139. registerTimeEvent(nextTime);
  2140. }
  2141. }
  2142. /* Handle startTime and constant extrapolation */
  2143. ipoPoint = dymTable[ID].ipoPoint;
  2144. if ( ipoPoint == 1 ) {
  2145. return 0.0;
  2146. } else if ( ipoPoint == 2 || nt1 <= 1 ) {
  2147. y1 = tableGet(1,icol);
  2148. return y1;
  2149. } else if ( ipoPoint == 3 ) {
  2150. y1 = tableGet(nt1,icol);
  2151. return y1;
  2152. }
  2153. /* Get interpolation data */
  2154. iu = dymTable[ID].last[0];
  2155. if (dymTable[ID].ipoType==1) {
  2156. {
  2157. /* Spline interpolation */
  2158. double q[4];
  2159. double p0=dymTableSpline(q,table,iu+1,icol,colWise,nt1,nt2);
  2160. double dx = table_time - p0;
  2161. return q[0]+dx*(q[1]+dx*(q[2]+dx*q[3]));
  2162. }
  2163. }
  2164. u1 = tableGet(iu , 1);
  2165. u2 = tableGet(iu+1, 1);
  2166. y1 = tableGet(iu ,icol);
  2167. y2 = tableGet(iu+1,icol);
  2168. /* Check for zero division
  2169. (only possible, if the last two time values in the table are identical)
  2170. */
  2171. if ( u1 >= timeLimit(u2) ) {
  2172. return y2;
  2173. }
  2174. /* Interpolate and return */
  2175. return linearIpo(table_time,u1,u2,y1,y2);
  2176. /* An error occured */
  2177. ERROR_: DymosimError(amatError);
  2178. return 0.0;
  2179. }
  2180. DYMOLA_STATIC void dymTablePrint(void) {
  2181. /* Print actual definition of tables to log-file. Output format:
  2182. table (123,123,123) fileName
  2183. 123456789012345 123456789012345
  2184. */
  2185. char mess[500], format[20], *str;
  2186. int ilen, i, j, lentab, lenfile, ndim;
  2187. /* Determine maximum tableName length */
  2188. lentab = 1;
  2189. lenfile = 1;
  2190. for (i=0; i<=lastTableID; i++) {
  2191. ilen = (int) strlen( dymTable[i].tableName );
  2192. lentab = Dymola_max(lentab, ilen);
  2193. ilen = (int) strlen( dymTable[i].fileName );
  2194. lenfile = Dymola_max(lenfile, ilen);
  2195. }
  2196. /* Print heading */
  2197. ilen = (int) strlen( "file name" );
  2198. lenfile = Dymola_max(lenfile, ilen);
  2199. sprintf(format," %%-%ds table", (int) lenfile);
  2200. sprintf(mess, format, "file name" );
  2201. DymosimMessage( mess );
  2202. ilen = (int) strlen(mess);
  2203. str = &mess[1];
  2204. for (i=1; i<ilen; i++) {sprintf(str,"_"); str++;};
  2205. sprintf(str,"\n");
  2206. DymosimMessage( mess );
  2207. /* Print table entries */
  2208. sprintf(format," %%-%ds %%-%ds(%%d", lenfile, lentab);
  2209. ndim = 2;
  2210. for (i=0; i<=lastTableID; i++) {
  2211. sprintf(mess, format, dymTable[i].fileName,
  2212. dymTable[i].tableName, dymTable[i].dim[0]);
  2213. ilen = (int) strlen(mess);
  2214. for (j=1; j<ndim; j++) {
  2215. str = &mess[ilen];
  2216. ilen = sprintf(str, ",%d", dymTable[i].dim[j]);
  2217. if ( ilen > 0 ) {
  2218. str = &str[ilen];
  2219. } else { /* error */
  2220. sprintf(mess," ????");
  2221. goto LABEL1;
  2222. }
  2223. }
  2224. sprintf(str,")");
  2225. LABEL1: DymosimMessage(mess);
  2226. }
  2227. DymosimMessage(" ");
  2228. }
  2229. /* Internal functions of this file ---------------------------------------- */
  2230. static int dymTableCheckName(const char *name) {
  2231. /* Check the type of name. Return 0, if name is NULL, contains
  2232. only blank strings or "NoName". Otherwise return 1.
  2233. */
  2234. int i=0;
  2235. if ( name == NULL ) return 0;
  2236. if ( strcmp(name,"NoName") == 0 ) return 0;
  2237. while ( *name ) {
  2238. if ( *name != ' ' ) {i = 1; break; }
  2239. name++;
  2240. }
  2241. return i;
  2242. }
  2243. static TableType dymTableType(const char *tableName, const char *fileName) {
  2244. /* Determine in which way the table values are defined */
  2245. TableType tType;
  2246. int tableNameGiven, fileNameGiven;
  2247. /* Determine which names are provided */
  2248. tableNameGiven = dymTableCheckName(tableName);
  2249. fileNameGiven = dymTableCheckName(fileName);
  2250. /* Determine in which way the table values are defined */
  2251. if ( tableNameGiven == 0 ) {
  2252. /* No table name is given */
  2253. if ( fileNameGiven != 0 ) {
  2254. sprintf(amatError,"The file name for a table (= \"%s\") is defined,\n"
  2255. "but not the corresponding table name (tableName).\n",
  2256. fileName);
  2257. goto ERROR_;
  2258. }
  2259. tType = TABLE_FROM_FIELD;
  2260. } else {
  2261. /* A tableName is given */
  2262. #ifdef NO_FILE
  2263. if ( fileNameGiven != 0 ) {
  2264. sprintf(amatError,"The file name (= \"%s\") for a table (= \"%s\") is defined. \n"
  2265. "Since dymosim is used in an environment where storage cannot\n"
  2266. "be allocated (#define NO_FILE), tables\n"
  2267. "CANNOT be read from file.\n",
  2268. fileName, tableName);
  2269. goto ERROR_;
  2270. }
  2271. tType = TABLE_FROM_FUNCTION;
  2272. #else
  2273. tType = fileNameGiven == 0 ? TABLE_FROM_FUNCTION : TABLE_FROM_FILE;
  2274. #endif
  2275. }
  2276. return tType;
  2277. /* Error handling */
  2278. ERROR_: DymosimError(amatError);
  2279. return TABLE_ERROR;
  2280. }
  2281. #ifndef NO_FILE
  2282. static int strcaselesscmp(const char*f1,const char*f2) {
  2283. int cf1,cf2; /* Use int since toupper returns int */
  2284. for(;(cf1=toupper(*f1))==(cf2=toupper(*f2)) && cf1 && cf2;++f1,++f2);
  2285. return cf1<cf2 ? -1: cf1==cf2? 0: 1;
  2286. }
  2287. static DymFileList* dymTableLocateFile(const char *fileName, int *ierr) {
  2288. /* Read all matrices stored on file "fileName"
  2289. -> fileName: name of file
  2290. <- ierr : = 0: no error occured.
  2291. = 1: an error occured (message already printed).
  2292. <- RETURN : pointer to header of file list
  2293. (if NULL is returned then an error occured).
  2294. */
  2295. DymFileList *nextFile = fileList;
  2296. DymFileList **lastFile = &fileList;
  2297. DymMatrixList *nextMatrix = NULL;
  2298. DymMatrixList **lastMatrix = NULL;
  2299. Amatrix *matrix;
  2300. AmatGetFile file;
  2301. int status;
  2302. char mess[500];
  2303. /* Transform file name in lower case letters on Windows System
  2304. in order to get a unique filename and not to read the file
  2305. several times if a different spelling is given
  2306. */
  2307. /* If this file is already stored in memory, return the file header */
  2308. if (!ierr) return 0; /* Null-pointer check */
  2309. *ierr = 0;
  2310. while ( nextFile ) {
  2311. #ifdef WIN32_ONLY
  2312. if (strcaselesscmp(nextFile->name,fileName)==0) {
  2313. #else
  2314. if ( strcmp(nextFile->name, fileName) == 0 ) {
  2315. #endif
  2316. return nextFile;
  2317. }
  2318. lastFile = &(nextFile->next);
  2319. nextFile = nextFile->next;
  2320. }
  2321. /* Print info message, that file is loaded */
  2322. sprintf(mess,"... \"%s\" loading (tables for interpolation)", fileName);
  2323. DymosimMessage(mess);
  2324. /* Open file */
  2325. if ( amatGetOpen((char *) fileName, "NoClass", NULL, &file ) != 0 ) {
  2326. DymosimError(amatError);
  2327. *ierr = 1;
  2328. return NULL;
  2329. }
  2330. /* Allocate storage for a fileList element */
  2331. nextFile = (DymFileList *) malloc( sizeof(DymFileList) );
  2332. if ( nextFile == NULL ) {
  2333. sprintf(amatError,"Not enough storage available to read matrices\n"
  2334. "from file \"%s\".\n", fileName);
  2335. DymosimError(amatError);
  2336. amatGetClose(&file);
  2337. *ierr = 1;
  2338. return NULL;
  2339. }
  2340. nextFile->name = (char *) fileName;
  2341. nextFile->next = NULL;
  2342. nextFile->firstMatrix = NULL;
  2343. *lastFile = nextFile;
  2344. /* Read all matrices from file */
  2345. lastMatrix = &(nextFile->firstMatrix);
  2346. for(;;) {
  2347. /* Allocate storage for a new matrix list element and initialize it */
  2348. nextMatrix = (DymMatrixList *) malloc( sizeof(DymMatrixList) );
  2349. if ( nextMatrix == NULL ) {
  2350. sprintf(amatError,"Not enough storage available to read matrix\n"
  2351. "from file \"%s\".\n", fileName);
  2352. goto ERROR_;
  2353. }
  2354. nextMatrix->next = NULL;
  2355. matrix = &(nextMatrix->matrix);
  2356. amatInit( matrix );
  2357. /* Read next matrix from file and break if EOF reached */
  2358. status = amatGetMatrix(&file, matrix);
  2359. if ( status == 3 ) break; /* EOF */
  2360. if ( status != 0 ) goto ERROR_;
  2361. /* Store matrix in file data structure */
  2362. *lastMatrix = nextMatrix;
  2363. lastMatrix = &(nextMatrix->next);
  2364. }
  2365. /* Close file */
  2366. amatGetClose(&file);
  2367. return nextFile;
  2368. /* Error handling */
  2369. ERROR_ : DymosimError(amatError);
  2370. amatGetClose(&file);
  2371. free( nextMatrix );
  2372. *ierr = 1;
  2373. return NULL;
  2374. }
  2375. static Amatrix* dymTableLocateMatrix(const DymFileList *file, const char *tableName,
  2376. AmatType matrixType) {
  2377. /* Locate matrix "tableName" in file list "file".
  2378. Matrix has to be of type "matrixType".
  2379. Return a pointer to the matrix.
  2380. */
  2381. DymMatrixList *matrixList = 0;
  2382. if (!file) return 0; /* Error */
  2383. matrixList = file->firstMatrix;
  2384. /* Search matrix */
  2385. while ( matrixList ) {
  2386. if ( strcmp(matrixList->matrix.name, tableName) == 0 ) {
  2387. /* Check matrix type */
  2388. if ( matrixList->matrix.type != matrixType ) {
  2389. sprintf(amatError,"Table matrix \"%s\" has not the required datatype\n",
  2390. tableName);
  2391. DymosimError(amatError);
  2392. return NULL;
  2393. }
  2394. return &(matrixList->matrix);
  2395. }
  2396. matrixList = matrixList->next;
  2397. }
  2398. /* Error */
  2399. sprintf(amatError, "Matrix \"%s\" not found on file \"%s\".\n",
  2400. tableName, file->name);
  2401. DymosimError(amatError);
  2402. return NULL;
  2403. }
  2404. #if 0
  2405. static int dymTableTranspose( Amatrix *matrix, char *fileName ) {
  2406. /* Transpose the data of the matrix,
  2407. if it is of type doubleMatrix
  2408. */
  2409. double *m2, *m1;
  2410. int nrow = matrix->nrow;
  2411. int ncol = matrix->ncol;
  2412. int i, j;
  2413. /* Return, if matrix need not to be transposed */
  2414. if ( matrix->type != doubleMatrix || nrow <= 1 || ncol <= 1 ) return 0;
  2415. /* Allocate storage for new matrix data */
  2416. m1 = matrix->data.d;
  2417. m2 = (double *) malloc( sizeof(double)*((size_t) nrow*ncol) );
  2418. if ( m2 == NULL ) {
  2419. sprintf(amatError,"Not enough storage available to read matrix\n"
  2420. "from file \"%s\".\n", fileName);
  2421. return 1;
  2422. }
  2423. /* Copy old matrix to new matrix and transpose it */
  2424. for (i=0; i<nrow; i++) {
  2425. for (j=0; j<ncol; j++) {
  2426. m2[j + i*ncol] = m1[i + j*nrow];
  2427. }
  2428. }
  2429. /* Remove old storage */
  2430. free( m1 );
  2431. matrix->data.d = m2;
  2432. return 0;
  2433. }
  2434. #endif
  2435. #endif