ModelicaStandardTables.c 188 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748
  1. /* ModelicaStandardTables.c - External table functions
  2. Copyright (C) 2013-2016, Modelica Association, DLR, and ITI GmbH
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  12. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  13. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /* Implementation of external functions for table computation
  23. in the Modelica Standard Library:
  24. Modelica.Blocks.Sources.CombiTimeTable
  25. Modelica.Blocks.Tables.CombiTable1D
  26. Modelica.Blocks.Tables.CombiTable1Ds
  27. Modelica.Blocks.Tables.CombiTable2D
  28. The following #define's are available.
  29. NO_FILE_SYSTEM : A file system is not present (e.g. on dSPACE or xPC).
  30. DEBUG_TIME_EVENTS : Trace time events of CombiTimeTable
  31. DUMMY_FUNCTION_USERTAB: Use a dummy function "usertab"
  32. NO_TABLE_COPY : Do not copy table data passed to _init functions
  33. This is a potentially unsafe optimization (ticket #1143).
  34. TABLE_SHARE : If NO_FILE_SYTEM is not defined then common/shared table
  35. arrays are stored in a global hash table in order to
  36. avoid superfluous file input access and to decrease the
  37. utilized memory (tickets #1110 and #1550).
  38. Release Notes:
  39. Dec. 16, 2015: by Thomas Beutlich, ITI GmbH
  40. Added univariate Steffen-spline interpolation (ticket #1814)
  41. Nov. 25, 2015: by Thomas Beutlich, ITI GmbH
  42. Added support of struct variables of MATLAB MAT-files
  43. (ticket #1840)
  44. Nov. 16, 2015: by Thomas Beutlich, ITI GmbH
  45. Fixed support of 2x3 and 3x2 2D tables with spline
  46. interpolation (ticket #1820)
  47. Nov. 03, 2015: by Thomas Beutlich, ITI GmbH
  48. Added range checks for column indices of CombiTimeTable and
  49. CombiTable1D (ticket #1816)
  50. Aug. 31, 2015: by Thomas Beutlich, ITI GmbH
  51. Fixed event detection of CombiTimeTable when using a fixed time
  52. step integrator with a step size greater than the event
  53. resolution of the table (ticket #1768)
  54. May 11, 2015: by Thomas Beutlich, ITI GmbH
  55. Added univariate Fritsch-Butland-spline interpolation
  56. (ticket #1717)
  57. Apr. 16, 2015: by Thomas Beutlich, ITI GmbH
  58. Fixed event detection of CombiTimeTable with scaled time
  59. (ticket #1627)
  60. Mar. 18, 2015: by Thomas Beutlich, ITI GmbH
  61. Fixed poor performance of readMatTable for MATLAB MAT-files with
  62. compressed array streams and large (> 1e5) number of rows
  63. (ticket #1682)
  64. Jan. 02, 2015: by Thomas Beutlich, ITI GmbH
  65. Fixed event detection of CombiTimeTable with non-zero start time
  66. (ticket #1619)
  67. Aug. 22, 2014: by Thomas Beutlich, ITI GmbH
  68. Fixed multi-threaded access of common/shared table arrays
  69. (ticket #1556)
  70. Aug. 07, 2014: by Thomas Beutlich, ITI GmbH
  71. Added pure C implementation of common/shared table arrays
  72. (ticket #1550)
  73. May 21, 2014: by Thomas Beutlich, ITI GmbH
  74. Fixed bivariate Akima-spline extrapolation (ticket #1465)
  75. Oct. 17, 2013: by Thomas Beutlich, ITI GmbH
  76. Added support of 2D tables that actually degrade to 1D tables
  77. (ticket #1307)
  78. Sep. 05, 2013: by Thomas Beutlich, ITI GmbH
  79. Fixed ANSI-C compliance (ticket #1263)
  80. Fixed reading table files with Windows line endings on Linux
  81. (ticket #1264)
  82. Apr. 09, 2013: by Thomas Beutlich, ITI GmbH
  83. Implemented a first version
  84. */
  85. #if defined(__gnu_linux__) && !defined(NO_FILE_SYSTEM)
  86. #define _GNU_SOURCE 1
  87. #endif
  88. #include "ModelicaStandardTables.h"
  89. #include "ModelicaUtilities.h"
  90. #if !defined(NO_FILE_SYSTEM)
  91. #include <stdio.h>
  92. #include <locale.h>
  93. #include "ModelicaMatIO.h"
  94. #if defined(TABLE_SHARE)
  95. #define uthash_fatal(msg) ModelicaFormatMessage("Error: %s\n", msg); break
  96. #include "uthash.h"
  97. #include "gconstructor.h"
  98. #endif
  99. #endif
  100. #include <float.h>
  101. #include <math.h>
  102. #include <string.h>
  103. #if defined(TABLE_SHARE) && !defined(NO_FILE_SYSTEM)
  104. /* The standard way to detect posix is to check _POSIX_VERSION,
  105. * which is defined in <unistd.h>
  106. */
  107. #if defined(__unix__) || defined(__linux__) || defined(__APPLE_CC__)
  108. #include <unistd.h>
  109. #endif
  110. #if !defined(_POSIX_) && defined(_POSIX_VERSION)
  111. #define _POSIX_ 1
  112. #endif
  113. #endif
  114. /* ----- Interface enumerations ----- */
  115. enum Smoothness {
  116. LINEAR_SEGMENTS = 1,
  117. CONTINUOUS_DERIVATIVE,
  118. CONSTANT_SEGMENTS,
  119. FRITSCH_BUTLAND_MONOTONE_C1,
  120. STEFFEN_MONOTONE_C1,
  121. AKIMA_C1 = CONTINUOUS_DERIVATIVE
  122. };
  123. enum Extrapolation {
  124. HOLD_LAST_POINT = 1,
  125. LAST_TWO_POINTS,
  126. PERIODIC,
  127. NO_EXTRAPOLATION
  128. };
  129. /* ----- Internal enumerations ----- */
  130. enum PointInterval {
  131. LEFT = -1,
  132. IN_TABLE = 0,
  133. RIGHT = 1
  134. };
  135. enum TableSource {
  136. TABLESOURCE_MODEL = 1,
  137. TABLESOURCE_FILE,
  138. TABLESOURCE_FUNCTION,
  139. TABLESOURCE_FUNCTION_TRANSPOSE
  140. };
  141. /* ----- Internal table memory ----- */
  142. /* 3 (of 4) 1D cubic Hermite spline coefficients (per interval) */
  143. typedef double CubicHermite1D[3];
  144. /* 15 (of 16) 2D cubic Hermite spline coefficients (per grid) */
  145. typedef double CubicHermite2D[15];
  146. /* Left and right interval indices (per interval) */
  147. typedef size_t Interval[2];
  148. typedef struct CombiTimeTable {
  149. char* fileName; /* Name of table file */
  150. char* tableName; /* Name of table */
  151. double* table; /* Table values */
  152. size_t nRow; /* Number of rows of table */
  153. size_t nCol; /* Number of columns of table */
  154. size_t last; /* Last accessed row index of table */
  155. enum Smoothness smoothness; /* Smoothness kind */
  156. enum Extrapolation extrapolation; /* Extrapolation kind */
  157. enum TableSource source; /* Source kind */
  158. int* cols; /* Columns of table to be interpolated */
  159. size_t nCols; /* Number of columns of table to be interpolated */
  160. double startTime; /* Start time of interpolation */
  161. CubicHermite1D* spline; /* Pre-calculated cubic Hermite spline coefficients,
  162. only used if smoothness is AKIMA_C1 or
  163. FRITSCH_BUTLAND_MONOTONE_C1 or STEFFEN_MONOTONE_C1 */
  164. size_t nEvent; /* Time event counter, discrete */
  165. double preNextTimeEvent; /* Time of previous time event, discrete */
  166. double preNextTimeEventCalled; /* Time of previous call of
  167. ModelicaStandardTables_CombiTimeTable_nextTimeEvent, discrete */
  168. size_t maxEvents; /* Maximum number of time events (per period/cycle) */
  169. size_t eventInterval; /* Event interval marker, discrete,
  170. In case of periodic extrapolation this is the current event interval,
  171. otherwise it is the next event interval. */
  172. double tOffset; /* Time offset, calculated by floor function, discrete,
  173. only used if extrapolation is PERIODIC */
  174. Interval* intervals; /* Event interval indices */
  175. } CombiTimeTable;
  176. typedef struct CombiTable1D {
  177. char* fileName; /* Name of table file */
  178. char* tableName; /* Name of table */
  179. double* table; /* Table values */
  180. size_t nRow; /* Number of rows of table */
  181. size_t nCol; /* Number of columns of table */
  182. size_t last; /* Last accessed row index of table */
  183. enum Smoothness smoothness; /* Smoothness kind */
  184. enum TableSource source; /* Source kind */
  185. int* cols; /* Columns of table to be interpolated */
  186. size_t nCols; /* Number of columns of table to be interpolated */
  187. CubicHermite1D* spline; /* Pre-calculated cubic Hermite spline coefficients,
  188. only used if smoothness is AKIMA_C1 or
  189. FRITSCH_BUTLAND_MONOTONE_C1 or STEFFEN_MONOTONE_C1 */
  190. } CombiTable1D;
  191. typedef struct CombiTable2D {
  192. char* fileName; /* Name of table file */
  193. char* tableName; /* Name of table */
  194. double* table; /* Table values */
  195. size_t nRow; /* Number of rows of table */
  196. size_t nCol; /* Number of columns of table */
  197. size_t last1; /* Last accessed row index of table */
  198. size_t last2; /* Last accessed column index of table */
  199. enum Smoothness smoothness; /* Smoothness kind */
  200. enum TableSource source; /* Source kind */
  201. CubicHermite2D* spline; /* Pre-calculated cubic Hermite spline coefficients,
  202. only used if smoothness is AKIMA_C1 */
  203. } CombiTable2D;
  204. /* ----- Internal constants ----- */
  205. #define _EPSILON (1e-10)
  206. #ifndef MAX_TABLE_DIMENSIONS
  207. #define MAX_TABLE_DIMENSIONS (3)
  208. #endif
  209. #define LINE_BUFFER_LENGTH (64)
  210. /* ----- Internal shortcuts ----- */
  211. #define IDX(i, j, n) ((i)*(n) + (j))
  212. #define TABLE(i, j) table[IDX(i, j, nCol)]
  213. #define TABLE_ROW0(j) table[j]
  214. #define TABLE_COL0(i) table[(i)*nCol]
  215. #define LINEAR(u, u0, u1, y0, y1) \
  216. y = (y0) + ((y1) - (y0))*((u) - (u0))/((u1) - (u0));
  217. /*
  218. LINEAR(u0, ...) -> y0
  219. LINEAR(u1, ...) -> y1
  220. */
  221. #define LINEAR_SLOPE(y0, dy_du, du) ((y0) + (dy_du)*(du))
  222. #define BILINEAR(u1, u2, u10, u11, u20, u21, y00, y01, y10, y11) {\
  223. const double tmp = ((u2) - (u20))/((u20) - (u21)); \
  224. y = (y00) + tmp*((y00) - (y01)) + ((u1) - (u10))/((u10) - (u11))* \
  225. ((1 + tmp)*((y00) - (y10)) + tmp*((y11) - (y01))); \
  226. }
  227. /*
  228. BILINEAR(u10, u20, ...) -> y00
  229. BILINEAR(u10, u21, ...) -> y01
  230. BILINEAR(u11, u20, ...) -> y10
  231. BILINEAR(u11, u21, ...) -> y11
  232. */
  233. #if defined(TABLE_SHARE) && !defined(NO_FILE_SYSTEM)
  234. typedef struct TableShare {
  235. char* key; /* Key consisting of concatenated names of table and file */
  236. size_t refCount; /* Reference counter */
  237. size_t nRow; /* Number of rows of table */
  238. size_t nCol; /* Number of columns of table */
  239. double* table; /* Table values */
  240. UT_hash_handle hh; /* Hashable structure */
  241. } TableShare;
  242. /* ----- Static variables ----- */
  243. static TableShare* tableShare = NULL;
  244. #if defined(_POSIX_) && !defined(LABCAR)
  245. #include <pthread.h>
  246. #if defined(G_HAS_CONSTRUCTORS)
  247. static pthread_mutex_t m;
  248. G_DEFINE_CONSTRUCTOR(initializeMutex)
  249. static void initializeMutex(void) {
  250. if (pthread_mutex_init(&m, NULL) != 0) {
  251. ModelicaError("Initialization of mutex failed\n");
  252. }
  253. }
  254. G_DEFINE_DESTRUCTOR(destroyMutex)
  255. static void destroyMutex(void) {
  256. if (pthread_mutex_destroy(&m) != 0) {
  257. ModelicaError("Destruction of mutex failed\n");
  258. }
  259. }
  260. #else
  261. static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  262. #endif
  263. #define MUTEX_LOCK() pthread_mutex_lock(&m)
  264. #define MUTEX_UNLOCK() pthread_mutex_unlock(&m)
  265. #elif defined(_WIN32) && defined(G_HAS_CONSTRUCTORS)
  266. #if !defined(WIN32_LEAN_AND_MEAN)
  267. #define WIN32_LEAN_AND_MEAN
  268. #endif
  269. #include <Windows.h>
  270. static CRITICAL_SECTION cs;
  271. #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
  272. #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(initializeCS)
  273. #endif
  274. G_DEFINE_CONSTRUCTOR(initializeCS)
  275. static void initializeCS(void) {
  276. InitializeCriticalSection(&cs);
  277. }
  278. #ifdef G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
  279. #pragma G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(deleteCS)
  280. #endif
  281. G_DEFINE_DESTRUCTOR(deleteCS)
  282. static void deleteCS(void) {
  283. DeleteCriticalSection(&cs);
  284. }
  285. #define MUTEX_LOCK() EnterCriticalSection(&cs)
  286. #define MUTEX_UNLOCK() LeaveCriticalSection(&cs)
  287. #else
  288. #define MUTEX_LOCK()
  289. #define MUTEX_UNLOCK()
  290. #endif
  291. #endif
  292. /* ----- Function declarations ----- */
  293. extern int usertab(char* tableName, int nipo, int dim[], int* colWise,
  294. double** table);
  295. /* Define tables by statically storing them in function usertab.
  296. This function can be adapted by the user to his/her needs.
  297. -> tableName: Name of table
  298. -> nipo : = 0: time-table required (time interpolation)
  299. = 1: 1D-table required
  300. = 2: 2D-table required
  301. <- dim: Actual values of dimensions
  302. <- colWise: = 0: table stored row-wise (row_1, row_2, ..., row_n)
  303. = 1: table stored column-wise (column_1, column_2, ...)
  304. <- table: Pointer to vector containing a matrix with dimensions "dim"
  305. <- RETURN: = 0: No error
  306. = 1: An error occurred. An error message is printed from usertab.
  307. */
  308. static int isNearlyEqual(double x, double y);
  309. /* Compare two floating-point numbers by threshold _EPSILON */
  310. static size_t findRowIndex(const double* table, size_t nRow, size_t nCol,
  311. size_t last, double x);
  312. /* Find the row index i using binary search such that
  313. * i + 1 < nRow
  314. * table[i*nCol] <= x
  315. * table[(i + 1)*nCol] > x for i + 2 < nRow
  316. */
  317. static size_t findColIndex(_In_ const double* table, size_t nCol, size_t last,
  318. double x) MODELICA_NONNULLATTR;
  319. /* Same as findRowIndex but works on rows */
  320. static int isValidName(_In_z_ const char* name) MODELICA_NONNULLATTR;
  321. /* Check, whether a file or table name is valid */
  322. static int isValidCombiTimeTable(const CombiTimeTable* tableID);
  323. /* Check, whether a CombiTimeTable is well parameterized */
  324. static int isValidCombiTable1D(const CombiTable1D* tableID);
  325. /* Check, whether a CombiTable1D is well parameterized */
  326. static int isValidCombiTable2D(const CombiTable2D* tableID);
  327. /* Check, whether a CombiTable2D is well parameterized */
  328. static enum TableSource getTableSource(_In_z_ const char *tableName,
  329. _In_z_ const char *fileName) MODELICA_NONNULLATTR;
  330. /* Determine table source (file, model or "usertab" function) from table
  331. and file names
  332. */
  333. static void transpose(_Inout_ double* table, size_t nRow, size_t nCol) MODELICA_NONNULLATTR;
  334. /* Cycle-based in-place array transposition */
  335. #if !defined(NO_FILE_SYSTEM)
  336. static double* readTable(_In_z_ const char* tableName, _In_z_ const char* fileName,
  337. _Inout_ size_t* nRow, _Inout_ size_t* nCol, int verbose,
  338. int force) MODELICA_NONNULLATTR;
  339. /* Read a table from an ASCII text or MATLAB MAT-file
  340. <- RETURN: Pointer to array (row-wise storage) of table values
  341. */
  342. static double* readMatTable(_In_z_ const char* tableName, _In_z_ const char* fileName,
  343. _Inout_ size_t* nRow, _Inout_ size_t* nCol) MODELICA_NONNULLATTR;
  344. /* Read a table from a MATLAB MAT-file using MatIO functions
  345. <- RETURN: Pointer to array (row-wise storage) of table values
  346. */
  347. static double* readTxtTable(_In_z_ const char* tableName, _In_z_ const char* fileName,
  348. _Inout_ size_t* nRow, _Inout_ size_t* nCol) MODELICA_NONNULLATTR;
  349. /* Read a table from an ASCII text file
  350. <- RETURN: Pointer to array (row-wise storage) of table values
  351. */
  352. static int readLine(_In_ char** buf, _In_ int* bufLen, _In_ FILE* fp) MODELICA_NONNULLATTR;
  353. /* Read line (of unknown and arbitrary length) from an ASCII text file */
  354. #endif /* #if !defined(NO_FILE_SYSTEM) */
  355. static CubicHermite1D* akimaSpline1DInit(_In_ const double* table, size_t nRow,
  356. size_t nCol, _In_ const int* cols,
  357. size_t nCols) MODELICA_NONNULLATTR;
  358. /* Calculate the coefficients for univariate cubic Hermite spline
  359. interpolation with the Akima slope approximation
  360. <- RETURN: Pointer to array of coefficients
  361. */
  362. static CubicHermite1D* fritschButlandSpline1DInit(_In_ const double* table,
  363. size_t nRow, size_t nCol,
  364. _In_ const int* cols,
  365. size_t nCols) MODELICA_NONNULLATTR;
  366. /* Calculate the coefficients for univariate cubic Hermite spline
  367. interpolation with the Fritsch-Butland slope approximation
  368. <- RETURN: Pointer to array of coefficients
  369. */
  370. static CubicHermite1D* steffenSpline1DInit(_In_ const double* table,
  371. size_t nRow, size_t nCol,
  372. _In_ const int* cols,
  373. size_t nCols) MODELICA_NONNULLATTR;
  374. /* Calculate the coefficients for univariate cubic Hermite spline
  375. interpolation with the Steffen slope approximation
  376. <- RETURN: Pointer to array of coefficients
  377. */
  378. static void spline1DClose(CubicHermite1D** spline);
  379. /* Free allocated memory of the 1D cubic Hermite spline coefficients */
  380. static CubicHermite2D* spline2DInit(_In_ const double* table, size_t nRow,
  381. size_t nCol) MODELICA_NONNULLATTR;
  382. /* Calculate the coefficients for bivariate cubic Hermite spline
  383. interpolation with the Akima algorithm
  384. <- RETURN: Pointer to array of coefficients
  385. */
  386. static void spline2DClose(CubicHermite2D** spline);
  387. /* Free allocated memory of the 2D cubic Hermite spline coefficients */
  388. /* ----- Interface functions ----- */
  389. DYMOLA_STATIC void* ModelicaStandardTables_CombiTimeTable_init(const char* tableName,
  390. const char* fileName,
  391. const double* table,
  392. size_t nRow, size_t nColumn,
  393. double startTime, const int* cols,
  394. size_t nCols, int smoothness,
  395. int extrapolation) {
  396. CombiTimeTable* tableID = (CombiTimeTable*)calloc(1, sizeof(CombiTimeTable));
  397. if (tableID != NULL) {
  398. tableID->smoothness = (enum Smoothness)smoothness;
  399. tableID->extrapolation = (enum Extrapolation)extrapolation;
  400. tableID->nCols = nCols;
  401. if (nCols > 0) {
  402. tableID->cols = (int*)malloc(tableID->nCols*sizeof(int));
  403. if (tableID->cols != NULL) {
  404. memcpy(tableID->cols, cols, tableID->nCols*sizeof(int));
  405. }
  406. else {
  407. free(tableID);
  408. ModelicaError("Memory allocation error\n");
  409. return NULL;
  410. }
  411. }
  412. tableID->startTime = startTime;
  413. tableID->source = getTableSource(tableName, fileName);
  414. switch (tableID->source) {
  415. case TABLESOURCE_FILE:
  416. tableID->tableName = (char*)malloc((strlen(tableName) + 1)*sizeof(char));
  417. if (tableID->tableName != NULL) {
  418. strcpy(tableID->tableName, tableName);
  419. }
  420. else {
  421. if (nCols > 0) {
  422. free(tableID->cols);
  423. }
  424. free(tableID);
  425. ModelicaError("Memory allocation error\n");
  426. return NULL;
  427. }
  428. tableID->fileName = (char*)malloc((strlen(fileName) + 1)*sizeof(char));
  429. if (tableID->fileName != NULL) {
  430. strcpy(tableID->fileName, fileName);
  431. }
  432. else {
  433. free(tableID->tableName);
  434. if (nCols > 0) {
  435. free(tableID->cols);
  436. }
  437. free(tableID);
  438. ModelicaError("Memory allocation error\n");
  439. return NULL;
  440. }
  441. break;
  442. case TABLESOURCE_MODEL:
  443. tableID->nRow = nRow;
  444. tableID->nCol = nColumn;
  445. tableID->table = (double*) table;
  446. if (isValidCombiTimeTable((const CombiTimeTable*)tableID)) {
  447. if (tableID->nRow <= 2) {
  448. if (tableID->smoothness == AKIMA_C1 ||
  449. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  450. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  451. tableID->smoothness = LINEAR_SEGMENTS;
  452. }
  453. }
  454. if (tableID->smoothness == AKIMA_C1) {
  455. /* Initialization of the cubic Hermite spline coefficients */
  456. tableID->spline = akimaSpline1DInit(table,
  457. tableID->nRow, tableID->nCol, (const int*)cols,
  458. tableID->nCols);
  459. if (tableID->spline == NULL) {
  460. if (nCols > 0) {
  461. free(tableID->cols);
  462. }
  463. free(tableID);
  464. ModelicaError("Memory allocation error\n");
  465. return NULL;
  466. }
  467. }
  468. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  469. /* Initialization of the cubic Hermite spline coefficients */
  470. tableID->spline = fritschButlandSpline1DInit(table,
  471. tableID->nRow, tableID->nCol, (const int*)cols,
  472. tableID->nCols);
  473. if (tableID->spline == NULL) {
  474. if (nCols > 0) {
  475. free(tableID->cols);
  476. }
  477. free(tableID);
  478. ModelicaError("Memory allocation error\n");
  479. return NULL;
  480. }
  481. }
  482. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  483. /* Initialization of the cubic Hermite spline coefficients */
  484. tableID->spline = steffenSpline1DInit(table,
  485. tableID->nRow, tableID->nCol, (const int*)cols,
  486. tableID->nCols);
  487. if (tableID->spline == NULL) {
  488. if (nCols > 0) {
  489. free(tableID->cols);
  490. }
  491. free(tableID);
  492. ModelicaError("Memory allocation error\n");
  493. return NULL;
  494. }
  495. }
  496. #if !defined(NO_TABLE_COPY)
  497. tableID->table = (double*)malloc(
  498. tableID->nRow*tableID->nCol*sizeof(double));
  499. if (tableID->table != NULL) {
  500. memcpy(tableID->table, table, tableID->nRow*
  501. tableID->nCol*sizeof(double));
  502. }
  503. else {
  504. if (nCols > 0) {
  505. free(tableID->cols);
  506. }
  507. spline1DClose(&tableID->spline);
  508. free(tableID);
  509. ModelicaError("Memory allocation error\n");
  510. return NULL;
  511. }
  512. #endif
  513. }
  514. else {
  515. tableID->table = NULL;
  516. }
  517. break;
  518. case TABLESOURCE_FUNCTION: {
  519. int colWise;
  520. int dim[MAX_TABLE_DIMENSIONS];
  521. if (usertab((char*)tableName, 0 /* Time-interpolation */, dim,
  522. &colWise, &tableID->table) == 0) {
  523. if (colWise == 0) {
  524. tableID->nRow = (size_t)dim[0];
  525. tableID->nCol = (size_t)dim[1];
  526. }
  527. else {
  528. /* Need to transpose */
  529. double* tableT = (double*)malloc(dim[0]*dim[1]*sizeof(double));
  530. if (tableT != NULL) {
  531. memcpy(tableT, tableID->table, dim[0]*dim[1]*sizeof(double));
  532. tableID->table = tableT;
  533. tableID->nRow = (size_t)dim[1];
  534. tableID->nCol = (size_t)dim[0];
  535. tableID->source = TABLESOURCE_FUNCTION_TRANSPOSE;
  536. transpose(tableID->table, tableID->nRow, tableID->nCol);
  537. }
  538. else {
  539. if (nCols > 0) {
  540. free(tableID->cols);
  541. }
  542. free(tableID);
  543. ModelicaError("Memory allocation error\n");
  544. return NULL;
  545. }
  546. }
  547. if (isValidCombiTimeTable((const CombiTimeTable*)tableID)) {
  548. if (tableID->nRow <= 2) {
  549. if (tableID->smoothness == AKIMA_C1 ||
  550. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  551. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  552. tableID->smoothness = LINEAR_SEGMENTS;
  553. }
  554. }
  555. if (tableID->smoothness == AKIMA_C1) {
  556. /* Initialization of the cubic Hermite spline coefficients */
  557. tableID->spline = akimaSpline1DInit(table,
  558. tableID->nRow, tableID->nCol, (const int*)cols,
  559. tableID->nCols);
  560. if (tableID->spline == NULL) {
  561. if (nCols > 0) {
  562. free(tableID->cols);
  563. }
  564. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  565. free(tableID->table);
  566. }
  567. free(tableID);
  568. ModelicaError("Memory allocation error\n");
  569. return NULL;
  570. }
  571. }
  572. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  573. /* Initialization of the cubic Hermite spline coefficients */
  574. tableID->spline = fritschButlandSpline1DInit(table,
  575. tableID->nRow, tableID->nCol, (const int*)cols,
  576. tableID->nCols);
  577. if (tableID->spline == NULL) {
  578. if (nCols > 0) {
  579. free(tableID->cols);
  580. }
  581. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  582. free(tableID->table);
  583. }
  584. free(tableID);
  585. ModelicaError("Memory allocation error\n");
  586. return NULL;
  587. }
  588. }
  589. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  590. /* Initialization of the cubic Hermite spline coefficients */
  591. tableID->spline = steffenSpline1DInit(table,
  592. tableID->nRow, tableID->nCol, (const int*)cols,
  593. tableID->nCols);
  594. if (tableID->spline == NULL) {
  595. if (nCols > 0) {
  596. free(tableID->cols);
  597. }
  598. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  599. free(tableID->table);
  600. }
  601. free(tableID);
  602. ModelicaError("Memory allocation error\n");
  603. return NULL;
  604. }
  605. }
  606. }
  607. }
  608. break;
  609. }
  610. case TABLESOURCE_FUNCTION_TRANSPOSE:
  611. /* Should not be possible to get here */
  612. break;
  613. default:
  614. if (nCols > 0) {
  615. free(tableID->cols);
  616. }
  617. free(tableID);
  618. ModelicaError("Table source error\n");
  619. return NULL;
  620. }
  621. }
  622. else {
  623. ModelicaError("Memory allocation error\n");
  624. }
  625. return (void*)tableID;
  626. }
  627. DYMOLA_STATIC void ModelicaStandardTables_CombiTimeTable_close(void* _tableID) {
  628. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  629. if (tableID != NULL) {
  630. if (tableID->table != NULL && tableID->source == TABLESOURCE_FILE) {
  631. #if defined(TABLE_SHARE) && !defined(NO_FILE_SYSTEM)
  632. if (tableID->tableName != NULL && tableID->fileName != NULL) {
  633. char* key = malloc((strlen(tableID->tableName) +
  634. strlen(tableID->fileName) + 2)*sizeof(char));
  635. if (key != NULL) {
  636. TableShare *iter;
  637. strcpy(key, tableID->tableName);
  638. strcat(key, "|");
  639. strcat(key, tableID->fileName);
  640. MUTEX_LOCK();
  641. HASH_FIND_STR(tableShare, key, iter);
  642. if (iter != NULL) {
  643. /* Share hit */
  644. if (--iter->refCount == 0) {
  645. free(iter->table);
  646. free(iter->key);
  647. HASH_DEL(tableShare, iter);
  648. free(iter);
  649. }
  650. }
  651. MUTEX_UNLOCK();
  652. free(key);
  653. }
  654. }
  655. else {
  656. /* Should not be possible to get here */
  657. free(tableID->table);
  658. }
  659. #else
  660. free(tableID->table);
  661. #endif
  662. tableID->table = NULL;
  663. }
  664. else if (tableID->table != NULL && (
  665. #if !defined(NO_TABLE_COPY)
  666. tableID->source == TABLESOURCE_MODEL ||
  667. #endif
  668. tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE)) {
  669. free(tableID->table);
  670. tableID->table = NULL;
  671. }
  672. if (tableID->nCols > 0 && tableID->cols != NULL) {
  673. free(tableID->cols);
  674. tableID->cols = NULL;
  675. }
  676. if (tableID->tableName != NULL) {
  677. free(tableID->tableName);
  678. tableID->tableName = NULL;
  679. }
  680. if (tableID->fileName != NULL) {
  681. free(tableID->fileName);
  682. tableID->fileName = NULL;
  683. }
  684. if (tableID->intervals != NULL) {
  685. free(tableID->intervals);
  686. tableID->intervals = NULL;
  687. }
  688. spline1DClose(&tableID->spline);
  689. free(tableID);
  690. }
  691. }
  692. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_getValue(void* _tableID, int iCol,
  693. double t, double nextTimeEvent,
  694. double preNextTimeEvent) {
  695. double y = 0.;
  696. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  697. if (tableID != NULL && tableID->table != NULL && tableID->cols != NULL) {
  698. /* Shift time by start time */
  699. const double tOld = t;
  700. t -= tableID->startTime;
  701. if (t >= 0 && nextTimeEvent < DBL_MAX &&
  702. nextTimeEvent == preNextTimeEvent &&
  703. tableID->startTime >= nextTimeEvent) {
  704. /* Before start time event iteration: Return zero */
  705. return 0.;
  706. }
  707. else if (t >= 0) {
  708. const double* table = tableID->table;
  709. const size_t nRow = tableID->nRow;
  710. const size_t nCol = tableID->nCol;
  711. const size_t col = (size_t)tableID->cols[iCol - 1] - 1;
  712. if (nRow == 1) {
  713. /* Single row */
  714. y = TABLE_ROW0(col);
  715. }
  716. else {
  717. enum PointInterval extrapolate = IN_TABLE;
  718. /* Periodic extrapolation */
  719. if (tableID->extrapolation == PERIODIC) {
  720. const double tMin = TABLE_ROW0(0);
  721. const double tMax = TABLE_COL0(nRow - 1);
  722. const double T = tMax - tMin;
  723. /* Event handling for periodic extrapolation */
  724. if (nextTimeEvent == preNextTimeEvent &&
  725. tOld >= nextTimeEvent) {
  726. /* Before event iteration: Return previous
  727. interval value */
  728. size_t i;
  729. if (tableID->smoothness == CONSTANT_SEGMENTS) {
  730. i = tableID->intervals[
  731. tableID->eventInterval - 1][0];
  732. }
  733. else {
  734. i = tableID->intervals[
  735. tableID->eventInterval - 1][1];
  736. }
  737. y = TABLE(i, col);
  738. return y;
  739. }
  740. else if (nextTimeEvent > preNextTimeEvent &&
  741. tOld >= preNextTimeEvent &&
  742. tableID->startTime < preNextTimeEvent) {
  743. /* In regular (= not start time) event iteration:
  744. Return left interval value */
  745. size_t i = tableID->intervals[
  746. tableID->eventInterval - 1][0];
  747. y = TABLE(i, col);
  748. return y;
  749. }
  750. else {
  751. /* After event iteration */
  752. const size_t i0 = tableID->intervals[
  753. tableID->eventInterval - 1][0];
  754. const size_t i1 = tableID->intervals[
  755. tableID->eventInterval - 1][1];
  756. t -= tableID->tOffset;
  757. if (t < tMin) {
  758. t += T;
  759. }
  760. else if (t > tMax) {
  761. t -= T;
  762. }
  763. tableID->last = findRowIndex(
  764. table, nRow, nCol, tableID->last, t);
  765. /* Event interval correction */
  766. if (tableID->last < i0) {
  767. t = TABLE_COL0(i0);
  768. }
  769. if (tableID->last >= i1) {
  770. if (tableID->eventInterval == 1) {
  771. t = TABLE_COL0(i0);
  772. }
  773. else {
  774. t = TABLE_COL0(i1);
  775. }
  776. }
  777. }
  778. }
  779. else if (t < TABLE_ROW0(0)) {
  780. extrapolate = LEFT;
  781. }
  782. else if (t >= TABLE_COL0(nRow - 1)) {
  783. extrapolate = RIGHT;
  784. if (tableID->extrapolation != PERIODIC) {
  785. /* Event handling for non-periodic extrapolation */
  786. if (nextTimeEvent == preNextTimeEvent &&
  787. nextTimeEvent < DBL_MAX && tOld >= nextTimeEvent) {
  788. /* Before event iteration */
  789. extrapolate = IN_TABLE;
  790. }
  791. }
  792. }
  793. if (extrapolate == IN_TABLE) {
  794. size_t last;
  795. if (tableID->extrapolation == PERIODIC) {
  796. last = findRowIndex(table, nRow, nCol,
  797. tableID->last, t);
  798. }
  799. else {
  800. /* Event handling for non-periodic extrapolation */
  801. if (nextTimeEvent == preNextTimeEvent &&
  802. nextTimeEvent < DBL_MAX && tOld >= nextTimeEvent) {
  803. /* Before event iteration: Return previous
  804. interval value */
  805. if (tableID->eventInterval == 1) {
  806. last = 0;
  807. }
  808. else if (tableID->smoothness == CONSTANT_SEGMENTS) {
  809. last = tableID->intervals[
  810. tableID->eventInterval - 2][0];
  811. }
  812. else if (tableID->smoothness == LINEAR_SEGMENTS) {
  813. last = tableID->intervals[
  814. tableID->eventInterval - 2][1];
  815. }
  816. else if (t >= TABLE_COL0(nRow - 1)) {
  817. last = nRow - 1;
  818. }
  819. else {
  820. last = findRowIndex(table, nRow, nCol,
  821. tableID->last, t);
  822. }
  823. y = TABLE(last, col);
  824. return y;
  825. }
  826. else {
  827. last = findRowIndex(table, nRow, nCol,
  828. tableID->last, t);
  829. if (tableID->eventInterval > 1) {
  830. const size_t i0 = tableID->intervals[
  831. tableID->eventInterval - 2][0];
  832. const size_t i1 = tableID->intervals[
  833. tableID->eventInterval - 2][1];
  834. /* Event interval correction */
  835. if (last < i0) {
  836. last = i0;
  837. }
  838. if (last >= i1) {
  839. last = i0;
  840. }
  841. }
  842. }
  843. }
  844. tableID->last = last;
  845. /* Interpolation */
  846. switch (tableID->smoothness) {
  847. case CONSTANT_SEGMENTS:
  848. if (t >= TABLE_COL0(last + 1)) {
  849. last += 1;
  850. }
  851. y = TABLE(last, col);
  852. break;
  853. case LINEAR_SEGMENTS: {
  854. const double t0 = TABLE_COL0(last);
  855. const double t1 = TABLE_COL0(last + 1);
  856. const double y0 = TABLE(last, col);
  857. const double y1 = TABLE(last + 1, col);
  858. if (isNearlyEqual(t0, t1)) {
  859. y = y1;
  860. }
  861. else {
  862. LINEAR(t, t0, t1, y0, y1)
  863. }
  864. break;
  865. }
  866. case AKIMA_C1:
  867. case FRITSCH_BUTLAND_MONOTONE_C1:
  868. case STEFFEN_MONOTONE_C1:
  869. if (tableID->spline != NULL) {
  870. const double* c = tableID->spline[
  871. IDX(last, iCol - 1, tableID->nCols)];
  872. t -= TABLE_COL0(last);
  873. y = TABLE(last, col); /* c[3] = y0 */
  874. y += ((c[0]*t + c[1])*t + c[2])*t;
  875. }
  876. break;
  877. default:
  878. ModelicaError("Unknown smoothness kind\n");
  879. return y;
  880. }
  881. }
  882. else {
  883. /* Extrapolation */
  884. switch (tableID->extrapolation) {
  885. case NO_EXTRAPOLATION:
  886. ModelicaError("Extrapolation error\n");
  887. return y;
  888. case HOLD_LAST_POINT:
  889. y = (extrapolate == RIGHT) ? TABLE(nRow - 1, col) :
  890. TABLE_ROW0(col);
  891. break;
  892. case LAST_TWO_POINTS: {
  893. const size_t last =
  894. (extrapolate == RIGHT) ? nRow - 2 : 0;
  895. const double t0 = TABLE_COL0(last);
  896. const double y0 = TABLE(last, col);
  897. if (tableID->smoothness == AKIMA_C1 ||
  898. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  899. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  900. if (tableID->spline != NULL) {
  901. const double* c = tableID->spline[
  902. IDX(last, iCol - 1, tableID->nCols)];
  903. if (extrapolate == LEFT) {
  904. y = LINEAR_SLOPE(y0, c[2], t - t0);
  905. }
  906. else /* if (extrapolate == RIGHT) */ {
  907. const double t1 = TABLE_COL0(last + 1);
  908. const double v = t1 - t0;
  909. y = LINEAR_SLOPE(TABLE(last + 1, col),
  910. (3*c[0]*v + 2*c[1])*v + c[2],
  911. t - t1);
  912. }
  913. }
  914. }
  915. else {
  916. const double t1 = TABLE_COL0(last + 1);
  917. const double y1 = TABLE(last + 1, col);
  918. if (isNearlyEqual(t0, t1)) {
  919. y = y1;
  920. }
  921. else {
  922. LINEAR(t, t0, t1, y0, y1)
  923. }
  924. }
  925. break;
  926. }
  927. case PERIODIC:
  928. /* Should not be possible to get here */
  929. break;
  930. default:
  931. ModelicaError("Unknown extrapolation kind\n");
  932. return y;
  933. }
  934. }
  935. }
  936. }
  937. }
  938. return y;
  939. }
  940. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_getDerValue(void* _tableID, int iCol,
  941. double t,
  942. double nextTimeEvent,
  943. double preNextTimeEvent,
  944. double der_t) {
  945. double der_y = 0.;
  946. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  947. if (tableID != NULL && tableID->table != NULL && tableID->cols != NULL) {
  948. /* Shift time by start time */
  949. const double tOld = t;
  950. t -= tableID->startTime;
  951. if (t >= 0 && nextTimeEvent < DBL_MAX &&
  952. nextTimeEvent == preNextTimeEvent &&
  953. tableID->startTime >= nextTimeEvent) {
  954. /* Before start time event iteration: Return zero */
  955. return 0.;
  956. }
  957. else if (t >= 0) {
  958. const double* table = tableID->table;
  959. const size_t nRow = tableID->nRow;
  960. const size_t nCol = tableID->nCol;
  961. const size_t col = (size_t)tableID->cols[iCol - 1] - 1;
  962. if (nRow > 1) {
  963. enum PointInterval extrapolate = IN_TABLE;
  964. size_t last = 0;
  965. int haveLast = 0;
  966. /* Periodic extrapolation */
  967. if (tableID->extrapolation == PERIODIC) {
  968. const double tMin = TABLE_ROW0(0);
  969. const double tMax = TABLE_COL0(nRow - 1);
  970. const double T = tMax - tMin;
  971. /* Event handling for periodic extrapolation */
  972. if (nextTimeEvent == preNextTimeEvent &&
  973. tOld >= nextTimeEvent) {
  974. /* Before event iteration: Return previous
  975. interval value */
  976. last = tableID->intervals[
  977. tableID->eventInterval - 1][1] - 1;
  978. haveLast = 1;
  979. }
  980. else if (nextTimeEvent > preNextTimeEvent &&
  981. tOld >= preNextTimeEvent &&
  982. tableID->startTime < preNextTimeEvent) {
  983. /* In regular (= not start time) event iteration:
  984. Return left interval value */
  985. last = tableID->intervals[
  986. tableID->eventInterval - 1][0];
  987. haveLast = 1;
  988. }
  989. else {
  990. /* After event iteration */
  991. const size_t i0 = tableID->intervals[
  992. tableID->eventInterval - 1][0];
  993. const size_t i1 = tableID->intervals[
  994. tableID->eventInterval - 1][1];
  995. t -= tableID->tOffset;
  996. if (t < tMin) {
  997. t += T;
  998. }
  999. else if (t > tMax) {
  1000. t -= T;
  1001. }
  1002. tableID->last = findRowIndex(
  1003. table, nRow, nCol, tableID->last, t);
  1004. /* Event interval correction */
  1005. if (tableID->last < i0) {
  1006. t = TABLE_COL0(i0);
  1007. }
  1008. if (tableID->last >= i1) {
  1009. if (tableID->eventInterval == 1) {
  1010. t = TABLE_COL0(i0);
  1011. }
  1012. else {
  1013. t = TABLE_COL0(i1);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. else if (t < TABLE_ROW0(0)) {
  1019. extrapolate = LEFT;
  1020. }
  1021. else if (t >= TABLE_COL0(nRow - 1)) {
  1022. extrapolate = RIGHT;
  1023. if (tableID->extrapolation != PERIODIC) {
  1024. /* Event handling for non-periodic extrapolation */
  1025. if (nextTimeEvent == preNextTimeEvent &&
  1026. nextTimeEvent < DBL_MAX && tOld >= nextTimeEvent) {
  1027. /* Before event iteration */
  1028. extrapolate = IN_TABLE;
  1029. }
  1030. }
  1031. }
  1032. if (extrapolate == IN_TABLE) {
  1033. if (tableID->extrapolation != PERIODIC) {
  1034. /* Event handling for non-periodic extrapolation */
  1035. if (nextTimeEvent == preNextTimeEvent &&
  1036. nextTimeEvent < DBL_MAX && tOld >= nextTimeEvent) {
  1037. /* Before event iteration */
  1038. if (tableID->eventInterval == 1) {
  1039. last = 0;
  1040. extrapolate = LEFT;
  1041. }
  1042. else if (tableID->smoothness == CONSTANT_SEGMENTS) {
  1043. last = tableID->intervals[
  1044. tableID->eventInterval - 2][0];
  1045. }
  1046. else if (tableID->smoothness == LINEAR_SEGMENTS) {
  1047. last = tableID->intervals[
  1048. tableID->eventInterval - 2][1];
  1049. }
  1050. else if (t >= TABLE_COL0(nRow - 1)) {
  1051. last = nRow - 1;
  1052. }
  1053. else {
  1054. last = findRowIndex(table, nRow, nCol,
  1055. tableID->last, t);
  1056. tableID->last = last;
  1057. }
  1058. if (last > 0 && extrapolate == IN_TABLE) {
  1059. last--;
  1060. }
  1061. haveLast = 1;
  1062. }
  1063. }
  1064. if (!haveLast) {
  1065. last = findRowIndex(table, nRow, nCol, tableID->last, t);
  1066. tableID->last = last;
  1067. }
  1068. if (tableID->extrapolation != PERIODIC &&
  1069. tableID->eventInterval > 1) {
  1070. const size_t i0 = tableID->intervals[
  1071. tableID->eventInterval - 2][0];
  1072. const size_t i1 = tableID->intervals[
  1073. tableID->eventInterval - 2][1];
  1074. if (last < i0) {
  1075. last = i0;
  1076. }
  1077. if (last >= i1) {
  1078. last = i0;
  1079. }
  1080. }
  1081. }
  1082. if (extrapolate == IN_TABLE) {
  1083. /* Interpolation */
  1084. switch (tableID->smoothness) {
  1085. case CONSTANT_SEGMENTS:
  1086. break;
  1087. case LINEAR_SEGMENTS: {
  1088. const double t0 = TABLE_COL0(last);
  1089. const double t1 = TABLE_COL0(last + 1);
  1090. if (!isNearlyEqual(t0, t1)) {
  1091. der_y = (TABLE(last + 1, col) - TABLE(last, col))/
  1092. (t1 - t0);
  1093. der_y *= der_t;
  1094. }
  1095. break;
  1096. }
  1097. case AKIMA_C1:
  1098. case FRITSCH_BUTLAND_MONOTONE_C1:
  1099. case STEFFEN_MONOTONE_C1:
  1100. if (tableID->spline != NULL) {
  1101. const double* c = tableID->spline[
  1102. IDX(last, iCol - 1, tableID->nCols)];
  1103. t -= TABLE_COL0(last);
  1104. der_y = (3*c[0]*t + 2*c[1])*t + c[2];
  1105. der_y *= der_t;
  1106. }
  1107. break;
  1108. default:
  1109. ModelicaError("Unknown smoothness kind\n");
  1110. return der_y;
  1111. }
  1112. }
  1113. else {
  1114. /* Extrapolation */
  1115. switch (tableID->extrapolation) {
  1116. case NO_EXTRAPOLATION:
  1117. ModelicaError("Extrapolation error\n");
  1118. return der_y;
  1119. case HOLD_LAST_POINT:
  1120. break;
  1121. case LAST_TWO_POINTS:
  1122. last = (extrapolate == RIGHT) ? nRow - 2 : 0;
  1123. if (tableID->smoothness == AKIMA_C1 ||
  1124. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1125. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1126. if(tableID->spline) {
  1127. const double* c = tableID->spline[
  1128. IDX(last, iCol - 1, tableID->nCols)];
  1129. if (extrapolate == LEFT) {
  1130. der_y = c[2];
  1131. }
  1132. else /* if (extrapolate == RIGHT) */ {
  1133. der_y = TABLE_COL0(last + 1) -
  1134. TABLE_COL0(last); /* = (t1 - t0) */
  1135. der_y = (3*c[0]*der_y + 2*c[1])*
  1136. der_y + c[2];
  1137. }
  1138. }
  1139. }
  1140. else {
  1141. const double t0 = TABLE_COL0(last);
  1142. const double t1 = TABLE_COL0(last + 1);
  1143. if (!isNearlyEqual(t0, t1)) {
  1144. der_y = (TABLE(last + 1, col) - TABLE(last, col))/
  1145. (t1 - t0);
  1146. }
  1147. }
  1148. der_y *= der_t;
  1149. break;
  1150. case PERIODIC:
  1151. /* Should not be possible to get here */
  1152. break;
  1153. default:
  1154. ModelicaError("Unknown extrapolation kind\n");
  1155. return der_y;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. }
  1161. return der_y;
  1162. }
  1163. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_minimumTime(void* _tableID) {
  1164. double tMin = 0.;
  1165. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  1166. if (tableID != NULL && tableID->table != NULL) {
  1167. const double* table = tableID->table;
  1168. tMin = TABLE_ROW0(0);
  1169. }
  1170. return tMin;
  1171. }
  1172. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_maximumTime(void* _tableID) {
  1173. double tMax = 0.;
  1174. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  1175. if (tableID != NULL && tableID->table != NULL) {
  1176. const double* table = tableID->table;
  1177. const size_t nCol = tableID->nCol;
  1178. tMax = TABLE_COL0(tableID->nRow - 1);
  1179. }
  1180. return tMax;
  1181. }
  1182. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_nextTimeEvent(void* _tableID,
  1183. double t) {
  1184. double nextTimeEvent = DBL_MAX;
  1185. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  1186. if (tableID != NULL && tableID->table != NULL) {
  1187. const double* table = tableID->table;
  1188. const size_t nRow = tableID->nRow;
  1189. const size_t nCol = tableID->nCol;
  1190. if (tableID->nEvent > 0) {
  1191. if (t > tableID->preNextTimeEventCalled) {
  1192. tableID->preNextTimeEventCalled = t;
  1193. }
  1194. else {
  1195. return tableID->preNextTimeEvent;
  1196. }
  1197. }
  1198. else {
  1199. /* Determine maximum number of time events (per period) */
  1200. double tEvent = TABLE_ROW0(0);
  1201. const double tMax = TABLE_COL0(nRow - 1);
  1202. size_t i, eventInterval;
  1203. /* There is at least one time event at the interval boundaries */
  1204. tableID->maxEvents = 1;
  1205. for (i = 0; i < nRow - 1; i++) {
  1206. double t0 = TABLE_COL0(i);
  1207. double t1 = TABLE_COL0(i + 1);
  1208. if (t1 > tEvent && !isNearlyEqual(t1, tMax)) {
  1209. if (tableID->smoothness == CONSTANT_SEGMENTS) {
  1210. if (!isNearlyEqual(t0, t1)) {
  1211. tEvent = t1;
  1212. tableID->maxEvents++;
  1213. }
  1214. }
  1215. else if (isNearlyEqual(t0, t1)) {
  1216. tEvent = t1;
  1217. tableID->maxEvents++;
  1218. }
  1219. }
  1220. }
  1221. /* Once again with storage of indices of event intervals */
  1222. tableID->intervals = (Interval*)calloc(tableID->maxEvents,
  1223. sizeof(Interval));
  1224. if (tableID->intervals == NULL) {
  1225. ModelicaError("Memory allocation error\n");
  1226. return nextTimeEvent;
  1227. }
  1228. tEvent = TABLE_ROW0(0);
  1229. eventInterval = 0;
  1230. if (tableID->smoothness == CONSTANT_SEGMENTS) {
  1231. for (i = 0; i < nRow - 1 &&
  1232. eventInterval < tableID->maxEvents; i++) {
  1233. double t0 = TABLE_COL0(i);
  1234. double t1 = TABLE_COL0(i + 1);
  1235. if (t1 > tEvent) {
  1236. if (!isNearlyEqual(t0, t1)) {
  1237. tEvent = t1;
  1238. tableID->intervals[eventInterval][0] = i;
  1239. tableID->intervals[eventInterval][1] = i + 1;
  1240. eventInterval++;
  1241. }
  1242. else {
  1243. tableID->intervals[eventInterval][0] = i + 1;
  1244. }
  1245. }
  1246. else {
  1247. tableID->intervals[eventInterval][1] = i + 1;
  1248. }
  1249. }
  1250. }
  1251. else {
  1252. for (i = 0; i < nRow - 1 &&
  1253. eventInterval < tableID->maxEvents; i++) {
  1254. double t0 = TABLE_COL0(i);
  1255. double t1 = TABLE_COL0(i + 1);
  1256. if (t1 > tEvent) {
  1257. if (isNearlyEqual(t0, t1)) {
  1258. tEvent = t1;
  1259. tableID->intervals[eventInterval][1] = i;
  1260. eventInterval++;
  1261. if (eventInterval < tableID->maxEvents) {
  1262. tableID->intervals[eventInterval][0] = i + 1;
  1263. }
  1264. }
  1265. else {
  1266. tableID->intervals[eventInterval][1] = i + 1;
  1267. }
  1268. }
  1269. else {
  1270. tableID->intervals[eventInterval][0] = i + 1;
  1271. }
  1272. }
  1273. }
  1274. }
  1275. if (t < tableID->startTime) {
  1276. nextTimeEvent = tableID->startTime;
  1277. }
  1278. else if (nRow > 1) {
  1279. const double tMin = TABLE_ROW0(0);
  1280. const double tMax = TABLE_COL0(nRow - 1);
  1281. const double T = tMax - tMin;
  1282. if (tableID->eventInterval == 0) {
  1283. /* Initialization of event interval */
  1284. #if defined(DEBUG_TIME_EVENTS)
  1285. const double tOld = t;
  1286. #endif
  1287. double tEvent = tMin;
  1288. size_t i, iStart, iEnd;
  1289. t -= tableID->startTime;
  1290. if (tableID->extrapolation == PERIODIC) {
  1291. /* Initialization of offset time */
  1292. tableID->tOffset = floor((t - tMin)/T)*T;
  1293. t -= tableID->tOffset;
  1294. if (t < tMin) {
  1295. t += T;
  1296. }
  1297. else if (t > tMax) {
  1298. t -= T;
  1299. }
  1300. iStart = findRowIndex(table, nRow, nCol, tableID->last,
  1301. t + _EPSILON*T);
  1302. nextTimeEvent = tMax;
  1303. tableID->eventInterval = 1;
  1304. iEnd = iStart < (nRow - 1) ? iStart : (nRow - 1);
  1305. }
  1306. else if (t > tMax) {
  1307. iStart = nRow - 1;
  1308. tableID->eventInterval = tableID->maxEvents + 1;
  1309. iEnd = 0;
  1310. }
  1311. else if (t < tMin) {
  1312. iStart = nRow - 1;
  1313. nextTimeEvent = tMin;
  1314. tableID->eventInterval = 1;
  1315. iEnd = 0;
  1316. }
  1317. else if (tableID->smoothness == AKIMA_C1 ||
  1318. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1319. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1320. iStart = nRow - 1;
  1321. nextTimeEvent = tMax;
  1322. iEnd = 0;
  1323. }
  1324. else {
  1325. iStart = findRowIndex(table, nRow, nCol, tableID->last,
  1326. t + _EPSILON*T);
  1327. nextTimeEvent = tMax;
  1328. tableID->eventInterval = 2;
  1329. iEnd = iStart < (nRow - 1) ? iStart : (nRow - 1);
  1330. }
  1331. for (i = iStart + 1; i < nRow - 1; i++) {
  1332. double t0 = TABLE_COL0(i);
  1333. double t1 = TABLE_COL0(i + 1);
  1334. if (t0 > t) {
  1335. if (tableID->smoothness == CONSTANT_SEGMENTS) {
  1336. nextTimeEvent = t0;
  1337. break;
  1338. }
  1339. else if (isNearlyEqual(t0, t1)) {
  1340. nextTimeEvent = t0;
  1341. break;
  1342. }
  1343. }
  1344. }
  1345. for (i = 0; i < iEnd; i++) {
  1346. double t0 = TABLE_COL0(i);
  1347. double t1 = TABLE_COL0(i + 1);
  1348. if (t1 > tEvent && !isNearlyEqual(t1, tMax)) {
  1349. if (tableID->smoothness == CONSTANT_SEGMENTS) {
  1350. if (!isNearlyEqual(t0, t1)) {
  1351. tEvent = t1;
  1352. tableID->eventInterval++;
  1353. }
  1354. }
  1355. else if (isNearlyEqual(t0, t1)) {
  1356. tEvent = t1;
  1357. tableID->eventInterval++;
  1358. }
  1359. }
  1360. }
  1361. if (tableID->extrapolation == PERIODIC) {
  1362. nextTimeEvent += tableID->tOffset;
  1363. if (tableID->eventInterval == tableID->maxEvents) {
  1364. tableID->tOffset += T;
  1365. }
  1366. }
  1367. #if defined(DEBUG_TIME_EVENTS)
  1368. t = tOld;
  1369. #endif
  1370. if (nextTimeEvent < DBL_MAX) {
  1371. nextTimeEvent += tableID->startTime;
  1372. }
  1373. }
  1374. else {
  1375. do {
  1376. if (tableID->extrapolation == PERIODIC) {
  1377. /* Increment event interval */
  1378. tableID->eventInterval =
  1379. 1 + tableID->eventInterval % tableID->maxEvents;
  1380. if (tableID->eventInterval == tableID->maxEvents) {
  1381. nextTimeEvent = tMax + tableID->tOffset +
  1382. tableID->startTime;
  1383. tableID->tOffset += T;
  1384. }
  1385. else {
  1386. size_t i = tableID->intervals[
  1387. tableID->eventInterval - 1][1];
  1388. nextTimeEvent = TABLE_COL0(i) + tableID->tOffset +
  1389. tableID->startTime;
  1390. }
  1391. }
  1392. else if (tableID->eventInterval <= tableID->maxEvents) {
  1393. size_t i = tableID->intervals[
  1394. tableID->eventInterval - 1][1];
  1395. nextTimeEvent = TABLE_COL0(i) + tableID->startTime;
  1396. /* Increment event interval */
  1397. tableID->eventInterval++;
  1398. }
  1399. else {
  1400. nextTimeEvent = DBL_MAX;
  1401. }
  1402. } while (nextTimeEvent < t);
  1403. }
  1404. }
  1405. if (nextTimeEvent > tableID->preNextTimeEvent) {
  1406. tableID->preNextTimeEvent = nextTimeEvent;
  1407. tableID->nEvent++;
  1408. }
  1409. #if defined(DEBUG_TIME_EVENTS)
  1410. if (nextTimeEvent < DBL_MAX) {
  1411. ModelicaFormatMessage("At time %.17lg: %lu. time event at %.17lg\n", t,
  1412. (unsigned long)tableID->nEvent, nextTimeEvent);
  1413. }
  1414. else {
  1415. ModelicaFormatMessage("No more time events for time > %.17lg\n", t);
  1416. }
  1417. #endif
  1418. }
  1419. else {
  1420. /* Should not be possible to get here */
  1421. ModelicaError(
  1422. "No table data available for detection of time events\n");
  1423. return nextTimeEvent;
  1424. }
  1425. return nextTimeEvent;
  1426. }
  1427. DYMOLA_STATIC double ModelicaStandardTables_CombiTimeTable_read(void* _tableID, int force,
  1428. int verbose) {
  1429. #if !defined(NO_FILE_SYSTEM)
  1430. CombiTimeTable* tableID = (CombiTimeTable*)_tableID;
  1431. if (tableID != NULL && tableID->source == TABLESOURCE_FILE) {
  1432. if (force || tableID->table == NULL) {
  1433. #if !defined(TABLE_SHARE)
  1434. if (tableID->table != NULL) {
  1435. free(tableID->table);
  1436. }
  1437. #endif
  1438. tableID->table = readTable(tableID->tableName,
  1439. tableID->fileName, &tableID->nRow, &tableID->nCol,
  1440. verbose, force);
  1441. if (tableID->table == NULL) {
  1442. return 0.; /* Error */
  1443. }
  1444. if (!isValidCombiTimeTable((const CombiTimeTable*)tableID)) {
  1445. return 0.; /* Error */
  1446. }
  1447. if (tableID->nRow <= 2) {
  1448. if (tableID->smoothness == AKIMA_C1 ||
  1449. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1450. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1451. tableID->smoothness = LINEAR_SEGMENTS;
  1452. }
  1453. }
  1454. if (tableID->smoothness == AKIMA_C1) {
  1455. /* Reinitialization of the cubic Hermite spline coefficients */
  1456. spline1DClose(&tableID->spline);
  1457. tableID->spline = akimaSpline1DInit(
  1458. (const double*)tableID->table, tableID->nRow,
  1459. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1460. if (tableID->spline == NULL) {
  1461. ModelicaError("Memory allocation error\n");
  1462. return 0.; /* Error */
  1463. }
  1464. }
  1465. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  1466. /* Reinitialization of the cubic Hermite spline coefficients */
  1467. spline1DClose(&tableID->spline);
  1468. tableID->spline = fritschButlandSpline1DInit(
  1469. (const double*)tableID->table, tableID->nRow,
  1470. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1471. if (tableID->spline == NULL) {
  1472. ModelicaError("Memory allocation error\n");
  1473. return 0.; /* Error */
  1474. }
  1475. }
  1476. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1477. /* Reinitialization of the cubic Hermite spline coefficients */
  1478. spline1DClose(&tableID->spline);
  1479. tableID->spline = steffenSpline1DInit(
  1480. (const double*)tableID->table, tableID->nRow,
  1481. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1482. if (tableID->spline == NULL) {
  1483. ModelicaError("Memory allocation error\n");
  1484. return 0.; /* Error */
  1485. }
  1486. }
  1487. }
  1488. }
  1489. #endif
  1490. return 1.; /* Success */
  1491. }
  1492. DYMOLA_STATIC void* ModelicaStandardTables_CombiTable1D_init(const char* tableName,
  1493. const char* fileName,
  1494. const double* table, size_t nRow,
  1495. size_t nColumn, const int* cols,
  1496. size_t nCols, int smoothness) {
  1497. CombiTable1D* tableID = (CombiTable1D*)calloc(1, sizeof(CombiTable1D));
  1498. if (tableID != NULL) {
  1499. tableID->smoothness = (enum Smoothness)smoothness;
  1500. tableID->nCols = nCols;
  1501. if (nCols > 0) {
  1502. tableID->cols = (int*)malloc(tableID->nCols*sizeof(int));
  1503. if (tableID->cols != NULL) {
  1504. memcpy(tableID->cols, cols, tableID->nCols*sizeof(int));
  1505. }
  1506. else {
  1507. free(tableID);
  1508. ModelicaError("Memory allocation error\n");
  1509. return NULL;
  1510. }
  1511. }
  1512. tableID->source = getTableSource(tableName, fileName);
  1513. switch (tableID->source) {
  1514. case TABLESOURCE_FILE:
  1515. tableID->tableName = (char*)malloc(
  1516. (strlen(tableName) + 1)*sizeof(char));
  1517. if (tableID->tableName != NULL) {
  1518. strcpy(tableID->tableName, tableName);
  1519. }
  1520. else {
  1521. if (nCols > 0) {
  1522. free(tableID->cols);
  1523. }
  1524. free(tableID);
  1525. ModelicaError("Memory allocation error\n");
  1526. return NULL;
  1527. }
  1528. tableID->fileName = (char*)malloc(
  1529. (strlen(fileName) + 1)*sizeof(char));
  1530. if (tableID->fileName != NULL) {
  1531. strcpy(tableID->fileName, fileName);
  1532. }
  1533. else {
  1534. free(tableID->tableName);
  1535. if (nCols > 0) {
  1536. free(tableID->cols);
  1537. }
  1538. free(tableID);
  1539. ModelicaError("Memory allocation error\n");
  1540. return NULL;
  1541. }
  1542. break;
  1543. case TABLESOURCE_MODEL:
  1544. tableID->nRow = nRow;
  1545. tableID->nCol = nColumn;
  1546. tableID->table = (double*) table;
  1547. if (isValidCombiTable1D((const CombiTable1D*)tableID)) {
  1548. if (tableID->nRow <= 2) {
  1549. if (tableID->smoothness == AKIMA_C1 ||
  1550. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1551. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1552. tableID->smoothness = LINEAR_SEGMENTS;
  1553. }
  1554. }
  1555. if (tableID->smoothness == AKIMA_C1) {
  1556. /* Initialization of the cubic Hermite spline coefficients */
  1557. tableID->spline = akimaSpline1DInit(table,
  1558. tableID->nRow, tableID->nCol, (const int*)cols,
  1559. tableID->nCols);
  1560. if (tableID->spline == NULL) {
  1561. if (nCols > 0) {
  1562. free(tableID->cols);
  1563. }
  1564. free(tableID);
  1565. ModelicaError("Memory allocation error\n");
  1566. return NULL;
  1567. }
  1568. }
  1569. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  1570. /* Initialization of the cubic Hermite spline coefficients */
  1571. tableID->spline = fritschButlandSpline1DInit(table,
  1572. tableID->nRow, tableID->nCol, (const int*)cols,
  1573. tableID->nCols);
  1574. if (tableID->spline == NULL) {
  1575. if (nCols > 0) {
  1576. free(tableID->cols);
  1577. }
  1578. free(tableID);
  1579. ModelicaError("Memory allocation error\n");
  1580. return NULL;
  1581. }
  1582. }
  1583. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1584. /* Initialization of the cubic Hermite spline coefficients */
  1585. tableID->spline = steffenSpline1DInit(table,
  1586. tableID->nRow, tableID->nCol, (const int*)cols,
  1587. tableID->nCols);
  1588. if (tableID->spline == NULL) {
  1589. if (nCols > 0) {
  1590. free(tableID->cols);
  1591. }
  1592. free(tableID);
  1593. ModelicaError("Memory allocation error\n");
  1594. return NULL;
  1595. }
  1596. }
  1597. #if !defined(NO_TABLE_COPY)
  1598. tableID->table = (double*)malloc(
  1599. tableID->nRow*tableID->nCol*sizeof(double));
  1600. if (tableID->table != NULL) {
  1601. memcpy(tableID->table, table, tableID->nRow*
  1602. tableID->nCol*sizeof(double));
  1603. }
  1604. else {
  1605. if (nCols > 0) {
  1606. free(tableID->cols);
  1607. }
  1608. spline1DClose(&tableID->spline);
  1609. free(tableID);
  1610. ModelicaError("Memory allocation error\n");
  1611. return NULL;
  1612. }
  1613. #endif
  1614. }
  1615. else {
  1616. tableID->table = NULL;
  1617. }
  1618. break;
  1619. case TABLESOURCE_FUNCTION: {
  1620. int colWise;
  1621. int dim[MAX_TABLE_DIMENSIONS];
  1622. if (usertab((char*)tableName, 1 /* 1D-interpolation */, dim,
  1623. &colWise, &tableID->table) == 0) {
  1624. if (colWise == 0) {
  1625. tableID->nRow = (size_t)dim[0];
  1626. tableID->nCol = (size_t)dim[1];
  1627. }
  1628. else {
  1629. /* Need to transpose */
  1630. double* tableT = (double*)malloc(dim[0]*dim[1]*sizeof(double));
  1631. if (tableT != NULL) {
  1632. memcpy(tableT, tableID->table, dim[0]*dim[1]*sizeof(double));
  1633. tableID->table = tableT;
  1634. tableID->nRow = (size_t)dim[1];
  1635. tableID->nCol = (size_t)dim[0];
  1636. tableID->source = TABLESOURCE_FUNCTION_TRANSPOSE;
  1637. transpose(tableID->table, tableID->nRow, tableID->nCol);
  1638. }
  1639. else {
  1640. if (nCols > 0) {
  1641. free(tableID->cols);
  1642. }
  1643. free(tableID);
  1644. ModelicaError("Memory allocation error\n");
  1645. return NULL;
  1646. }
  1647. }
  1648. if (isValidCombiTable1D((const CombiTable1D*)tableID)) {
  1649. if (tableID->nRow <= 2) {
  1650. if (tableID->smoothness == AKIMA_C1 ||
  1651. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1652. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1653. tableID->smoothness = LINEAR_SEGMENTS;
  1654. }
  1655. }
  1656. if (tableID->smoothness == AKIMA_C1) {
  1657. /* Initialization of the cubic Hermite spline coefficients */
  1658. tableID->spline = akimaSpline1DInit(table,
  1659. tableID->nRow, tableID->nCol, (const int*)cols,
  1660. tableID->nCols);
  1661. if (tableID->spline == NULL) {
  1662. if (nCols > 0) {
  1663. free(tableID->cols);
  1664. }
  1665. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  1666. free(tableID->table);
  1667. }
  1668. free(tableID);
  1669. ModelicaError("Memory allocation error\n");
  1670. return NULL;
  1671. }
  1672. }
  1673. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  1674. /* Initialization of the cubic Hermite spline coefficients */
  1675. tableID->spline = fritschButlandSpline1DInit(table,
  1676. tableID->nRow, tableID->nCol, (const int*)cols,
  1677. tableID->nCols);
  1678. if (tableID->spline == NULL) {
  1679. if (nCols > 0) {
  1680. free(tableID->cols);
  1681. }
  1682. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  1683. free(tableID->table);
  1684. }
  1685. free(tableID);
  1686. ModelicaError("Memory allocation error\n");
  1687. return NULL;
  1688. }
  1689. }
  1690. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1691. /* Initialization of the cubic Hermite spline coefficients */
  1692. tableID->spline = steffenSpline1DInit(table,
  1693. tableID->nRow, tableID->nCol, (const int*)cols,
  1694. tableID->nCols);
  1695. if (tableID->spline == NULL) {
  1696. if (nCols > 0) {
  1697. free(tableID->cols);
  1698. }
  1699. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  1700. free(tableID->table);
  1701. }
  1702. free(tableID);
  1703. ModelicaError("Memory allocation error\n");
  1704. return NULL;
  1705. }
  1706. }
  1707. }
  1708. }
  1709. break;
  1710. }
  1711. case TABLESOURCE_FUNCTION_TRANSPOSE:
  1712. /* Should not be possible to get here */
  1713. break;
  1714. default:
  1715. if (nCols > 0) {
  1716. free(tableID->cols);
  1717. }
  1718. free(tableID);
  1719. ModelicaError("Table source error\n");
  1720. return NULL;
  1721. }
  1722. }
  1723. else {
  1724. ModelicaError("Memory allocation error\n");
  1725. }
  1726. return (void*)tableID;
  1727. }
  1728. DYMOLA_STATIC void ModelicaStandardTables_CombiTable1D_close(void* _tableID) {
  1729. CombiTable1D* tableID = (CombiTable1D*)_tableID;
  1730. if (tableID != NULL) {
  1731. if (tableID->table != NULL && tableID->source == TABLESOURCE_FILE) {
  1732. #if defined(TABLE_SHARE) && !defined(NO_FILE_SYSTEM)
  1733. if (tableID->tableName != NULL && tableID->fileName != NULL) {
  1734. char* key = malloc((strlen(tableID->tableName) +
  1735. strlen(tableID->fileName) + 2)*sizeof(char));
  1736. if (key != NULL) {
  1737. TableShare *iter;
  1738. strcpy(key, tableID->tableName);
  1739. strcat(key, "|");
  1740. strcat(key, tableID->fileName);
  1741. MUTEX_LOCK();
  1742. HASH_FIND_STR(tableShare, key, iter);
  1743. if (iter != NULL) {
  1744. /* Share hit */
  1745. if (--iter->refCount == 0) {
  1746. free(iter->table);
  1747. free(iter->key);
  1748. HASH_DEL(tableShare, iter);
  1749. free(iter);
  1750. }
  1751. }
  1752. MUTEX_UNLOCK();
  1753. free(key);
  1754. }
  1755. }
  1756. else {
  1757. /* Should not be possible to get here */
  1758. free(tableID->table);
  1759. }
  1760. #else
  1761. free(tableID->table);
  1762. #endif
  1763. tableID->table = NULL;
  1764. }
  1765. else if (tableID->table != NULL && (
  1766. #if !defined(NO_TABLE_COPY)
  1767. tableID->source == TABLESOURCE_MODEL ||
  1768. #endif
  1769. tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE)) {
  1770. free(tableID->table);
  1771. tableID->table = NULL;
  1772. }
  1773. if (tableID->nCols > 0 && tableID->cols != NULL) {
  1774. free(tableID->cols);
  1775. tableID->cols = NULL;
  1776. }
  1777. if (tableID->tableName != NULL) {
  1778. free(tableID->tableName);
  1779. tableID->tableName = NULL;
  1780. }
  1781. if (tableID->fileName != NULL) {
  1782. free(tableID->fileName);
  1783. tableID->fileName = NULL;
  1784. }
  1785. spline1DClose(&tableID->spline);
  1786. free(tableID);
  1787. }
  1788. }
  1789. DYMOLA_STATIC double ModelicaStandardTables_CombiTable1D_getValue(void* _tableID, int iCol,
  1790. double u) {
  1791. double y = 0.;
  1792. CombiTable1D* tableID = (CombiTable1D*)_tableID;
  1793. if (tableID != NULL && tableID->table != NULL && tableID->cols != NULL) {
  1794. const double* table = tableID->table;
  1795. const size_t nRow = tableID->nRow;
  1796. const size_t nCol = tableID->nCol;
  1797. const size_t col = (size_t)tableID->cols[iCol - 1] - 1;
  1798. if (nRow == 1) {
  1799. /* Single row */
  1800. y = TABLE_ROW0(col);
  1801. }
  1802. else {
  1803. enum PointInterval extrapolate = IN_TABLE;
  1804. size_t last;
  1805. if (u < TABLE_ROW0(0)) {
  1806. extrapolate = LEFT;
  1807. last = 0;
  1808. }
  1809. else if (u > TABLE_COL0(nRow - 1)) {
  1810. extrapolate = RIGHT;
  1811. last = nRow - 2;
  1812. }
  1813. else {
  1814. last = findRowIndex(table, nRow, nCol, tableID->last, u);
  1815. tableID->last = last;
  1816. }
  1817. switch (tableID->smoothness) {
  1818. case CONSTANT_SEGMENTS:
  1819. if (extrapolate == IN_TABLE) {
  1820. if (u >= TABLE_COL0(last + 1)) {
  1821. last += 1;
  1822. }
  1823. y = TABLE(last, col);
  1824. break;
  1825. }
  1826. /* Fall through: linear extrapolation */
  1827. case LINEAR_SEGMENTS: {
  1828. const double u0 = TABLE_COL0(last);
  1829. const double u1 = TABLE_COL0(last + 1);
  1830. const double y0 = TABLE(last, col);
  1831. const double y1 = TABLE(last + 1, col);
  1832. LINEAR(u, u0, u1, y0, y1)
  1833. break;
  1834. }
  1835. case AKIMA_C1:
  1836. case FRITSCH_BUTLAND_MONOTONE_C1:
  1837. case STEFFEN_MONOTONE_C1:
  1838. if (tableID->spline != NULL) {
  1839. const double* c = tableID->spline[
  1840. IDX(last, iCol - 1, tableID->nCols)];
  1841. const double u0 = TABLE_COL0(last);
  1842. if (extrapolate == IN_TABLE) {
  1843. const double v = u - u0;
  1844. y = TABLE(last, col); /* c[3] = y0 */
  1845. y += ((c[0]*v + c[1])*v + c[2])*v;
  1846. }
  1847. else if (extrapolate == LEFT) {
  1848. y = LINEAR_SLOPE(TABLE(last, col), c[2], u - u0);
  1849. }
  1850. else /* if (extrapolate == RIGHT) */ {
  1851. const double u1 = TABLE_COL0(last + 1);
  1852. const double v = u1 - u0;
  1853. y = LINEAR_SLOPE(TABLE(last + 1, col),
  1854. (3*c[0]*v + 2*c[1])*v + c[2], u - u1);
  1855. }
  1856. }
  1857. break;
  1858. default:
  1859. ModelicaError("Unknown smoothness kind\n");
  1860. return y;
  1861. }
  1862. }
  1863. }
  1864. return y;
  1865. }
  1866. DYMOLA_STATIC double ModelicaStandardTables_CombiTable1D_getDerValue(void* _tableID, int iCol,
  1867. double u, double der_u) {
  1868. double der_y = 0.;
  1869. CombiTable1D* tableID = (CombiTable1D*)_tableID;
  1870. if (tableID != NULL && tableID->table != NULL && tableID->cols != NULL) {
  1871. const double* table = tableID->table;
  1872. const size_t nRow = tableID->nRow;
  1873. const size_t nCol = tableID->nCol;
  1874. const size_t col = (size_t)tableID->cols[iCol - 1] - 1;
  1875. if (nRow > 1) {
  1876. enum PointInterval extrapolate = IN_TABLE;
  1877. size_t last;
  1878. if (u < TABLE_ROW0(0)) {
  1879. extrapolate = LEFT;
  1880. last = 0;
  1881. }
  1882. else if (u > TABLE_COL0(nRow - 1)) {
  1883. extrapolate = RIGHT;
  1884. last = nRow - 2;
  1885. }
  1886. else {
  1887. last = findRowIndex(table, nRow, nCol, tableID->last, u);
  1888. tableID->last = last;
  1889. }
  1890. switch (tableID->smoothness) {
  1891. case CONSTANT_SEGMENTS:
  1892. if (extrapolate == IN_TABLE) {
  1893. break;
  1894. }
  1895. /* Fall through: linear extrapolation */
  1896. case LINEAR_SEGMENTS:
  1897. der_y = (TABLE(last + 1, col) - TABLE(last, col))/
  1898. (TABLE_COL0(last + 1) - TABLE_COL0(last));
  1899. der_y *= der_u;
  1900. break;
  1901. case AKIMA_C1:
  1902. case FRITSCH_BUTLAND_MONOTONE_C1:
  1903. case STEFFEN_MONOTONE_C1:
  1904. if (tableID->spline != NULL) {
  1905. const double* c = tableID->spline[
  1906. IDX(last, iCol - 1, tableID->nCols)];
  1907. if (extrapolate == IN_TABLE) {
  1908. const double v = u - TABLE_COL0(last);
  1909. der_y = (3*c[0]*v + 2*c[1])*v + c[2];
  1910. }
  1911. else if (extrapolate == LEFT) {
  1912. der_y = c[2];
  1913. }
  1914. else /* if (extrapolate == RIGHT) */ {
  1915. const double v = TABLE_COL0(last + 1) -
  1916. TABLE_COL0(last);
  1917. der_y = (3*c[0]*v + 2*c[1])*v + c[2];
  1918. }
  1919. der_y *= der_u;
  1920. }
  1921. break;
  1922. default:
  1923. ModelicaError("Unknown smoothness kind\n");
  1924. return der_y;
  1925. }
  1926. }
  1927. }
  1928. return der_y;
  1929. }
  1930. DYMOLA_STATIC double ModelicaStandardTables_CombiTable1D_read(void* _tableID, int force,
  1931. int verbose) {
  1932. #if !defined(NO_FILE_SYSTEM)
  1933. CombiTable1D* tableID = (CombiTable1D*)_tableID;
  1934. if (tableID != NULL && tableID->source == TABLESOURCE_FILE) {
  1935. if (force || tableID->table == NULL) {
  1936. #if !defined(TABLE_SHARE)
  1937. if (tableID->table != NULL) {
  1938. free(tableID->table);
  1939. }
  1940. #endif
  1941. tableID->table = readTable(tableID->tableName,
  1942. tableID->fileName, &tableID->nRow, &tableID->nCol,
  1943. verbose, force);
  1944. if (tableID->table == NULL) {
  1945. return 0.; /* Error */
  1946. }
  1947. if (!isValidCombiTable1D((const CombiTable1D*)tableID)) {
  1948. return 0.; /* Error */
  1949. }
  1950. if (tableID->nRow <= 2) {
  1951. if (tableID->smoothness == AKIMA_C1 ||
  1952. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  1953. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1954. tableID->smoothness = LINEAR_SEGMENTS;
  1955. }
  1956. }
  1957. if (tableID->smoothness == AKIMA_C1) {
  1958. /* Reinitialization of the cubic Hermite spline coefficients */
  1959. spline1DClose(&tableID->spline);
  1960. tableID->spline = akimaSpline1DInit(
  1961. (const double*)tableID->table, tableID->nRow,
  1962. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1963. if (tableID->spline == NULL) {
  1964. ModelicaError("Memory allocation error\n");
  1965. return 0.; /* Error */
  1966. }
  1967. }
  1968. else if (tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1) {
  1969. /* Reinitialization of the cubic Hermite spline coefficients */
  1970. spline1DClose(&tableID->spline);
  1971. tableID->spline = fritschButlandSpline1DInit(
  1972. (const double*)tableID->table, tableID->nRow,
  1973. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1974. if (tableID->spline == NULL) {
  1975. ModelicaError("Memory allocation error\n");
  1976. return 0.; /* Error */
  1977. }
  1978. }
  1979. else if (tableID->smoothness == STEFFEN_MONOTONE_C1) {
  1980. /* Reinitialization of the cubic Hermite spline coefficients */
  1981. spline1DClose(&tableID->spline);
  1982. tableID->spline = steffenSpline1DInit(
  1983. (const double*)tableID->table, tableID->nRow,
  1984. tableID->nCol, (const int*)tableID->cols, tableID->nCols);
  1985. if (tableID->spline == NULL) {
  1986. ModelicaError("Memory allocation error\n");
  1987. return 0.; /* Error */
  1988. }
  1989. }
  1990. }
  1991. }
  1992. #endif
  1993. return 1.; /* Success */
  1994. }
  1995. DYMOLA_STATIC void* ModelicaStandardTables_CombiTable2D_init(const char* tableName,
  1996. const char* fileName,
  1997. const double* table, size_t nRow,
  1998. size_t nColumn, int smoothness) {
  1999. CombiTable2D* tableID = (CombiTable2D*)calloc(1, sizeof(CombiTable2D));
  2000. if (tableID != NULL) {
  2001. tableID->smoothness = (enum Smoothness)smoothness;
  2002. tableID->source = getTableSource(tableName, fileName);
  2003. switch (tableID->source) {
  2004. case TABLESOURCE_FILE:
  2005. tableID->tableName = (char*)malloc(
  2006. (strlen(tableName) + 1)*sizeof(char));
  2007. if (tableID->tableName != NULL) {
  2008. strcpy(tableID->tableName, tableName);
  2009. }
  2010. else {
  2011. free(tableID);
  2012. ModelicaError("Memory allocation error\n");
  2013. return NULL;
  2014. }
  2015. tableID->fileName = (char*)malloc((strlen(fileName) + 1)*sizeof(char));
  2016. if (tableID->fileName != NULL) {
  2017. strcpy(tableID->fileName, fileName);
  2018. }
  2019. else {
  2020. free(tableID->tableName);
  2021. free(tableID);
  2022. ModelicaError("Memory allocation error\n");
  2023. return NULL;
  2024. }
  2025. break;
  2026. case TABLESOURCE_MODEL:
  2027. tableID->nRow = nRow;
  2028. tableID->nCol = nColumn;
  2029. tableID->table = (double*) table;
  2030. if (isValidCombiTable2D((const CombiTable2D*)tableID)) {
  2031. if (tableID->smoothness == AKIMA_C1 &&
  2032. tableID->nRow <= 3 && tableID->nCol <= 3) {
  2033. tableID->smoothness = LINEAR_SEGMENTS;
  2034. }
  2035. if (tableID->smoothness == AKIMA_C1) {
  2036. /* Initialization of the Akima-spline coefficients */
  2037. tableID->spline = spline2DInit(table, tableID->nRow,
  2038. tableID->nCol);
  2039. if (tableID->spline == NULL) {
  2040. free(tableID);
  2041. ModelicaError("Memory allocation error\n");
  2042. return NULL;
  2043. }
  2044. }
  2045. #if !defined(NO_TABLE_COPY)
  2046. tableID->table = (double*)malloc(
  2047. tableID->nRow*tableID->nCol*sizeof(double));
  2048. if (tableID->table != NULL) {
  2049. memcpy(tableID->table, table, tableID->nRow*
  2050. tableID->nCol*sizeof(double));
  2051. }
  2052. else {
  2053. spline2DClose(&tableID->spline);
  2054. free(tableID);
  2055. ModelicaError("Memory allocation error\n");
  2056. return NULL;
  2057. }
  2058. #endif
  2059. }
  2060. else {
  2061. tableID->table = NULL;
  2062. }
  2063. break;
  2064. case TABLESOURCE_FUNCTION: {
  2065. int colWise;
  2066. int dim[MAX_TABLE_DIMENSIONS];
  2067. if (usertab((char*)tableName, 2 /* 2D-interpolation */, dim,
  2068. &colWise, &tableID->table) == 0) {
  2069. if (colWise == 0) {
  2070. tableID->nRow = (size_t)dim[0];
  2071. tableID->nCol = (size_t)dim[1];
  2072. }
  2073. else {
  2074. /* Need to transpose */
  2075. double* tableT = (double*)malloc(dim[0]*dim[1]*sizeof(double));
  2076. if (tableT != NULL) {
  2077. memcpy(tableT, tableID->table, dim[0]*dim[1]*sizeof(double));
  2078. tableID->table = tableT;
  2079. tableID->nRow = (size_t)dim[1];
  2080. tableID->nCol = (size_t)dim[0];
  2081. tableID->source = TABLESOURCE_FUNCTION_TRANSPOSE;
  2082. transpose(tableID->table, tableID->nRow, tableID->nCol);
  2083. }
  2084. else {
  2085. free(tableID);
  2086. ModelicaError("Memory allocation error\n");
  2087. return NULL;
  2088. }
  2089. }
  2090. if (isValidCombiTable2D((const CombiTable2D*)tableID)) {
  2091. if (tableID->smoothness == AKIMA_C1 &&
  2092. tableID->nRow <= 3 && tableID->nCol <= 3) {
  2093. tableID->smoothness = LINEAR_SEGMENTS;
  2094. }
  2095. if (tableID->smoothness == AKIMA_C1) {
  2096. /* Initialization of the Akima-spline coefficients */
  2097. tableID->spline = spline2DInit(tableID->table,
  2098. tableID->nRow, tableID->nCol);
  2099. if (tableID->spline == NULL) {
  2100. if (tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE) {
  2101. free(tableID->table);
  2102. }
  2103. free(tableID);
  2104. ModelicaError("Memory allocation error\n");
  2105. return NULL;
  2106. }
  2107. }
  2108. }
  2109. }
  2110. break;
  2111. }
  2112. case TABLESOURCE_FUNCTION_TRANSPOSE:
  2113. /* Should not be possible to get here */
  2114. break;
  2115. default:
  2116. free(tableID);
  2117. ModelicaError("Table source error\n");
  2118. return NULL;
  2119. }
  2120. }
  2121. else {
  2122. ModelicaError("Memory allocation error\n");
  2123. }
  2124. return (void*)tableID;
  2125. }
  2126. DYMOLA_STATIC void ModelicaStandardTables_CombiTable2D_close(void* _tableID) {
  2127. CombiTable2D* tableID = (CombiTable2D*)_tableID;
  2128. if (tableID != NULL) {
  2129. if (tableID->table != NULL && tableID->source == TABLESOURCE_FILE) {
  2130. #if defined(TABLE_SHARE) && !defined(NO_FILE_SYSTEM)
  2131. if (tableID->tableName != NULL && tableID->fileName != NULL) {
  2132. char* key = malloc((strlen(tableID->tableName) +
  2133. strlen(tableID->fileName) + 2)*sizeof(char));
  2134. if (key != NULL) {
  2135. TableShare *iter;
  2136. strcpy(key, tableID->tableName);
  2137. strcat(key, "|");
  2138. strcat(key, tableID->fileName);
  2139. MUTEX_LOCK();
  2140. HASH_FIND_STR(tableShare, key, iter);
  2141. if (iter != NULL) {
  2142. /* Share hit */
  2143. if (--iter->refCount == 0) {
  2144. free(iter->table);
  2145. free(iter->key);
  2146. HASH_DEL(tableShare, iter);
  2147. free(iter);
  2148. }
  2149. }
  2150. MUTEX_UNLOCK();
  2151. free(key);
  2152. }
  2153. }
  2154. else {
  2155. /* Should not be possible to get here */
  2156. free(tableID->table);
  2157. }
  2158. #else
  2159. free(tableID->table);
  2160. #endif
  2161. tableID->table = NULL;
  2162. }
  2163. else if (tableID->table != NULL && (
  2164. #if !defined(NO_TABLE_COPY)
  2165. tableID->source == TABLESOURCE_MODEL ||
  2166. #endif
  2167. tableID->source == TABLESOURCE_FUNCTION_TRANSPOSE)) {
  2168. free(tableID->table);
  2169. tableID->table = NULL;
  2170. }
  2171. if (tableID->tableName != NULL) {
  2172. free(tableID->tableName);
  2173. tableID->tableName = NULL;
  2174. }
  2175. if (tableID->fileName != NULL) {
  2176. free(tableID->fileName);
  2177. tableID->fileName = NULL;
  2178. }
  2179. spline2DClose(&tableID->spline);
  2180. free(tableID);
  2181. }
  2182. }
  2183. DYMOLA_STATIC double ModelicaStandardTables_CombiTable2D_read(void* _tableID, int force,
  2184. int verbose) {
  2185. #if !defined(NO_FILE_SYSTEM)
  2186. CombiTable2D* tableID = (CombiTable2D*)_tableID;
  2187. if (tableID != NULL && tableID->source == TABLESOURCE_FILE) {
  2188. if (force || tableID->table == NULL) {
  2189. #if !defined(TABLE_SHARE)
  2190. if (tableID->table != NULL) {
  2191. free(tableID->table);
  2192. }
  2193. #endif
  2194. tableID->table = readTable(tableID->tableName,
  2195. tableID->fileName, &tableID->nRow, &tableID->nCol,
  2196. verbose, force);
  2197. if (tableID->table == NULL) {
  2198. return 0.; /* Error */
  2199. }
  2200. if (!isValidCombiTable2D((const CombiTable2D*)tableID)) {
  2201. return 0.; /* Error */
  2202. }
  2203. if (tableID->smoothness == AKIMA_C1 &&
  2204. tableID->nRow <= 3 && tableID->nCol <= 3) {
  2205. tableID->smoothness = LINEAR_SEGMENTS;
  2206. }
  2207. if (tableID->smoothness == AKIMA_C1) {
  2208. /* Reinitialization of the Akima-spline coefficients */
  2209. spline2DClose(&tableID->spline);
  2210. tableID->spline = spline2DInit(tableID->table, tableID->nRow,
  2211. tableID->nCol);
  2212. if (tableID->spline == NULL) {
  2213. ModelicaError("Memory allocation error\n");
  2214. return 0.; /* Error */
  2215. }
  2216. }
  2217. }
  2218. }
  2219. #endif
  2220. return 1.; /* Success */
  2221. }
  2222. DYMOLA_STATIC double ModelicaStandardTables_CombiTable2D_getValue(void* _tableID, double u1,
  2223. double u2) {
  2224. double y = 0;
  2225. CombiTable2D* tableID = (CombiTable2D*)_tableID;
  2226. if (tableID != NULL && tableID->table != NULL) {
  2227. const double* table = tableID->table;
  2228. const size_t nRow = tableID->nRow;
  2229. const size_t nCol = tableID->nCol;
  2230. if (nRow == 2 && nCol == 2) {
  2231. /* Single row */
  2232. y = TABLE(1, 1);
  2233. }
  2234. else if (nRow == 2 && nCol > 2) {
  2235. enum PointInterval extrapolate2 = IN_TABLE;
  2236. size_t last2;
  2237. if (u2 < TABLE_ROW0(1)) {
  2238. extrapolate2 = LEFT;
  2239. last2 = 0;
  2240. }
  2241. else if (u2 > TABLE_ROW0(nCol - 1)) {
  2242. extrapolate2 = RIGHT;
  2243. last2 = nCol - 3;
  2244. }
  2245. else {
  2246. last2 = findColIndex(&TABLE(0, 1), nCol - 1,
  2247. tableID->last2, u2);
  2248. tableID->last2 = last2;
  2249. }
  2250. switch (tableID->smoothness) {
  2251. case CONSTANT_SEGMENTS:
  2252. if (extrapolate2 == IN_TABLE) {
  2253. if (u2 >= TABLE_ROW0(last2 + 2)) {
  2254. last2 += 1;
  2255. }
  2256. y = TABLE(1, last2 + 1);
  2257. break;
  2258. }
  2259. /* Fall through: linear extrapolation */
  2260. case LINEAR_SEGMENTS: {
  2261. const double u20 = TABLE_ROW0(last2 + 1);
  2262. const double u21 = TABLE_ROW0(last2 + 2);
  2263. const double y0 = TABLE(1, last2 + 1);
  2264. const double y1 = TABLE(1, last2 + 2);
  2265. LINEAR(u2, u20, u21, y0, y1)
  2266. break;
  2267. }
  2268. case AKIMA_C1:
  2269. if (tableID->spline != NULL) {
  2270. const double* c = tableID->spline[last2];
  2271. const double u20 = TABLE_ROW0(last2 + 1);
  2272. if (extrapolate2 == IN_TABLE) {
  2273. u2 -= u20;
  2274. y = TABLE(1, last2 + 1); /* c[3] = y0 */
  2275. y += ((c[0]*u2 + c[1])*u2 + c[2])*u2;
  2276. }
  2277. else if (extrapolate2 == LEFT) {
  2278. y = LINEAR_SLOPE(TABLE(1, last2 + 1), c[2],
  2279. u2 - u20);
  2280. }
  2281. else /* if (extrapolate2 == RIGHT) */ {
  2282. const double u21 = TABLE_ROW0(last2 + 2);
  2283. const double v2 = u21 - u20;
  2284. y = LINEAR_SLOPE(TABLE(1, last2 + 2), (3*c[0]*v2 +
  2285. 2*c[1])*v2 + c[2], u2 - u21);
  2286. }
  2287. }
  2288. break;
  2289. case FRITSCH_BUTLAND_MONOTONE_C1:
  2290. case STEFFEN_MONOTONE_C1:
  2291. ModelicaError("Bivariate monotone C1 interpolation is "
  2292. "not implemented\n");
  2293. return y;
  2294. default:
  2295. ModelicaError("Unknown smoothness kind\n");
  2296. return y;
  2297. }
  2298. }
  2299. else if (nRow > 2 && nCol == 2) {
  2300. enum PointInterval extrapolate1 = IN_TABLE;
  2301. size_t last1;
  2302. if (u1 < TABLE_COL0(1)) {
  2303. extrapolate1 = LEFT;
  2304. last1 = 0;
  2305. }
  2306. else if (u1 > TABLE_COL0(nRow - 1)) {
  2307. extrapolate1 = RIGHT;
  2308. last1 = nRow - 3;
  2309. }
  2310. else {
  2311. last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
  2312. tableID->last1, u1);
  2313. tableID->last1 = last1;
  2314. }
  2315. switch (tableID->smoothness) {
  2316. case CONSTANT_SEGMENTS:
  2317. if (extrapolate1 == IN_TABLE) {
  2318. if (u1 >= TABLE_COL0(last1 + 2)) {
  2319. last1 += 1;
  2320. }
  2321. y = TABLE(last1 + 1, 1);
  2322. break;
  2323. }
  2324. /* Fall through: linear extrapolation */
  2325. case LINEAR_SEGMENTS: {
  2326. const double u10 = TABLE_COL0(last1 + 1);
  2327. const double u11 = TABLE_COL0(last1 + 2);
  2328. const double y0 = TABLE(last1 + 1, 1);
  2329. const double y1 = TABLE(last1 + 2, 1);
  2330. LINEAR(u1, u10, u11, y0, y1)
  2331. break;
  2332. }
  2333. case AKIMA_C1:
  2334. if (tableID->spline != NULL) {
  2335. const double* c = tableID->spline[last1];
  2336. const double u10 = TABLE_COL0(last1 + 1);
  2337. if (extrapolate1 == IN_TABLE) {
  2338. u1 -= u10;
  2339. y = TABLE(last1 + 1, 1); /* c[3] = y0 */
  2340. y += ((c[0]*u1 + c[1])*u1 + c[2])*u1;
  2341. }
  2342. else if (extrapolate1 == LEFT) {
  2343. y = LINEAR_SLOPE(TABLE(last1 + 1, 1), c[2],
  2344. u1 - u10);
  2345. }
  2346. else /* if (extrapolate1 == RIGHT) */ {
  2347. const double u11 = TABLE_COL0(last1 + 2);
  2348. const double v1 = u11 - u10;
  2349. y = LINEAR_SLOPE(TABLE(last1 + 2, 1), (3*c[0]*v1 +
  2350. 2*c[1])*v1 + c[2], u1 - u11);
  2351. }
  2352. }
  2353. break;
  2354. case FRITSCH_BUTLAND_MONOTONE_C1:
  2355. case STEFFEN_MONOTONE_C1:
  2356. ModelicaError("Bivariate monotone C1 interpolation is "
  2357. "not implemented\n");
  2358. return y;
  2359. default:
  2360. ModelicaError("Unknown smoothness kind\n");
  2361. return y;
  2362. }
  2363. }
  2364. else if (nRow > 2 && nCol > 2) {
  2365. enum PointInterval extrapolate1 = IN_TABLE;
  2366. enum PointInterval extrapolate2 = IN_TABLE;
  2367. size_t last1, last2;
  2368. if (u1 < TABLE_COL0(1)) {
  2369. extrapolate1 = LEFT;
  2370. last1 = 0;
  2371. }
  2372. else if (u1 > TABLE_COL0(nRow - 1)) {
  2373. extrapolate1 = RIGHT;
  2374. last1 = nRow - 3;
  2375. }
  2376. else {
  2377. last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
  2378. tableID->last1, u1);
  2379. tableID->last1 = last1;
  2380. }
  2381. if (u2 < TABLE_ROW0(1)) {
  2382. extrapolate2 = LEFT;
  2383. last2 = 0;
  2384. }
  2385. else if (u2 > TABLE_ROW0(nCol - 1)) {
  2386. extrapolate2 = RIGHT;
  2387. last2 = nCol - 3;
  2388. }
  2389. else {
  2390. last2 = findColIndex(&TABLE(0, 1), nCol - 1,
  2391. tableID->last2, u2);
  2392. tableID->last2 = last2;
  2393. }
  2394. switch (tableID->smoothness) {
  2395. case CONSTANT_SEGMENTS:
  2396. if (extrapolate1 == IN_TABLE && extrapolate2 == IN_TABLE) {
  2397. if (u1 >= TABLE_COL0(last1 + 2)) {
  2398. last1 += 1;
  2399. }
  2400. if (u2 >= TABLE_ROW0(last2 + 2)) {
  2401. last2 += 1;
  2402. }
  2403. y = TABLE(last1 + 1, last2 + 1);
  2404. break;
  2405. }
  2406. /* Fall through: bilinear extrapolation */
  2407. case LINEAR_SEGMENTS: {
  2408. const double u10 = TABLE_COL0(last1 + 1);
  2409. const double u11 = TABLE_COL0(last1 + 2);
  2410. const double u20 = TABLE_ROW0(last2 + 1);
  2411. const double u21 = TABLE_ROW0(last2 + 2);
  2412. const double y00 = TABLE(last1 + 1, last2 + 1);
  2413. const double y01 = TABLE(last1 + 1, last2 + 2);
  2414. const double y10 = TABLE(last1 + 2, last2 + 1);
  2415. const double y11 = TABLE(last1 + 2, last2 + 2);
  2416. BILINEAR(u1, u2,
  2417. u10, u11, u20, u21, y00, y01, y10, y11)
  2418. break;
  2419. }
  2420. case AKIMA_C1:
  2421. if (tableID->spline != NULL) {
  2422. const double* c = tableID->spline[
  2423. IDX(last1, last2, nCol - 2)];
  2424. if (extrapolate1 == IN_TABLE) {
  2425. u1 -= TABLE_COL0(last1 + 1);
  2426. y = TABLE(last1 + 1, last2 + 1); /* c[15] = y00 */
  2427. if (extrapolate2 == IN_TABLE) {
  2428. double p1, p2, p3;
  2429. u2 -= TABLE_ROW0(last2 + 1);
  2430. p1 = ((c[0]*u2 + c[1])*u2 + c[2])*u2 + c[3];
  2431. p2 = ((c[4]*u2 + c[5])*u2 + c[6])*u2 + c[7];
  2432. p3 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2433. y += ((c[12]*u2 + c[13])*u2 + c[14])*u2; /* p4 */
  2434. y += ((p1*u1 + p2)*u1 + p3)*u1;
  2435. }
  2436. else if (extrapolate2 == LEFT) {
  2437. double der_y2;
  2438. u2 -= TABLE_ROW0(1);
  2439. der_y2 = ((c[2]*u1 + c[6])*u1 + c[10])*u1 + c[14];
  2440. y += ((c[3]*u1 + c[7])*u1 + c[11])*u1;
  2441. y += der_y2*u2;
  2442. }
  2443. else /* if (extrapolate2 == RIGHT) */ {
  2444. const double v2 = TABLE_ROW0(nCol - 1) -
  2445. TABLE_ROW0(nCol - 2);
  2446. double p1, p2, p3;
  2447. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2448. double der_y2;
  2449. u2 -= TABLE_ROW0(nCol - 1);
  2450. p1 = ((c[0]*v2 + c[1])*v2 + c[2])*v2 + c[3];
  2451. p2 = ((c[4]*v2 + c[5])*v2 + c[6])*v2 + c[7];
  2452. p3 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2453. dp1_u2 = (3*c[0]*v2 + 2*c[1])*v2 + c[2];
  2454. dp2_u2 = (3*c[4]*v2 + 2*c[5])*v2 + c[6];
  2455. dp3_u2 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2456. dp4_u2 = (3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2457. der_y2 = ((dp1_u2*u1 + dp2_u2)*u1 + dp3_u2)*u1 + dp4_u2;
  2458. y += ((c[12]*v2 + c[13])*v2 + c[14])*v2; /* p4 */
  2459. y += ((p1*u1 + p2)*u1 + p3)*u1;
  2460. y += der_y2*u2;
  2461. }
  2462. }
  2463. else if (extrapolate1 == LEFT) {
  2464. u1 -= TABLE_COL0(1);
  2465. if (extrapolate2 == IN_TABLE) {
  2466. double der_y1;
  2467. u2 -= TABLE_ROW0(last2 + 1);
  2468. der_y1 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2469. y = TABLE(last1 + 1, last2 + 1); /* c[15] = y00 */
  2470. y += ((c[12]*u2 + c[13])*u2 + c[14])*u2; /* p4 */
  2471. y += der_y1*u1;
  2472. }
  2473. else if (extrapolate2 == LEFT) {
  2474. double der_y1, der_y2, der_y12;
  2475. u2 -= TABLE_ROW0(1);
  2476. der_y1 = c[11];
  2477. der_y2 = c[14];
  2478. der_y12 = c[10];
  2479. y = TABLE(1, 1);
  2480. y += der_y1*u1 + der_y2*u2 + der_y12*u1*u2;
  2481. }
  2482. else /* if (extrapolate2 == RIGHT) */ {
  2483. const double v2 = TABLE_ROW0(nCol - 1) -
  2484. TABLE_ROW0(nCol - 2);
  2485. double der_y1, der_y2, der_y12;
  2486. u2 -= TABLE_ROW0(nCol - 1);
  2487. der_y1 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2488. der_y2 =(3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2489. der_y12 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2490. y = TABLE(1, nCol - 1);
  2491. y += der_y1*u1 + der_y2*u2 + der_y12*u1*u2;
  2492. }
  2493. }
  2494. else /* if (extrapolate1 == RIGHT) */ {
  2495. const double v1 = TABLE_COL0(nRow - 1) -
  2496. TABLE_COL0(nRow - 2);
  2497. u1 -= TABLE_COL0(nRow - 1);
  2498. if (extrapolate2 == IN_TABLE) {
  2499. double p1, p2, p3;
  2500. double der_y1;
  2501. u2 -= TABLE_ROW0(last2 + 1);
  2502. p1 = ((c[0]*u2 + c[1])*u2 + c[2])*u2 + c[3];
  2503. p2 = ((c[4]*u2 + c[5])*u2 + c[6])*u2 + c[7];
  2504. p3 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2505. der_y1 = (3*p1*v1 + 2*p2)*v1 + p3;
  2506. y = TABLE(last1 + 1, last2 + 1); /* c[15] = y00 */
  2507. y += ((c[12]*u2 + c[13])*u2 + c[14])*u2; /* p4 */
  2508. y += ((p1*v1 + p2)*v1 + p3)*v1;
  2509. y += der_y1*u1;
  2510. }
  2511. else if (extrapolate2 == LEFT) {
  2512. double der_y1, der_y2, der_y12;
  2513. u2 -= TABLE_ROW0(1);
  2514. der_y1 = (3*c[3]*v1 + 2*c[7])*v1 + c[11];
  2515. der_y2 = ((c[2]*v1 + c[6])*v1 + c[10])*v1 + c[14];
  2516. der_y12 = (3*c[2]*v1 + 2*c[6])*v1 + c[10];
  2517. y = TABLE(nRow - 1, 1);
  2518. y += der_y1*u1 + der_y2*u2 + der_y12*u1*u2;
  2519. }
  2520. else /* if (extrapolate2 == RIGHT) */ {
  2521. const double v2 = TABLE_ROW0(nCol - 1) -
  2522. TABLE_ROW0(nCol - 2);
  2523. double p1, p2, p3;
  2524. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2525. double der_y1, der_y2, der_y12;
  2526. u2 -= TABLE_ROW0(nCol - 1);
  2527. p1 = ((c[0]*v2 + c[1])*v2 + c[2])*v2 + c[3];
  2528. p2 = ((c[4]*v2 + c[5])*v2 + c[6])*v2 + c[7];
  2529. p3 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2530. dp1_u2 = (3*c[0]*v2 + 2*c[1])*v2 + c[2];
  2531. dp2_u2 = (3*c[4]*v2 + 2*c[5])*v2 + c[6];
  2532. dp3_u2 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2533. dp4_u2 = (3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2534. der_y1 = (3*p1*v1 + 2*p2)*v1 + p3;
  2535. der_y2 = ((dp1_u2*v1 + dp2_u2)*v1 + dp3_u2)*v1 + dp4_u2;
  2536. der_y12 = (3*dp1_u2*v1 + 2*dp2_u2)*v1 + dp3_u2;
  2537. y = TABLE(nRow - 1, nCol - 1);
  2538. y += der_y1*u1 + der_y2*u2 + der_y12*u1*u2;
  2539. }
  2540. }
  2541. }
  2542. break;
  2543. case FRITSCH_BUTLAND_MONOTONE_C1:
  2544. case STEFFEN_MONOTONE_C1:
  2545. ModelicaError("Bivariate monotone C1 interpolation is "
  2546. "not implemented\n");
  2547. return y;
  2548. default:
  2549. ModelicaError("Unknown smoothness kind\n");
  2550. return y;
  2551. }
  2552. }
  2553. }
  2554. return y;
  2555. }
  2556. DYMOLA_STATIC double ModelicaStandardTables_CombiTable2D_getDerValue(void* _tableID, double u1,
  2557. double u2, double der_u1,
  2558. double der_u2) {
  2559. double der_y = 0;
  2560. CombiTable2D* tableID = (CombiTable2D*)_tableID;
  2561. if (tableID != NULL && tableID->table != NULL) {
  2562. const double* table = tableID->table;
  2563. const size_t nRow = tableID->nRow;
  2564. const size_t nCol = tableID->nCol;
  2565. if (nRow == 2 && nCol == 2) {
  2566. }
  2567. else if (nRow == 2 && nCol > 2) {
  2568. enum PointInterval extrapolate2 = IN_TABLE;
  2569. size_t last2;
  2570. if (u2 < TABLE_ROW0(1)) {
  2571. extrapolate2 = LEFT;
  2572. last2 = 0;
  2573. }
  2574. else if (u2 > TABLE_ROW0(nCol - 1)) {
  2575. extrapolate2 = RIGHT;
  2576. last2 = nCol - 3;
  2577. }
  2578. else {
  2579. last2 = findColIndex(&TABLE(0, 1), nCol - 1,
  2580. tableID->last2, u2);
  2581. tableID->last2 = last2;
  2582. }
  2583. switch (tableID->smoothness) {
  2584. case CONSTANT_SEGMENTS:
  2585. if (extrapolate2 == IN_TABLE) {
  2586. break;
  2587. }
  2588. /* Fall through: linear extrapolation */
  2589. case LINEAR_SEGMENTS: {
  2590. der_y = (TABLE(1, last2 + 2) - TABLE(1, last2 + 1))/
  2591. (TABLE_ROW0(last2 + 2) - TABLE_ROW0(last2 + 1));
  2592. der_y *= der_u2;
  2593. break;
  2594. }
  2595. case AKIMA_C1:
  2596. if (tableID->spline != NULL) {
  2597. const double* c = tableID->spline[last2];
  2598. const double u20 = TABLE_ROW0(last2 + 1);
  2599. if (extrapolate2 == IN_TABLE) {
  2600. u2 -= u20;
  2601. der_y = (3*c[0]*u2 + 2*c[1])*u2 + c[2];
  2602. }
  2603. else if (extrapolate2 == LEFT) {
  2604. der_y = c[2];
  2605. }
  2606. else /* if (extrapolate2 == RIGHT) */ {
  2607. const double u21 = TABLE_ROW0(last2 + 2);
  2608. der_y = u21 - u20;
  2609. der_y = (3*c[0]*der_y + 2*c[1])*der_y + c[2];
  2610. }
  2611. der_y *= der_u2;
  2612. }
  2613. break;
  2614. case FRITSCH_BUTLAND_MONOTONE_C1:
  2615. case STEFFEN_MONOTONE_C1:
  2616. ModelicaError("Bivariate monotone C1 interpolation is "
  2617. "not implemented\n");
  2618. return der_y;
  2619. default:
  2620. ModelicaError("Unknown smoothness kind\n");
  2621. return der_y;
  2622. }
  2623. }
  2624. else if (nRow > 2 && nCol == 2) {
  2625. enum PointInterval extrapolate1 = IN_TABLE;
  2626. size_t last1;
  2627. if (u1 < TABLE_COL0(1)) {
  2628. extrapolate1 = LEFT;
  2629. last1 = 0;
  2630. }
  2631. else if (u1 > TABLE_COL0(nRow - 1)) {
  2632. extrapolate1 = RIGHT;
  2633. last1 = nRow - 3;
  2634. }
  2635. else {
  2636. last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
  2637. tableID->last1, u1);
  2638. tableID->last1 = last1;
  2639. }
  2640. switch (tableID->smoothness) {
  2641. case CONSTANT_SEGMENTS:
  2642. if (extrapolate1 == IN_TABLE) {
  2643. break;
  2644. }
  2645. /* Fall through: linear extrapolation */
  2646. case LINEAR_SEGMENTS: {
  2647. der_y = (TABLE(last1 + 2, 1) - TABLE(last1 + 1, 1))/
  2648. (TABLE_COL0(last1 + 2) - TABLE_COL0(last1 + 1));
  2649. der_y *= der_u1;
  2650. break;
  2651. }
  2652. case AKIMA_C1:
  2653. if (tableID->spline != NULL) {
  2654. const double* c = tableID->spline[last1];
  2655. const double u10 = TABLE_COL0(last1 + 1);
  2656. if (extrapolate1 == IN_TABLE) {
  2657. u1 -= u10;
  2658. der_y = (3*c[0]*u1 + 2*c[1])*u1 + c[2];
  2659. }
  2660. else if (extrapolate1 == LEFT) {
  2661. der_y = c[2];
  2662. }
  2663. else /* if (extrapolate1 == RIGHT) */ {
  2664. const double u11 = TABLE_COL0(last1 + 2);
  2665. der_y = u11 - u10;
  2666. der_y = (3*c[0]*der_y + 2*c[1])*der_y + c[2];
  2667. }
  2668. der_y *= der_u1;
  2669. }
  2670. break;
  2671. case FRITSCH_BUTLAND_MONOTONE_C1:
  2672. case STEFFEN_MONOTONE_C1:
  2673. ModelicaError("Bivariate monotone C1 interpolation is "
  2674. "not implemented\n");
  2675. return der_y;
  2676. default:
  2677. ModelicaError("Unknown smoothness kind\n");
  2678. return der_y;
  2679. }
  2680. }
  2681. else if (nRow > 2 && nCol > 2) {
  2682. enum PointInterval extrapolate1 = IN_TABLE;
  2683. enum PointInterval extrapolate2 = IN_TABLE;
  2684. size_t last1, last2;
  2685. if (u1 < TABLE_COL0(1)) {
  2686. extrapolate1 = LEFT;
  2687. last1 = 0;
  2688. }
  2689. else if (u1 > TABLE_COL0(nRow - 1)) {
  2690. extrapolate1 = RIGHT;
  2691. last1 = nRow - 3;
  2692. }
  2693. else {
  2694. last1 = findRowIndex(&TABLE(1, 0), nRow - 1, nCol,
  2695. tableID->last1, u1);
  2696. tableID->last1 = last1;
  2697. }
  2698. if (u2 < TABLE_ROW0(1)) {
  2699. extrapolate2 = LEFT;
  2700. last2 = 0;
  2701. }
  2702. else if (u2 > TABLE_ROW0(nCol - 1)) {
  2703. extrapolate2 = RIGHT;
  2704. last2 = nCol - 3;
  2705. }
  2706. else {
  2707. last2 = findColIndex(&TABLE(0, 1), nCol - 1,
  2708. tableID->last2, u2);
  2709. tableID->last2 = last2;
  2710. }
  2711. switch (tableID->smoothness) {
  2712. case CONSTANT_SEGMENTS:
  2713. if (extrapolate1 == IN_TABLE && extrapolate2 == IN_TABLE) {
  2714. break;
  2715. }
  2716. /* Fall through: bilinear extrapolation */
  2717. case LINEAR_SEGMENTS: {
  2718. const double u10 = TABLE_COL0(last1 + 1);
  2719. const double u11 = TABLE_COL0(last1 + 2);
  2720. const double u20 = TABLE_ROW0(last2 + 1);
  2721. const double u21 = TABLE_ROW0(last2 + 2);
  2722. const double y00 = TABLE(last1 + 1, last2 + 1);
  2723. const double y01 = TABLE(last1 + 1, last2 + 2);
  2724. const double y10 = TABLE(last1 + 2, last2 + 1);
  2725. const double y11 = TABLE(last1 + 2, last2 + 2);
  2726. der_y = (u21*(y10 - y00) + u20*(y01 - y11) +
  2727. u2*(y00 - y01 - y10 + y11))*der_u1;
  2728. der_y += (u11*(y01 - y00) + u10*(y10 - y11) +
  2729. u1*(y00 - y01 - y10 + y11))*der_u2;
  2730. der_y /= (u10 - u11);
  2731. der_y /= (u20 - u21);
  2732. break;
  2733. }
  2734. case AKIMA_C1:
  2735. if (tableID->spline != NULL) {
  2736. const double* c = tableID->spline[
  2737. IDX(last1, last2, nCol - 2)];
  2738. if (extrapolate1 == IN_TABLE) {
  2739. double der_y1, der_y2;
  2740. u1 -= TABLE_COL0(last1 + 1);
  2741. if (extrapolate2 == IN_TABLE) {
  2742. double p1, p2, p3;
  2743. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2744. u2 -= TABLE_ROW0(last2 + 1);
  2745. p1 = ((c[0]*u2 + c[1])*u2 + c[2])*u2 + c[3];
  2746. p2 = ((c[4]*u2 + c[5])*u2 + c[6])*u2 + c[7];
  2747. p3 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2748. dp1_u2 = (3*c[0]*u2 + 2*c[1])*u2 + c[2];
  2749. dp2_u2 = (3*c[4]*u2 + 2*c[5])*u2 + c[6];
  2750. dp3_u2 = (3*c[8]*u2 + 2*c[9])*u2 + c[10];
  2751. dp4_u2 = (3*c[12]*u2 + 2*c[13])*u2 + c[14];
  2752. der_y1 = (3*p1*u1 + 2*p2)*u1 + p3;
  2753. der_y2 = ((dp1_u2*u1 + dp2_u2)*u1 + dp3_u2)*u1 + dp4_u2;
  2754. }
  2755. else if (extrapolate2 == LEFT) {
  2756. u2 -= TABLE_ROW0(1);
  2757. der_y1 = (3*c[3]*u1 + 2*c[7])*u1 + c[11];
  2758. der_y1 += ((3*c[2]*u1 + 2*c[6])*u1 + c[10])*u2;
  2759. der_y2 = ((c[2]*u1 + c[6])*u1 + c[10])*u1 + c[14];
  2760. }
  2761. else /* if (extrapolate2 == RIGHT) */ {
  2762. const double v2 = TABLE_ROW0(nCol - 1) -
  2763. TABLE_ROW0(nCol - 2);
  2764. double p1, p2, p3;
  2765. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2766. u2 -= TABLE_ROW0(nCol - 1);
  2767. p1 = ((c[0]*v2 + c[1])*v2 + c[2])*v2 + c[3];
  2768. p2 = ((c[4]*v2 + c[5])*v2 + c[6])*v2 + c[7];
  2769. p3 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2770. dp1_u2 = (3*c[0]*v2 + 2*c[1])*v2 + c[2];
  2771. dp2_u2 = (3*c[4]*v2 + 2*c[5])*v2 + c[6];
  2772. dp3_u2 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2773. dp4_u2 = (3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2774. der_y1 = (3*p1*u1 + 2*p2)*u1 + p3;
  2775. der_y1 += ((3*dp1_u2*u1 + 2*dp2_u2)*u1 + dp3_u2)*u2;
  2776. der_y2 = ((dp1_u2*u1 + dp2_u2)*u1 + dp3_u2)*u1 + dp4_u2;
  2777. }
  2778. der_y = der_y1*der_u1 + der_y2*der_u2;
  2779. }
  2780. else if (extrapolate1 == LEFT) {
  2781. u1 -= TABLE_COL0(1);
  2782. if (extrapolate2 == IN_TABLE) {
  2783. double der_y1, der_y2;
  2784. u2 -= TABLE_ROW0(last2 + 1);
  2785. der_y1 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2786. der_y2 = (3*c[12]*u2 + 2*c[13])*u2 + c[14];
  2787. der_y2 += ((3*c[8]*u2 + 2*c[9])*u2 + c[10])*u1;
  2788. der_y = der_y1*der_u1 + der_y2*der_u2;
  2789. }
  2790. else if (extrapolate2 == LEFT) {
  2791. double der_y1, der_y2, der_y12;
  2792. u2 -= TABLE_ROW0(1);
  2793. der_y1 = c[11];
  2794. der_y2 = c[14];
  2795. der_y12 = c[10];
  2796. der_y = (der_y1 + der_y12*u2)*der_u1;
  2797. der_y += (der_y2 + der_y12*u1)*der_u2;
  2798. }
  2799. else /* if (extrapolate2 == RIGHT) */ {
  2800. const double v2 = TABLE_ROW0(nCol - 1) -
  2801. TABLE_ROW0(nCol - 2);
  2802. double der_y1, der_y2, der_y12;
  2803. u2 -= TABLE_ROW0(nCol - 1);
  2804. der_y1 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2805. der_y2 =(3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2806. der_y12 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2807. der_y = (der_y1 + der_y12*u2)*der_u1;
  2808. der_y += (der_y2 + der_y12*u1)*der_u2;
  2809. }
  2810. }
  2811. else /* if (extrapolate1 == RIGHT) */ {
  2812. const double v1 = TABLE_COL0(nRow - 1) -
  2813. TABLE_COL0(nRow - 2);
  2814. u1 -= TABLE_COL0(nRow - 1);
  2815. if (extrapolate2 == IN_TABLE) {
  2816. double p1, p2, p3;
  2817. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2818. double der_y1, der_y2;
  2819. u2 -= TABLE_ROW0(last2 + 1);
  2820. p1 = ((c[0]*u2 + c[1])*u2 + c[2])*u2 + c[3];
  2821. p2 = ((c[4]*u2 + c[5])*u2 + c[6])*u2 + c[7];
  2822. p3 = ((c[8]*u2 + c[9])*u2 + c[10])*u2 + c[11];
  2823. dp1_u2 = (3*c[0]*u2 + 2*c[1])*u2 + c[2];
  2824. dp2_u2 = (3*c[4]*u2 + 2*c[5])*u2 + c[6];
  2825. dp3_u2 = (3*c[8]*u2 + 2*c[9])*u2 + c[10];
  2826. dp4_u2 = (3*c[12]*u2 + 2*c[13])*u2 + c[14];
  2827. der_y1 = (3*p1*v1 + 2*p2)*v1 + p3;
  2828. der_y2 = ((dp1_u2*v1 + dp2_u2)*v1 + dp3_u2)*v1 + dp4_u2;
  2829. der_y2 += ((3*dp1_u2*v1 + 2*dp2_u2)*v1 + dp3_u2)*u1;
  2830. der_y = der_y1*der_u1 + der_y2*der_u2;
  2831. }
  2832. else if (extrapolate2 == LEFT) {
  2833. double der_y1, der_y2, der_y12;
  2834. u2 -= TABLE_ROW0(1);
  2835. der_y1 = (3*c[3]*v1 + 2*c[7])*v1 + c[11];
  2836. der_y2 = ((c[2]*v1 + c[6])*v1 + c[10])*v1 + c[14];
  2837. der_y12 = (3*c[2]*v1 + 2*c[6])*v1 + c[10];
  2838. der_y = (der_y1 + der_y12*u2)*der_u1;
  2839. der_y += (der_y2 + der_y12*u1)*der_u2;
  2840. }
  2841. else /* if (extrapolate2 == RIGHT) */ {
  2842. const double v2 = TABLE_ROW0(nCol - 1) -
  2843. TABLE_ROW0(nCol - 2);
  2844. double p1, p2, p3;
  2845. double dp1_u2, dp2_u2, dp3_u2, dp4_u2;
  2846. double der_y1, der_y2, der_y12;
  2847. u2 -= TABLE_ROW0(nCol - 1);
  2848. p1 = ((c[0]*v2 + c[1])*v2 + c[2])*v2 + c[3];
  2849. p2 = ((c[4]*v2 + c[5])*v2 + c[6])*v2 + c[7];
  2850. p3 = ((c[8]*v2 + c[9])*v2 + c[10])*v2 + c[11];
  2851. dp1_u2 = (3*c[0]*v2 + 2*c[1])*v2 + c[2];
  2852. dp2_u2 = (3*c[4]*v2 + 2*c[5])*v2 + c[6];
  2853. dp3_u2 = (3*c[8]*v2 + 2*c[9])*v2 + c[10];
  2854. dp4_u2 = (3*c[12]*v2 + 2*c[13])*v2 + c[14];
  2855. der_y1 = (3*p1*v1 + 2*p2)*v1 + p3;
  2856. der_y2 = ((dp1_u2*v1 + dp2_u2)*v1 + dp3_u2)*v1 + dp4_u2;
  2857. der_y12 = (3*dp1_u2*v1 + 2*dp2_u2)*v1 + dp3_u2;
  2858. der_y = (der_y1 + der_y12*u2)*der_u1;
  2859. der_y += (der_y2 + der_y12*u1)*der_u2;
  2860. }
  2861. }
  2862. }
  2863. break;
  2864. case FRITSCH_BUTLAND_MONOTONE_C1:
  2865. case STEFFEN_MONOTONE_C1:
  2866. ModelicaError("Bivariate monotone C1 interpolation is "
  2867. "not implemented\n");
  2868. return der_y;
  2869. default:
  2870. ModelicaError("Unknown smoothness kind\n");
  2871. return der_y;
  2872. }
  2873. }
  2874. }
  2875. return der_y;
  2876. }
  2877. /* ----- Internal functions ----- */
  2878. static int isNearlyEqual(double x, double y) {
  2879. const double fx = fabs(x);
  2880. const double fy = fabs(y);
  2881. double cmp = fx > fy ? fx : fy;
  2882. if (cmp < _EPSILON) {
  2883. cmp = _EPSILON;
  2884. }
  2885. cmp *= _EPSILON;
  2886. return fabs(y - x) < cmp;
  2887. }
  2888. static size_t findRowIndex(const double* table, size_t nRow, size_t nCol,
  2889. size_t last, double x) {
  2890. size_t i0 = 0;
  2891. size_t i1 = nRow - 1;
  2892. if (x < TABLE_COL0(last)) {
  2893. i1 = last;
  2894. }
  2895. else if (x >= TABLE_COL0(last + 1)) {
  2896. i0 = last;
  2897. }
  2898. else {
  2899. return last;
  2900. }
  2901. /* Binary search */
  2902. while (i1 > i0 + 1) {
  2903. const size_t i = (i0 + i1)/2;
  2904. if (x < TABLE_COL0(i)) {
  2905. i1 = i;
  2906. }
  2907. else {
  2908. i0 = i;
  2909. }
  2910. }
  2911. return i0;
  2912. }
  2913. static size_t findColIndex(const double* table, size_t nCol, size_t last,
  2914. double x) {
  2915. size_t i0 = 0;
  2916. size_t i1 = nCol - 1;
  2917. if (x < TABLE_ROW0(last)) {
  2918. i1 = last;
  2919. }
  2920. else if (x >= TABLE_ROW0(last + 1)) {
  2921. i0 = last;
  2922. }
  2923. else {
  2924. return last;
  2925. }
  2926. /* Binary search */
  2927. while (i1 > i0 + 1) {
  2928. const size_t i = (i0 + i1)/2;
  2929. if (x < TABLE_ROW0(i)) {
  2930. i1 = i;
  2931. }
  2932. else {
  2933. i0 = i;
  2934. }
  2935. }
  2936. return i0;
  2937. }
  2938. /* ----- Internal check functions ----- */
  2939. static int isValidName(const char* name) {
  2940. int isValid = 0;
  2941. if (name != NULL) {
  2942. if (strcmp(name, "NoName") != 0) {
  2943. size_t i;
  2944. size_t len = strlen(name);
  2945. for (i = 0; i < len; i++) {
  2946. if (name[i] != ' ') {
  2947. isValid = 1;
  2948. break;
  2949. }
  2950. }
  2951. }
  2952. }
  2953. return isValid;
  2954. }
  2955. static int isValidCombiTimeTable(const CombiTimeTable* tableID) {
  2956. int isValid = 1;
  2957. if (tableID != NULL) {
  2958. const size_t nRow = tableID->nRow;
  2959. const size_t nCol = tableID->nCol;
  2960. const char* tableName;
  2961. const char* tableDummyName = "NoName";
  2962. size_t iCol;
  2963. if (tableID->source == TABLESOURCE_MODEL) {
  2964. tableName = tableDummyName;
  2965. }
  2966. else {
  2967. tableName = tableID->tableName;
  2968. }
  2969. /* Check dimensions */
  2970. if (nRow < 1 || nCol < 2) {
  2971. ModelicaFormatError(
  2972. "Table matrix \"%s(%lu,%lu)\" does not have appropriate "
  2973. "dimensions for time interpolation.\n", tableName,
  2974. (unsigned long)nRow, (unsigned long)nCol);
  2975. isValid = 0;
  2976. return isValid;
  2977. }
  2978. /* Check column indices */
  2979. for (iCol = 0; iCol < tableID->nCols; ++iCol) {
  2980. const size_t col = (size_t)tableID->cols[iCol];
  2981. if (col < 1 || col > tableID->nCol) {
  2982. ModelicaFormatError("The column index %d is out of range "
  2983. "for table matrix \"%s(%lu,%lu)\".\n", tableID->cols[iCol],
  2984. tableName, (unsigned long)nRow, (unsigned long)nCol);
  2985. }
  2986. }
  2987. if (tableID->table != NULL && nRow > 1) {
  2988. const double* table = tableID->table;
  2989. /* Check period */
  2990. if (tableID->extrapolation == PERIODIC) {
  2991. const double tMin = TABLE_ROW0(0);
  2992. const double tMax = TABLE_COL0(nRow - 1);
  2993. const double T = tMax - tMin;
  2994. if (T <= 0) {
  2995. ModelicaFormatError(
  2996. "Table matrix \"%s\" does not have a positive period/cylce "
  2997. "time for time interpolation with periodic "
  2998. "extrapolation.\n", tableName);
  2999. isValid = 0;
  3000. return isValid;
  3001. }
  3002. }
  3003. /* Check, whether first column values are monotonically or strictly
  3004. increasing */
  3005. if (tableID->smoothness == AKIMA_C1 ||
  3006. tableID->smoothness == FRITSCH_BUTLAND_MONOTONE_C1 ||
  3007. tableID->smoothness == STEFFEN_MONOTONE_C1) {
  3008. size_t i;
  3009. for (i = 0; i < nRow - 1; i++) {
  3010. double t0 = TABLE_COL0(i);
  3011. double t1 = TABLE_COL0(i + 1);
  3012. if (t0 >= t1) {
  3013. ModelicaFormatError(
  3014. "The values of the first column of table \"%s(%lu,%lu)\" "
  3015. "are not strictly increasing because %s(%lu,1) (=%lf) "
  3016. ">= %s(%lu,1) (=%lf).\n", tableName, (unsigned long)nRow,
  3017. (unsigned long)nCol, tableName, (unsigned long)i + 1, t0,
  3018. tableName, (unsigned long)i + 2, t1);
  3019. isValid = 0;
  3020. return isValid;
  3021. }
  3022. }
  3023. }
  3024. else {
  3025. size_t i;
  3026. for (i = 0; i < nRow - 1; i++) {
  3027. double t0 = TABLE_COL0(i);
  3028. double t1 = TABLE_COL0(i + 1);
  3029. if (t0 > t1) {
  3030. ModelicaFormatError(
  3031. "The values of the first column of table \"%s(%lu,%lu)\" "
  3032. "are not monotonically increasing because %s(%lu,1) "
  3033. "(=%lf) > %s(%lu,1) (=%lf).\n", tableName,
  3034. (unsigned long)nRow, (unsigned long)nCol, tableName,
  3035. (unsigned long)i + 1, t0, tableName, (unsigned long)i +
  3036. 2, t1);
  3037. isValid = 0;
  3038. return isValid;
  3039. }
  3040. }
  3041. }
  3042. }
  3043. }
  3044. return isValid;
  3045. }
  3046. static int isValidCombiTable1D(const CombiTable1D* tableID) {
  3047. int isValid = 1;
  3048. if (tableID != NULL) {
  3049. const size_t nRow = tableID->nRow;
  3050. const size_t nCol = tableID->nCol;
  3051. const char* tableName;
  3052. const char* tableDummyName = "NoName";
  3053. size_t iCol;
  3054. if (tableID->source == TABLESOURCE_MODEL) {
  3055. tableName = tableDummyName;
  3056. }
  3057. else {
  3058. tableName = tableID->tableName;
  3059. }
  3060. /* Check dimensions */
  3061. if (nRow < 1 || nCol < 2) {
  3062. ModelicaFormatError(
  3063. "Table matrix \"%s(%lu,%lu)\" does not have appropriate "
  3064. "dimensions for 1D-interpolation.\n", tableName,
  3065. (unsigned long)nRow, (unsigned long)nCol);
  3066. isValid = 0;
  3067. return isValid;
  3068. }
  3069. /* Check column indices */
  3070. for (iCol = 0; iCol < tableID->nCols; ++iCol) {
  3071. const size_t col = (size_t)tableID->cols[iCol];
  3072. if (col < 1 || col > tableID->nCol) {
  3073. ModelicaFormatError("The column index %d is out of range "
  3074. "for table matrix \"%s(%lu,%lu)\".\n", tableID->cols[iCol],
  3075. tableName, (unsigned long)nRow, (unsigned long)nCol);
  3076. }
  3077. }
  3078. if (tableID->table != NULL) {
  3079. const double* table = tableID->table;
  3080. size_t i;
  3081. /* Check, whether first column values are strictly increasing */
  3082. for (i = 0; i < nRow - 1; i++) {
  3083. double x0 = TABLE_COL0(i);
  3084. double x1 = TABLE_COL0(i + 1);
  3085. if (x0 >= x1) {
  3086. ModelicaFormatError(
  3087. "The values of the first column of table \"%s(%lu,%lu)\" are "
  3088. "not strictly increasing because %s(%lu,1) (=%lf) >= "
  3089. "%s(%lu,1) (=%lf).\n", tableName, (unsigned long)nRow,
  3090. (unsigned long)nCol, tableName, (unsigned long)i + 1, x0,
  3091. tableName, (unsigned long)i + 2, x1);
  3092. isValid = 0;
  3093. return isValid;
  3094. }
  3095. }
  3096. }
  3097. }
  3098. return isValid;
  3099. }
  3100. static int isValidCombiTable2D(const CombiTable2D* tableID) {
  3101. int isValid = 1;
  3102. if (tableID != NULL) {
  3103. const size_t nRow = tableID->nRow;
  3104. const size_t nCol = tableID->nCol;
  3105. const char* tableName;
  3106. const char* tableDummyName = "NoName";
  3107. if (tableID->source == TABLESOURCE_MODEL) {
  3108. tableName = tableDummyName;
  3109. }
  3110. else {
  3111. tableName = tableID->tableName;
  3112. }
  3113. /* Check dimensions */
  3114. if (nRow < 2 || nCol < 2) {
  3115. ModelicaFormatError(
  3116. "Table matrix \"%s(%lu,%lu)\" does not have appropriate "
  3117. "dimensions for 2D-interpolation.\n", tableName,
  3118. (unsigned long)nRow, (unsigned long)nCol);
  3119. isValid = 0;
  3120. return isValid;
  3121. }
  3122. if (tableID->table != NULL) {
  3123. const double* table = tableID->table;
  3124. size_t i;
  3125. /* Check, whether first column values are strictly increasing */
  3126. for (i = 1; i < nRow - 1; i++) {
  3127. double x0 = TABLE_COL0(i);
  3128. double x1 = TABLE_COL0(i + 1);
  3129. if (x0 >= x1) {
  3130. ModelicaFormatError(
  3131. "The values of the first column of table \"%s(%lu,%lu)\" are "
  3132. "not strictly increasing because %s(%lu,1) (=%lf) >= "
  3133. "%s(%lu,1) (=%lf).\n", tableName, (unsigned long)nRow,
  3134. (unsigned long)nCol, tableName, (unsigned long)i + 1,
  3135. x0, tableName, (unsigned long)i + 2, x1);
  3136. isValid = 0;
  3137. return isValid;
  3138. }
  3139. }
  3140. /* Check, whether first row values are strictly increasing */
  3141. for (i = 1; i < nCol - 1; i++) {
  3142. double y0 = TABLE_ROW0(i);
  3143. double y1 = TABLE_ROW0(i + 1);
  3144. if (y0 >= y1) {
  3145. ModelicaFormatError(
  3146. "The values of the first row of table \"%s(%lu,%lu)\" are "
  3147. "not strictly increasing because %s(1,%lu) (=%lf) >= "
  3148. "%s(1,%lu) (=%lf).\n", tableName, (unsigned long)nRow,
  3149. (unsigned long)nCol, tableName, (unsigned long)i + 1,
  3150. y0, tableName, (unsigned long)i + 2, y1);
  3151. isValid = 0;
  3152. return isValid;
  3153. }
  3154. }
  3155. }
  3156. }
  3157. return isValid;
  3158. }
  3159. static enum TableSource getTableSource(const char *tableName,
  3160. const char *fileName) {
  3161. enum TableSource tableSource;
  3162. int tableNameGiven = isValidName(tableName);
  3163. int fileNameGiven = isValidName(fileName);
  3164. /* Determine in which way the table values are defined */
  3165. if (tableNameGiven == 0) {
  3166. /* No table name is given */
  3167. if (fileNameGiven != 0) {
  3168. ModelicaFormatError(
  3169. "The file name for a table (= \"%s\") is defined, "
  3170. "but not the corresponding table name.\n", fileName);
  3171. }
  3172. tableSource = TABLESOURCE_MODEL;
  3173. }
  3174. else {
  3175. /* A table name is given */
  3176. #if defined(NO_FILE_SYSTEM)
  3177. if (fileNameGiven != 0) {
  3178. ModelicaFormatError(
  3179. "The file name (= \"%s\") for a table (= \"%s\") is defined. "
  3180. "Since Modelica is used in an environment where storage cannot "
  3181. "be allocated (#define NO_FILE_SYSTEM), tables cannot be read "
  3182. "from file.\n", fileName, tableName);
  3183. }
  3184. tableSource = TABLESOURCE_FUNCTION;
  3185. #else
  3186. tableSource =
  3187. fileNameGiven == 0 ? TABLESOURCE_FUNCTION : TABLESOURCE_FILE;
  3188. #endif
  3189. }
  3190. return tableSource;
  3191. }
  3192. /* ----- Internal univariate spline functions ---- */
  3193. static CubicHermite1D* akimaSpline1DInit(const double* table, size_t nRow,
  3194. size_t nCol, const int* cols,
  3195. size_t nCols) {
  3196. /* Reference:
  3197. Hiroshi Akima. A new method of interpolation and smooth curve fitting
  3198. based on local procedures. Journal of the ACM, 17(4), 589-602, Oct. 1970.
  3199. (http://dx.doi.org/10.1145/321607.321609)
  3200. */
  3201. CubicHermite1D* spline = NULL;
  3202. double* d; /* Divided differences */
  3203. size_t col;
  3204. /* Actually there is no need for consecutive memory */
  3205. spline = (CubicHermite1D*)malloc((nRow - 1)*nCols*sizeof(CubicHermite1D));
  3206. if (spline == NULL) {
  3207. return NULL;
  3208. }
  3209. d = (double*)malloc((nRow + 3)*sizeof(double));
  3210. if (d == NULL) {
  3211. free(spline);
  3212. return NULL;
  3213. }
  3214. for (col = 0; col < nCols; col++) {
  3215. size_t i;
  3216. double c2;
  3217. /* Calculation of the divided differences */
  3218. for (i = 0; i < nRow - 1; i++) {
  3219. d[i + 2] =
  3220. (TABLE(i + 1, cols[col] - 1) - TABLE(i, cols[col] - 1))/
  3221. (TABLE_COL0(i + 1) - TABLE_COL0(i));
  3222. }
  3223. /* Extrapolation using non-periodic boundary conditions */
  3224. d[0] = 3*d[2] - 2*d[3];
  3225. d[1] = 2*d[2] - d[3];
  3226. d[nRow + 1] = 2*d[nRow] - d[nRow - 1];
  3227. d[nRow + 2] = 3*d[nRow] - 2*d[nRow - 1];
  3228. /* Initialization of the left boundary slope */
  3229. c2 = fabs(d[3] - d[2]) + fabs(d[1] - d[0]);
  3230. if (c2 > 0) {
  3231. const double a = fabs(d[1] - d[0])/c2;
  3232. c2 = (1 - a)*d[1] + a*d[2];
  3233. }
  3234. else {
  3235. c2 = 0.5*d[1] + 0.5*d[2];
  3236. }
  3237. /* Calculation of the 3(4) coefficients per interval */
  3238. for (i = 0; i < nRow - 1; i++) {
  3239. const double dx = TABLE_COL0(i + 1) - TABLE_COL0(i);
  3240. double* c = spline[IDX(i, col, nCols)];
  3241. c[2] = c2;
  3242. c2 = fabs(d[i + 4] - d[i + 3]) + fabs(d[i + 2] - d[i + 1]);
  3243. if (c2 > 0) {
  3244. const double a = fabs(d[i + 2] - d[i + 1])/c2;
  3245. c2 = (1 - a)*d[i + 2] + a*d[i + 3];
  3246. }
  3247. else {
  3248. c2 = 0.5*d[i + 2] + 0.5*d[i + 3];
  3249. }
  3250. c[1] = (3*d[i + 2] - 2*c[2] - c2)/dx;
  3251. c[0] = (c[2] + c2 - 2*d[i + 2])/(dx*dx);
  3252. /* No need to store the absolute term y0 */
  3253. /* c[3] = TABLE(i, cols[col] - 1); */
  3254. }
  3255. }
  3256. free(d);
  3257. return spline;
  3258. }
  3259. static CubicHermite1D* fritschButlandSpline1DInit(const double* table,
  3260. size_t nRow, size_t nCol,
  3261. const int* cols,
  3262. size_t nCols) {
  3263. /* Reference:
  3264. Frederick N. Fritsch and Judy Butland. A method for constructing local
  3265. monotone piecewise cubic interpolants. SIAM Journal on Scientific and
  3266. Statistical Computing, 5(2), 300-304, June 1984.
  3267. (http://dx.doi.org/10.1137/0905021)
  3268. */
  3269. CubicHermite1D* spline = NULL;
  3270. double* d; /* Divided differences */
  3271. size_t col;
  3272. /* Actually there is no need for consecutive memory */
  3273. spline = (CubicHermite1D*)malloc((nRow - 1)*nCols*sizeof(CubicHermite1D));
  3274. if (spline == NULL) {
  3275. return NULL;
  3276. }
  3277. d = (double*)malloc((nRow - 1)*sizeof(double));
  3278. if (d == NULL) {
  3279. free(spline);
  3280. return NULL;
  3281. }
  3282. for (col = 0; col < nCols; col++) {
  3283. size_t i;
  3284. double c2;
  3285. /* Calculation of the divided differences */
  3286. for (i = 0; i < nRow - 1; i++) {
  3287. d[i] =
  3288. (TABLE(i + 1, cols[col] - 1) - TABLE(i, cols[col] - 1))/
  3289. (TABLE_COL0(i + 1) - TABLE_COL0(i));
  3290. }
  3291. /* Initialization of the left boundary slope */
  3292. c2 = d[0];
  3293. /* Calculation of the 3(4) coefficients per interval */
  3294. for (i = 0; i < nRow - 1; i++) {
  3295. const double dx = TABLE_COL0(i + 1) - TABLE_COL0(i);
  3296. double* c = spline[IDX(i, col, nCols)];
  3297. c[2] = c2;
  3298. if (i == nRow - 2) {
  3299. c2 = d[nRow - 2];
  3300. }
  3301. else if (d[i] == 0 || d[i + 1] == 0 ||
  3302. (d[i] < 0 && d[i + 1] > 0) || (d[i] > 0 && d[i + 1] < 0)) {
  3303. c2 = 0;
  3304. }
  3305. else {
  3306. const double dx_ = TABLE_COL0(i + 2) - TABLE_COL0(i + 1);
  3307. c2 = 3*(dx + dx_)/((dx + 2*dx_)/d[i] + (dx_ + 2*dx)/d[i + 1]);
  3308. }
  3309. c[1] = (3*d[i] - 2*c[2] - c2)/dx;
  3310. c[0] = (c[2] + c2 - 2*d[i])/(dx*dx);
  3311. /* No need to store the absolute term y0 */
  3312. /* c[3] = TABLE(i, cols[col] - 1); */
  3313. }
  3314. }
  3315. free(d);
  3316. return spline;
  3317. }
  3318. static CubicHermite1D* steffenSpline1DInit(const double* table,
  3319. size_t nRow, size_t nCol,
  3320. const int* cols,
  3321. size_t nCols) {
  3322. /* Reference:
  3323. Matthias Steffen. A simple method for monotonic interpolation in one
  3324. dimension. Astronomy and Astrophysic, 239, 443-450, August 1990.
  3325. (https://trac.modelica.org/Modelica/attachment/ticket/1814/1990_Astronomy_and_Astrophysic_239_443S.pdf)
  3326. */
  3327. CubicHermite1D* spline = NULL;
  3328. double* d; /* Divided differences */
  3329. size_t col;
  3330. /* Actually there is no need for consecutive memory */
  3331. spline = (CubicHermite1D*)malloc((nRow - 1)*nCols*sizeof(CubicHermite1D));
  3332. if (spline == NULL) {
  3333. return NULL;
  3334. }
  3335. d = (double*)malloc((nRow - 1)*sizeof(double));
  3336. if (d == NULL) {
  3337. free(spline);
  3338. return NULL;
  3339. }
  3340. for (col = 0; col < nCols; col++) {
  3341. size_t i;
  3342. double c2;
  3343. /* Calculation of the divided differences */
  3344. for (i = 0; i < nRow - 1; i++) {
  3345. d[i] =
  3346. (TABLE(i + 1, cols[col] - 1) - TABLE(i, cols[col] - 1))/
  3347. (TABLE_COL0(i + 1) - TABLE_COL0(i));
  3348. }
  3349. /* Initialization of the left boundary slope */
  3350. c2 = d[0];
  3351. /* Calculation of the 3(4) coefficients per interval */
  3352. for (i = 0; i < nRow - 1; i++) {
  3353. const double dx = TABLE_COL0(i + 1) - TABLE_COL0(i);
  3354. double* c = spline[IDX(i, col, nCols)];
  3355. c[2] = c2;
  3356. if (i == nRow - 2) {
  3357. c2 = d[nRow - 2];
  3358. }
  3359. else if (d[i] == 0 || d[i + 1] == 0 ||
  3360. (d[i] < 0 && d[i + 1] > 0) || (d[i] > 0 && d[i + 1] < 0)) {
  3361. c2 = 0;
  3362. }
  3363. else {
  3364. const double dx_ = TABLE_COL0(i + 2) - TABLE_COL0(i + 1);
  3365. double half_abs_c2, abs_di, abs_di1;
  3366. c2 = (d[i]*dx_ + d[i + 1]*dx)/(dx + dx_);
  3367. half_abs_c2 = 0.5*fabs(c2);
  3368. abs_di = fabs(d[i]);
  3369. abs_di1 = fabs(d[i + 1]);
  3370. if (half_abs_c2 > abs_di || half_abs_c2 > abs_di1) {
  3371. const double two_a = d[i] > 0 ? 2 : -2;
  3372. c2 = two_a*(abs_di < abs_di1 ? abs_di : abs_di1);
  3373. }
  3374. }
  3375. c[1] = (3*d[i] - 2*c[2] - c2)/dx;
  3376. c[0] = (c[2] + c2 - 2*d[i])/(dx*dx);
  3377. /* No need to store the absolute term y0 */
  3378. /* c[3] = TABLE(i, cols[col] - 1); */
  3379. }
  3380. }
  3381. free(d);
  3382. return spline;
  3383. }
  3384. static void spline1DClose(CubicHermite1D** spline) {
  3385. if (spline != NULL && *spline != NULL) {
  3386. free(*spline);
  3387. *spline = NULL;
  3388. }
  3389. }
  3390. /* ----- Internal bivariate spline functions ---- */
  3391. static void spline1DExtrapolateLeft(double x1, double x2, double x3, double x4,
  3392. double x5, double* y1, double* y2,
  3393. double y3, double y4, double y5) {
  3394. const double a = x4 - x5;
  3395. const double b = x3 - x4;
  3396. const double c = x2 - x3;
  3397. if (a == 0. || b == 0. || c == 0.) {
  3398. *y2 = y3;
  3399. *y1 = y3;
  3400. }
  3401. else {
  3402. const double d = x1 - x2;
  3403. const double e = y4 - y5;
  3404. const double f = y3 - y4;
  3405. *y2 = c*(2*f/b - e/a) + y3;
  3406. *y1 = *y2 + d*((*y2 - y3)/c + f/b - e/a);
  3407. }
  3408. }
  3409. static void spline1DExtrapolateRight(double x1, double x2, double x3, double x4,
  3410. double x5, double y1, double y2, double y3,
  3411. double* y4, double* y5) {
  3412. const double a = x2 - x1;
  3413. const double b = x3 - x2;
  3414. const double c = x4 - x3;
  3415. if (a == 0. || b == 0. || c == 0.) {
  3416. *y4 = y3;
  3417. *y5 = y3;
  3418. }
  3419. else {
  3420. const double d = x5 - x4;
  3421. const double e = y2 - y1;
  3422. const double f = y3 - y2;
  3423. *y4 = c*(2*f/b - e/a) + y3;
  3424. *y5 = *y4 + d*((*y4 - y3)/c + f/b - e/a);
  3425. }
  3426. }
  3427. #define TABLE_EX(i, j) tableEx[IDX(i, j, nCol + 3)]
  3428. static CubicHermite2D* spline2DInit(const double* table, size_t nRow, size_t nCol) {
  3429. /* Reference:
  3430. Hiroshi Akima. A method of bivariate interpolation and smooth surface
  3431. fitting based on local procedures. Communications of the ACM, 17(1), 18-20,
  3432. Jan. 1974. (http://dx.doi.org/10.1145/360767.360779)
  3433. */
  3434. CubicHermite2D* spline = NULL;
  3435. if (nRow == 2 /* && nCol > 3 */) {
  3436. CubicHermite1D* spline1D;
  3437. size_t j;
  3438. int cols = 2;
  3439. /* Need to transpose */
  3440. double* tableT = (double*)malloc(2*(nCol - 1)*sizeof(double));
  3441. if (tableT == NULL) {
  3442. return NULL;
  3443. }
  3444. spline = (CubicHermite2D*)malloc((nCol - 1)*sizeof(CubicHermite2D));
  3445. if (spline == NULL) {
  3446. free(tableT);
  3447. return NULL;
  3448. }
  3449. for (j = 1; j < nCol; j++) {
  3450. tableT[IDX(j - 1, 0, 2)] = TABLE_ROW0(j);
  3451. tableT[IDX(j - 1, 1, 2)] = TABLE(1, j);
  3452. }
  3453. spline1D = akimaSpline1DInit(tableT, nCol - 1, 2, &cols, 1);
  3454. free(tableT);
  3455. if (spline1D == NULL) {
  3456. free(spline);
  3457. return NULL;
  3458. }
  3459. /* Copy coefficients */
  3460. for (j = 0; j < nCol - 1; j++) {
  3461. const double* c1 = spline1D[j];
  3462. double* c2 = spline[j];
  3463. c2[0] = c1[0];
  3464. c2[1] = c1[1];
  3465. c2[2] = c1[2];
  3466. }
  3467. spline1DClose(&spline1D);
  3468. }
  3469. else if (/*nRow > 3 && */ nCol == 2) {
  3470. CubicHermite1D* spline1D;
  3471. size_t i;
  3472. int cols = 2;
  3473. spline = (CubicHermite2D*)malloc((nRow - 1)*sizeof(CubicHermite2D));
  3474. if (spline == NULL) {
  3475. return NULL;
  3476. }
  3477. spline1D = akimaSpline1DInit(&table[2], nRow - 1, 2, &cols, 1);
  3478. if (spline1D == NULL) {
  3479. free(spline);
  3480. return NULL;
  3481. }
  3482. /* Copy coefficients */
  3483. for (i = 0; i < nRow - 1; i++) {
  3484. const double* c1 = spline1D[i];
  3485. double* c2 = spline[i];
  3486. c2[0] = c1[0];
  3487. c2[1] = c1[1];
  3488. c2[2] = c1[2];
  3489. }
  3490. spline1DClose(&spline1D);
  3491. }
  3492. else /* if (nRow > 2 && nCol > 2) */ {
  3493. size_t i, j;
  3494. double* dz_dx;
  3495. double* dz_dy;
  3496. double* d2z_dxdy;
  3497. double* tableEx;
  3498. double* x;
  3499. double* y;
  3500. /* This is not the most memory efficient implementation since the table
  3501. memory is temporarily doubled for the sake of a clean calculation of
  3502. the partial derivatives without special consideration of the boundary
  3503. regions.
  3504. */
  3505. /* Copy of x coordinates with extrapolated boundary coordinates */
  3506. x = (double*)malloc((nRow + 3)*sizeof(double));
  3507. if (x == NULL) {
  3508. return NULL;
  3509. }
  3510. if (nRow == 3) {
  3511. /* Linear extrapolation */
  3512. x[0] = 3*TABLE_COL0(1) - 2*TABLE_COL0(2);
  3513. x[1] = 2*TABLE_COL0(1) - TABLE_COL0(2);
  3514. x[2] = TABLE_COL0(1);
  3515. x[3] = TABLE_COL0(2);
  3516. x[4] = 2*TABLE_COL0(2) - TABLE_COL0(1);
  3517. x[5] = 3*TABLE_COL0(2) - 2*TABLE_COL0(1);
  3518. }
  3519. else {
  3520. x[0] = 2*TABLE_COL0(1) - TABLE_COL0(3);
  3521. x[1] = TABLE_COL0(1) + TABLE_COL0(2) - TABLE_COL0(3);
  3522. for (i = 1; i < nRow; i++) {
  3523. x[i + 1] = TABLE_COL0(i);
  3524. }
  3525. x[nRow + 1] = TABLE_COL0(nRow - 1) +
  3526. TABLE_COL0(nRow - 2) - TABLE_COL0(nRow - 3);
  3527. x[nRow + 2] = 2*TABLE_COL0(nRow - 1) - TABLE_COL0(nRow - 3);
  3528. }
  3529. /* Copy of y coordinates with extrapolated boundary coordinates */
  3530. y = (double*)malloc((nCol + 3)*sizeof(double));
  3531. if (y == NULL) {
  3532. free(x);
  3533. return NULL;
  3534. }
  3535. if (nCol == 3) {
  3536. /* Linear extrapolation */
  3537. y[0] = 3*TABLE_ROW0(1) - 2*TABLE_ROW0(2);
  3538. y[1] = 2*TABLE_ROW0(1) - TABLE_ROW0(2);
  3539. y[2] = TABLE_ROW0(1);
  3540. y[3] = TABLE_ROW0(2);
  3541. y[4] = 2*TABLE_ROW0(2) - TABLE_ROW0(1);
  3542. y[5] = 3*TABLE_ROW0(2) - 2*TABLE_ROW0(1);
  3543. }
  3544. else {
  3545. y[0] = 2*TABLE_ROW0(1) - TABLE_ROW0(3);
  3546. y[1] = TABLE_ROW0(1) + TABLE_ROW0(2) - TABLE_ROW0(3);
  3547. memcpy(&y[2], &TABLE_ROW0(1), (nCol - 1)*sizeof(double));
  3548. y[nCol + 1] = TABLE_ROW0(nCol - 1) +
  3549. TABLE_ROW0(nCol - 2) - TABLE_ROW0(nCol - 3);
  3550. y[nCol + 2] = 2*TABLE_ROW0(nCol - 1) - TABLE_ROW0(nCol - 3);
  3551. }
  3552. /* Copy of table with extrapolated boundary values */
  3553. tableEx = (double*)malloc((nRow + 3)*(nCol + 3)*sizeof(double));
  3554. if (tableEx == NULL) {
  3555. free(y);
  3556. free(x);
  3557. return NULL;
  3558. }
  3559. for (i = 1; i < nRow; i++) {
  3560. /* Copy table row */
  3561. memcpy(&TABLE_EX(i + 1, 2), &TABLE(i, 1), (nCol - 1)*sizeof(double));
  3562. }
  3563. if (nCol == 3) {
  3564. /* Linear extrapolation in y direction */
  3565. for (i = 1; i < nRow; i++) {
  3566. TABLE_EX(i + 1, 0) = 3*TABLE(i, 1) - 2*TABLE(i, 2);
  3567. TABLE_EX(i + 1, 1) = 2*TABLE(i, 1) - TABLE(i, 2);
  3568. TABLE_EX(i + 1, 4) = 2*TABLE(i, 2) - TABLE(i, 1);
  3569. TABLE_EX(i + 1, 5) = 3*TABLE(i, 2) - 2*TABLE(i, 1);
  3570. }
  3571. }
  3572. else {
  3573. for (i = 1; i < nRow; i++) {
  3574. /* Extrapolate table data in y direction */
  3575. spline1DExtrapolateLeft(y[0], y[1], y[2], y[3], y[4],
  3576. &TABLE_EX(i + 1, 0), &TABLE_EX(i + 1, 1), TABLE_EX(i + 1, 2),
  3577. TABLE_EX(i + 1, 3), TABLE_EX(i + 1, 4));
  3578. spline1DExtrapolateRight(y[nCol - 2], y[nCol - 1], y[nCol],
  3579. y[nCol + 1], y[nCol + 2], TABLE_EX(i + 1, nCol - 2),
  3580. TABLE_EX(i + 1, nCol - 1), TABLE_EX(i + 1, nCol),
  3581. &TABLE_EX(i + 1, nCol + 1), &TABLE_EX(i + 1, nCol + 2));
  3582. }
  3583. }
  3584. if (nRow == 3) {
  3585. /* Linear extrapolation in x direction */
  3586. for (j = 0; j < nCol + 3; j++) {
  3587. TABLE_EX(0, j) = 3*TABLE_EX(2, j) - 2*TABLE_EX(3, j);
  3588. TABLE_EX(1, j) = 2*TABLE_EX(2, j) - TABLE_EX(3, j);
  3589. TABLE_EX(4, j) = 2*TABLE_EX(3, j) - TABLE_EX(2, j);
  3590. TABLE_EX(5, j) = 3*TABLE_EX(3, j) - 2*TABLE_EX(2, j);
  3591. }
  3592. }
  3593. else {
  3594. for (j = 0; j < nCol + 3; j++) {
  3595. /* Extrapolate table data in x direction */
  3596. spline1DExtrapolateLeft(x[0], x[1], x[2], x[3], x[4],
  3597. &TABLE_EX(0, j), &TABLE_EX(1, j), TABLE_EX(2, j),
  3598. TABLE_EX(3, j), TABLE_EX(4, j));
  3599. spline1DExtrapolateRight(x[nRow - 2], x[nRow - 1], x[nRow],
  3600. x[nRow + 1], x[nRow + 2], TABLE_EX(nRow - 2, j),
  3601. TABLE_EX(nRow - 1, j), TABLE_EX(nRow, j),
  3602. &TABLE_EX(nRow + 1, j), &TABLE_EX(nRow + 2, j));
  3603. }
  3604. }
  3605. dz_dx = (double*)malloc((nRow - 1)*(nCol - 1)*sizeof(double));
  3606. if (dz_dx == NULL) {
  3607. free(tableEx);
  3608. free(y);
  3609. free(x);
  3610. return NULL;
  3611. }
  3612. dz_dy = (double*)malloc((nRow - 1)*(nCol - 1)*sizeof(double));
  3613. if (dz_dy == NULL) {
  3614. free(dz_dx);
  3615. free(tableEx);
  3616. free(y);
  3617. free(x);
  3618. return NULL;
  3619. }
  3620. d2z_dxdy = (double*)malloc((nRow - 1)*(nCol - 1)*sizeof(double));
  3621. if (d2z_dxdy == NULL) {
  3622. free(dz_dy);
  3623. free(dz_dx);
  3624. free(tableEx);
  3625. free(y);
  3626. free(x);
  3627. return NULL;
  3628. }
  3629. /* Calculation of the partial derivatives */
  3630. for (i = 2; i < nRow + 1; i++) {
  3631. for (j = 2; j < nCol + 1; j++) {
  3632. /* Divided differences */
  3633. double d31, d32, d33, d34, d22, d23, d42, d43;
  3634. /* Weights */
  3635. double wx2, wx3, wy2, wy3;
  3636. /* Partial derivatives in x direction */
  3637. d31 = (TABLE_EX(i - 1, j) - TABLE_EX(i - 2, j))/
  3638. (x[i - 1] - x[i - 2]); /* = c13 */
  3639. d32 = (TABLE_EX(i, j) - TABLE_EX(i - 1, j))/
  3640. (x[i] - x[i - 1]); /* = c23 */
  3641. d33 = (TABLE_EX(i + 1, j) - TABLE_EX(i, j))/
  3642. (x[i + 1] - x[i]); /* = c33 */
  3643. d34 = (TABLE_EX(i + 2, j) - TABLE_EX(i + 1, j))/
  3644. (x[i + 2] - x[i + 1]); /* = c43 */
  3645. if (d31 == d32 && d33 == d34) {
  3646. wx2 = 0.;
  3647. wx3 = 0.;
  3648. dz_dx[IDX(i - 2, j - 2, nCol - 1)] = 0.5*d32 + 0.5*d33;
  3649. }
  3650. else {
  3651. wx2 = fabs(d34 - d33);
  3652. wx3 = fabs(d32 - d31);
  3653. dz_dx[IDX(i - 2, j - 2, nCol - 1)] = (wx2*d32 + wx3*d33)/
  3654. (wx2 + wx3);
  3655. }
  3656. /* Partial derivatives in y direction */
  3657. d31 = (TABLE_EX(i, j - 1) - TABLE_EX(i, j - 2))/
  3658. (y[j - 1] - y[j - 2]);
  3659. d32 = (TABLE_EX(i, j) - TABLE_EX(i, j - 1))/
  3660. (y[j] - y[j - 1]);
  3661. d33 = (TABLE_EX(i, j + 1) - TABLE_EX(i, j))/
  3662. (y[j + 1] - y[j]);
  3663. d34 = (TABLE_EX(i, j + 2) - TABLE_EX(i, j + 1))/
  3664. (y[j + 2] - y[j + 1]);
  3665. if (d31 == d32 && d33 == d34) {
  3666. wy2 = 0.;
  3667. wy3 = 0.;
  3668. dz_dy[IDX(i - 2, j - 2, nCol - 1)] = 0.5*d32 + 0.5*d33;
  3669. }
  3670. else {
  3671. wy2 = fabs(d34 - d33);
  3672. wy3 = fabs(d32 - d31);
  3673. dz_dy[IDX(i - 2, j - 2, nCol - 1)] = (wy2*d32 + wy3*d33)/
  3674. (wy2 + wy3);
  3675. }
  3676. /* Partial cross derivatives */
  3677. d22 = (TABLE_EX(i - 1, j) - TABLE_EX(i - 1, j - 1))/
  3678. (y[j] - y[j - 1]);
  3679. d23 = (TABLE_EX(i - 1, j + 1) - TABLE_EX(i - 1, j))/
  3680. (y[j + 1] - y[j]);
  3681. d42 = (TABLE_EX(i + 1, j) - TABLE_EX(i + 1, j - 1))/
  3682. (y[j] - y[j - 1]);
  3683. d43 = (TABLE_EX(i + 1, j + 1) - TABLE_EX(i + 1, j))/
  3684. (y[j + 1] - y[j]);
  3685. d22 = (d32 - d22)/(x[i] - x[i - 1]); /* = e22 */
  3686. d23 = (d33 - d23)/(x[i] - x[i - 1]); /* = e23 */
  3687. d32 = (d42 - d32)/(x[i + 1] - x[i]); /* = e32 */
  3688. d33 = (d43 - d33)/(x[i + 1] - x[i]); /* = e33 */
  3689. if (wx2 == 0. && wx3 == 0.) {
  3690. wx2 = 1.;
  3691. wx3 = 1.;
  3692. }
  3693. if (wy2 == 0. && wy3 == 0.) {
  3694. wy2 = 1.;
  3695. wy3 = 1.;
  3696. }
  3697. d2z_dxdy[IDX(i - 2, j - 2, nCol - 1)] =
  3698. (wx2*(wy2*d22 + wy3*d23) + wx3*(wy2*d32 + wy3*d33))/
  3699. ((wx2 + wx3)*(wy2 + wy3));
  3700. }
  3701. }
  3702. free(tableEx);
  3703. free(y);
  3704. free(x);
  3705. /* Actually there is no need for consecutive memory */
  3706. spline = (CubicHermite2D*)malloc((nRow - 2)*(nCol - 2)*sizeof(CubicHermite2D));
  3707. if (spline == NULL) {
  3708. free(dz_dx);
  3709. free(dz_dy);
  3710. free(d2z_dxdy);
  3711. return NULL;
  3712. }
  3713. /* Calculation of the 15(16) coefficients per grid */
  3714. for (i = 0; i < nRow - 2; i++) {
  3715. const double dx = TABLE_COL0(i + 2) - TABLE_COL0(i + 1);
  3716. const double dx_2 = dx*dx;
  3717. const double dx_3 = dx_2*dx;
  3718. for (j = 0; j < nCol - 2; j++) {
  3719. const double z00 = TABLE(i + 1, j + 1);
  3720. const double z01 = TABLE(i + 1, j + 2);
  3721. const double z10 = TABLE(i + 2, j + 1);
  3722. const double z11 = TABLE(i + 2, j + 2);
  3723. const double dy = TABLE_ROW0(j + 2) - TABLE_ROW0(j + 1);
  3724. const double dy_2 = dy*dy;
  3725. const double dy_3 = dy_2*dy;
  3726. double zx00, zx01, zx10, zx11;
  3727. double zy00, zy01, zy10, zy11;
  3728. double zxy00, zxy01, zxy10, zxy11;
  3729. double t1, t2, t3, t4, t5, t6, t7, t8, t9;
  3730. double t10, t11, t12, t13, t14;
  3731. double* c = spline[IDX(i, j, nCol - 2)];
  3732. c[11] = dz_dx[IDX(i, j, nCol - 1)];
  3733. zx00 = c[11]*dx;
  3734. zx01 = dz_dx[IDX(i, j + 1, nCol - 1)]*dx;
  3735. zx10 = dz_dx[IDX(i + 1, j, nCol - 1)]*dx;
  3736. zx11 = dz_dx[IDX(i + 1, j + 1, nCol - 1)]*dx;
  3737. c[14] = dz_dy[IDX(i, j, nCol - 1)];
  3738. zy00 = c[14]*dy;
  3739. zy01 = dz_dy[IDX(i, j + 1, nCol - 1)]*dy;
  3740. zy10 = dz_dy[IDX(i + 1, j, nCol - 1)]*dy;
  3741. zy11 = dz_dy[IDX(i + 1, j + 1, nCol - 1)]*dy;
  3742. c[10] = d2z_dxdy[IDX(i, j, nCol - 1)];
  3743. zxy00 = c[10]*dx*dy;
  3744. zxy01 = d2z_dxdy[IDX(i, j + 1, nCol - 1)]*dx*dy;
  3745. zxy10 = d2z_dxdy[IDX(i + 1, j, nCol - 1)]*dx*dy;
  3746. zxy11 = d2z_dxdy[IDX(i + 1, j + 1, nCol - 1)]*dx*dy;
  3747. t1 = z00 - z10;
  3748. t2 = zx00 + zx10;
  3749. t3 = zy00 - zy10;
  3750. t4 = zy11 - zy01;
  3751. t5 = zxy00 + zxy10;
  3752. t6 = zxy11 + zxy01;
  3753. t7 = 2*zx00 + zx10;
  3754. t8 = 2*zxy00 + zxy10;
  3755. t9 = zxy11 + 2*zxy01;
  3756. t10 = zx00 - zx01;
  3757. t11 = z00 - z01;
  3758. t12 = t1 + (z11 - z01);
  3759. t13 = t3 - t4;
  3760. t4 = 2*t3 - t4;
  3761. t14 = 2*t12 + (t2 - (zx11 + zx01));
  3762. t12 = 3*t12 + (t7 - (zx11 + 2*zx01));
  3763. c[0] = (2*t14 + (2*t13 + (t5 + t6)))/(dx_3*dy_3);
  3764. c[1] = -(3*t14 + (2*t4 + (2*t5 + t6)))/(dx_3*dy_2);
  3765. c[2] = (2*t3 + t5)/(dx_3*dy);
  3766. c[3] = (2*t1 + t2)/dx_3;
  3767. c[4] = -(2*t12 + (3*t13 + (t8 + t9)))/(dx_2*dy_3);
  3768. c[5] = (3*t12 + (3*t4 + (2*t8 + t9)))/(dx_2*dy_2);
  3769. c[6] = -(3*t3 + t8)/(dx_2*dy);
  3770. c[7] = -(3*t1 + t7)/dx_2;
  3771. c[8] = (2*t10 + (zxy00 + zxy01))/(dx*dy_3);
  3772. c[9] = -(3*t10 + (2*zxy00 + zxy01))/(dx*dy_2);
  3773. c[12] = (2*t11 + (zy00 + zy01))/dy_3;
  3774. c[13] = -(3*t11 + (2*zy00 + zy01))/dy_2;
  3775. /* No need to store the absolute term z00 */
  3776. /* c[15] = z00; */
  3777. }
  3778. }
  3779. free(dz_dx);
  3780. free(dz_dy);
  3781. free(d2z_dxdy);
  3782. }
  3783. return spline;
  3784. }
  3785. #undef TABLE_EX
  3786. static void spline2DClose(CubicHermite2D** spline) {
  3787. if (spline != NULL && *spline != NULL) {
  3788. free(*spline);
  3789. *spline = NULL;
  3790. }
  3791. }
  3792. static void transpose(double* table, size_t nRow, size_t nCol) {
  3793. /* Reference:
  3794. Cycle-based in-place array transposition
  3795. (http://en.wikipedia.org/wiki/In-place_matrix_transposition#Non-square_matrices:_Following_the_cycles)
  3796. */
  3797. size_t i;
  3798. for (i = 1; i < nRow*nCol - 1; i++) {
  3799. size_t x = nRow*(i % nCol) + i/nCol; /* predecessor of i in the cycle */
  3800. /* Continue if cycle is of length one or predecessor already was visited */
  3801. if (x <= i) {
  3802. continue;
  3803. }
  3804. /* Continue if cycle already was visited */
  3805. while (x > i) {
  3806. x = nRow*(x % nCol) + x/nCol;
  3807. }
  3808. if (x < i) {
  3809. continue;
  3810. }
  3811. {
  3812. double tmp = table[i];
  3813. size_t s = i; /* start index in the cycle */
  3814. x = nRow*(i % nCol) + i/nCol; /* predecessor of i in the cycle */
  3815. while (x != i) {
  3816. table[s] = table[x];
  3817. s = x;
  3818. x = nRow*(x % nCol) + x/nCol;
  3819. }
  3820. table[s] = tmp;
  3821. }
  3822. }
  3823. }
  3824. /* ----- Internal I/O functions ----- */
  3825. #if !defined(NO_FILE_SYSTEM)
  3826. static double* readTable(const char* tableName, const char* fileName,
  3827. size_t* nRow, size_t* nCol, int verbose, int force) {
  3828. double* table = NULL;
  3829. if (tableName != NULL && fileName != NULL && nRow != NULL && nCol != NULL) {
  3830. #if defined(TABLE_SHARE)
  3831. char* key = malloc((strlen(tableName) +
  3832. strlen(fileName) + 2)*sizeof(char));
  3833. if (key != NULL) {
  3834. int updateError = 0;
  3835. TableShare *iter;
  3836. strcpy(key, tableName);
  3837. strcat(key, "|");
  3838. strcat(key, fileName);
  3839. MUTEX_LOCK();
  3840. HASH_FIND_STR(tableShare, key, iter);
  3841. if (iter == NULL || force) {
  3842. #endif
  3843. const char* ext;
  3844. int isMatExt = 0;
  3845. /* Table file can be either ASCII text or binary MATLAB MAT-file */
  3846. ext = strrchr(fileName, '.');
  3847. if (ext != NULL) {
  3848. if (0 == strncmp(ext, ".mat", 4) ||
  3849. 0 == strncmp(ext, ".MAT", 4)) {
  3850. isMatExt = 1;
  3851. }
  3852. }
  3853. if (verbose == 1) {
  3854. /* Print info message, that table / file is loading */
  3855. ModelicaFormatMessage("... loading \"%s\" from \"%s\"\n",
  3856. tableName, fileName);
  3857. }
  3858. #if defined(TABLE_SHARE)
  3859. /* Release lock since readMatTable/readTxtTable may fail with
  3860. ModelicaError
  3861. */
  3862. MUTEX_UNLOCK();
  3863. #endif
  3864. if (isMatExt) {
  3865. table = readMatTable(tableName, fileName, nRow, nCol);
  3866. }
  3867. else {
  3868. table = readTxtTable(tableName, fileName, nRow, nCol);
  3869. }
  3870. if (table == NULL) {
  3871. #if defined(TABLE_SHARE)
  3872. free(key);
  3873. #endif
  3874. return table;
  3875. }
  3876. #if defined(TABLE_SHARE)
  3877. /* Again ask for lock and search in hash table share */
  3878. MUTEX_LOCK();
  3879. HASH_FIND_STR(tableShare, key, iter);
  3880. }
  3881. if (iter == NULL) {
  3882. /* Share miss -> Insert new table */
  3883. iter = malloc(sizeof(TableShare));
  3884. if (iter != NULL) {
  3885. iter->key = key;
  3886. iter->refCount = 1;
  3887. iter->nRow = *nRow;
  3888. iter->nCol = *nCol;
  3889. iter->table = table;
  3890. HASH_ADD_KEYPTR(hh, tableShare, key, strlen(key), iter);
  3891. }
  3892. else {
  3893. free(key);
  3894. }
  3895. }
  3896. else if (force) {
  3897. /* Share hit -> Update table share (only if not shared
  3898. by multiple table objects)
  3899. */
  3900. free(key);
  3901. if (iter->refCount == 1) {
  3902. free(iter->table);
  3903. iter->nRow = *nRow;
  3904. iter->nCol = *nCol;
  3905. iter->table = table;
  3906. }
  3907. else {
  3908. updateError = 1;
  3909. }
  3910. }
  3911. else {
  3912. /* Share hit -> Read from table share and increment table
  3913. reference counter
  3914. */
  3915. free(key);
  3916. if (table != NULL) {
  3917. free(table);
  3918. }
  3919. iter->refCount++;
  3920. table = iter->table;
  3921. *nRow = iter->nRow;
  3922. *nCol = iter->nCol;
  3923. }
  3924. MUTEX_UNLOCK();
  3925. if (updateError == 1) {
  3926. ModelicaFormatError("Not possible to update shared "
  3927. "table \"%s\" from \"%s\": File and table name "
  3928. "must be unique.\n", tableName, fileName);
  3929. }
  3930. }
  3931. #endif
  3932. }
  3933. return table;
  3934. }
  3935. static double* readMatTable(const char* tableName, const char* fileName,
  3936. size_t* _nRow, size_t* _nCol) {
  3937. double* table = NULL;
  3938. if (tableName != NULL && fileName != NULL && _nRow != NULL && _nCol != NULL) {
  3939. mat_t* mat;
  3940. matvar_t* matvar;
  3941. matvar_t* matvarRoot;
  3942. size_t nRow, nCol;
  3943. int tableReadError = 0;
  3944. char* tableNameCopy;
  3945. char* token;
  3946. tableNameCopy = (char*)malloc((strlen(tableName) + 1)*sizeof(char));
  3947. if (tableNameCopy != NULL) {
  3948. strcpy(tableNameCopy, tableName);
  3949. }
  3950. else {
  3951. ModelicaError("Memory allocation error\n");
  3952. return NULL;
  3953. }
  3954. mat = Mat_Open(fileName, (int)MAT_ACC_RDONLY);
  3955. if (mat == NULL) {
  3956. free(tableNameCopy);
  3957. ModelicaFormatError("Not possible to open file \"%s\": "
  3958. "No such file or directory\n", fileName);
  3959. return NULL;
  3960. }
  3961. token = strtok(tableNameCopy, ".");
  3962. matvarRoot = Mat_VarReadInfo(mat, token == NULL ? tableName : token);
  3963. if (matvarRoot == NULL) {
  3964. free(tableNameCopy);
  3965. (void)Mat_Close(mat);
  3966. ModelicaFormatError(
  3967. "Table variable \"%s\" not found on file \"%s\".\n",
  3968. token == NULL ? tableName : token, fileName);
  3969. return NULL;
  3970. }
  3971. matvar = matvarRoot;
  3972. token = strtok(NULL, ".");
  3973. /* Get field while matvar is of struct class and of 1x1 size */
  3974. while (token != NULL && matvar != NULL &&
  3975. matvar->class_type == MAT_C_STRUCT && matvar->rank == 2 &&
  3976. matvar->dims[0] == 1 && matvar->dims[1] == 1) {
  3977. matvar = Mat_VarGetStructField(matvar, (void*)token, MAT_BY_NAME, 0);
  3978. token = strtok(NULL, ".");
  3979. }
  3980. free(tableNameCopy);
  3981. if (matvar == NULL) {
  3982. Mat_VarFree(matvarRoot);
  3983. (void)Mat_Close(mat);
  3984. ModelicaFormatError(
  3985. "Table matrix \"%s\" not found on file \"%s\".\n", tableName,
  3986. fileName);
  3987. return NULL;
  3988. }
  3989. /* Check if matvar is a matrix */
  3990. if (matvar->rank != 2) {
  3991. Mat_VarFree(matvarRoot);
  3992. (void)Mat_Close(mat);
  3993. ModelicaFormatError(
  3994. "Table array \"%s\" has not the required rank 2.\n", tableName);
  3995. return NULL;
  3996. }
  3997. /* Check if matvar is of double precision class (and thus non-sparse) */
  3998. if (matvar->class_type != MAT_C_DOUBLE) {
  3999. Mat_VarFree(matvarRoot);
  4000. (void)Mat_Close(mat);
  4001. ModelicaFormatError("Table matrix \"%s\" has not the required "
  4002. "double precision class.\n", tableName);
  4003. return NULL;
  4004. }
  4005. /* Check if matvar is purely real-valued */
  4006. if (matvar->isComplex) {
  4007. Mat_VarFree(matvarRoot);
  4008. (void)Mat_Close(mat);
  4009. ModelicaFormatError("Table matrix \"%s\" must not be complex.\n",
  4010. tableName);
  4011. return NULL;
  4012. }
  4013. table = (double*)malloc(matvar->dims[0]*matvar->dims[1]*sizeof(double));
  4014. if (table == NULL) {
  4015. Mat_VarFree(matvarRoot);
  4016. (void)Mat_Close(mat);
  4017. ModelicaError("Memory allocation error\n");
  4018. return NULL;
  4019. }
  4020. nRow = matvar->dims[0];
  4021. nCol = matvar->dims[1];
  4022. {
  4023. int start[2] = {0, 0};
  4024. int stride[2] = {1, 1};
  4025. int edge[2];
  4026. edge[0] = (int)nRow;
  4027. edge[1] = (int)nCol;
  4028. tableReadError = Mat_VarReadData(mat, matvar, table, start, stride, edge);
  4029. }
  4030. Mat_VarFree(matvarRoot);
  4031. (void)Mat_Close(mat);
  4032. if (tableReadError == 0) {
  4033. /* Array is stored column-wise -> need to transpose */
  4034. transpose(table, nRow, nCol);
  4035. *_nRow = nRow;
  4036. *_nCol = nCol;
  4037. }
  4038. else {
  4039. free(table);
  4040. *_nRow = 0;
  4041. *_nCol = 0;
  4042. ModelicaFormatError(
  4043. "Error when reading numeric data of matrix \"%s(%lu,%lu)\" "
  4044. "from file \"%s\"\n", tableName, (unsigned long)nRow,
  4045. (unsigned long)nCol, fileName);
  4046. return NULL;
  4047. }
  4048. }
  4049. return table;
  4050. }
  4051. #define DELIM_TABLE_HEADER " \t(,)\r"
  4052. #define DELIM_TABLE_NUMBER " \t,;\r"
  4053. static double* readTxtTable(const char* tableName, const char* fileName,
  4054. size_t* _nRow, size_t* _nCol) {
  4055. double* table = NULL;
  4056. if (tableName != NULL && fileName != NULL && _nRow != NULL && _nCol != NULL) {
  4057. char* buf;
  4058. int bufLen = LINE_BUFFER_LENGTH;
  4059. FILE* fp;
  4060. int foundTable = 0;
  4061. int tableReadError;
  4062. unsigned int nRow = 0;
  4063. unsigned int nCol = 0;
  4064. unsigned int lineNo = 1;
  4065. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4066. _locale_t loc;
  4067. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4068. locale_t loc;
  4069. #else
  4070. char* dec;
  4071. #endif
  4072. fp = fopen(fileName, "r");
  4073. if (fp == NULL) {
  4074. ModelicaFormatError("Not possible to open file \"%s\": "
  4075. "No such file or directory\n", fileName);
  4076. return NULL;
  4077. }
  4078. buf = (char*)malloc(LINE_BUFFER_LENGTH*sizeof(char));
  4079. if (buf == NULL) {
  4080. fclose(fp);
  4081. ModelicaError("Memory allocation error\n");
  4082. return NULL;
  4083. }
  4084. /* Read file header */
  4085. if ((tableReadError = readLine(&buf, &bufLen, fp)) != 0) {
  4086. free(buf);
  4087. fclose(fp);
  4088. if (tableReadError < 0) {
  4089. ModelicaFormatError(
  4090. "Error reading first line from file \"%s\": "
  4091. "End-Of-File reached.\n", fileName);
  4092. }
  4093. return NULL;
  4094. }
  4095. /* Expected file header format: "#1" */
  4096. if (0 != strncmp(buf, "#1", 2)) {
  4097. size_t len = strlen(buf);
  4098. fclose(fp);
  4099. if (len == 0) {
  4100. free(buf);
  4101. ModelicaFormatError(
  4102. "Error reading format and version information in first "
  4103. "line of file \"%s\": \"#1\" expected.\n", fileName);
  4104. }
  4105. else if (len == 1) {
  4106. char c0 = buf[0];
  4107. free(buf);
  4108. ModelicaFormatError(
  4109. "Error reading format and version information in first "
  4110. "line of file \"%s\": \"#1\" expected, but \"%c\" found.\n",
  4111. fileName, c0);
  4112. }
  4113. else {
  4114. char c0 = buf[0];
  4115. char c1 = buf[1];
  4116. free(buf);
  4117. ModelicaFormatError(
  4118. "Error reading format and version information in first "
  4119. "line of file \"%s\": \"#1\" expected, but \"%c%c\" "
  4120. "found.\n", fileName, c0, c1);
  4121. }
  4122. return NULL;
  4123. }
  4124. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4125. loc = _create_locale(LC_NUMERIC, "C");
  4126. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4127. loc = newlocale(LC_NUMERIC, "C", NULL);
  4128. #else
  4129. dec = localeconv()->decimal_point;
  4130. #endif
  4131. /* Loop over lines of file */
  4132. while (readLine(&buf, &bufLen, fp) == 0) {
  4133. char* token;
  4134. char* endptr;
  4135. lineNo++;
  4136. /* Expected table header format: "dataType tableName(nRow,nCol)" */
  4137. token = strtok(buf, DELIM_TABLE_HEADER);
  4138. if (token == NULL) {
  4139. continue;
  4140. }
  4141. if ((0 != strcmp(token, "double")) && (0 != strcmp(token, "float"))) {
  4142. continue;
  4143. }
  4144. token = strtok(NULL, DELIM_TABLE_HEADER);
  4145. if (token == NULL) {
  4146. continue;
  4147. }
  4148. if (0 == strcmp(token, tableName)) {
  4149. foundTable = 1;
  4150. }
  4151. else {
  4152. continue;
  4153. }
  4154. token = strtok(NULL, DELIM_TABLE_HEADER);
  4155. if (token == NULL) {
  4156. continue;
  4157. }
  4158. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4159. nRow = (unsigned long)_strtol_l(token, &endptr, 10, loc);
  4160. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4161. nRow = (unsigned long)strtol_l(token, &endptr, 10, loc);
  4162. #else
  4163. nRow = (unsigned long)strtol(token, &endptr, 10);
  4164. #endif
  4165. if (*endptr != 0) {
  4166. continue;
  4167. }
  4168. token = strtok(NULL, DELIM_TABLE_HEADER);
  4169. if (token == NULL) {
  4170. continue;
  4171. }
  4172. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4173. nCol = (unsigned long)_strtol_l(token, &endptr, 10, loc);
  4174. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4175. nCol = (unsigned long)strtol_l(token, &endptr, 10, loc);
  4176. #else
  4177. nCol = (unsigned long)strtol(token, &endptr, 10);
  4178. #endif
  4179. if (*endptr != 0) {
  4180. continue;
  4181. }
  4182. { /* foundTable == 1 */
  4183. size_t i = 0;
  4184. size_t j = 0;
  4185. table = (double*)malloc(nRow*nCol*sizeof(double));
  4186. if (table == NULL) {
  4187. *_nRow = 0;
  4188. *_nCol = 0;
  4189. free(buf);
  4190. fclose(fp);
  4191. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4192. _free_locale(loc);
  4193. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4194. freelocale(loc);
  4195. #endif
  4196. ModelicaError("Memory allocation error\n");
  4197. return table;
  4198. }
  4199. /* Loop over rows and store table row-wise */
  4200. while (tableReadError == 0 && i < nRow) {
  4201. int k = 0;
  4202. lineNo++;
  4203. if ((tableReadError = readLine(&buf, &bufLen, fp)) != 0) {
  4204. break;
  4205. }
  4206. /* Ignore leading white space */
  4207. while (k < bufLen - 1) {
  4208. if (buf[k] != ' ' && buf[k] != '\t') {
  4209. break;
  4210. }
  4211. k++;
  4212. }
  4213. if (buf[k] == '\0' || buf[k] == '#') {
  4214. /* Skip empty or comment line */
  4215. continue;
  4216. }
  4217. token = strtok(&buf[k], DELIM_TABLE_NUMBER);
  4218. while (token != NULL && i < nRow && j < nCol) {
  4219. if (token[0] == '#') {
  4220. /* Skip trailing comment line */
  4221. break;
  4222. }
  4223. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4224. TABLE(i, j) = _strtod_l(token, &endptr, loc);
  4225. if (*endptr != 0) {
  4226. tableReadError = 1;
  4227. }
  4228. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4229. TABLE(i, j) = strtod_l(token, &endptr, loc);
  4230. if (*endptr != 0) {
  4231. tableReadError = 1;
  4232. }
  4233. #else
  4234. if (*dec == '.') {
  4235. TABLE(i, j) = strtod(token, &endptr);
  4236. }
  4237. else if (NULL == strchr(token, '.')) {
  4238. TABLE(i, j) = strtod(token, &endptr);
  4239. }
  4240. else {
  4241. char* token2 = (char*)malloc(
  4242. (strlen(token) + 1)*sizeof(char));
  4243. if (token2 != NULL) {
  4244. char* p;
  4245. strcpy(token2, token);
  4246. p = strchr(token2, '.');
  4247. *p = *dec;
  4248. TABLE(i, j) = strtod(token2, &endptr);
  4249. if (*endptr != 0) {
  4250. tableReadError = 1;
  4251. }
  4252. free(token2);
  4253. }
  4254. else {
  4255. *_nRow = 0;
  4256. *_nCol = 0;
  4257. free(buf);
  4258. fclose(fp);
  4259. tableReadError = 1;
  4260. ModelicaError("Memory allocation error\n");
  4261. break;
  4262. }
  4263. }
  4264. #endif
  4265. if (++j == nCol) {
  4266. i++; /* Increment row index */
  4267. j = 0; /* Reset column index */
  4268. }
  4269. if (tableReadError == 0) {
  4270. token = strtok(NULL, DELIM_TABLE_NUMBER);
  4271. continue;
  4272. }
  4273. else {
  4274. break;
  4275. }
  4276. }
  4277. /* Check for trailing non-comment character */
  4278. if (token != NULL && token[0] != '#') {
  4279. /* Check for trailing number */
  4280. if (i == nRow) {
  4281. int foundExponentSign = 0;
  4282. int foundExponent = 0;
  4283. int foundDec = 0;
  4284. tableReadError = 2;
  4285. if (token[0] == '+' || token[0] == '-') {
  4286. k = 1;
  4287. }
  4288. else {
  4289. k = 0;
  4290. }
  4291. while (token[k] != '\0') {
  4292. if (token[k] >= '0' && token[k] <= '9') {
  4293. k++;
  4294. }
  4295. else if (token[k] == '.' && foundDec == 0 &&
  4296. foundExponent == 0 && foundExponentSign == 0) {
  4297. foundDec = 1;
  4298. k++;
  4299. }
  4300. else if ((token[k] == 'e' || token[k] == 'E') &&
  4301. foundExponent == 0) {
  4302. foundExponent = 1;
  4303. k++;
  4304. }
  4305. else if ((token[k] == '+' || token[k] == '-') &&
  4306. foundExponent == 1 && foundExponentSign == 0) {
  4307. foundExponentSign = 1;
  4308. k++;
  4309. }
  4310. else {
  4311. tableReadError = 1;
  4312. break;
  4313. }
  4314. }
  4315. }
  4316. else {
  4317. tableReadError = 1;
  4318. }
  4319. break;
  4320. }
  4321. }
  4322. break;
  4323. }
  4324. }
  4325. free(buf);
  4326. fclose(fp);
  4327. #if defined(_MSC_VER) && _MSC_VER >= 1400
  4328. _free_locale(loc);
  4329. #elif defined(__GLIBC__) && defined(__GLIBC_MINOR__) && ((__GLIBC__ << 16) + __GLIBC_MINOR__ >= (2 << 16) + 3) && !defined(LABCAR)
  4330. freelocale(loc);
  4331. #endif
  4332. if (foundTable == 0) {
  4333. ModelicaFormatError(
  4334. "Table matrix \"%s\" not found on file \"%s\".\n",
  4335. tableName, fileName);
  4336. return table;
  4337. }
  4338. if (tableReadError == 0) {
  4339. *_nRow = (size_t)nRow;
  4340. *_nCol = (size_t)nCol;
  4341. }
  4342. else {
  4343. free(table);
  4344. table = NULL;
  4345. *_nRow = 0;
  4346. *_nCol = 0;
  4347. if (tableReadError == EOF) {
  4348. ModelicaFormatError(
  4349. "End-of-file reached when reading numeric data of matrix "
  4350. "\"%s(%lu,%lu)\" from file \"%s\"\n", tableName, nRow,
  4351. nCol, fileName);
  4352. }
  4353. else if (tableReadError == 2) {
  4354. ModelicaFormatError(
  4355. "The table dimensions of matrix \"%s(%lu,%lu)\" from file "
  4356. "\"%s\" do not match the actual table size.\n", tableName,
  4357. nRow, nCol, fileName);
  4358. }
  4359. else {
  4360. ModelicaFormatError(
  4361. "Error in line %lu when reading numeric data of matrix "
  4362. "\"%s(%lu,%lu)\" from file \"%s\"\n", lineNo, tableName,
  4363. nRow, nCol, fileName);
  4364. }
  4365. }
  4366. }
  4367. return table;
  4368. }
  4369. #undef DELIM_TABLE_HEADER
  4370. #undef DELIM_TABLE_NUMBER
  4371. static int readLine(char** buf, int* bufLen, FILE* fp) {
  4372. char* offset;
  4373. int oldBufLen;
  4374. if (fgets(*buf, *bufLen, fp) == NULL) {
  4375. return EOF;
  4376. }
  4377. do {
  4378. char* p;
  4379. char* tmp;
  4380. if ((p = strchr(*buf, '\n')) != NULL) {
  4381. *p = '\0';
  4382. return 0;
  4383. }
  4384. oldBufLen = *bufLen;
  4385. *bufLen *= 2;
  4386. tmp = (char*)realloc(*buf, (size_t)*bufLen);
  4387. if (tmp == NULL) {
  4388. fclose(fp);
  4389. free(*buf);
  4390. ModelicaError("Memory allocation error\n");
  4391. return 1;
  4392. }
  4393. *buf = tmp;
  4394. offset = &((*buf)[oldBufLen - 1]);
  4395. } while (fgets(offset, oldBufLen + 1, fp));
  4396. return 0;
  4397. }
  4398. #endif /* #if !defined(NO_FILE_SYSTEM) */
  4399. #if defined(DUMMY_FUNCTION_USERTAB)
  4400. int usertab(char* tableName, int nipo, int dim[], int* colWise,
  4401. double** table) {
  4402. ModelicaError("Function \"usertab\" is not implemented\n");
  4403. return 1; /* Error */
  4404. }
  4405. #endif /* #if defined(DUMMY_FUNCTION_USERTAB) */