Changeset 26660


Ignore:
Timestamp:
11/23/21 12:54:53 (3 years ago)
Author:
jdquinn
Message:

CHG: Review and cleanup

Location:
issm/trunk-jpl/src/m/classes
Files:
2 edited

Legend:

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

    r26640 r26660  
    55
    66classdef stochasticforcing
    7         properties (SetAccess=public) 
    8                 isstochasticforcing  = 0;
    9                 fields               = NaN;
    10                 defaultdimension     = 0;
    11       default_id           = NaN;
    12                 covariance           = NaN;
    13                 randomflag           = 1;
     7        properties (SetAccess=public)
     8                isstochasticforcing     = 0;
     9                fields                                  = NaN;
     10                defaultdimension                = 0;
     11                default_id                              = NaN;
     12                covariance                              = NaN;
     13                randomflag                              = 1;
    1414        end
    1515        methods
     
    2626                end % }}}
    2727                function self = setdefaultparameters(self) % {{{
    28                         self.isstochasticforcing  = 0; %stochasticforcing is turned off by default
    29                         self.randomflag           = 1; %true randomness is implemented by default
     28                        self.isstochasticforcing        = 0; %stochasticforcing is turned off by default
     29                        self.randomflag                         = 1; %true randomness is implemented by default
    3030                end % }}}
    3131                function md = checkconsistency(self,md,solution,analyses) % {{{
     
    3535
    3636                        num_fields = numel(self.fields);
    37                        
     37
    3838                        %Check that covariance matrix is positive definite
    3939                        try
     
    4343                        end
    4444
    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                         structstoch   = structstochforcing();
     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 fields does not have its own dimensionality
     47                        structstoch             = structstochforcing();
    4848                        for field=self.fields
    49             %Checking agreement of classes
    50             if(contains(field,'SMB'))
     49                                %Checking agreement of classes
     50                                if(contains(field,'SMB'))
    5151                                        mdname = structstoch.mdnames(find(strcmp(structstoch.fields,char(field))));
    5252                                        if~(isequal(class(md.smb),char(mdname)))
    53                   error('md.smb does not agree with stochasticforcing field %s', char(field));
    54                end
    55             end
    56             if(contains(field,'FrontalForcings'))
     53                                                error('md.smb does not agree with stochasticforcing field %s', char(field));
     54                                        end
     55                                end
     56                                if(contains(field,'FrontalForcings'))
    5757                                        mdname = structstoch.mdnames(find(strcmp(structstoch.fields,char(field))));
    58                if~(isequal(class(md.frontalforcings),char(mdname)))
    59                   error('md.frontalforcings does not agree with stochasticforcing field %s', char(field));
    60                end
    61             end
     58                                        if~(isequal(class(md.frontalforcings),char(mdname)))
     59                                                error('md.frontalforcings does not agree with stochasticforcing field %s', char(field));
     60                                        end
     61                                end
    6262                                if(contains(field,'Calving'))
    6363                                        mdname = structstoch.mdnames(find(strcmp(structstoch.fields,char(field))));
    64                if~(isequal(class(md.calving),char(mdname)))
    65                   error('md.calving does not agree with stochasticforcing field %s', char(field));
    66                end
    67             end
     64                                        if~(isequal(class(md.calving),char(mdname)))
     65                                                error('md.calving does not agree with stochasticforcing field %s', char(field));
     66                                        end
     67                                end
    6868                                if(contains(field,'BasalforcingsFloatingice'))
    6969                                        mdname = structstoch.mdnames(find(strcmp(structstoch.fields,char(field))));
    70                if~(isequal(class(md.basalforcings),char(mdname)))
    71                   error('md.basalforcings does not agree with stochasticforcing field %s', char(field));
    72                end
    73             end
    74             %Checking for specific dimensions
    75             if ~(strcmp(field,'SMBautoregression') || strcmp(field,'FrontalForcingsRignotAutoregression'))
    76                checkdefaults = true; %field with non-specific dimensionality
    77             end
    78          end
     70                                        if~(isequal(class(md.basalforcings),char(mdname)))
     71                                                error('md.basalforcings does not agree with stochasticforcing field %s', char(field));
     72                                        end
     73                                end
     74                                %Checking for specific dimensions
     75                                if ~(strcmp(field,'SMBautoregression') || strcmp(field,'FrontalForcingsRignotAutoregression'))
     76                                        checkdefaults = true; %field with non-specific dimensionality
     77                                end
     78                        end
    7979
    8080                        %Retrieve sum of all the field dimensionalities
    81                         size_tot   = self.defaultdimension*num_fields;
    82                         indSMBar = -1; %about to check for index of SMBautoregression
    83                         indTFar  = -1; %about to check for index of FrontalForcingsRignotAutoregression
     81                        size_tot        = self.defaultdimension*num_fields;
     82                        indSMBar        = -1; %about to check for index of SMBautoregression
     83                        indTFar = -1; %about to check for index of FrontalForcingsRignotAutoregression
    8484                        if any(contains(self.fields,'SMBautoregression'))
    85             size_tot = size_tot-self.defaultdimension+md.smb.num_basins;
     85                                size_tot = size_tot-self.defaultdimension+md.smb.num_basins;
    8686                                indSMBar = find(contains(self.fields,'SMBautoregression')); %index of SMBar, now check for consistency with TFar timestep (08Nov2021)
    87          end
    88          if any(contains(self.fields,'FrontalForcingsRignotAutoregression'))
    89             size_tot = size_tot-self.defaultdimension+md.frontalforcings.num_basins;
    90                                 indTFar  = find(contains(self.fields,'FrontalForcingsRignotAutoregression')); %index of TFar, now check for consistency with SMBar timestep (08Nov2021)
    91          end
     87                        end
     88                        if any(contains(self.fields,'FrontalForcingsRignotAutoregression'))
     89                                size_tot        = size_tot-self.defaultdimension+md.frontalforcings.num_basins;
     90                                indTFar = find(contains(self.fields,'FrontalForcingsRignotAutoregression')); %index of TFar, now check for consistency with SMBar timestep (08Nov2021)
     91                        end
    9292
    9393                        if(indSMBar~=-1 && indTFar~=-1) %both autoregressive models are used: check autoregressive time step consistency
     
    102102                        md = checkfield(md,'fieldname','stochasticforcing.randomflag','numel',[1],'values',[0 1]);
    103103                        if(checkdefaults) %need to check the defaults
    104             md = checkfield(md,'fieldname','stochasticforcing.defaultdimension','numel',1,'NaN',1,'Inf',1,'>',0);
    105             md = checkfield(md,'fieldname','stochasticforcing.default_id','Inf',1,'>=',0,'<=',self.defaultdimension,'size',[md.mesh.numberofelements,1]);
     104                                md = checkfield(md,'fieldname','stochasticforcing.defaultdimension','numel',1,'NaN',1,'Inf',1,'>',0);
     105                                md = checkfield(md,'fieldname','stochasticforcing.default_id','Inf',1,'>=',0,'<=',self.defaultdimension,'size',[md.mesh.numberofelements,1]);
    106106                        end
    107107                end % }}}
     
    111111                        fielddisplay(self,'fields','fields with stochasticity applied, ex: {''SMBautoregression''}, or {''FrontalForcingsRignotAutoregression''}');
    112112                        fielddisplay(self,'defaultdimension','dimensionality of the noise terms (does not apply to fields with their specific dimension)');
    113          fielddisplay(self,'default_id','id of each element for partitioning of the noise terms (does not apply to fields with their specific partition)');
     113                        fielddisplay(self,'default_id','id of each element for partitioning of the noise terms (does not apply to fields with their specific partition)');
    114114                        fielddisplay(self,'covariance','covariance matrix for within- and between-fields covariance (units must be squared field units)');
    115115                        fielddisplay(self,'randomflag','whether to apply real randomness (true) or pseudo-randomness with fixed seed (false)');
    116116                        disp('Available fields:');
    117117                        for field=supportedstochforcings()
    118                                 fprintf('   %s \n',string(field));
     118                                fprintf('   %s\n',string(field));
    119119                        end
    120120                end % }}}
     
    164164                                WriteData(fid,prefix,'object',self,'fieldname','fields','format','StringArray');
    165165                                WriteData(fid,prefix,'data',dimensions,'name','md.stochasticforcing.dimensions','format','IntMat');
    166             WriteData(fid,prefix,'object',self,'fieldname','default_id','data',self.default_id-1,'format','IntMat','mattype',2); %0-indexed
     166                                WriteData(fid,prefix,'object',self,'fieldname','default_id','data',self.default_id-1,'format','IntMat','mattype',2); %0-indexed
    167167                                WriteData(fid,prefix,'object',self,'fieldname','defaultdimension','format','Integer');
    168168                                WriteData(fid,prefix,'data',tempcovariance,'name','md.stochasticforcing.covariance','format','DoubleMat');
     
    173173end
    174174function list = supportedstochforcings() % {{{
    175    % Defines list of fields supported
    176    % by the class md.stochasticforcing
     175        % Defines list of fields supported
     176        % by the class md.stochasticforcing
    177177
    178    list = structstochforcing();
     178        list = structstochforcing();
    179179        list = list.fields;
    180180end % }}}
     
    183183        % supported and corresponding md names
    184184        structure.fields = {...
    185       'DefaultCalving',...
    186       'FloatingMeltRate',...
    187       'FrontalForcingsRignotAutoregression',...
    188       'SMBautoregression'
    189       };
     185                'DefaultCalving',...
     186                'FloatingMeltRate',...
     187                'FrontalForcingsRignotAutoregression',...
     188                'SMBautoregression'
     189                };
    190190        structure.mdnames = {...
    191191                'calving',...
     
    195195        };
    196196end % }}}
    197 
    198 
    199 
    200 
  • issm/trunk-jpl/src/m/classes/stochasticforcing.py

    r26647 r26660  
    4242    def setdefaultparameters(self):  # {{{
    4343        # Type of stabilization used
    44         self.isstochasticforcing = 0  # stochasticforcing is turned off by default
    45         self.fields = []  # Need to initialize to list to avoid "RuntimeError: object of type 'float' has no len()" on import of class
    46         self.randomflag = 1  # true randomness is implemented by default
     44        self.isstochasticforcing = 0 # stochasticforcing is turned off by default
     45        self.fields = [] # Need to initialize to list to avoid "RuntimeError: object of type 'float' has no len()" on import of class
     46        self.randomflag = 1 # true randomness is implemented by default
    4747        return self
    4848    #}}}
     
    5555        num_fields = len(self.fields)
    5656
    57         #Check that covariance matrix is positive definite this is done internaly by linalg
    58         np.linalg.cholesky(self.covariance)
     57        # Check that covariance matrix is positive definite (this is done internally by linalg)
     58        try:
     59            np.linalg.cholesky(self.covariance)
     60        except LinAlgError:
     61            error('md.stochasticforcing.covariance is not positive definite')
    5962
    60         # Check that all fields agree with the corresponding md class
    61         checkdefaults = False
     63        # Check that all fields agree with the corresponding md class and if any field needs the default params
     64        checkdefaults = False # Need to check defaults only if one of the fields does not have its own dimensionality
    6265        structstoch = self.structstochforcing()
    6366        for field in self.fields:
    64             # Checks are temporarily commented out: need to debug why this does not work (18Nov2021) #
     67            # Checking agreement of classes
    6568            if 'SMB' in field:
    6669                mdname = structstoch[field]
     
    7881                mdname = structstoch[field]
    7982                if (type(md.basalforcings).__name__ != mdname):
    80                     raise TypeError('md.basalforcings does not agree with stochasticforcing field {}'.format(mdname))  #Checking for specific dimensions
     83                    raise TypeError('md.basalforcings does not agree with stochasticforcing field {}'.format(mdname))
     84            # Checking for specific dimensions
    8185            if not (field == 'SMBautoregression' or field == 'FrontalForcingsRignotAutoregression'):
    82                 checkdefaults = True   #field with non-specific dimensionality
     86                checkdefaults = True # field with non-specific dimensionality
    8387
    84         #Retrieve sum of all the field dimensionalities
     88        # Retrieve sum of all the field dimensionalities
    8589        size_tot = self.defaultdimension * num_fields
    86         indSMBar = -1  #about to check for index of SMBautoregression
    87         indTFar = -1  #about to check for index of FrontalForcingsRignotAutoregression
     90        indSMBar = -1 # About to check for index of SMBautoregression
     91        indTFar = -1 # About to check for index of FrontalForcingsRignotAutoregression
    8892        if ('SMBautoregression' in self.fields):
    8993            size_tot = size_tot - self.defaultdimension + md.smb.num_basins
    90             indSMBar = self.fields.index('SMBautoregression')
     94            indSMBar = self.fields.index('SMBautoregression') # Index of SMBar, now check for consistency with TFar timestep (08Nov2021)
    9195        if ('FrontalForcingsRignotAutoregression' in self.fields):
    9296            size_tot = size_tot - self.defaultdimension + md.frontalforcings.num_basins
    93             indTFar = self.fields.index('FrontalForcingsRignotAutoregression')
    94         if (indSMBar != -1 and indTFar != -1):
     97            indTFar = self.fields.index('FrontalForcingsRignotAutoregression') # Index of TFar, now check for consistency with SMBar timestep (08Nov2021)
     98        if (indSMBar != -1 and indTFar != -1): # Both autoregressive models are used: check autoregressive time step consistency
    9599            covsum = self.covariance[np.sum(self.defaultdimensions[0:indSMBar]).astype(int):np.sum(self.defaultdimensions[0:indSMBar + 1]).astype(int), np.sum(self.defaultdimensions[0:indTFar]).astype(int):np.sum(self.defaultdimensions[0:indTFar + 1]).astype(int)]
    96100            if((md.smb.ar_timestep != md.frontalforcings.ar_timestep) and np.any(covsum != 0)):
     
    99103        md = checkfield(md, 'fieldname', 'stochasticforcing.isstochasticforcing', 'values', [0, 1])
    100104        md = checkfield(md, 'fieldname', 'stochasticforcing.fields', 'numel', num_fields, 'cell', 1, 'values', self.supportedstochforcings())
    101         #md = checkfield(md, 'fieldname', 'stochasticforcing.dimensions', 'NaN', 1, 'Inf', 1, '>', 0, 'size', [num_fields]) # specific dimension for each field; NOTE: As opposed to MATLAB implementation, pass list
    102105        md = checkfield(md, 'fieldname', 'stochasticforcing.covariance', 'NaN', 1, 'Inf', 1, 'size', [size_tot, size_tot])  # global covariance matrix
    103106        md = checkfield(md, 'fieldname', 'stochasticforcing.randomflag', 'numel', [1], 'values', [0, 1])
     
    121124            return md
    122125        else:
    123             #Retrieve dimensionality of each field
     126            # Retrieve dimensionality of each field
    124127            dimensions = self.defaultdimension * np.ones(num_fields)
    125128            for ind, field in enumerate(self.fields):
    126                 #Checking for specific dimensions
     129                # Checking for specific dimensions
    127130                if (field == 'SMBautoregression'):
    128131                    dimensions[ind] = md.smb.num_basins
     
    131134
    132135            # Scaling covariance matrix (scale column-by-column and row-by-row)
    133             scaledfields = ['DefaultCalving', 'SMBautoregression']  # list of fields that need scaling * 1/yts
     136            scaledfields = ['DefaultCalving', 'SMBautoregression'] # list of fields that need scaling * 1/yts
    134137            tempcovariance = np.copy(self.covariance)
    135138            for i in range(num_fields):
     
    140143                    for col in inds:  # scale columns corresponding to scaled field
    141144                        tempcovariance[:, col] = 1 / yts * tempcovariance[:, col]
    142             #Set dummy default_id vector if defaults not used
     145            # Set dummy default_id vector if defaults not used
    143146            if np.isnan(self.default_id):
    144147                self.default_id = np.zeros(md.mesh.numberofelements)
Note: See TracChangeset for help on using the changeset viewer.