Changeset 13862


Ignore:
Timestamp:
10/31/12 15:42:39 (12 years ago)
Author:
jschierm
Message:

NEW: Updated python autodiff to matlab version and added python dependent and independent (plus matlab cosmetic changes).

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

Legend:

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

    r13646 r13862  
    5555
    5656                        %early return
    57                         if ~obj.isautodiff, 
    58                                 WriteData(fid,'data',false,'enum',AutodiffMassFluxSegmentsPresentEnum,'format','Boolean');
    59                                 WriteData(fid,'data',false,'enum',AutodiffKeepEnum,'format','Boolean');
    60                                 return; 
     57                        if ~obj.isautodiff,
     58                                WriteData(fid,'data',false,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean');
     59                                WriteData(fid,'data',false,'enum',AutodiffKeepEnum(),'format','Boolean');
     60                                return;
    6161                        end
    6262
     
    118118                                end
    119119                                index=index-1; %get c-index numbering going
    120                                 WriteData(fid,'data',index,'enum',AutodiffFosForwardIndexEnum(),'format','Integer'); 
     120                                WriteData(fid,'data',index,'enum',AutodiffFosForwardIndexEnum(),'format','Integer');
    121121                        end
    122122                        %}}}
     
    139139                                end
    140140                                index=index-1; %get c-index numbering going
    141                                 WriteData(fid,'data',index,'enum',AutodiffFosReverseIndexEnum(),'format','Integer'); 
     141                                WriteData(fid,'data',index,'enum',AutodiffFosReverseIndexEnum(),'format','Integer');
    142142                        end
    143143                        %}}}
     
    160160                                end
    161161                                indices=indices-1; %get c-indices numbering going
    162 
    163                                 WriteData(fid,'data',indices,'enum',AutodiffFovForwardIndicesEnum,'format','IntMat','mattype',3);
     162                                WriteData(fid,'data',indices,'enum',AutodiffFovForwardIndicesEnum(),'format','IntMat','mattype',3);
    164163                        end
    165164                        %}}}
     
    173172                        end
    174173                        if ~isempty(mass_flux_segments),
    175                                 WriteData(fid,'data',mass_flux_segments,'enum',MassFluxSegmentsEnum,'format','MatArray');
     174                                WriteData(fid,'data',mass_flux_segments,'enum',MassFluxSegmentsEnum(),'format','MatArray');
    176175                                flag=true;
    177                         else 
    178                                 flag=false; 
    179                         end
    180                         WriteData(fid,'data',flag,'enum',AutodiffMassFluxSegmentsPresentEnum,'format','Boolean');
     176                        else
     177                                flag=false;
     178                        end
     179                        WriteData(fid,'data',flag,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean');
    181180                        %}}}
    182181                        %deal with trace keep on: {{{
     
    199198                                end
    200199                        end
    201                         WriteData(fid,'data',keep,'enum',AutodiffKeepEnum,'format','Boolean');
     200                        WriteData(fid,'data',keep,'enum',AutodiffKeepEnum(),'format','Boolean');
    202201                        %}}}
    203202
  • issm/trunk-jpl/src/m/classes/autodiff.py

    r13516 r13862  
    11#module imports
     2from dependent import *
     3from independent import *
    24from fielddisplay import fielddisplay
    35from EnumDefinitions import *
     
    1416
    1517        #properties
    16         def __init__(self):
    17                 # {{{ Properties
    18                 self.isautodiff = False
     18        def __init__(self,*args):    # {{{
     19                self.isautodiff   = False
     20                self.dependents   = []
     21                self.independents = []
     22                self.driver       = 'fos_forward'
    1923
    20                 #set defaults
    21                 self.setdefaultparameters()
     24                if not len(args):
     25                        self.setdefaultparameters()
     26                else:
     27                        raise RuntimeError("constructor not supported")
     28        # }}}
    2229
    23                 #}}}
    24         def __repr__(self):
    25                 # {{{ Display
    26                 string='   automatic differentiation parameters:'
    27                 string="%s\n%s"%(string,fielddisplay(self,'isautodiff','indicates if the automatic differentiation is activated'))
    28                 return string
    29                 #}}}
    30         def setdefaultparameters(self):
    31                 # {{{setdefaultparameters
     30        def __repr__(self):    # {{{
     31                s ="   automatic differentiation parameters:"
     32
     33                s+="%s\n" % fielddisplay(self,'isautodiff',"indicates if the automatic differentiation is activated")
     34                s+="%s\n" % fielddisplay(self,'dependents',"list of dependent variables")
     35                s+="%s\n" % fielddisplay(self,'independents',"list of independent variables")
     36                s+="%s\n" % fielddisplay(self,'driver',"ADOLC driver ('fos_forward' or 'fov_forward'")
     37
     38                return s
     39        # }}}
     40
     41        def setdefaultparameters(self):    # {{{
    3242                return self
    33         #}}}
     43        # }}}
    3444
    3545        def checkconsistency(self,md,solution,analyses):    # {{{
     46
     47                #Early return
     48                if not self.isautodiff:
     49                        return md
     50
     51                #Driver value:
     52                md = checkfield(md,'autodiff.driver','values',['fos_forward','fov_forward','fov_forward_all','fos_reverse','fov_reverse','fov_reverse_all'])
     53
     54                #go through our dependents and independents and check consistency:
     55                for dep in self.dependents:
     56                        dep.checkconsistency(md,solution,analyses)
     57                for i,indep in enumerate(self.independents):
     58                        indep.checkconsistency(md,i,solution,analyses,self.driver)
     59
    3660                return md
    3761        # }}}
     
    3963        def marshall(self,fid):    # {{{
    4064                WriteData(fid,'object',self,'fieldname','isautodiff','format','Boolean')
    41                 WriteData(fid,'data',False,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean');
    42                 WriteData(fid,'data',False,'enum',AutodiffKeepEnum(),'format','Boolean');
     65                WriteData(fid,'object',self,'fieldname','driver','format','String')
     66
     67                #early return
     68                if not self.isautodiff:
     69                        WriteData(fid,'data',False,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean')
     70                        WriteData(fid,'data',False,'enum',AutodiffKeepEnum(),'format','Boolean')
     71                        return
     72
     73                #process dependent variables {{{
     74                num_dependent_objects=len(self.dependents)
     75                WriteData(fid,'data',num_dependent_objects,'enum',AutodiffNumDependentObjectsEnum(),'format','Integer')
     76
     77                if num_dependent_objects:
     78                        names=numpy.zeros(num_dependent_objects)
     79                        types=numpy.zeros(num_dependent_objects)
     80                        indices=numpy.zeros(num_dependent_objects)
     81
     82                        for i,dep in enumerate(self.dependents):
     83                                names[i]=StringToEnum(dep.name)[0]
     84                                types[i]=dep.typetoscalar()
     85                                indices[i]=dep.index
     86
     87                        WriteData(fid,'data',names,'enum',AutodiffDependentObjectNamesEnum(),'format','IntMat','mattype',3)
     88                        WriteData(fid,'data',types,'enum',AutodiffDependentObjectTypesEnum(),'format','IntMat','mattype',3)
     89                        WriteData(fid,'data',indices,'enum',AutodiffDependentObjectIndicesEnum(),'format','IntMat','mattype',3)
     90                #}}}
     91
     92                #process independent variables {{{
     93                num_independent_objects=len(self.independents)
     94                WriteData(fid,'data',num_independent_objects,'enum',AutodiffNumIndependentObjectsEnum(),'format','Integer')
     95
     96                if num_independent_objects:
     97                        names=numpy.zeros(num_independent_objects)
     98                        types=numpy.zeros(num_independent_objects)
     99
     100                        for i,indep in enumerate(self.independents):
     101                                names[i]=StringToEnum(indep.name)[0]
     102                                types[i]=indep.typetoscalar()
     103
     104                        WriteData(fid,'data',names,'enum',AutodiffIndependentObjectNamesEnum(),'format','IntMat','mattype',3)
     105                        WriteData(fid,'data',types,'enum',AutodiffIndependentObjectTypesEnum(),'format','IntMat','mattype',3)
     106                #}}}
     107
     108                #if driver is fos_forward, build index:  {{{
     109                if strcmpi(self.driver,'fos_forward'):
     110                        index=0
     111
     112                        for indep in self.independents:
     113                                if not isnan(indep.fos_forward_index):
     114                                        index+=indep.fos_forward_index
     115                                        break
     116                                else:
     117                                        if strcmpi(indep.type,'scalar'):
     118                                                index+=1
     119                                        else:
     120                                                index+=indep.nods
     121
     122                        index-=1    #get c-index numbering going
     123                        WriteData(fid,'data',index,'enum',AutodiffFosForwardIndexEnum(),'format','Integer')
     124                #}}}
     125
     126                #if driver is fos_reverse, build index:  {{{
     127                if strcmpi(self.driver,'fos_reverse'):
     128                        index=0
     129
     130                        for dep in self.dependents:
     131                                if not isnan(dep.fos_reverse_index):
     132                                        index+=dep.fos_reverse_index
     133                                        break
     134                                else:
     135                                        if strcmpi(dep.type,'scalar'):
     136                                                index+=1
     137                                        else:
     138                                                index+=dep.nods
     139
     140                        index-=1    #get c-index numbering going
     141                        WriteData(fid,'data',index,'enum',AutodiffFosReverseIndexEnum(),'format','Integer')
     142                end
     143                #}}}
     144
     145                #if driver is fov_forward, build indices:  {{{
     146                if strcmpi(self.driver,'fov_forward'):
     147                        indices=0
     148
     149                        for indep in self.independents:
     150                                if indep.fos_forward_index:
     151                                        indices+=indep.fov_forward_indices
     152                                        break
     153                                else:
     154                                        if strcmpi(indep.type,'scalar'):
     155                                                indices+=1
     156                                        else:
     157                                                indices+=indep.nods
     158
     159                        indices-=1    #get c-indices numbering going
     160                        WriteData(fid,'data',indices,'enum',AutodiffFovForwardIndicesEnum(),'format','IntMat','mattype',3)
     161                end
     162                #}}}
     163
     164                #deal with mass fluxes:  {{{
     165                mass_flux_segments=[dep.segments for dep in self.dependents if strcmpi(dep.name,'MassFlux')]
     166
     167                if mass_flux_segments:
     168                        WriteData(fid,'data',mass_flux_segments,'enum',MassFluxSegmentsEnum(),'format','MatArray')
     169                        flag=True
     170                else:
     171                        flag=False
     172                WriteData(fid,'data',flag,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean')
     173                #}}}
     174
     175                #deal with trace keep on: {{{
     176                keep=False
     177
     178                #From ADOLC userdoc:
     179                # The optional integer argument keep of trace on determines whether the numerical values of all active variables are
     180                # recorded in a buffered temporary array or file called the taylor stack. This option takes effect if keep = 1 and
     181                # prepares the scene for an immediately following gradient evaluation by a call to a routine implementing the reverse
     182                # mode as described in the Section 4 and Section 5.
     183                #
     184
     185                if len(self.driver)<=3:
     186                        keep=False    #there is no "_reverse" string within the driver string:
     187                else:
     188                        if strncmpi(self.driver[3:],'_reverse',8):
     189                                keep=True
     190                        else:
     191                                keep=False
     192                WriteData(fid,'data',keep,'enum',AutodiffKeepEnum(),'format','Boolean')
     193                #}}}
     194
     195                return
    43196        # }}}
    44197
  • issm/trunk-jpl/src/m/classes/dependent.m

    r13646 r13862  
    1515        end
    1616        methods
    17                  function obj= dependent(varargin) % {{{
     17                function obj = dependent(varargin) % {{{
    1818
    19                          %use provided options to change fields
    20                          options=pairoptions(varargin{:});
     19                        %use provided options to change fields
     20                        options=pairoptions(varargin{:});
    2121
    22                          obj.name=getfieldvalue(options,'name','');
    23                          obj.type=getfieldvalue(options,'type','');
    24                          obj.exp=getfieldvalue(options,'exp','');
    25                          obj.segments=getfieldvalue(options,'segments',[]);
    26                          obj.index=getfieldvalue(options,'index',-1);
    27                          obj.nods=getfieldvalue(options,'nods',0);
     22                        obj.name=getfieldvalue(options,'name','');
     23                        obj.type=getfieldvalue(options,'type','');
     24                        obj.exp=getfieldvalue(options,'exp','');
     25                        obj.segments=getfieldvalue(options,'segments',[]);
     26                        obj.index=getfieldvalue(options,'index',-1);
     27                        obj.nods=getfieldvalue(options,'nods',0);
    2828
    29                          %if name is mass flux:
    30                          if strcmpi(obj.name,'MassFlux'),
    31                                  %make sure that we supplied a file and that it exists!
    32                                  if exist(obj.exp)~=2,
    33                                          error('dependent checkconsistency: specified ''exp'' file does not exist!');
    34                                  end
    35                                  %process the file and retrieve segments
    36                                  mesh=getfieldvalue(options,'mesh');
    37                                  obj.segments=MeshProfileIntersection(mesh.elements,mesh.x,mesh.y,obj.exp);
    38                          end
    39                  end
    40                  %}}}
     29                        %if name is mass flux:
     30                        if strcmpi(obj.name,'MassFlux'),
     31                                %make sure that we supplied a file and that it exists!
     32                                if exist(obj.exp)~=2,
     33                                        error('dependent checkconsistency: specified ''exp'' file does not exist!');
     34                                end
     35                                %process the file and retrieve segments
     36                                mesh=getfieldvalue(options,'mesh');
     37                                obj.segments=MeshProfileIntersection(mesh.elements,mesh.x,mesh.y,obj.exp);
     38                        end
     39                end
     40                %}}}
    4141                function obj = setdefaultparameters(obj) % {{{
    42                 %do nothing
     42                        %do nothing
    4343                end % }}}
    4444                function md = checkconsistency(obj,md,solution,analyses) % {{{
    45                         %do nothing for now
    4645                        if strcmpi(obj.name,'MassFlux'),
    4746                                if isempty(obj.segments),
     
    5756                                end
    5857                                if obj.nods==0,
    59                                         error('ependent checkconsistency error: nods should be set to the size of the independent variable');
     58                                        error('dependent checkconsistency error: nods should be set to the size of the independent variable');
    6059                                end
    61 
    6260                        end
    6361
    6462                end % }}}
    6563                function disp(obj) % {{{
    66                         disp(sprintf('   dependent variable :'));
     64                        disp(sprintf('   dependent variable:'));
    6765
    6866                        fielddisplay(obj,'name','variable name (must match corresponding Enum)');
     
    7977                end % }}}
    8078                function scalar=typetoscalar(obj) % {{{
    81                 if strcmpi(obj.type,'scalar'),
    82                         scalar=0;
    83                 elseif strcmpi(obj.type,'vertex'),
    84                         scalar=1;
    85                 end
     79                        if strcmpi(obj.type,'scalar'),
     80                                scalar=0;
     81                        elseif strcmpi(obj.type,'vertex'),
     82                                scalar=1;
     83                        end
    8684
    8785                end % }}}
  • issm/trunk-jpl/src/m/classes/independent.m

    r13646 r13862  
    1313        end
    1414        methods
    15                  function obj= independent(varargin) % {{{
     15                function obj = independent(varargin) % {{{
    1616
    17                          %use provided options to change fields
    18                          options=pairoptions(varargin{:});
     17                        %use provided options to change fields
     18                        options=pairoptions(varargin{:});
    1919
    20                          %OK get other fields
    21                          obj=AssignObjectFields(pairoptions(varargin{:}),obj);
     20                        %OK get other fields
     21                        obj=AssignObjectFields(pairoptions(varargin{:}),obj);
    2222
    23                  end
    24                  %}}}
     23                end
     24                %}}}
    2525                function obj = setdefaultparameters(obj) % {{{
    2626                        %do nothing
     
    2828                end % }}}
    2929                function md = checkconsistency(obj,md,i,solution,analyses,driver) % {{{
    30                         %do nothing for now
    3130                        if ~isnan(obj.fos_forward_index),
    3231                                if ~strcmpi(driver,'fos_forward'),
     
    4241                                        error('cannot declare an independent with fov_forward_indices when the driver is not fov_forward!');
    4342                                end
    44 
    4543                                if obj.nods==0,
    4644                                        error('independent checkconsistency error: nods should be set to the size of the independent variable');
     
    5149                end % }}}
    5250                function disp(obj) % {{{
    53                         disp(sprintf('   independent variable :'));
     51                        disp(sprintf('   independent variable:'));
    5452
    5553                        fielddisplay(obj,'name','variable name (must match corresponding Enum)');
Note: See TracChangeset for help on using the changeset viewer.