mttWriteSystemInitialisation.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. function mttWriteSystemInitialisation(model)
  2. assignment.numpar = assign_numerical_parameters(model) ;
  3. assignment.sympar = assign_symbolic_parameters(model) ;
  4. assignment.input = assign_input_variables(model) ;
  5. assignment.invar = assign_input_covariables(model) ;
  6. assignment.state = assign_state_variables(model) ;
  7. write_set_numpar(assignment,model) ;
  8. write_set_input(assignment,model) ;
  9. write_set_state(assignment,model) ;
  10. function code = assign_symbolic_parameters(model)
  11. mttNotify('...assigning symbolic parameters') ;
  12. mttWriteNewLine ;
  13. model_name = mttDetachText(model.source,'/') ;
  14. code = [] ;
  15. line = 0 ;
  16. number_of_variables = mttGetFieldLength(model,'sympar') ;
  17. for i = 1:number_of_variables
  18. variable_name = model.sympar{i} ;
  19. default = model.sympar_default{i} ;
  20. if isempty(default)
  21. line = line + 1 ;
  22. code{line} = [model_name,'___',variable_name,' = 1.0 ;'] ;
  23. else
  24. line = line + 1 ;
  25. code{line} = [model_name,'___',variable_name,' = ',default,' ;'] ;
  26. end
  27. end
  28. function code = assign_numerical_parameters(model,root,branch)
  29. is_root_model = nargin==1 ;
  30. if is_root_model
  31. mttNotify('...assigning numerical parameters') ;
  32. mttWriteNewLine ;
  33. root = model ;
  34. current_branch = mttDetachText(model.source,'/') ;
  35. else
  36. current_branch = branch ;
  37. end
  38. code = [] ;
  39. line = 0 ;
  40. number_of_variables = mttGetFieldLength(model,'numpar') ;
  41. for i = 1:number_of_variables
  42. variable_name = model.numpar{i} ;
  43. default = model.numpar_default{i} ;
  44. line = line + 1 ;
  45. if isempty(default)
  46. code{line} = [current_branch,'___',variable_name,' = 1.0 ;'] ;
  47. else
  48. code{line} = [current_branch,'___',variable_name,' = ',default,' ;'] ;
  49. end
  50. end
  51. objects = mttGetFieldNames(model,'obj') ;
  52. for i = 1:length(objects)
  53. object_name = objects{i} ;
  54. object = getfield(model,'obj',object_name) ;
  55. next_branch = [current_branch,'__',object_name] ;
  56. additional_code = [] ;
  57. if ~isempty(object.abg)
  58. additional_code = assign_numerical_parameters(object,root,next_branch) ;
  59. elseif ~isempty(object.cr)
  60. additional_code = assign_numerical_parameters(object.cr,root,next_branch) ;
  61. end
  62. if isempty(code)
  63. code = additional_code ;
  64. else
  65. line = length(code) ;
  66. for j = 1:length(additional_code)
  67. if ~ismember(additional_code{j},code)
  68. line = line + 1 ;
  69. code{line} = additional_code{j} ;
  70. end
  71. end
  72. end
  73. end
  74. function code = assign_input_variables(model,root,branch)
  75. is_root_model = nargin==1 ;
  76. if is_root_model
  77. mttNotify('...assigning input variables') ;
  78. mttWriteNewLine ;
  79. root = model ;
  80. current_branch = mttDetachText(model.source,'/') ;
  81. else
  82. current_branch = branch ;
  83. end
  84. code = [] ;
  85. line = 0 ;
  86. number_of_variables = mttGetFieldLength(model,'input') ;
  87. for i = 1:number_of_variables
  88. variable_name = model.input{i} ;
  89. default = model.input_default{i} ;
  90. line = line + 1 ;
  91. if isempty(default)
  92. code{line} = [current_branch,'___',variable_name,' = 0.0 ;'] ;
  93. else
  94. code{line} = [current_branch,'___',variable_name,' = ',default,' ;'] ;
  95. end
  96. end
  97. objects = mttGetFieldNames(model,'obj') ;
  98. for i = 1:length(objects)
  99. object_name = objects{i} ;
  100. object = getfield(model,'obj',object_name) ;
  101. next_branch = [current_branch,'__',object_name] ;
  102. additional_code = [] ;
  103. if ~isempty(object.abg)
  104. additional_code = assign_input_variables(object,root,next_branch) ;
  105. elseif ~isempty(object.cr)
  106. additional_code = assign_input_variables(object.cr,root,next_branch) ;
  107. end
  108. if isempty(code)
  109. code = additional_code ;
  110. else
  111. line = length(code) ;
  112. for j = 1:length(additional_code)
  113. if ~ismember(additional_code{j},code)
  114. line = line + 1 ;
  115. code{line} = additional_code{j} ;
  116. end
  117. end
  118. end
  119. end
  120. function code = assign_input_covariables(model,root,branch,env)
  121. is_root_model = nargin==1 ;
  122. if is_root_model
  123. mttNotify('...assigning input covariables') ;
  124. mttWriteNewLine ;
  125. root = model ;
  126. current_branch = mttDetachText(model.source,'/') ;
  127. env = model.env ;
  128. else
  129. current_branch = branch ;
  130. end
  131. code = [] ;
  132. line = 0 ;
  133. objects = mttGetFieldNames(model,'obj') ;
  134. for i = 1:length(objects)
  135. object_name = objects{i} ;
  136. object = getfield(model,'obj',object_name) ;
  137. next_branch = [current_branch,'__',object_name] ;
  138. switch object.class
  139. case {'SS','Se','Sf','De','Df'}
  140. inbond_number = object.interface.in ;
  141. outbond_number = object.interface.out ;
  142. inbond = model.bond(inbond_number) ;
  143. outbond = model.bond(outbond_number) ;
  144. if ~isempty(inbond)
  145. covariables = mttGetCovariables(env,inbond.domain,inbond.domain_item) ;
  146. covar = [] ;
  147. if ~inbond.effort
  148. if ~strcmp(object.class,'Df')
  149. covar = covariables.effort ;
  150. end
  151. elseif inbond.flow
  152. if ~strcmp(object.class,'De')
  153. covar = covariables.flow ;
  154. end
  155. end
  156. for j = 1:length(covar)
  157. line = line + 1 ;
  158. covariable = strrep(covar{j},'.','__') ;
  159. code{line} = [current_branch,'__',object_name,'.',covariable,' = 0.0 ;'] ;
  160. end
  161. end
  162. if ~isempty(outbond)
  163. covariables = mttGetCovariables(env,outbond.domain,outbond.domain_item) ;
  164. if outbond.effort
  165. covar = covariables.effort ;
  166. elseif ~outbond.flow
  167. covar = covariables.flow ;
  168. end
  169. for j = 1:length(covar)
  170. line = line + 1 ;
  171. covariable = strrep(covar{j},'.','__') ;
  172. code{line} = [current_branch,'__',object_name,'.',covariable,' = 0.0 ;'] ;
  173. end
  174. end
  175. end
  176. additional_code = [] ;
  177. if ~isempty(object.abg)
  178. additional_code = assign_input_covariables(object,root,next_branch,env) ;
  179. end
  180. if isempty(code)
  181. code = additional_code ;
  182. else
  183. line = length(code) ;
  184. for j = 1:length(additional_code)
  185. if ~ismember(additional_code{j},code)
  186. line = line + 1 ;
  187. code{line} = additional_code{j} ;
  188. end
  189. end
  190. end
  191. end
  192. function code = assign_state_variables(model,root,branch,env)
  193. is_root_model = nargin==1 ;
  194. if is_root_model
  195. mttNotify('...assigning state variables') ;
  196. mttWriteNewLine ;
  197. root = model ;
  198. current_branch = mttDetachText(model.source,'/') ;
  199. env = model.env ;
  200. else
  201. current_branch = branch ;
  202. end
  203. code = [] ;
  204. line = 0 ;
  205. if isfield(model,'state')
  206. assigned_states = [] ;
  207. counter = 0 ;
  208. port_names = mttGetFieldNames(model.interface,'port') ;
  209. for j = 1:length(port_names)
  210. port_name = port_names{j} ;
  211. port = getfield(model,'interface','port',port_name) ;
  212. if ~isempty(port.assign)
  213. assignment = port.assign ;
  214. if port.was_generic & ~isempty(port.domain)
  215. covariables = mttGetCovariables(env,port.domain,port.domain_item) ;
  216. if port.is_effort_state
  217. covar = covariables.effort ;
  218. default = covariables.effort_default ;
  219. else
  220. covar = covariables.flow ;
  221. default = covariables.flow_default ;
  222. end
  223. block_size = length(covar) ;
  224. for k = 1:length(assignment.state)
  225. counter = counter + 1 ;
  226. assigned_states{counter} = assignment.state{k} ;
  227. for var = 1:block_size
  228. line = line + 1 ;
  229. if isempty(default{var})
  230. code{line} = [current_branch,'___',assignment.state{k},'___',covar{var},'.state = 0.0 ;'] ;
  231. else
  232. code{line} = [current_branch,'___',assignment.state{k},'___',covar{var},'.state = ',default{var},' ;'] ;
  233. end
  234. end
  235. end
  236. end
  237. end
  238. end
  239. number_of_variables = mttGetFieldLength(model,'state') ;
  240. for i = 1:number_of_variables
  241. variable_name = model.state{i} ;
  242. default = model.state_default{i} ;
  243. if ~ismember(variable_name,assigned_states)
  244. line = line + 1 ;
  245. if isempty(default)
  246. code{line} = [current_branch,'___',variable_name,'.state = 0.0 ;'] ;
  247. else
  248. code{line} = [current_branch,'___',variable_name,'.state = ',default,' ;'] ;
  249. end
  250. end
  251. end
  252. end
  253. objects = mttGetFieldNames(model,'obj') ;
  254. for i = 1:length(objects)
  255. object_name = objects{i} ;
  256. object = getfield(model,'obj',object_name) ;
  257. next_branch = [current_branch,'__',object_name] ;
  258. additional_code = [] ;
  259. if ~isempty(object.abg)
  260. additional_code = assign_state_variables(object,root,next_branch,env) ;
  261. elseif ~isempty(object.cr)
  262. additional_code = assign_state_variables(object.cr,root,next_branch,env) ;
  263. end
  264. if isempty(code)
  265. code = additional_code ;
  266. else
  267. line = length(code) ;
  268. for j = 1:length(additional_code)
  269. if ~ismember(additional_code{j},code)
  270. line = line + 1 ;
  271. code{line} = additional_code{j} ;
  272. end
  273. end
  274. end
  275. end
  276. function write_set_numpar(assignment,model)
  277. filename = [model.source,'_include_set_numpar.h'] ;
  278. fid = fopen(filename,'w') ;
  279. mttNotify(['...creating ',filename]) ;
  280. mttWriteNewLine ;
  281. fprintf(fid,['// Numerical Parameters for Ordinary Differential Equations\n']) ;
  282. fprintf(fid,'\n') ;
  283. fprintf(fid,['// file: ',filename,'\n']) ;
  284. fprintf(fid,['// written by MTT on ',datestr(now),'\n']) ;
  285. model_name = mttDetachText(model.source,'/') ;
  286. write_assigned_list(fid,[],assignment.numpar,model_name) ;
  287. fclose(fid) ;
  288. function write_set_input(assignment,model)
  289. filename = [model.source,'_include_set_input.h'] ;
  290. fid = fopen(filename,'w') ;
  291. mttNotify(['...creating ',filename]) ;
  292. mttWriteNewLine ;
  293. fprintf(fid,['// Default Inputs for Ordinary Differential Equations\n']) ;
  294. fprintf(fid,'\n') ;
  295. fprintf(fid,['// file: ',filename,'\n']) ;
  296. fprintf(fid,['// written by MTT on ',datestr(now),'\n']) ;
  297. model_name = mttDetachText(model.source,'/') ;
  298. write_assigned_list(fid,'symbolic_parameters',assignment.sympar,model_name) ;
  299. write_assigned_list(fid,'input_variables',assignment.input,model_name) ;
  300. write_assigned_list(fid,'input_covariables',assignment.invar,model_name) ;
  301. fclose(fid) ;
  302. function write_set_state(assignment,model)
  303. filename = [model.source,'_include_set_state.h'] ;
  304. fid = fopen(filename,'w') ;
  305. mttNotify(['...creating ',filename]) ;
  306. mttWriteNewLine ;
  307. fprintf(fid,['// Initial states for Ordinary Differential Equations\n']) ;
  308. fprintf(fid,'\n') ;
  309. fprintf(fid,['// file: ',filename,'\n']) ;
  310. fprintf(fid,['// written by MTT on ',datestr(now),'\n']) ;
  311. model_name = mttDetachText(model.source,'/') ;
  312. write_assigned_list(fid,[],assignment.state,model_name) ;
  313. fclose(fid) ;
  314. function write_assigned_list(fid,var_type,var_list,model_name)
  315. if ~isempty(var_list)
  316. if ~isempty(var_type)
  317. fprintf(fid,'\n\n') ;
  318. fprintf(fid,['// ',var_type]) ;
  319. end
  320. fprintf(fid,'\n') ;
  321. var_list = sort(var_list) ;
  322. width = 0 ;
  323. for i = 1:length(var_list)
  324. [left,right] = mttCutText(var_list{i},'=') ;
  325. width = max(width,length(left)) ;
  326. end
  327. tab = char(32*ones(1,3)) ;
  328. for i = 1:length(var_list)
  329. [left,right] = mttCutText(var_list{i},'=') ;
  330. nominal_width = width ;
  331. actual_width = length(left) ;
  332. gap = nominal_width - actual_width ;
  333. whitespace = char(32*ones(1,gap)) ;
  334. formatted_assignment = [tab,left,whitespace,' = ',right,'\n'] ;
  335. fprintf(fid,formatted_assignment) ;
  336. end
  337. end