mttCreateUnifiedModel.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. function model = mttCreateUnifiedModel(model,bondgraph,specification)
  2. model.source = mttCutText(specification.specification,'_abg.txt') ;
  3. model.bondgraph = mttCutText(bondgraph.bondgraph,'_abg.fig') ;
  4. model.obj = bondgraph.obj ;
  5. model.bond = bondgraph.bond ;
  6. object_names = mttGetFieldNames(model,'obj') ;
  7. specified_object_names = mttGetFieldNames(specification,'obj') ;
  8. for n = 1:length(object_names)
  9. object_name = object_names{n} ;
  10. object = getfield(model,'obj',object_name) ;
  11. if ~mttIsPassiveClass(object.class)
  12. mttAssert(ismember(object_name,specified_object_names),...
  13. ['Object name "',object_name,'" does not appear in specification']) ;
  14. end
  15. end
  16. for n = 1:length(specified_object_names)
  17. specified_object_name = specified_object_names{n} ;
  18. mttAssert(ismember(specified_object_name,object_names),...
  19. ['Specified object name "',specified_object_name,...
  20. '" does not appear in bond graph']) ;
  21. specified_object = getfield(specification,'obj',specified_object_name) ;
  22. object = getfield(model,'obj',specified_object_name) ;
  23. if strcmp(specified_object.name.class,object.class)
  24. mttAssert(~mttIsFoundationClass(object.class),...
  25. 'Cannot redefine MTT foundation class') ;
  26. is_abg = 1 ;
  27. else
  28. mttAssert(~mttIsPassiveClass(object.class),...
  29. 'Cannot modify MTT junctions or ports') ;
  30. is_abg = 0 ;
  31. end
  32. object.is_abg = is_abg ;
  33. object.name = specified_object.name ;
  34. object.parameter = specified_object.parameter ;
  35. model = setfield(model,'obj',specified_object_name,object) ;
  36. end
  37. model.sympar = specification.sympar ;
  38. model.numpar = specification.numpar ;
  39. model.input = specification.input ;
  40. model.sympar_default = specification.sympar_default ;
  41. model.numpar_default = specification.numpar_default ;
  42. model.input_default = specification.input_default ;
  43. for n = 1:length(object_names)
  44. object_name = object_names{n} ;
  45. object = getfield(model,'obj',object_name) ;
  46. if mttIsPortClass(object.class)
  47. mttAssert(mttGetFieldLength(object,'interface')==1,...
  48. ['Port components must have one and only one interface']) ;
  49. switch object.class
  50. case {'Se','Sf'},
  51. mttAssert(~isempty(object.interface.out),...
  52. 'Each source port must have an outward bond') ;
  53. case {'De','Df'},
  54. mttAssert(~isempty(object.interface.in),...
  55. 'Each detector port must have an inward bond') ;
  56. end
  57. end
  58. if ~ismember(object_name,specified_object_names)
  59. object.name.item = [] ;
  60. object.name.class = object.class ;
  61. object.name.path = [] ;
  62. object.parameter = [] ;
  63. object.is_abg = 1 ;
  64. end
  65. [rubbish,working_directory] = mttDetachText(model.source,'/') ;
  66. local_directory = object.name.path ;
  67. directory = mttLocateDirectory(working_directory,local_directory) ;
  68. source = [directory,'/',object.name.class] ;
  69. current_leaf = model.leaf ;
  70. if object.is_abg
  71. if mttIsPassiveClass(object.name.class)
  72. object.abg = [] ;
  73. else
  74. current_branch = model.branch ;
  75. mttAssert(~ismember(source,current_branch),...
  76. ['Recursive definition of "',source,'"']) ;
  77. if isempty(model.abgs)
  78. existing_abg = [] ;
  79. else
  80. existing_abg = strmatch(source,model.abgs,'exact') ;
  81. end
  82. if isempty(existing_abg)
  83. if isempty(current_branch)
  84. next_branch = {source} ;
  85. next_leaf = object_name ;
  86. else
  87. next_branch = [current_branch,{source}] ;
  88. next_leaf = [current_leaf,'/',object_name] ;
  89. end
  90. model.branch = next_branch ;
  91. model.leaf = next_leaf ;
  92. class_model = mttCreateAcausalBondgraph(source,model) ;
  93. model.leaf = current_leaf ;
  94. model.branch = current_branch ;
  95. next_abg = 1 + mttGetFieldLength(model,'abg') ;
  96. for i = 1:mttGetFieldLength(class_model,'abg')
  97. model.abg(next_abg) = class_model.abg(i) ;
  98. model.abgs = class_model.abgs ;
  99. next_abg = next_abg + 1 ;
  100. end
  101. class_model = mttDeleteField(class_model,'abg') ;
  102. model.crs = class_model.crs ;
  103. class_model = mttDeleteField(class_model,'crs') ;
  104. model.cr_usage = class_model.cr_usage ;
  105. class_model = mttDeleteField(class_model,'cr_usage') ;
  106. model.abg(next_abg) = class_model ;
  107. model = mttAppend(model,'abgs',{source}) ;
  108. index = length(model.abgs) ;
  109. mttNotify([' ...made "#',num2str(index),':',object.name.class,'" definition']) ;
  110. mttWriteNewLine ;
  111. end
  112. object.abg = strmatch(source,model.abgs,'exact') ;
  113. end
  114. object.cr = [] ;
  115. object.cr_item = [] ;
  116. else
  117. if isempty(model.crs)
  118. existing_cr = [] ;
  119. else
  120. existing_cr = strmatch(source,model.crs,'exact') ;
  121. end
  122. if isempty(current_leaf)
  123. cr_user = object_name ;
  124. else
  125. cr_user = [current_leaf,'/',object_name] ;
  126. end
  127. if isempty(existing_cr)
  128. model = mttAppend(model,'crs',{source}) ;
  129. N = length(model.crs) ;
  130. model.cr_usage(N).obj{1} = cr_user ;
  131. else
  132. N = 1+length(model.cr_usage(existing_cr).obj) ;
  133. model.cr_usage(existing_cr).obj{N} = cr_user ;
  134. end
  135. object.abg = [] ;
  136. object.cr = strmatch(source,model.crs,'exact') ;
  137. if isempty(object.name.item)
  138. object.cr_item = mttDetachText(model.crs{object.cr},'/') ;
  139. else
  140. object.cr_item = object.name.item ;
  141. end
  142. end
  143. object = mttDeleteField(object,'name') ;
  144. object = mttDeleteField(object,'is_abg') ;
  145. model = setfield(model,'obj',object_name,object) ;
  146. end