t__init__.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
HTML git clone git://src.adamsgaard.dk/pism
DIR Log
DIR Files
DIR Refs
DIR LICENSE
---
t__init__.py (5252B)
---
1 # Copyright (C) 2011, 2014, 2015, 2016, 2018, 2019 David Maxwell and Constantine Khrulev
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 """PISM's Python bindings and SSA inversions tools."""
20
21 # See if we have been imported by sphinx. If so, we won't import any
22 # packages that are not needed just to compile documentation.
23 imported_from_sphinx = False
24 import inspect
25 caller_mod = inspect.getmodule(inspect.currentframe().f_back)
26 if (caller_mod is not None) and caller_mod.__name__.startswith("sphinx"):
27 imported_from_sphinx = True # pragma: no cover
28
29 def import_symbols(module, namespace):
30 "Import symbols from a module into the namespace, excluding SWIG internals."
31 for name in dir(module):
32 if name.startswith("_") or name.endswith("_swigregister"):
33 continue
34 else:
35 namespace[name] = getattr(module, name)
36
37 if not imported_from_sphinx:
38 import petsc4py
39
40 try:
41 # Look if petsc4py has already been initialized
42 PETSc = petsc4py.__getattribute__('PETSc')
43 except AttributeError:
44 # If not, initialize petsc4py with the PETSc that PISM was compiled against.
45 import sys
46 import PISM.version_info
47 try:
48 petsc4py.init(sys.argv, arch=PISM.version_info.PETSC_ARCH)
49 except TypeError:
50 # petsc4py on Debian 9 does not recognize the PETSC_ARCH of PETSc in the .deb package
51 petsc4py.init(sys.argv)
52 from petsc4py import PETSc
53
54 import PISM.cpp
55
56 import_symbols(PISM.cpp, globals())
57
58 else: # pragma: no cover
59 # The following constants will be imported from 'cpp' if we are not
60 # running inside sphinx. But if we are inside sphinx, then we'll
61 # need them to be able to import submodules.
62 WITH_GHOSTS = True
63 WITHOUT_GHOSTS = False
64 SSAFEM = None
65 SSAFD = None
66 SIAFD = None
67 IPDesignVariableParamIdent = None
68 IPDesignVariableParamSquare = None
69 IPDesignVariableParamExp = None
70 IPDesignVariableParamTruncatedIdent = None
71 IP_SSATaucForwardProblem = None
72 IP_SSAHardavForwardProblem = None
73
74 class IP_SSATaucTaoTikhonovProblemLCLListener(object):
75 "Defined to build Sphinx docs."
76 pass
77
78 class IP_SSATaucTaoTikhonovProblemListener(object):
79 "Defined to build Sphinx docs."
80 pass
81
82 class IP_SSAHardavTaoTikhonovProblemListener(object):
83 "Defined to build Sphinx docs."
84 pass
85
86 import PISM.util
87 import PISM.vec
88 import PISM.ssa
89 import PISM.sia
90 import PISM.logging
91
92
93 class Context(object):
94
95 """Maintains PISM data that needs to exist only once per processor.
96
97 * ``com`` an MPI Comm
98 * ``rank`` the MPI rank of the current processor
99 * ``size`` the number of processors
100 * ``config`` an :cpp:class:`Config`
101
102 There is only ever one :class:`Context`. If you make another one, you'll get
103 the first one. You obtain the singleton as so::
104
105 context = PISM.Context()
106 """
107
108 # Implement a Singleton pattern by overriding __new__
109 _instance = None
110 ctx = None
111
112 config = None
113 unit_system = None
114 enthalpy_converter = None
115 log = None
116 time = None
117 com = None
118 rank = None
119 size = None
120
121 def __new__(cls):
122 if cls._instance is None:
123 cls._instance = super(Context, cls).__new__(cls)
124 cls._instance.__init_once__()
125 return cls._instance
126
127 # Since __init__ is always called after __new__, we don't
128 # want to put code that only gets run once in __init__.
129 def __init_once__(self):
130 ctx = PISM.context_from_options(self.com, "python")
131 self.ctx = ctx
132
133 self.com = ctx.com()
134 self.rank = ctx.rank()
135 self.size = ctx.size()
136
137 self.config = ctx.config()
138 self.unit_system = ctx.unit_system()
139 self.enthalpy_converter = ctx.enthalpy_converter()
140 self.log = ctx.log()
141 self.time = ctx.time()
142
143 class AlgorithmFailureException(Exception):
144
145 """Python exception wrapping a PISM :cpp:class:`TerminationReason`"""
146
147 def __init__(self, reason):
148 """:param reason: a :cpp:class:`TerminationReason`"""
149 Exception.__init__(self)
150 self._reason = reason
151
152 def __str__(self):
153 return self._reason
154
155 def reason(self):
156 "Return the stored TerminationReason."
157 return self._reason
158
159
160 def verbPrintf(verbosity, com, msg, *args):
161 """Mimics PISM's :cpp:func:`verbPrintf` but does formatting on the python side."""
162 if len(args) > 0:
163 msg = msg % args
164 log = PISM.Context().log
165 log.message(verbosity, msg)