ss2dym_rt_omp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /* ss2dym.c
  2. SS2DYM - SimStruct to Dymola interface.
  3. Author: Hilding Elmqvist, Dynasim AB
  4. Version:
  5. 1.0, 1995-03-19
  6. 1.1, 1996-09-27:
  7. Second S-Function parameter x0 for initial conditions added.
  8. Two parameters param and x0 must now be provided in the
  9. S-Function block. Courtesy: ABB Corporate Research, Switzerland
  10. 1.2, 1996-11-06:
  11. Modifications in order to allow approximate state event handling.
  12. 1.3, 1997-04-17: Corrected non-essential index bug.
  13. 1.4, 1997-07-18: Initialization of iErr added.
  14. 1.5, 1997-11-26: Support of state events (zero crossing)
  15. in Simulink 2.1 added.
  16. 1.6, 1997-11-29: Fixes for compatibility with Matlab 4.2
  17. 1.7, 1998-02-10: Fixed problem with ssIsSampleHitEvent in mdlUpdate
  18. 1.8, 1998-03-28: Activated state event handling.
  19. 1.9, 1998-09-13: Introduced call to ssSetSolverNeedsReset(S) to
  20. handle init(x).
  21. Clean-up for dSPACE use.
  22. 1.10 1999-01-20: Further clean-up for dSPACE and MATLAB 4.2
  23. 1.11 1999-06-20: Support for time events.
  24. 1.12 1999-06-21: Support for sections.
  25. 1.13 2000-10-09: Improve event handling and stiff solvers, set solverHandleEq_
  26. 1.14 2001-02-16: Add terminate call.
  27. 1.15 2001-10-01: Correct FeedThrough for Matlab 6.1.
  28. This is a SimStruct to Dymola interface. It enables models generated
  29. by Dymola to be incorporated in SIMULINK.
  30. */
  31. /*
  32. * Copyright (C) 1995-2005 Dynasim AB.
  33. * All rights reserved.
  34. *
  35. */
  36. /* OpenMP RT interface */
  37. #ifndef _GNU_SOURCE
  38. #define _GNU_SOURCE
  39. #endif
  40. #include <sched.h>
  41. #include <mpadvise.h>
  42. #include <omp.h>
  43. #define NUM_THREADS 4
  44. /* Uncomment only if the model that is exported to Simulink contains an imported FMU. */
  45. /* #define ModelWithFMU */
  46. #ifndef S_FUNCTION_LEVEL
  47. #define S_FUNCTION_LEVEL 2
  48. #endif
  49. /* Must increase to 2 for Jacobians. Can only use level 2 for Matlab 6.1 (and Matlab 6.0)
  50. and later (since some functions were not present in Matlab 5.3). */
  51. #if S_FUNCTION_LEVEL > 1 && !defined(Matlab6) && !((defined(RT) || defined(NRT)) && !defined(DYMOLA_DSPACE))
  52. #undef S_FUNCTION_LEVEL
  53. #define S_FUNCTION_LEVEL 1
  54. #endif
  55. #if !defined(DYMOLA_STATIC)
  56. #if defined(__cplusplus)
  57. #define DYMOLA_STATIC extern
  58. #elif defined(RT) && !defined(DYM2DS) && !defined(DYM2CCUR)
  59. #define DYMOLA_STATIC static
  60. #else
  61. #define DYMOLA_STATIC
  62. #endif
  63. #endif
  64. #if (defined(RT) || defined(NRT)) && !defined(DYMOLA_DSPACE)
  65. #if !defined(Matlab51)
  66. #define Matlab51
  67. #endif
  68. #if !defined(Matlab6)
  69. #define Matlab6
  70. #endif
  71. #if !defined(LABCAR)
  72. #define NO_FILE
  73. #endif
  74. #define DYMOSIM_RT_IMP
  75. #endif
  76. #if !defined(S_FUNCTION_NAME)
  77. #if !defined(Matlab51) && defined(DYMOLA_DSPACE)
  78. #define S_FUNCTION_NAME simstr
  79. #else
  80. #define S_FUNCTION_NAME SimStr
  81. #endif
  82. #endif
  83. #if defined(RT) && !defined(DYM2CCUR)
  84. static int simulinkInterface_;
  85. static int DirectFeedThrough_;
  86. #endif
  87. /*#define AUTO_RESET_AFTER 5*/
  88. #if defined(AUTO_RESET_AFTER)
  89. static int isAutoResetting=0;
  90. static double autoResetTime=0;
  91. #endif
  92. #define HAVE_GENERATED_JACOBIAN 0
  93. /* Change to 1 to use Jacobian code (must generate Jacobian code in Dymola) */
  94. /* Requires Matlab 6.1 (or Matlab 6.0?) */
  95. /* You also have to uncomment S_FUNCTION_LEVEL as above */
  96. #if HAVE_GENERATED_JACOBIAN && !(S_FUNCTION_LEVEL > 1)
  97. #error Jacobian require S_FUNCTION_LEVEL 2
  98. #undef HAVE_GENERATED_JACOBIAN
  99. /* Note: This error check might fail!!!! */
  100. #endif
  101. #if defined(DYMOLA_DSPACE)
  102. /* Assume we are compiling for dSPACE application. */
  103. #if !defined(__GNUC__)
  104. #if !defined(_MICROTEC)
  105. /* If this is not the Microtec GCC compiler, assume TI DSP (DS1003) */
  106. #define double real_T
  107. #endif
  108. #endif
  109. #include <dsdefs.h>
  110. #endif
  111. #if defined(Matlab51)
  112. #include "tmwtypes.h"
  113. #endif
  114. #include <math.h>
  115. #include "simstruc.h"
  116. #include "dsblock.h"
  117. #include "userdefs.h"
  118. #include "sprwat.h"
  119. #define LogCalls 0 /* Log calls to mdl-functions or not. */
  120. #if 0
  121. #define DirectFeedThrough 0 /* Old manual setting of DirectFeedThrough, no longer used.
  122. Change this to 0 if there is no direct feed
  123. through and you need to use the block in a
  124. loop. This will make the simulation a little
  125. slower. Setting it to 0 can crash in Matlab 6.1.*/
  126. #else
  127. extern int DirectFeedThrough_;
  128. #define DirectFeedThrough DirectFeedThrough_
  129. #endif
  130. static int NotInitialized = 1;
  131. /* To re-calculate bound parameters */
  132. static int ParamChange = 0;
  133. static int EventInUpdate = 1;
  134. static int HaveEventIterated_=0;
  135. static double InitTime_;
  136. #ifndef DYMOLA_TIMES
  137. #define DYMOLA_TIMES
  138. struct DymolaTimes {
  139. int num;
  140. double maxim;
  141. double minim;
  142. double total;
  143. int numAccum;
  144. double maximAccum;
  145. double minimAccum;
  146. double totalAccum;
  147. const char*name;
  148. };
  149. #endif
  150. #ifdef GenerateResultInNonDymosim
  151. #if !GenerateResultInNonDymosim
  152. /* Since it is not legal to embed = in mex-scripts */
  153. #undef GenerateResultInNonDymosim
  154. #define GenerateResultInNonDymosim 1
  155. #endif
  156. #endif
  157. #if GenerateResultInNonDymosim==1 && (defined(RT) || defined(NRT) || defined(DYMOLA_DSPACE))
  158. #undef GenerateResultInNonDymosim
  159. /* In case we only wanted resulted in normal Simulink */
  160. #endif
  161. #ifdef GenerateResultInNonDymosim
  162. #include "GenerateResultInNonDymosim.c"
  163. #include "atraj.c"
  164. struct DeclarePhase phase={0};
  165. #endif
  166. DYMOLA_STATIC void InitializeDymosimStruct(struct BasicDDymosimStruct*,struct BasicIDymosimStruct*);
  167. DYMOLA_STATIC void declare_(double*,double*,double*,void**,int*);
  168. DYMOLA_STATIC void dymTableClear(void);
  169. DYMOLA_STATIC void delayBuffersClose(void);
  170. DYMOLA_STATIC void SetDymolaJacobianPointers(struct DYNInstanceData*did_, double * QJacobian_,double * QBJacobian_,double * QCJacobian_,double * QDJacobian_,int QJacobianN_,
  171. int QJacobianNU_,int QJacobianNY_,double * QJacobianSparse_,int * QJacobianSparseR_,int * QJacobianSparseC_,int QJacobianNZ_);
  172. DYMOLA_STATIC void SetDymolaEventOptional(struct DYNInstanceData*did_, int val);
  173. DYMOLA_STATIC int GetDymolaHaveEventIterated(struct DYNInstanceData*did_);
  174. double timeToAdd = 0.0;
  175. DYMOLA_STATIC void simblock_(SimStruct *S,
  176. int *idemand_, int *icall_,
  177. double *time, double x_[], double xd_[], double u_[],
  178. double dp_[], int ip_[], Dymola_bool lp_[],
  179. double f_[], double y_[], double w_[], double qz_[],
  180. double duser_[],
  181. int *ierr_) {
  182. int idem;
  183. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  184. int actual_np;
  185. int isResetting=0;
  186. void**cuser_;
  187. int*iuser_;
  188. double*duser2_;
  189. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  190. duser2_=duser_+7+nu;
  191. cuser_=&(ssGetPWork(S)[1]);
  192. iuser_=(int*)(ssGetPWork(S)[0]);
  193. setBasicStruct(duser2_,iuser_);
  194. start:
  195. *ierr_ = 0;
  196. *icall_ = 0;
  197. (*time) += timeToAdd;
  198. if (NotInitialized) {
  199. #if !GenerateResultInNonDymosim
  200. if (cuser_)
  201. declare_(0,0,0,cuser_,ierr_);
  202. #endif
  203. InitializeDymosimStruct(getBasicDDymosimStruct(),getBasicIDymosimStruct());
  204. }
  205. #if defined(AUTO_RESET_AFTER)
  206. if (NotInitialized)
  207. isAutoResetting=0;
  208. if (isAutoResetting) {
  209. if (*time>=autoResetTime && *idemand_==5) {
  210. NotInitialized=1;
  211. isResetting=1;
  212. isAutoResetting=0;
  213. } else return;
  214. }
  215. #endif
  216. if (NotInitialized)
  217. getBasicDDymosimStruct()->mNextTimeEvent = 1.0E37;
  218. if (NotInitialized) {
  219. extern int simulinkInterface_;
  220. #if GenerateResultInNonDymosim
  221. DeclarePhase9(&phase);
  222. DeclarePhase0(cuser_);
  223. #endif
  224. #if LogCalls
  225. DymosimMessage("simBlock initialize");
  226. #endif
  227. SetDymolaEventOptional(0, 1);
  228. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  229. /* See support e-mail 2003-05-26 08:34. */
  230. #if !defined(DYMOLA_DSPACE) && !defined(RT) && !defined(NRT)
  231. {
  232. static int firstTime=1;
  233. if (firstTime) {
  234. char str[1000];
  235. extern int Check5(char*);
  236. #if defined(MatlabR13)
  237. mxArray*mx=mexGetVariable("base", "DymolaRuntimeCode");
  238. #else
  239. mxArray*mx=mexGetArray("DymolaRuntimeCode", "base");
  240. #endif
  241. if (mx && mxGetClassID(mx)==mxCHAR_CLASS) {
  242. mxGetString(mx, str, sizeof(str));
  243. } else {str[0]=0;}
  244. if (!Check5(str)) {
  245. ssSetErrorStatus(S, "No license to run model");
  246. return;
  247. }
  248. firstTime=0;
  249. }
  250. }
  251. #endif
  252. if(ssGetSFcnParamsCount(S) == 0) {
  253. actual_np = 0;
  254. } else {
  255. #if S_FUNCTION_LEVEL > 1
  256. actual_np = mxGetM(ssGetSFcnParam(S,0));
  257. #else
  258. actual_np = mxGetM(ssGetArg(S,0));
  259. #endif
  260. }
  261. if (actual_np != np) {
  262. DymosimMessageInt("Expected number of parameters: ", np);
  263. DymosimMessageInt("Actual number of parameters: ", actual_np);
  264. ssSetErrorStatus(S, "Error encountered in S-function MEX file: wrong size of parameter vector.");
  265. return;
  266. }
  267. {
  268. /*char str[100];*/
  269. /*sprintf(str,"Var %d, order %d, %s inline %d\n",ssIsVariableStepSolver(S),ssIsVariableStepSolver(S),
  270. ssGetSolverName(S),
  271. inlineIntegration_);*/
  272. if (getBasicIDymosimStruct()->mInlineIntegration==1 && getBasicDDymosimStruct()->mDymolaFixedStepSize<=0) {
  273. if (ssIsVariableStepSolver(S) || (strcmp(ssGetSolverName(S),"FixedStepDiscrete")!=0 &&
  274. strcmp(ssGetSolverName(S),"ode1")!=0 &&
  275. strcmp(ssGetSolverName(S),"euler")!=0) &&
  276. strcmp(ssGetSolverName(S),"Euler")!=0) {
  277. ssSetErrorStatus(S, "Error encountered in Dymola block: \n"
  278. "Inline integration requires fixed-step ode1 (Euler) or discrete solver. \n\n"
  279. "To use other solvers, set Advanced.InlineAsDiscrete = true in Dymola.\n"
  280. "This will make the DymolaBlock a discrete block with the sampling time given\n"
  281. "by the Realtime tab in the Dymola Simulation Setup.");
  282. return;
  283. }
  284. }
  285. getBasicIDymosimStruct()->mFindEvent=(getBasicIDymosimStruct()->mInlineIntegration==1 && getBasicDDymosimStruct()->mDymolaFixedStepSize>0)
  286. || (!ssIsVariableStepSolver(S) && ssGetSolverMaxOrder(S)<=1);
  287. /*DymosimMessage(str);*/
  288. }
  289. idem = 0;
  290. getBasicIDymosimStruct()->mSolverHandleEq=1;
  291. InitTime_=*time;
  292. {
  293. int i_;
  294. getBasicDDymosimStruct()->mOrigTimeError=0;
  295. dymTableClear();
  296. delayBuffersClose();
  297. for(i_=0;i_<nw;i_++) w_[i_]=0;
  298. }
  299. {
  300. if (!DirectFeedThrough) {
  301. int i_;
  302. if (isResetting) {
  303. for(i_=0;i_<nu;i_++) duser_[3+i_]=u_[i_];
  304. }
  305. u_=&(duser_[3]);
  306. if (!isResetting) for(i_=0;i_<nu;i_++) u_[i_]=0.0;
  307. }
  308. #if defined(RT)
  309. /* For HILS, initialization is performed from mdlInitializeConditions (to avoid overruns in first time-step) */
  310. /* At this point input values are not yet computed. */
  311. /* Input start values provided in model are used. */
  312. /* NOTE: Offline siulation in Simulink uses actual input values during initialization! */
  313. {
  314. int i_;
  315. for(i_=0;i_<nu;i_++) u_[i_]=u0_[i_];
  316. }
  317. #endif
  318. #if LogCalls
  319. DymosimMessage("simBlock initialize call");
  320. #endif
  321. dsblock_(&idem, icall_, time, x_, xd_, u_, dp_, ip_, lp_,
  322. f_, y_, w_, qz_, duser2_, iuser_, cuser_, ierr_);
  323. #if LogCalls
  324. DymosimMessage("simBlock initialized");
  325. #endif
  326. #if GenerateResultInNonDymosim
  327. DeclarePhase1(&phase);
  328. DeclarePhase3(&phase,*time,ssGetTFinal(S),dp_,x_,xd_,y_,u_,w_);
  329. #endif
  330. }
  331. NotInitialized = 0;
  332. }
  333. if (!DirectFeedThrough && *idemand_==1 && InitTime_==*time)
  334. u_=&(duser_[3]);
  335. dsblock_(idemand_, icall_, time, x_, xd_, u_, dp_, ip_, lp_,
  336. f_, y_, w_, qz_, duser2_, iuser_, cuser_, ierr_);
  337. if (*ierr_==-998) {
  338. *ierr_=0;
  339. if (!isResetting) {
  340. isResetting=1;
  341. NotInitialized=1;
  342. goto start;
  343. }
  344. }
  345. #if defined(AUTO_RESET_AFTER)
  346. if (*ierr_!=0) {
  347. isAutoResetting=1;
  348. autoResetTime=*time+AUTO_RESET_AFTER;
  349. *ierr_=0;
  350. }
  351. #endif
  352. #if defined(Matlab51)
  353. HaveEventIterated_=0;
  354. if(*ierr_ != 0) {
  355. ssSetErrorStatus(S, "Error encountered in S-function MEX file. See Message in Matlab window.");
  356. return;
  357. } else if (isResetting && nx>0) {
  358. HaveEventIterated_=1;
  359. } else if (*idemand_==5 && GetDymolaHaveEventIterated(0)) {
  360. HaveEventIterated_=1;
  361. }
  362. #endif
  363. }
  364. /* Function: mdlInitializeSizes ===============================================
  365. * Abstract:
  366. *
  367. * The sizes information is used by SIMULINK to determine the S-function
  368. * block's characteristics (number of inputs, outputs, states, etc.).
  369. *
  370. */
  371. static void mdlInitializeSizes(SimStruct *S)
  372. {
  373. int nx, nx2, nu, ny, nw, np, nsp, nrel2, nrel, ncons, dae;
  374. #if LogCalls
  375. DymosimMessage("mdlInitializeSizes start");
  376. #endif
  377. #if defined(Matlab51)
  378. ssSetNumSFcnParams(S, 2); /* Number of expected parameters */
  379. /* See support e-mail 2003-05-26 08:34.
  380. if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
  381. ssSetErrorStatus(S, "Wrong number of parameters to S-function MEX file");
  382. return;
  383. }
  384. */
  385. if (sizeof(real_T) != sizeof(double))
  386. DymosimError("Incompatible size of real_T and double");
  387. #else
  388. ssSetNumInputArgs( S, 2); /* number of input arguments */
  389. #endif
  390. GetDimensions2(&nx, &nx2, &nu, &ny, &nw, &np, &nsp, &nrel2, &nrel, &ncons, &dae);
  391. ssSetNumContStates( S, nx); /* number of continuous states */
  392. {
  393. struct BasicDDymosimStruct basicD={0};
  394. struct BasicIDymosimStruct basicI={0};
  395. InitializeDymosimStruct(&basicD,&basicI);
  396. EventInUpdate=!DirectFeedThrough || basicI.mInlineIntegration;
  397. }
  398. #if GenerateResultInNonDymosim
  399. if (1)
  400. #else
  401. if (DirectFeedThrough && EventInUpdate)
  402. #endif
  403. ssSetNumDiscStates(S, 1);
  404. else
  405. ssSetNumDiscStates( S, 0); /* number of discrete states */
  406. /* ssSetNumDiscStates( S, 1);
  407. Use one discrete dummy state to force mdlUpdate to be called even if
  408. there is direct feedthrough. */
  409. #if S_FUNCTION_LEVEL > 1
  410. if (nu>0) {
  411. if (!ssSetNumInputPorts(S, 1))
  412. return;
  413. ssSetInputPortWidth(S, 0, nu); /* Width of input */
  414. ssSetInputPortRequiredContiguous(S, 0, 1);
  415. ssSetInputPortDirectFeedThrough(S, 0, DirectFeedThrough); /* direct feedthrough flag */
  416. }
  417. if (ny>0) {
  418. if (!ssSetNumOutputPorts(S, 1))
  419. return;
  420. ssSetOutputPortWidth(S, 0, ny); /* Width of output */
  421. }
  422. #else
  423. ssSetNumInputs( S, nu); /* number of inputs */
  424. ssSetNumOutputs( S, ny); /* number of outputs */
  425. ssSetDirectFeedThrough(S, DirectFeedThrough); /* direct feedthrough flag */
  426. #endif
  427. ssSetNumSampleTimes( S, 1); /* number of sample times */
  428. ssSetNumRWork( S, nu+nx+ny+nw+2*nrel+7+(sizeof(struct BasicDDymosimStruct)/sizeof(double))); /* number of real work vector elements */
  429. ssSetNumPWork( S, nsp+1); /* number of pointer work vector elements */
  430. #if defined(Matlab51)
  431. ssSetNumModes( S, 0); /* number of mode work vector elements */
  432. ssSetNumNonsampledZCs( S, 2*nrel + 1); /* number of zero crossings.
  433. The last one is used for time events. */
  434. ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); /* dsblock_ does use longjump. */
  435. #if HAVE_GENERATED_JACOBIAN
  436. {
  437. extern int QJacobianDefined_;
  438. ssSetJacobianNzMax(S, (ny+nx)*(nu+nx));
  439. ssSetNumRWork(S, ssGetNumRWork(S)+ssGetJacobianNzMax(S));
  440. if (!QJacobianDefined_) {
  441. ssSetErrorStatus(S, "HAVE_GENERATED_JACOBIAN defined, but no Jacobian in model.");
  442. return;
  443. }
  444. }
  445. #endif
  446. #endif
  447. NotInitialized=1;
  448. timeToAdd = 0.0;
  449. #if !defined(ModelWithFMU)
  450. ParamChange=1; /* Will enforce evaluating BoundParameterSection from mdlDerivatives */
  451. /* Required to get correct values of inputs if no direct feedthrough */
  452. #endif
  453. }
  454. /* Function: mdlInitializeSampleTimes =========================================
  455. * Abstract:
  456. *
  457. * This function is used to specify the sample time(s) for your S-function.
  458. * You must register the same number of sample times as specified in
  459. * ssSetNumSampleTimes.
  460. */
  461. static void mdlInitializeSampleTimes(SimStruct *S)
  462. {
  463. #if defined(Matlab5) || defined(Matlab51)
  464. struct BasicDDymosimStruct basicD={0};
  465. struct BasicIDymosimStruct basicI={0};
  466. InitializeDymosimStruct(&basicD,&basicI);
  467. if (basicI.mInlineIntegration && basicD.mDymolaFixedStepSize>0) {
  468. ssSetSampleTime(S, 0, basicD.mDymolaFixedStepSize);
  469. } else if (basicI.mInlineIntegration && basicD.mDymolaFixedStepSize<0) {
  470. ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
  471. } else {
  472. ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
  473. }
  474. ssSetOffsetTime(S, 0, 0.0);
  475. #else
  476. ssSetSampleTimeEvent(S, 0, 0.0);
  477. ssSetOffsetTimeEvent(S, 0, 0.0);
  478. #endif
  479. }
  480. #if S_FUNCTION_LEVEL > 1
  481. static void mdlOutputs(SimStruct *S, int_T tid);
  482. static void mdlUpdate(SimStruct *S, int_T tid);
  483. #endif
  484. /* Function: mdlInitializeConditions ==========================================
  485. * Abstract:
  486. *
  487. * In this function, you should initialize the continuous and discrete
  488. * states for your S-function block. The initial states are placed
  489. * in the x0 variable. You can also perform any other initialization
  490. * activities that your S-function may require.
  491. */
  492. #if S_FUNCTION_LEVEL > 1
  493. static void mdlInitializeConditions(SimStruct *S)
  494. #define MDL_INITIALIZE_CONDITIONS
  495. #elif defined(Matlab51)
  496. static void mdlInitializeConditions(real_T *x0, SimStruct *S)
  497. #else
  498. static void mdlInitializeConditions(double *x0, SimStruct *S)
  499. #endif
  500. {
  501. double *arg1;
  502. int i, nx, actual_nx;
  503. #if S_FUNCTION_LEVEL > 1
  504. double * x0 = (double*)ssGetContStates(S);
  505. #endif
  506. #if LogCalls
  507. DymosimMessage("mdlInitializeConditions");
  508. #endif
  509. nx = ssGetNumContStates(S);
  510. /* See support e-mail 2003-05-26 08:34. */
  511. if(ssGetSFcnParamsCount(S) == 0) {
  512. arg1 = NULL;
  513. actual_nx = 0;
  514. } else {
  515. #if S_FUNCTION_LEVEL > 1
  516. arg1 = (double *) mxGetPr(ssGetSFcnParam(S,1));
  517. actual_nx = mxGetM(ssGetSFcnParam(S,1));
  518. #else
  519. arg1 = (double *) mxGetPr(ssGetArg(S,1));
  520. actual_nx = mxGetM(ssGetArg(S,1));
  521. #endif
  522. }
  523. if (actual_nx != nx) {
  524. ssSetErrorStatus(S, "Error encountered in S-function MEX file: wrong size of x0 vector.");
  525. return;
  526. }
  527. {
  528. /*char str[100];*/
  529. int nx,nx2,nu,ny,nw,np,nrel,ncons,dae;
  530. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  531. /* Allocate */
  532. ssGetPWork(S)[0] = calloc(sizeof(struct BasicIDymosimStruct)/sizeof(int), sizeof(int));
  533. setBasicStruct(ssGetRWork(S)+nx+ny+nw+2*nrel+nu+7,(int*)(ssGetPWork(S)[0]));
  534. getBasicIDymosimStruct()->mFindEvent=!ssIsVariableStepSolver(S) && ssGetSolverMaxOrder(S)<=1;
  535. /*sprintf(str,"Var %d, order %d, %s inline %d\n",ssIsVariableStepSolver(S),ssIsVariableStepSolver(S),
  536. ssGetSolverName(S),
  537. inlineIntegration_);
  538. DymosimMessage(str);*/
  539. if (getBasicIDymosimStruct()->mInlineIntegration==1 && getBasicDDymosimStruct()->mDymolaFixedStepSize<=0 && !getBasicIDymosimStruct()->mFindEvent) {
  540. ssSetErrorStatus(S, "Error encounted in Dymola block: inline integration requires fixed step size "
  541. "euler or discrete solver.");
  542. return;
  543. }
  544. }
  545. for (i = 0; i < nx; i++)
  546. x0[i] = arg1[i];
  547. NotInitialized = 1;
  548. #if LogCalls
  549. DymosimMessage("mdlInitializeConditions done");
  550. #endif
  551. /* To force initialization before first sample for HILS */
  552. /* (using mdlOutputs as a shortcut to simblock_ for initialization) */
  553. /* Works only with level 2 S-functions */
  554. #if S_FUNCTION_LEVEL > 1
  555. #if defined(RT)
  556. /* Execute three time steps before simulation loop to avoid massive initial overruns */
  557. mdlOutputs(S, 0);
  558. mdlUpdate(S, 0);
  559. timeToAdd = 0.001;
  560. mdlOutputs(S, 0);
  561. mdlUpdate(S, 0);
  562. timeToAdd = 0.002;
  563. mdlOutputs(S, 0);
  564. mdlUpdate(S, 0);
  565. #endif
  566. #endif
  567. }
  568. #define MDL_CHECK_PARAMETERS
  569. #if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
  570. static void mdlCheckParameters(SimStruct *S)
  571. {
  572. #if !defined(ModelWithFMU)
  573. ParamChange=1;
  574. /* dymolaParametersNr_ will be set in mdlDerivatives to avoid problems
  575. with undefined input signals if dsblock_ is called from mdlOutputs */
  576. #endif
  577. #if LogCalls
  578. DymosimMessage("mdlCheckParameters called");
  579. #endif
  580. }
  581. #endif /* MDL_CHECK_PARAMETERS */
  582. #if defined(MDL_RTW) && defined(MATLAB_MEX_FILE)
  583. static void mdlRTW(SimStruct *S)
  584. {
  585. real_T* duser;
  586. int_T dlen;
  587. real_T *params = mxGetPr(ssGetSFcnParam(S, 0));
  588. int_T pLen = mxGetNumberOfElements(ssGetSFcnParam(S, 0));
  589. real_T *x0 = mxGetPr(ssGetSFcnParam(S, 1));
  590. int_T x0Len = mxGetNumberOfElements(ssGetSFcnParam(S, 1));
  591. if (!ssWriteRTWParameters(S,2,
  592. SSWRITE_VALUE_VECT,"params","",params,pLen,
  593. SSWRITE_VALUE_VECT,"x0","",x0,x0Len)) {
  594. return; /* An error occurred which will be reported by Simulink */
  595. }
  596. /* Write RWork vector to model.rtw file */
  597. duser = ssGetRWork(S);
  598. dlen = ssGetNumRWork(S);
  599. if ( !ssWriteRTWParamSettings(S, 1, SSWRITE_VALUE_VECT, "RWork", duser, dlen) ) {
  600. return; /* An error occured, which will be reported by Simulink. */
  601. }
  602. /*printf("Running mdlRTW!\n"); */
  603. }
  604. #endif /* MDL_RTW */
  605. /* Function: mdlOutputs =======================================================
  606. * Abstract:
  607. *
  608. * In this function, you compute the outputs of your S-function
  609. * block. The outputs are placed in the y variable.
  610. */
  611. #if S_FUNCTION_LEVEL > 1
  612. #define MDL_OUTPUTS
  613. static void mdlOutputs(SimStruct *S, int_T tid)
  614. #elif defined(Matlab51)
  615. static void mdlOutputs(real_T *y_, const real_T *x_, const real_T *u_,
  616. SimStruct *S, int_T tid)
  617. #else
  618. static void mdlOutputs(double *y_, double *x_, double *u_, SimStruct *S, int tid)
  619. #endif
  620. {
  621. double time, xd[1], *dp, *duser;
  622. int idemand, icall, ip[1], ierr;
  623. Dymola_bool lp[1];
  624. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  625. int i;
  626. int evaluateIt=1;
  627. double *y, *x, *u;
  628. #if LogCalls
  629. DymosimMessage("mdlOutputs start");
  630. #endif
  631. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  632. #if S_FUNCTION_LEVEL > 1
  633. if (nx>0) x = (double*)ssGetContStates(S); else x=0;
  634. if (nu>0) u = (double*)ssGetInputPortRealSignal(S, 0); else u = 0;
  635. if (ny>0) y = (double*)ssGetOutputPortRealSignal(S, 0); else y = 0;
  636. if (np>0) dp = (double*)mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  637. #else
  638. y = (double *) y_;
  639. x = (double *) x_;
  640. u = (double *) u_;
  641. if (np>0) dp = (double *) mxGetPr(ssGetArg(S,0)); else dp = 0;
  642. #endif
  643. time = ssGetT(S);
  644. duser = ssGetRWork(S);
  645. #if LogCalls
  646. DymosimMessageDouble("mdlOutput: Time = ", time);
  647. #endif
  648. if (!EventInUpdate && ssIsMajorTimeStep(S)) {
  649. idemand = 5; /* Event or time event */
  650. #if LogCalls
  651. DymosimMessage("mdlOutput - Possible Event");
  652. #endif
  653. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  654. &duser[0],
  655. &duser[nx],
  656. &duser[nx+ny],
  657. &duser[nx+ny+nw],
  658. &duser[nx+ny+nw+2*nrel],
  659. &ierr);
  660. evaluateIt=0;
  661. #if LogCalls
  662. DymosimMessageInt("Major time step, event:",HaveEventIterated_);
  663. #endif
  664. #if defined(Matlab51)
  665. if (HaveEventIterated_) ssSetSolverNeedsReset(S);
  666. #endif
  667. }
  668. if (ny > 0 || (EventInUpdate && ssIsMajorTimeStep(S))) {
  669. #if LogCalls
  670. DymosimMessage("mdlOutput: Actual outputs");
  671. #endif
  672. if (1 || ! DirectFeedThrough)
  673. idemand = 5; /* Calculate only outputs. AND EVENTS*/
  674. else
  675. idemand = 2; /* Calculate both output and derivatives. */
  676. if (evaluateIt)
  677. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  678. &duser[0],
  679. &duser[nx],
  680. &duser[nx+ny],
  681. &duser[nx+ny+nw],
  682. &duser[nx+ny+nw+2*nrel],
  683. &ierr);
  684. for (i = 0; i < ny; i++)
  685. y[i] = duser[nx+i];
  686. }
  687. #if LogCalls
  688. DymosimMessage("mdlOutput Done");
  689. #endif
  690. }
  691. /* Function: mdlUpdate ========================================================
  692. * Abstract:
  693. *
  694. * This function is called once for every major integration time step.
  695. * Discrete states are typically updated here, but this function is useful
  696. * for performing any tasks that should only take place once per integration
  697. * step.
  698. */
  699. #if S_FUNCTION_LEVEL > 1
  700. #define MDL_UPDATE
  701. static void mdlUpdate(SimStruct *S, int_T tid)
  702. #elif defined(Matlab51)
  703. static void mdlUpdate(real_T *x_, const real_T *u_, SimStruct *S, int_T tid)
  704. #else
  705. static void mdlUpdate(double *x_, double *u_, SimStruct *S, int tid)
  706. #endif
  707. {
  708. double time, xd[1], *dp, *duser;
  709. int idemand, icall, ip[1], ierr;
  710. Dymola_bool lp[1];
  711. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  712. double *x, *u;
  713. #if LogCalls
  714. DymosimMessage("mdlUpdate start");
  715. #endif
  716. return; /* Events handled during mdlOutputs */
  717. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  718. #if S_FUNCTION_LEVEL > 1
  719. if (nx>0) x = (double*)ssGetContStates(S); else x=0;
  720. if (nu>0) u = (double*)ssGetInputPortRealSignal(S, 0); else u = 0;
  721. if (np>0) dp = (double*)mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  722. #else
  723. x = (double *) x_;
  724. u = (double *) u_;
  725. if (np>0) dp = (double *) mxGetPr(ssGetArg(S,0)); else dp = 0;
  726. #endif
  727. time = ssGetT(S);
  728. duser = ssGetRWork(S);
  729. if (EventInUpdate) {
  730. idemand = 5; /* Event or time event */
  731. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  732. &duser[0],
  733. &duser[nx],
  734. &duser[nx+ny],
  735. &duser[nx+ny+nw],
  736. &duser[nx+ny+nw+2*nrel],
  737. &ierr);
  738. HaveEventIterated_=GetDymolaHaveEventIterated(0);
  739. #if LogCalls
  740. DymosimMessageInt("Update, event:",HaveEventIterated_);
  741. #endif
  742. #if defined(Matlab51)
  743. if (HaveEventIterated_) ssSetSolverNeedsReset(S);
  744. #endif
  745. }
  746. #if LogCalls
  747. DymosimMessageDouble("mdlUpdate: Time = ", time);
  748. #endif
  749. #if GenerateResultInNonDymosim
  750. DeclarePhase7(&phase,time,10,dp,x,&duser[0],&duser[nx],u,&duser[nx+ny]);
  751. #endif
  752. }
  753. /* Function: mdlDerivatives ===================================================
  754. * Abstract:
  755. *
  756. * In this function, you compute the S-function block's derivatives.
  757. * The derivatives are placed in the dx variable.
  758. */
  759. #if S_FUNCTION_LEVEL > 1
  760. #define MDL_DERIVATIVES
  761. static void mdlDerivatives(SimStruct *S)
  762. #elif defined(Matlab51)
  763. static void mdlDerivatives(real_T *dx_, const real_T *x_, const real_T *u_,
  764. SimStruct *S, int_T tid)
  765. #else
  766. static void mdlDerivatives(double *dx_, double *x_, double *u_, SimStruct *S, int tid)
  767. #endif
  768. {
  769. double time, xd[1], *dp, *duser;
  770. int idemand, icall, ip[1], ierr;
  771. Dymola_bool lp[1];
  772. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  773. int i;
  774. double *dx, *x, *u;
  775. #if LogCalls
  776. DymosimMessage("mdlDerivatives start");
  777. #endif
  778. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  779. #if S_FUNCTION_LEVEL > 1
  780. if (nx>0) dx = (double*)ssGetdX(S); else dx = 0;
  781. if (nx>0) x = (double*)ssGetContStates(S); else x=0;
  782. if (nu>0) u = (double*)ssGetInputPortRealSignal(S, 0); else u = 0;
  783. if (np>0) dp = (double*)mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  784. #else
  785. dx = (double *) dx_;
  786. x = (double *) x_;
  787. u = (double *) u_;
  788. if (np>0) dp = (double *) mxGetPr(ssGetArg(S,0)); else dp = 0;
  789. #endif
  790. time = ssGetT(S);
  791. duser = ssGetRWork(S);
  792. #if LogCalls
  793. DymosimMessageDouble("mdlDerivatives: Time = ", time);
  794. #endif
  795. if (ParamChange == 1) {
  796. ParamChange = 0;
  797. getBasicIDymosimStruct()->mParametersNr++;
  798. }
  799. if (nx > 0 /* && ! DirectFeedThrough*/) {
  800. idemand = 2;
  801. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  802. &duser[0],
  803. &duser[nx],
  804. &duser[nx+ny],
  805. &duser[nx+ny+nw],
  806. &duser[nx+ny+nw+2*nrel],
  807. &ierr);
  808. for (i = 0; i < nx; i++)
  809. dx[i] = duser[i];
  810. }
  811. }
  812. /* Function: mdlTerminate =====================================================
  813. * Abstract:
  814. *
  815. * In this function, you should perform any actions that are necessary
  816. * at the termination of a simulation. For example, if memory was allocated
  817. * in mdlInitializeConditions, this is the place to free it.
  818. */
  819. #define MDL_TERMINATE
  820. static void mdlTerminate(SimStruct *S)
  821. {
  822. double time, xd[1], *dp, *duser;
  823. int idemand, icall, ip[1], ierr;
  824. Dymola_bool lp[1];
  825. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  826. double *u, *x;
  827. if (NotInitialized)
  828. return;
  829. #if !defined(DYMOLA_CRAMAS_TERMINATE)
  830. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  831. #if S_FUNCTION_LEVEL > 1
  832. if (nx>0) x = (double*)ssGetContStates(S); else x=0;
  833. if (nu>0) u = (double*)ssGetInputPortRealSignal(S, 0); else u = 0;
  834. #else
  835. if (nx>0) x = (double *) ssGetX(S); else x = 0;
  836. if (nu>0) u = (double *) ssGetU(S); else u = 0;
  837. #endif
  838. time = ssGetT(S);
  839. if (np>0) dp = (double *) mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  840. duser = ssGetRWork(S);
  841. idemand=7;
  842. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  843. &duser[0],
  844. &duser[nx],
  845. &duser[nx+ny],
  846. &duser[nx+ny+nw],
  847. &duser[nx+ny+nw+2*nrel],
  848. &ierr);
  849. #endif
  850. #if GenerateResultInNonDymosim
  851. DeclarePhase9(&phase);
  852. #endif
  853. if (ssGetPWork(S)[0] != NULL) {
  854. free(ssGetPWork(S)[0]);
  855. }
  856. }
  857. #if defined(Matlab51)
  858. /*#if defined(MATLAB_MEX_FILE) || defined(NRT) */
  859. #define MDL_ZERO_CROSSINGS
  860. static void mdlZeroCrossings(SimStruct *S)
  861. {
  862. double time, xd[1], *dp, *duser;
  863. int idemand, icall, ip[1], ierr;
  864. Dymola_bool lp[1];
  865. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  866. int i;
  867. double *u, *x, *zcSignals;
  868. #if LogCalls
  869. DymosimMessage("mdlZeroCrossings start");
  870. #endif
  871. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  872. #if S_FUNCTION_LEVEL > 1
  873. if (nu>0) u = (double *) ssGetInputPortRealSignal(S, 0); else u = 0;
  874. if (np>0) dp = (double*)mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  875. if (nx>0) x = (double *) ssGetContStates(S); else x=0;
  876. #else
  877. if (nu>0) u = (double*)ssGetU(S); else u=0;
  878. if (np>0) dp = (double *) mxGetPr(ssGetArg(S,0)); else dp = 0;
  879. if (nx>0) x = (double*)ssGetX(S); else x=0;
  880. #endif
  881. zcSignals = (double *) ssGetNonsampledZCs(S);
  882. time = ssGetT(S);
  883. duser = ssGetRWork(S);
  884. #if LogCalls
  885. DymosimMessageDouble("mdlZeroCrossings: Time = ", time);
  886. #endif
  887. if (nrel > 0) {
  888. idemand = 4;
  889. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  890. &duser[0],
  891. &duser[nx],
  892. &duser[nx+ny],
  893. &duser[nx+ny+nw],
  894. &duser[nx+ny+nw+2*nrel],
  895. &ierr);
  896. for (i = 0; i < 2*nrel; i++)
  897. zcSignals[i] = duser[nx+ny+nw+i];
  898. }
  899. zcSignals[2*nrel] = getBasicDDymosimStruct()->mNextTimeEvent - time;
  900. #if LogCalls
  901. DymosimMessageDouble(" ZeroCrossing for time event: ", getBasicDDymosimStruct()->mNextTimeEvent - time);
  902. #endif
  903. }
  904. #endif
  905. #if defined(Matlab51) && HAVE_GENERATED_JACOBIAN
  906. #define MDL_JACOBIAN
  907. static void mdlJacobian(SimStruct *S) {
  908. double*QJacobian_;
  909. double*QBJacobian_;
  910. double*QCJacobian_;
  911. double*QDJacobian_;
  912. double time, xd[1], *dp, *duser;
  913. int idemand, icall, ip[1], ierr;
  914. Dymola_bool lp[1];
  915. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  916. int row,col;
  917. int n_row,n_col;
  918. double *u, *x;
  919. #if LogCalls
  920. DymosimMessage("mdlJacobian start");
  921. #endif
  922. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  923. #if S_FUNCTION_LEVEL > 1
  924. if (nu>0) u = (double*)ssGetInputPortRealSignal(S, 0); else u = 0;
  925. if (nx>0) x = (double*)ssGetContStates(S); else x=0;
  926. #else
  927. if (nu>0) u = (double*)ssGetU(S); else u=0;
  928. if (nx>0) x = (double*)ssGetX(S); else x=0;
  929. #endif
  930. time = ssGetT(S);
  931. if (np>0) dp = mxGetPr(ssGetSFcnParam(S,0)); else dp = 0;
  932. duser = ssGetRWork(S);
  933. idemand=2;
  934. QJacobian_=duser+nu+nx+ny+nw+2*nrel+7+(sizeof(struct BasicDDymosimStruct)/sizeof(double));
  935. QBJacobian_=QJacobian_+nx*nx;
  936. QCJacobian_=QBJacobian_+nx*nu;
  937. QDJacobian_=QCJacobian_+nx*ny;
  938. SetDymolaJacobianPointers(0, QJacobian, QBJacobian_, QCJacobian_, QDJacobian_, nx, nu, ny, 0, 0, 0, 0);
  939. #if LogCalls
  940. DymosimMessageDouble("mdlJacobian: Time = ", time);
  941. #endif
  942. simblock_(S, &idemand, &icall, &time, x, xd, u, dp, ip, lp,
  943. &duser[0],
  944. &duser[nx],
  945. &duser[nx+ny],
  946. &duser[nx+ny+nw],
  947. &duser[nx+ny+nw+2*nrel],
  948. &ierr);
  949. n_row = nx+ny;
  950. n_col = nx+nu;
  951. {
  952. double*Pr=(double*)(ssGetJacobianPr(S));
  953. int*Ir=(int*)(ssGetJacobianIr(S));
  954. int*Jc=(int*)(ssGetJacobianJc(S));
  955. int acc=0;
  956. for(col=0;col<n_col;++col) {
  957. Jc[col]=acc;
  958. for(row=0;row<n_row;++row,++acc) {
  959. if (row<nx && col<nx) {
  960. /* A*/
  961. Pr[acc]=QJacobian_[row+col*nx];
  962. } else if (row<nx) {
  963. /* B */
  964. Pr[acc]=QBJacobian_[row+(col-nx)*nx];
  965. } else if (col<nx) {
  966. /* C */
  967. Pr[acc]=QCJacobian_[(row-nx)+col*ny];
  968. } else {
  969. /* D */
  970. Pr[acc]=QDJacobian_[(row-nx)+(col-nx)*ny];
  971. }
  972. Ir[acc]=row;
  973. }
  974. }
  975. Jc[n_col]=acc;
  976. }
  977. QJacobian_=(double*)0;
  978. QBJacobian_=(double*)0;
  979. QCJacobian_=(double*)0;
  980. QDJacobian_=(double*)0;
  981. SetDymolaJacobianPointers(0, QJacobian, QBJacobian_, QCJacobian_, QDJacobian_, nx, nu, ny, 0, 0, 0, 0);
  982. #if LogCalls
  983. DymosimMessage("mdlJacobian done");
  984. #endif
  985. }
  986. #endif
  987. #if defined(Matlab51) && HAVE_GENERATED_JACOBIAN
  988. #define MDL_START /* Change to #undef to remove function */
  989. #if defined(MDL_START)
  990. /* Function: mdlStart =======================================================
  991. * Abstract:
  992. * This function is called once at start of model execution. If you
  993. * have states that should be initialized once, this is the place
  994. * to do it.
  995. */
  996. static void mdlStart(SimStruct *S)
  997. {
  998. int nx, nx2, nu, ny, nw, np, nrel, ncons, dae;
  999. #if LogCalls
  1000. DymosimMessage("mdlStart");
  1001. #endif
  1002. {
  1003. int*Ir=(int*)(ssGetJacobianIr(S));
  1004. int*Jc=(int*)(ssGetJacobianJc(S));
  1005. int acc=0;
  1006. int row,col;
  1007. int n_row,n_col;
  1008. if (!Ir || !Jc) {
  1009. #if LogCalls
  1010. DymosimMessage("mdlStart with no Jacobian");
  1011. #endif
  1012. return;
  1013. }
  1014. GetDimensions(&nx, &nx2, &nu, &ny, &nw, &np, &nrel, &ncons, &dae);
  1015. n_row = nx+ny;
  1016. n_col = nx+nu;
  1017. for(col=0;col<n_col;++col) {
  1018. Jc[col]=acc;
  1019. for(row=0;row<n_row;++row,++acc) {
  1020. Ir[acc]=row;
  1021. }
  1022. }
  1023. Jc[n_col]=acc;
  1024. }
  1025. }
  1026. #endif /* MDL_START */
  1027. #endif
  1028. #ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
  1029. #include "simulink.c" /* MEX-file interface mechanism */
  1030. #else
  1031. #include "cg_sfun.h" /* Code generation registration function */
  1032. #endif