source: issm/oecreview/Archive/13393-13976/ISSM-13861-13862.diff

Last change on this file was 13980, checked in by Mathieu Morlighem, 12 years ago

preparing oecreview for 13393-13976'

File size: 20.2 KB
  • ../trunk-jpl/src/m/classes/autodiff.py

     
    11#module imports
     2from dependent import *
     3from independent import *
    24from fielddisplay import fielddisplay
    35from EnumDefinitions import *
    46from checkfield import *
     
    1315        """
    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        # }}}
    3862
    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
  • ../trunk-jpl/src/m/classes/autodiff.m

     
    5454                        WriteData(fid,'object',obj,'fieldname','driver','format','String');
    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
    6363                        %process dependent variables {{{
     
    117117                                        end
    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                        %}}}
    123123                        %if driver is fos_reverse, build index:  {{{
     
    138138                                        end
    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                        %}}}
    144144                        %if driver is fov_forward, build indices:  {{{
     
    159159                                        end
    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                        %}}}
    166165                        %deal with mass fluxes:  {{{
     
    172171                                end
    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; 
     176                        else
     177                                flag=false;
    179178                        end
    180                         WriteData(fid,'data',flag,'enum',AutodiffMassFluxSegmentsPresentEnum,'format','Boolean');
     179                        WriteData(fid,'data',flag,'enum',AutodiffMassFluxSegmentsPresentEnum(),'format','Boolean');
    181180                        %}}}
    182181                        %deal with trace keep on: {{{
    183182                        keep=false;
     
    198197                                        keep=false;
    199198                                end
    200199                        end
    201                         WriteData(fid,'data',keep,'enum',AutodiffKeepEnum,'format','Boolean');
     200                        WriteData(fid,'data',keep,'enum',AutodiffKeepEnum(),'format','Boolean');
    202201                        %}}}
    203202
    204203                end % }}}
  • ../trunk-jpl/src/m/classes/dependent.m

     
    1414                nods                 = 0;
    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),
    4847                                        error('dependent checkconsistency error: need segments to compute this dependent response');
     
    5655                                        error('cannot declare a dependent with a fos_reverse_index when the driver is not fos_reverse!');
    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)');
    6967                        fielddisplay(obj,'type','type of variable (''vertex'' or ''scalar'')');
     
    7876
    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 % }}}
    8886        end
  • ../trunk-jpl/src/m/classes/independent.py

     
     1from pairoptions import *
     2from MatlabFuncs import *
     3from EnumDefinitions import *
     4from WriteData import *
     5
     6class independent(object):
     7        """
     8        INDEPENDENT class definition
     9
     10           Usage:
     11              independent=independent();
     12        """
     13
     14        def __init__(self,*args):    # {{{
     15                self.name                 = ''
     16                self.type                 = ''
     17                self.fos_forward_index    = float('NaN')
     18                self.fov_forward_indices  = numpy.array([])
     19                self.nods                 = 0
     20
     21                #set defaults
     22                self.setdefaultparameters()
     23
     24                #use provided options to change fields
     25                options=pairoptions(*args)
     26
     27                #OK get other fields
     28                self=options.AssignObjectFields(self)
     29        # }}}
     30
     31        def setdefaultparameters(self):    # {{{
     32                #do nothing
     33                return self
     34        # }}}
     35
     36        def checkconsistency(self,md,i,solution,analyses,driver):    # {{{
     37                if not isnan(self.fos_forward_index):
     38                        if not strcmpi(driver,'fos_forward'):
     39                                raise TypeError("cannot declare an independent with a fos_forward_index when the driver is not fos_forward!")
     40                        if self.nods==0:
     41                                raise TypeError("independent checkconsistency error: nods should be set to the size of the independent variable")
     42
     43                if self.fov_forward_indices:
     44                        if not strcmpi(driver,'fov_forward'):
     45                                raise TypeError("cannot declare an independent with fov_forward_indices when the driver is not fov_forward!")
     46                        if self.nods==0:
     47                                raise TypeError("independent checkconsistency error: nods should be set to the size of the independent variable")
     48                        end
     49                        md = checkfield(md,"autodiff.independents[%d].fov_forward_indices" % i,'>=',1,'<=',self.nods,'size',[float('NaN'),1])
     50
     51                return md
     52        # }}}
     53
     54        def __repr__(self):    # {{{
     55                s ="   independent variable:\n"
     56
     57                s+="%s\n" % fielddisplay(self,'name',"variable name (must match corresponding Enum)")
     58                s+="%s\n" % fielddisplay(self,'type',"type of variable ('vertex' or 'scalar')")
     59                if not isnan(self.fos_forward_index):
     60                        s+="%s\n" % fielddisplay(self,'fos_forward_index',"index for fos_foward driver of ADOLC")
     61                if numpy.any(numpy.logical_not(numpy.isnan(self.fov_forward_indices))):
     62                        s+="%s\n" % fielddisplay(self,'fov_forward_indices',"indices for fov_foward driver of ADOLC")
     63
     64                return s
     65        # }}}
     66
     67        def typetoscalar(self):    # {{{
     68                if   strcmpi(self.type,'scalar'):
     69                        scalar=0
     70                elif strcmpi(self.type,'vertex'):
     71                        scalar=1
     72
     73                return scalar
     74        # }}}
     75
  • ../trunk-jpl/src/m/classes/independent.m

     
    1212                nods                 = 0;
    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
    2727
    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'),
    3332                                        error('cannot declare an independent with a fos_forward_index when the driver is not fos_forward!');
     
    4140                                if ~strcmpi(driver,'fov_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');
    4745                                end
     
    5048
    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)');
    5654                        fielddisplay(obj,'type','type of variable (''vertex'' or ''scalar'')');
  • ../trunk-jpl/src/m/classes/dependent.py

     
     1import os.path
     2from pairoptions import *
     3from MatlabFuncs import *
     4from EnumDefinitions import *
     5from WriteData import *
     6#from MeshProfileIntersection import *
     7
     8class dependent(object):
     9        """
     10        DEPENDENT class definition
     11
     12           Usage:
     13              dependent=dependent();
     14        """
     15
     16        def __init__(self,*args):    # {{{
     17                self.name                 = ''
     18                self.type                 = ''
     19                self.fos_reverse_index    = float('NaN')
     20                self.exp                  = ''
     21                self.segments             = []
     22                self.index                = -1
     23                self.nods                 = 0
     24
     25                #set defaults
     26                self.setdefaultparameters()
     27
     28                #use provided options to change fields
     29                options=pairoptions(*args)
     30
     31                self.name=options.getfieldvalue('name','')
     32                self.type=options.getfieldvalue('type','')
     33                self.exp=options.getfieldvalue('exp','')
     34                self.segments=options.getfieldvalue('segments',[])
     35                self.index=options.getfieldvalue('index',-1)
     36                self.nods=options.getfieldvalue('nods',0)
     37
     38                #if name is mass flux:
     39                if strcmpi(self.name,'MassFlux'):
     40                        #make sure that we supplied a file and that it exists!
     41                        if not os.path.exists(self.exp):
     42                                raise IOError("dependent checkconsistency: specified 'exp' file does not exist!")
     43                        #process the file and retrieve segments
     44                        mesh=options.getfieldvalue('mesh')
     45                        raise RuntimeError("MeshProfileIntersection is not complete.")
     46                        self.segments=MeshProfileIntersection(mesh.elements,mesh.x,mesh.y,self.exp)
     47        # }}}
     48
     49        def setdefaultparameters(self):    # {{{
     50                #do nothing
     51                return self
     52        # }}}
     53
     54        def checkconsistency(self,md,solution,analyses):    # {{{
     55                if strcmpi(self.name,'MassFlux'):
     56                        if not self.segments:
     57                                raise RuntimeError("dependent checkconsistency error: need segments to compute this dependent response")
     58                        if self.index<0:
     59                                raise RuntimeError("dependent checkconsistency error: index for segments should be >=0")
     60
     61                if not isnan(self.fos_reverse_index):
     62                        if not strcmpi(driver,'fos_reverse'):
     63                                raise TypeError("cannot declare a dependent with a fos_reverse_index when the driver is not fos_reverse!")
     64                        if self.nods==0:
     65                                raise TypeError("dependent checkconsistency error: nods should be set to the size of the independent variable")
     66
     67                return md
     68        # }}}
     69
     70        def __repr__(self):    # {{{
     71                s ="   dependent variable:\n"
     72
     73                s+="%s\n" % fielddisplay(self,'name',"variable name (must match corresponding Enum)")
     74                s+="%s\n" % fielddisplay(self,'type',"type of variable ('vertex' or 'scalar')")
     75
     76                if not isnan(self.fos_reverse_index):
     77                        s+="%s\n" % fielddisplay(self,'fos_reverse_index',"index for fos_reverse driver of ADOLC")
     78                end
     79                if self.exp:
     80                        s+="%s\n" % fielddisplay(self,'exp',"file needed to compute dependent variable")
     81                        s+="%s\n" % fielddisplay(self,'segments',"mass flux segments")
     82
     83                return s
     84        # }}}
     85
     86        def typetoscalar(self):    # {{{
     87                if   strcmpi(self.type,'scalar'):
     88                        scalar=0
     89                elif strcmpi(self.type,'vertex'):
     90                        scalar=1
     91
     92                return scalar
     93        # }}}
     94
Note: See TracBrowser for help on using the repository browser.