Home > functions > internal > dsCombineModels.m

dsCombineModels

PURPOSE ^

COMBINEMODELS - combine subfields in two DynaSim model structures

SYNOPSIS ^

function model = dsCombineModels(model1,model2, varargin)

DESCRIPTION ^

COMBINEMODELS - combine subfields in two DynaSim model structures

 Usage:
   model=dsCombineModels(model1,model2)

 Inputs: two models to be combined

 Output: DynaSim model with fields combined from both models

 See also: dsCheckModel, dsGenerateModel

 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function model = dsCombineModels(model1,model2, varargin)
0002 %COMBINEMODELS - combine subfields in two DynaSim model structures
0003 %
0004 % Usage:
0005 %   model=dsCombineModels(model1,model2)
0006 %
0007 % Inputs: two models to be combined
0008 %
0009 % Output: DynaSim model with fields combined from both models
0010 %
0011 % See also: dsCheckModel, dsGenerateModel
0012 %
0013 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0014 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0015 
0016 %% auto_gen_test_data_flag argin
0017 options = dsCheckOptions(varargin,{'auto_gen_test_data_flag',0,{0,1}},false);
0018 if options.auto_gen_test_data_flag
0019   varargs = varargin;
0020   varargs{find(strcmp(varargs, 'auto_gen_test_data_flag'))+1} = 0;
0021   varargs(end+1:end+2) = {'unit_test_flag',1};
0022   argin = [{model1}, {model2}, varargs]; % specific to this function
0023 end
0024 
0025 % standardize model structures
0026 model1=dsCheckModel(model1, varargin{:});
0027 model2=dsCheckModel(model2, varargin{:});
0028 
0029 % combine fields from sub-structures
0030 model.parameters=concatenate_structures(model1.parameters,model2.parameters);
0031 model.fixed_variables=concatenate_structures(model1.fixed_variables,model2.fixed_variables);
0032 model.functions=concatenate_structures(model1.functions,model2.functions);
0033 model.monitors=concatenate_structures(model1.monitors,model2.monitors);
0034 model.ODEs=concatenate_structures(model1.ODEs,model2.ODEs);
0035 model.ICs=concatenate_structures(model1.ICs,model2.ICs);
0036 
0037 % concatenate cell and structure arrays
0038 model.state_variables=cat(2,model1.state_variables,model2.state_variables);
0039 model.conditionals=cat(2,model1.conditionals,model2.conditionals);
0040 model.linkers=cat(2,model1.linkers,model2.linkers);
0041 model.comments=cat(2,model1.comments,model2.comments);
0042 
0043 % combine .specification from model1 and model2 (this is necessary for
0044 % building a new model from two indepedent models to which connection
0045 % mechanisms are added...)
0046 % TODO: call something like old combine_models() function from old DynaSim
0047 % ...
0048 
0049 % standardize resulting model
0050 % model=dsCheckModel(model);
0051   % NOTE: if this call to dsCheckModel() is uncommented-out, the changes noted
0052   % in dsCheckModel() should also be made...
0053 
0054 % reorder fields according to first input
0055 model=orderfields(model,model1);
0056 
0057 %% auto_gen_test_data_flag argout
0058 if options.auto_gen_test_data_flag
0059   argout = {model}; % specific to this function
0060   
0061   dsUnitSaveAutoGenTestData(argin, argout);
0062 end
0063 
0064 end
0065 
0066 % SUBFUNCTIONS
0067 function out=concatenate_structures(a,b)
0068 if isempty(a) && ~isempty(b)
0069   out=b;
0070 elseif ~isempty(a) && isempty(b)
0071   out=a;
0072 elseif isempty(a) && isempty(b)
0073   out=a;
0074 elseif ~isempty(a) && ~isempty(b)
0075   out=catstruct(a,b);
0076 end
0077 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005