Python Interface to MAPL: Difference between revisions

New page: Here is a very high level example of a MAPL ocean/atmosphere couple model in Python: import MAPL atm = MAPL.GridComp(rc='atm.rc') ocn = MAPL.GridComp(rc='ocn.rc') atm.initialize() ...
 
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
Here is a very high level example of a MAPL ocean/atmosphere couple model in Python:
This document presents a very high level description of a python interface to MAPL.
 
= GEOS-5 Use Cases =
 
The examples below are based on the actual GEOS-5 GCM Application, and a few components at the very top of the hierarchy. Here we assumed that MAPL Grid Components can be implemented entirely in C/Fortran or in Python.
 
== GEOS-5 Application with Fortran Root Component ==
 
In this example of a GEOS-5 application written in Python, the very top root component is implemented entirely in a shared library.
 
import MAPL
 
if __name == "__main__":
 
#  Instantiate the root component
#  ------------------------------
    gcs = MAPL.GridComp(lib='/some/path/libGEOSgcs.so')
 
#  Instantiate MAPL
#  ----------------
    mapl = MAPL(root=gcs)
 
#  Run the cap
#  -----------
    mapl.go()
 
== GEOS-5 Application with Python Root Component ==
 
In this example, the very top root component is now implemented in Python.


  import MAPL
  import MAPL
import GEOSgcs  # the GEOSgcs component is implemented here


  atm = MAPL.GridComp(rc='atm.rc')
  if __name == "__main__":
ocn = MAPL.GridComp(rc='ocn.rc')
#  Instantiate the Python root component
#  -------------------------------------
    gcs = GEOSgcs(rc='gcs.rc')
#  Instantiate MAPL
#  ----------------
    mapl = MAPL(root=gcs)
#  Run the cap
#  -----------
    mapl.go()
 
== A python component with Children implemented in Fortran ==
 
In this example, the '''GEOSgcs''' component is implemented in Python, with its children, components '''ANA''' and '''GCM''', implemented in Fortran.
Notice that every component should be subclass of '''MAPL_GridComp'''.
 
import MAPL
 
class GridComp(MAPL.GridComp):
    """
    Implements the GEOSgcs component by subclassing MAPL.GridComp.
    """
    def SetServices(self):
        """
        SetServices for a composite component.
        """
 
#      Instantiate Children component
#      ------------------------------
        ana = MAPL.GridComp(lib='/some/path/libGEOSana.so')
        gcm = MAPL.GridComp(lib='/some/path/libGEOSgcm.so')
#      Adopt children
#      --------------
        self.AddChild(ana)
        self.AddChild(gcm)
#      Set connectivity among children
#      -------------------------------
        vars = ('Tinc', 'Sinc', 'Uinc', 'Vinc', 'SSHinc' )
        self.AddConnectivity(src=[ana,vars], dst=[gcm,vars])
        self.AddConnectivity(src=[gcm,'LWI'], dst=[ana,'ORO'])
#      Run the base class SetServices
#      ------------------------------
        MAPL.GridComp.__SetServices__(self)
 
Notice that the standard '''Initialize()''', '''Run()''' and '''Finalize()''' methods are inherited from the base class '''MAPL.GridComp'''.
 
== A python component with Children implemented in Python ==
 
In this example, the '''GEOSgcs''' component is implemented in Python, with its children, components '''ANA''' and '''GCM''', also implemented in Python.
 
import MAPL
import GEOSana
import GEOSgcm


  atm.initialize()
  class GridComp(MAPL.GridComp):
  ocn.initialize()
    """
    Implements the GEOSgcs component by subclassing MAPL.GridComp.
    """
 
    def SetServices(self):
        """
        SetServices for a composite component.
        """
 
#      Instantiate Children component
#      ------------------------------
        ana = GEOSana()
        gcm = GEOSgcm()
 
#      Adopt children
#      --------------
        self.AddChild(ana)
        self.AddChild(gcm)
#      Set connectivity among children
#      -------------------------------
        vars = ('Tinc', 'Sinc', 'Uinc', 'Vinc', 'SSHinc' )
        self.AddConnectivity(src=[ana,vars], dst=[gcm,vars])
        self.AddConnectivity(src=[gcm,'LWI'], dst=[ana,'ORO'])
  #      Run the base class SetServices
#      ------------------------------
        MAPL.GridComp.__SetServices__(self)


for t in clock.tick():
In this case we implement the standard '''Run()''' method, but inherit '''Initialize()''' and '''Finalize()''' from the base class '''MAPL.GridComp'''
    atm.run()
    ocn.run()


  atm.finalize()
    def Run(self,clock):
  ocn.finalize()
        """
        Implementation of a simple run method.
        """
       
#      Run the base class method
#      -------------------------
        MAPL.GridComp.__Run__(self,clock)
#      Retrieve some data from a state
#      -------------------------------
        uwnd, vwnd, = self.Export.GetPointer(['UWND', 'VWND'])
#      Print states
#      ------------
        self.Internal.print()
        self.Import.print()
        sllf.Export.print()
#      Just print it
#      -------------
        print "u-wind = ", uwnd.min(), uwnd.max()
        print "v-wind = ", vwnd.min(), vwnd.max()