Ignore:
Timestamp:
11/11/21 09:33:47 (3 years ago)
Author:
vverjans
Message:

CHG: making stochasticforcing.m more generic, extending stochasticforcing capability to DefaultCalving

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/classes/stochasticforcing.m

    r26553 r26604  
    88                isstochasticforcing  = 0;
    99                fields               = NaN;
    10                 dimensions           = NaN;
     10                defaultdimension     = 0;
     11      default_id           = NaN;
    1112                covariance           = NaN;
    1213                randomflag           = 1;
     
    3435
    3536                        num_fields = numel(self.fields);
    36                         size_tot   = sum(self.dimensions);
     37                       
     38                        %Check that covariance matrix is positive definite
     39                        try
     40                                chol(self.covariance);
     41                        catch
     42                                error('md.stochasticforcing.covariance is not positive definite');
     43                        end
     44
     45                        %Check that all fields agree with the corresponding md class and if any field needs the default params   
     46         checkdefaults = false; %need to check defaults only if one of the field does not have its own dimensionality
     47                        for field=self.fields
     48            %Checking agreement of classes
     49            if(contains(field,'SMB'))
     50               if~(isequal(class(md.smb),char(field)))
     51                  error('md.smb does not agree with stochasticforcing field %s', char(field));
     52               end
     53            end
     54            if(contains(field,'frontalforcings'))
     55               if~(isequal(class(md.frontalforcings),char(field)))
     56                  error('md.frontalforcings does not agree with stochasticforcing field %s', char(field));
     57               end
     58            end
     59            %Checking for specific dimensions
     60            if ~(strcmp(field,'SMBautoregression') || strcmp(field,'FrontalForcingsRignotAutoregression'))
     61               checkdefaults = true; %field with non-specific dimensionality
     62            end
     63         end
     64
     65                        %Retrieve sum of all the field dimensionalities
     66                        size_tot   = self.defaultdimension*num_fields;
     67                        indSMBar = -1; %about to check for index of SMBautoregression
     68                        indTFar  = -1; %about to check for index of FrontalForcingsRignotAutoregression
     69                        if any(contains(self.fields,'SMBautoregression'))
     70            size_tot = size_tot-self.defaultdimension+md.smb.num_basins;
     71                                indSMBar = find(contains(self.fields,'SMBautoregression')); %index of SMBar, now check for consistency with TFar timestep (08Nov2021)
     72         end
     73         if any(contains(self.fields,'FrontalForcingsRignotAutoregression'))
     74            size_tot = size_tot-self.defaultdimension+md.frontalforcings.num_basins;
     75                                indTFar  = find(contains(self.fields,'FrontalForcingsRignotAutoregression')); %index of TFar, now check for consistency with SMBar timestep (08Nov2021)
     76         end
     77
     78                        if(indSMBar~=-1 && indTFar~=-1) %both autoregressive models are used: check autoregressive time step consistency
     79                                if((md.smb.ar_timestep~=md.frontalforcings.ar_timestep) && any(self.covariance(1+sum(self.dimensions(1:indSMBar-1)):sum(self.dimensions(1:indSMBar)),1+sum(self.dimensions(1:indTFar-1)):sum(self.dimensions(1:indTFar)))))
     80                                        error('SMBautoregression and FrontalForcingsRignotAutoregression have different ar_timestep and non-zero covariance');
     81                                end
     82                        end
    3783
    3884                        md = checkfield(md,'fieldname','stochasticforcing.isstochasticforcing','values',[0 1]);
    3985                        md = checkfield(md,'fieldname','stochasticforcing.fields','numel',num_fields,'cell',1,'values',supportedstochforcings()); %VV check here 'cell' (19Oct2021)
    40                         md = checkfield(md,'fieldname','stochasticforcing.dimensions','NaN',1,'Inf',1,'>',0,'size',[1,num_fields]); %specific dimension for each field
    4186                        md = checkfield(md,'fieldname','stochasticforcing.covariance','NaN',1,'Inf',1,'size',[size_tot,size_tot]); %global covariance matrix
    4287                        md = checkfield(md,'fieldname','stochasticforcing.randomflag','numel',[1],'values',[0 1]);
    43 
    44                         %Check that all fields agree with the corresponding md class
    45                         for field=self.fields
    46                                 if(contains(field,'SMB'))
    47                                         if~(isequal(class(md.smb),char(field)))
    48                                                 error('md.smb does not agree with stochasticforcing field %s', char(field));
    49                                         end
    50                                 end
    51                                 if(contains(field,'frontalforcings'))
    52                                         if~(isequal(class(md.frontalforcings),char(field)))
    53                                                 error('md.frontalforcings does not agree with stochasticforcing field %s', char(field));
    54                                         end
    55                                 end
    56                         end
     88                        if(checkdefaults) %need to check the defaults
     89            md = checkfield(md,'fieldname','stochasticforcing.defaultdimension','numel',1,'NaN',1,'Inf',1,'>',0);
     90            md = checkfield(md,'fieldname','stochasticforcing.default_id','Inf',1,'>=',0,'<=',self.defaultdimension,'size',[md.mesh.numberofelements,1]);
     91         end
    5792                end % }}}
    5893                function disp(self) % {{{
     
    6095                        fielddisplay(self,'isstochasticforcing','is stochasticity activated?');
    6196                        fielddisplay(self,'fields','fields with stochasticity applied, ex: {''SMBautoregression''}, or {''FrontalForcingsRignotAutoregression''}');
    62                         fielddisplay(self,'dimensions','dimensionality of each field');
     97                        fielddisplay(self,'defaultdimension','dimensionality of the noise terms (does not apply to fields with their specific dimension)');
     98         fielddisplay(self,'default_id','id of each element for partitioning of the noise terms (does not apply to fields with their specific partition)');
    6399                        fielddisplay(self,'covariance','covariance matrix for within- and between-fields covariance (units must be squared field units)');
    64100                        fielddisplay(self,'randomflag','whether to apply real randomness (true) or pseudo-randomness with fixed seed (false)');
     
    77113                                return
    78114                        else
     115
     116                                %Retrieve dimensionality of each field
     117                                dimensions = self.defaultdimension*ones(1,num_fields);
     118                                ind = 1;
     119                                for field=self.fields
     120                                        %Checking for specific dimensions
     121                                        if(strcmp(field,'SMBautoregression'))
     122                                                dimensions(ind) = md.smb.num_basins;
     123                                        end
     124                                        if(strcmp(field,'FrontalForcingsRignotAutoregression'))
     125                                                dimensions(ind) = md.frontalforcings.num_basins;
     126                                        end
     127                                        ind = ind+1;
     128                                end
     129
    79130                                %Scaling covariance matrix (scale column-by-column and row-by-row)
    80                                 scaledfields = {'SMBautoregression'}; %list of fields that need scaling *1/yts
     131                                scaledfields = {'DefaultCalving','SMBautoregression'}; %list of fields that need scaling *1/yts
    81132                                tempcovariance = self.covariance; %copy of covariance to avoid writing back in member variable
    82133                                for i=1:num_fields
    83134                                        if any(strcmp(scaledfields,self.fields(i)))
    84                                                 inds = [1+sum(self.dimensions(1:i-1)):1:sum(self.dimensions(1:i))];
     135                                                inds = [1+sum(dimensions(1:i-1)):1:sum(dimensions(1:i))];
    85136                                                for row=inds %scale rows corresponding to scaled field
    86137                                                        tempcovariance(row,:) = 1./yts*tempcovariance(row,:);
     
    93144                                WriteData(fid,prefix,'data',num_fields,'name','md.stochasticforcing.num_fields','format','Integer');
    94145                                WriteData(fid,prefix,'object',self,'fieldname','fields','format','StringArray');
    95                                 WriteData(fid,prefix,'object',self,'fieldname','dimensions','format','IntMat');
     146                                WriteData(fid,prefix,'data',dimensions,'name','md.stochasticforcing.dimensions','format','IntMat');
     147            WriteData(fid,prefix,'object',self,'fieldname','default_id','data',self.default_id-1,'format','IntMat','mattype',2); %0-indexed
     148                                WriteData(fid,prefix,'object',self,'fieldname','defaultdimension','format','Integer');
    96149                                WriteData(fid,prefix,'data',tempcovariance,'name','md.stochasticforcing.covariance','format','DoubleMat');
    97150                                WriteData(fid,prefix,'object',self,'fieldname','randomflag','format','Boolean');
     
    105158
    106159   list = {...
    107       'SMBautoregression',...
    108       'FrontalForcingsRignotAutoregression'
     160      'DefaultCalving',...
     161                'FrontalForcingsRignotAutoregression',...
     162      'SMBautoregression'
    109163      };
    110164end % }}}
Note: See TracChangeset for help on using the changeset viewer.