tsipletools.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tsipletools.py (3327B)
       ---
            1 # Copyright (C) 2011, 2012, 2014, 2015, 2017 David Maxwell
            2 #
            3 # This file is part of PISM.
            4 #
            5 # PISM is free software; you can redistribute it and/or modify it under the
            6 # terms of the GNU General Public License as published by the Free Software
            7 # Foundation; either version 3 of the License, or (at your option) any later
            8 # version.
            9 #
           10 # PISM is distributed in the hope that it will be useful, but WITHOUT ANY
           11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
           12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
           13 # details.
           14 #
           15 # You should have received a copy of the GNU General Public License
           16 # along with PISM; if not, write to the Free Software
           17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
           18 
           19 """Classes for interfacing between PISM and the ``siple`` library."""
           20 
           21 import PISM
           22 import siple
           23 import petsc4py
           24 
           25 
           26 class PISMLocalVector(siple.linalg.AbstractVector):
           27 
           28     """Implements the :class:`siple.linalg.linalg_abstract.AbstractVector` interface for few kinds of PISM :cpp:class:`IceModelVec`."""
           29 
           30     def __init__(self, u):
           31         if isinstance(u, PISM.IceModelVec2S):
           32             self._core = u
           33             self.dof = 1
           34         elif isinstance(u, PISM.IceModelVec2V):
           35             self._core = u
           36             self.dof = 2
           37         else:
           38             raise ValueError("An PISMLocalVector wraps PISM IceModelVec2S or IceModelVec2V: found a %s" % u)
           39         self.grid = u.grid()
           40 
           41     def set(self, rhs):
           42         self._core.copy_from(rhs._core)
           43 
           44     def acc(self, rhs):
           45         self._core.add(1., rhs._core)
           46 
           47     def scale(self, t):
           48         self._core.scale(t)
           49 
           50     def axpy(self, t, v):
           51         self._core.add(t, v._core)
           52 
           53     def copy(self):
           54         c = self.vector_like()
           55         c._core.copy_from(self._core)
           56         return c
           57 
           58     def vector_like(self):
           59         if self.dof == 1:
           60             c = PISM.IceModelVec2S()
           61         else:
           62             c = PISM.IceModelVec2V()
           63         c.create(self.grid, "", PISM.WITH_GHOSTS, self._core.stencil_width())
           64         return PISMLocalVector(c)
           65 
           66     def zero_like(self):
           67         z = self.vector_like()
           68         z._core.set(0.)
           69         return z
           70 
           71     def dim(self):
           72         grid = self._core.grid()
           73         return self.dof * grid.Mx() * grid.My()
           74 
           75     def core(self):
           76         return self._core
           77 
           78     def norm(self, name):
           79 
           80         if name == 'linf':
           81             return self._core.norm(petsc4py.PETSc.NormType.NORM_INFINITY)
           82         if name == 'l2':
           83             return self._core.norm(petsc4py.PETSc.NormType.NORM_2)
           84         if name == 'l1':
           85             return self._core.norm(petsc4py.PETSc.NormType.NORM_1)
           86 
           87         raise ValueError()
           88 
           89 
           90 def pism_logger(message, severity):
           91     """Implements a ``siple`` logger that prints output to the terminal."""
           92     verbosity = severity
           93     PISM.logging.log(message + '\n', verbosity)
           94 
           95 
           96 def pism_pause(message_in=None, message_out=None):
           97     """Implements a ``siple`` pause callback appropriate for parallel runs."""
           98     import sys
           99     import os
          100     fd = sys.stdin.fileno()
          101     com = PISM.Context().com
          102     if os.isatty(fd):
          103         return siple.reporting.std_pause(message_in, message_out)
          104     if not message_in is None:
          105         PISM.verbPrintf(1, com, message_in + "\n")
          106     _ = sys.stdin.read(1)
          107     if not message_out is None:
          108         PISM.verbPrintf(1, com, message_out + "\n")