function model = mttCreateElementaryEquations(model,sorter_on) model.representation = 'ese' ; [model,ese,namelist] = specify_equations(model) ; if sorter_on [sse,counter] = sort_equations(ese,model,namelist) ; else sse = ese ; counter = 0 ; end model.equation = sse ; model.sorted_equations = counter ; model.namelist = namelist ; function [model,ese,namelist] = specify_equations(model,branch,root_namelist) is_root_model = (nargin==1) ; if is_root_model mttNotify('...specifying equations') ; mttWriteNewLine ; branch = mttDetachText(model.source,'/') ; name_counter = 1 ; namelist(name_counter).var = '0' ; namelist(name_counter).domain = [] ; namelist(name_counter).domain_item = [] ; else namelist = root_namelist ; name_counter = length(namelist) ; end mttNotify([' ...processing ',branch]) ; mttWriteNewLine ; objects = mttGetFieldNames(model,'obj') ; line = 0 ; for n = 1:length(objects) object_name = objects{n} ; object = getfield(model,'obj',object_name) ; here = [branch,':',object_name] ; switch object.class case {'SS','Se','Sf','De','Df'}, for j = 1:mttGetFieldLength(object,'interface') flow_equation = [] ; effort_equation = [] ; terminal = [branch,'__',object_name] ; inbond = object.interface(j).in ; outbond = object.interface(j).out ; if ~isempty(inbond) bond = model.bond(inbond) ; [specified_domain,specified_domain_item] = mttGetBondDomain(model,inbond) ; new_name = 0 ; line = line + 1 ; if bond.flow if strcmp(object.class,'De') ese(line) = specify_ese(branch,[2*inbond],branch,{1},[]) ; else ese(line) = specify_ese(branch,[2*inbond],branch,{name_counter+1},[]) ; new_name = 1 ; end else ese(line) = specify_ese(branch,{name_counter+1},branch,[2*inbond],[]) ; new_name = 1 ; end if new_name name_counter = name_counter + 1 ; namelist(name_counter).var = [terminal,'.flow'] ; namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; end new_name = 0 ; line = line + 1 ; if bond.effort ese(line) = specify_ese(branch,{name_counter+1},branch,[2*inbond-1],[]) ; new_name = 1 ; else if strcmp(object.class,'Df') ese(line) = specify_ese(branch,[2*inbond-1],branch,{1},[]) ; else ese(line) = specify_ese(branch,[2*inbond-1],branch,{name_counter+1},[]) ; new_name = 1 ; end end if new_name name_counter = name_counter + 1 ; namelist(name_counter).var = [terminal,'.effort'] ; namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; end end if ~isempty(outbond) bond = model.bond(outbond) ; [specified_domain,specified_domain_item] = mttGetBondDomain(model,outbond) ; new_name = 0 ; if bond.flow if ~strcmp(object.class,'Se') line = line + 1 ; ese(line) = specify_ese(branch,{name_counter+1},branch,[2*outbond],[]) ; new_name = 1 ; end else line = line + 1 ; ese(line) = specify_ese(branch,[2*outbond],branch,{name_counter+1},[]) ; new_name = 1 ; end if new_name name_counter = name_counter + 1 ; namelist(name_counter).var = [terminal,'.flow'] ; namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; end new_name = 0 ; if bond.effort line = line + 1 ; ese(line) = specify_ese(branch,[2*outbond-1],branch,{name_counter+1},[]) ; new_name = 1 ; else if ~strcmp(object.class,'Sf') line = line + 1 ; ese(line) = specify_ese(branch,{name_counter+1},branch,[2*outbond-1],[]) ; new_name = 1 ; end end if new_name name_counter = name_counter + 1 ; namelist(name_counter).var = [terminal,'.effort'] ; namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; end end end case '0', imposed_effort = [] ; resultant_flow = [] ; for i = 1:mttGetFieldLength(object,'interface') inbond = object.interface(i).in ; outbond = object.interface(i).out ; if isempty(inbond) bond_number(i) = outbond ; orientation(i) = 0 ; else bond_number(i) = inbond ; orientation(i) = 1 ; end [effort,flow] = mttGetBondCausality(model,bond_number(i)) ; if effort==orientation(i) imposed_effort = 2*bond_number(i)-1 ; end if flow==orientation(i) resultant_flow = 2*bond_number(i) ; resultant_orientation = orientation(i) ; end end for i = 1:mttGetFieldLength(object,'interface') effort = 2*bond_number(i)-1 ; if effort~=imposed_effort derived_effort = effort ; line = line + 1 ; ese(line) = specify_ese(branch,derived_effort,branch,imposed_effort,[]) ; end end flow_summation = [] ; for i = 1:mttGetFieldLength(object,'interface') flow = 2*bond_number(i) ; if flow~=resultant_flow impinging_flow = flow ; if orientation(i) if resultant_orientation flow_summation = [ flow_summation, -impinging_flow ] ; else flow_summation = [ flow_summation, impinging_flow ] ; end else if resultant_orientation flow_summation = [ flow_summation, impinging_flow ] ; else flow_summation = [ flow_summation, -impinging_flow ] ; end end end end line = line + 1 ; ese(line) = specify_ese(branch,resultant_flow,branch,flow_summation,[]) ; case '1', imposed_flow = [] ; resultant_effort = [] ; for i = 1:mttGetFieldLength(object,'interface') inbond = object.interface(i).in ; outbond = object.interface(i).out ; if isempty(inbond) bond_number(i) = outbond ; orientation(i) = 0 ; else bond_number(i) = inbond ; orientation(i) = 1 ; end [effort,flow] = mttGetBondCausality(model,bond_number(i)) ; if flow~=orientation(i) imposed_flow = 2*bond_number(i) ; end if effort~=orientation(i) resultant_effort = 2*bond_number(i)-1 ; resultant_orientation = orientation(i) ; end end for i = 1:mttGetFieldLength(object,'interface') flow = 2*bond_number(i) ; if flow~=imposed_flow derived_flow = flow ; line = line + 1 ; ese(line) = specify_ese(branch,derived_flow,branch,imposed_flow,[]) ; end end effort_summation = [] ; for i = 1:mttGetFieldLength(object,'interface') effort = 2*bond_number(i)-1 ; if effort~=resultant_effort impinging_effort = effort ; if orientation(i) if resultant_orientation effort_summation = [ effort_summation, -impinging_effort ] ; else effort_summation = [ effort_summation, impinging_effort ] ; end else if resultant_orientation effort_summation = [ effort_summation, impinging_effort ] ; else effort_summation = [ effort_summation, -impinging_effort ] ; end end end end line = line + 1 ; ese(line) = specify_ese(branch,resultant_effort,branch,effort_summation,[]) ; otherwise, if ~isempty(object.cr) interface = object.cr.interface ; port_names = mttGetFieldNames(interface,'port') ; number_of_ports = length(port_names) ; link_counter = 0 ; for i = 1:number_of_ports port_name = port_names{i} ; port = getfield(interface,'port',port_name) ; terminal = [branch,'__',object_name,'___',port_name] ; inbond = port.in ; outbond = port.out ; if ~isempty(inbond) bond = model.bond(inbond) ; [specified_domain,specified_domain_item] = mttGetBondDomain(model,inbond) ; name_counter = name_counter + 1 ; if port.is_flow_state namelist(name_counter).var = [terminal,'.flow_state'] ; else namelist(name_counter).var = [terminal,'.flow'] ; end namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; line = line + 1 ; link_counter = link_counter + 1 ; if bond.flow ese(line) = specify_ese(branch,[2*inbond],branch,{name_counter},[]) ; link(link_counter) = create_link(0,0,1,port_name,port.is_flow_state) ; else ese(line) = specify_ese(branch,{name_counter},branch,[2*inbond],[]) ; link(link_counter) = create_link(1,0,1,port_name,port.is_flow_state) ; end name_counter = name_counter + 1 ; if port.is_effort_state namelist(name_counter).var = [terminal,'.effort_state'] ; else namelist(name_counter).var = [terminal,'.effort'] ; end namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; line = line + 1 ; link_counter = link_counter + 1 ; if bond.effort ese(line) = specify_ese(branch,{name_counter},branch,[2*inbond-1],[]) ; link(link_counter) = create_link(1,1,0,port_name,port.is_effort_state) ; else ese(line) = specify_ese(branch,[2*inbond-1],branch,{name_counter},[]) ; link(link_counter) = create_link(0,1,0,port_name,port.is_effort_state) ; end end if ~isempty(outbond) bond = model.bond(outbond) ; [specified_domain,specified_domain_item] = mttGetBondDomain(model,outbond) ; name_counter = name_counter + 1 ; if port.is_flow_state namelist(name_counter).var = [terminal,'.flow_state'] ; else namelist(name_counter).var = [terminal,'.flow'] ; end namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; line = line + 1 ; link_counter = link_counter + 1 ; if bond.flow ese(line) = specify_ese(branch,{name_counter},branch,[2*outbond],[]) ; link(link_counter) = create_link(1,0,1,port_name,port.is_flow_state) ; else ese(line) = specify_ese(branch,[2*outbond],branch,{name_counter},[]) ; link(link_counter) = create_link(0,0,1,port_name,port.is_flow_state) ; end name_counter = name_counter + 1 ; if port.is_effort_state namelist(name_counter).var = [terminal,'.effort_state'] ; else namelist(name_counter).var = [terminal,'.effort'] ; end namelist(name_counter).domain = specified_domain ; namelist(name_counter).domain_item = specified_domain_item ; line = line + 1 ; link_counter = link_counter + 1 ; if bond.effort ese(line) = specify_ese(branch,[2*outbond-1],branch,{name_counter},[]) ; link(link_counter) = create_link(0,1,0,port_name,port.is_effort_state) ; else ese(line) = specify_ese(branch,{name_counter},branch,[2*outbond-1],[]) ; link(link_counter) = create_link(1,1,0,port_name,port.is_effort_state) ; end end end input_mask = zeros(number_of_ports,1) ; output_mask = zeros(number_of_ports,1) ; number_of_links = link_counter ; linked = zeros(number_of_links,1) ; operators = object.cr.operator ; number_of_operators = length(operators) ; object.cr.used_operator = zeros(1,number_of_operators) ; op_counter = 0 ; matching = 1 ; while matching op_counter = op_counter + 1 ; operator = operators(op_counter) ; full_operator_name = [branch,'__',object_name,'___',operator.name] ; number_of_op_links = length(operator.link) ; op_linked = zeros(number_of_op_links,1) ; for j = 1:number_of_op_links k = 0 ; comparing = 1 ; while comparing k = k + 1 ; if compare_links(link(k),operator.link(j)) op_linked(j) = k ; comparing = 0 ; end comparing = comparing & (k