/usr/lib/python3-escript-mpi/esys/downunder/coordinates.py is in python3-escript-mpi 5.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | ##############################################################################
#
# Copyright (c) 2003-2017 by The University of Queensland
# http://www.uq.edu.au
#
# Primary Business: Queensland, Australia
# Licensed under the Apache License, version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Development until 2012 by Earth Systems Science Computational Center (ESSCC)
# Development 2012-2013 by School of Earth Sciences
# Development from 2014 by Centre for Geoscience Computing (GeoComp)
#
##############################################################################
"""Functions to deal with coordinate systems"""
from __future__ import print_function, division
__copyright__="""Copyright (c) 2003-2017 by The University of Queensland
http://www.uq.edu.au
Primary Business: Queensland, Australia"""
__license__="""Licensed under the Apache License, version 2.0
http://www.apache.org/licenses/LICENSE-2.0"""
__url__="https://launchpad.net/escript-finley"
__all__ = ['ReferenceSystem', 'CartesianReferenceSystem',
'GeodeticReferenceSystem', 'SphericalReferenceSystem',
'WGS84ReferenceSystem', 'GRS80ReferenceSystem',
'SpatialCoordinateTransformation', 'GeodeticCoordinateTransformation',
'CartesianCoordinateTransformation', 'makeTransformation']
from esys.escript import unitsSI as U
import esys.escript as esc
class ReferenceSystem(object):
"""
Generic identifier for coordinate systems.
"""
def __init__(self,name='none'):
"""
initialization of reference system
:param name: name of the reference system
:type name : ``str``
"""
self.__name=name
def getName(self):
"""
returns the name of the reference system
"""
return self.__name
def __str__(self):
return ("%s (id %s)"%(self.getName(),id(self)))
def __eq__(self, other):
return self.isTheSame(other)
def __ne__(self, other):
return not self.isTheSame(other)
def isTheSame(self, other):
"""
test if argument ``other`` defines the same reference system
:param other: a second reference system
:type other: `ReferenceSystem`
:returns: ``True`` if other defines the same reference system
:rtype: ``bool``
.. note:: needs to be overwritten by a particular reference system
"""
raise NotImplementedError()
def isCartesian(self):
"""
returns if the reference system is Cartesian
.. note:: needs to be overwritten by a particular reference system
:rtype: ``bool``
"""
raise NotImplementedError()
def createTransformation(self, domain):
"""
creates an appropriate coordinate transformation on a given domain
.. note:: needs to be overwritten by a particular reference system
:param domain: domain of transformation
:type domain: `esys.escript.AbstractDomain`
:rtype: `SpatialCoordinateTransformation`
"""
raise NotImplementedError()
class CartesianReferenceSystem(ReferenceSystem):
"""
Identifies the Cartesian coordinate system
"""
def __init__(self, name="CARTESIAN"):
"""
set up Cartesian coordinate system
"""
super(CartesianReferenceSystem, self).__init__(name)
def isTheSame(self, other):
"""
test if argument ``other`` defines the same reference system
:param other: a second reference system
:type other: `ReferenceSystem`
:returns: ``True`` if ``other`` is a `CartesianReferenceSystem` instance.
:rtype: ``bool``
:note: every two `CartesianReferenceSystem` instances are considered
as being the same.
"""
return isinstance(other, CartesianReferenceSystem)
def createTransformation(self, domain):
"""
creates an appropriate coordinate transformation on a given domain
:param domain: domain of transformation
:type domain: `esys.escript.AbstractDomain`
:rtype: `SpatialCoordinateTransformation`
"""
return SpatialCoordinateTransformation(domain, reference=self)
def isCartesian(self):
"""
returns if the reference system is Cartesian
:rtype: ``bool``
"""
return True
class GeodeticReferenceSystem(ReferenceSystem):
"""
Identifies a Geodetic coordinate system
"""
def __init__(self, a=6378137.0 *U.m, f=1/298.257223563, angular_unit=1.*U.DEG, height_unit=1.*U.km, name="WGS84"):
"""
initializes a geodetic reference system
:param a: semi-major axis in meter
:type a: positive ``double``
:param f: flattening
:type f: non-negative ``double``, less than one
:param name: name of the reference system
:type name: ``str``
:param angular_unit: factor to scale the unit of latitude and
longitude to radians.
:type angular_unit: positive ``double``
:param height_unit: factor to scale the unit of latitude and
longitude to radians.
:type height_unit: positive ``double``
"""
if not a>0:
raise ValueError("length of semi-major axis a must be positive.")
if not ( f>=0 and f<1 ):
raise ValueError("flattening f must be non-negative and less than one.")
if not angular_unit > 0:
raise ValueError("angular_unit must be positive.")
if not height_unit > 0:
raise ValueError("height_unit must be positive.")
super(GeodeticReferenceSystem, self).__init__(name)
self.__a=a
self.__f=f
self.__angular_unit=angular_unit
self.__height_unit=height_unit
def isCartesian(self):
"""
returns if the reference system is Cartesian
:rtype: ``bool``
"""
return False
def getAngularUnit(self):
"""
returns the angular unit
"""
return self.__angular_unit
def getHeightUnit(self):
"""
returns the height unit
"""
return self.__height_unit
def getSemiMajorAxis(self):
"""
returns the length of semi major axis
"""
return self.__a
def getSemiMinorAxis(self):
"""
returns the length of semi minor axis
"""
a=self.getSemiMajorAxis()
f=self.getFlattening()
return a*(1-f)
def getFlattening(self):
"""
returns the flattening
"""
return self.__f
def isTheSame(self, other):
"""
test if ``other`` defines the same reference system
:param other: a second reference system
:type other: `ReferenceSystem`
:returns: ``True`` if other defines then same reference system
:rtype: ``bool``
.. note:: two `GeodeticReferenceSystem` are considered to be the same
if the use the same semi major axis, the same flattening
and the same angular unit.
"""
if isinstance(other, GeodeticReferenceSystem):
if self.getSemiMajorAxis() == other.getSemiMajorAxis() \
and self.getFlattening() == other.getFlattening() \
and self.getAngularUnit() == other.getAngularUnit():
return True
else:
return False
else:
return False
def createTransformation(self, domain):
"""
creates an appropriate coordinate transformation on a given domain
:param domain: domain of transformation
:type domain: `esys.escript.AbstractDomain`
:rtype: `SpatialCoordinateTransformation`
"""
return GeodeticCoordinateTransformation(domain, reference=self)
def SphericalReferenceSystem(R=6378137.0*U.m):
"""
returns the `GeodeticReferenceSystem` of a sphere
:param R: sphere radius
:type R: positive ``double``
"""
return GeodeticReferenceSystem(a=R, f=0, angular_unit=1*U.DEG, height_unit=1.*U.km, name="SPHERE")
def WGS84ReferenceSystem():
"""
returns the `GeodeticReferenceSystem` for the WGS84 Ellipsoid
"""
return GeodeticReferenceSystem(a=6378137.0 *U.m, f=1/298.257223563, angular_unit=1*U.DEG, height_unit=100.*U.km, name="WGS84")
def GRS80ReferenceSystem():
"""
returns the `GeodeticReferenceSystem` for the GRS80 Ellipsoid eg. used by Geocentric Datum of Australia GDA94
"""
return GeodeticReferenceSystem(a=6378137.0 *U.m, f=1/298.257222101, angular_unit=1*U.DEG, height_unit=1.*U.km, name="GRS80")
class SpatialCoordinateTransformation(object):
"""
Defines an orthogonal coordinate transformation from a domain into the
Cartesian domain using a coordinate transformation.
The default implementation is the identity transformation (i.e.
no changes are applied to the domain). Overwrite the appropriate
methods to define other coordinate systems.
"""
def __init__(self, domain, reference=CartesianReferenceSystem()):
"""
set up the orthogonal coordinate transformation.
:param domain: domain in the domain of the coordinate transformation
:type domain: `esys.escript.AbstractDomain`
:param reference: the reference system
:type reference: `ReferenceSystem`
"""
self.__domain = domain
self.__reference_system=reference
self._volumefactor=esc.Scalar(1., esc.Function(domain))
self._scaling_factors = esc.Vector(1., esc.Function(domain))
def __eq__(self, other):
return self.isTheSame(other)
def __ne__(self, other):
return not self.isTheSame(other)
def isTheSame(self, other):
"""
test if argument ``other`` defines the same coordinate transformation
:param other: a second coordinate transformation
:type other: `SpatialCoordinateTransformation`
:returns: ``True`` if other defines then same coordinate transformation
:rtype: ``bool``
"""
if isinstance(other, SpatialCoordinateTransformation):
if self.getDomain() == other.getDomain() \
and self.getReferenceSystem() == other.getReferenceSystem():
return True
return False
def isCartesian(self):
"""
returns ``True`` if the scaling factors (and the volume factor) are equal to 1
:rtype: ``bool``
"""
return self.__reference_system.isCartesian()
def getDomain(self):
"""
returns the domain of the coordinate transformation.
:rtype: `esys.escript.AbstractDomain`
"""
return self.__domain
def getReferenceSystem(self):
"""
returns the reference system used to to define the coordinate transformation
:rtype: `ReferenceSystem`
"""
return self.__reference_system
def getVolumeFactor(self):
"""
returns the volume factor for the coordinate transformation
:rtype: `esys.escript.Scalar`
"""
return self._volumefactor
def getScalingFactors(self):
"""
returns the scaling factors
:rtype: `esys.escript.Vector`
"""
return self._scaling_factors
def getGradient(self, u):
"""
returns the gradient of a scalar function in direction of the
coordinate axis.
:rtype: `esys.escript.Vector
"""
g=esc.grad(u)
if not self.isCartesian():
d=self.getScalingFactors()
g*=d
return g
def CartesianCoordinateTransformation(domain, reference=CartesianReferenceSystem() ):
return SpatialCoordinateTransformation(domain, reference)
class GeodeticCoordinateTransformation(SpatialCoordinateTransformation):
"""
A geodetic coordinate transformation
"""
def __init__(self, domain, reference=WGS84ReferenceSystem() ):
"""
set up the orthogonal coordinate transformation.
:param domain: domain in the domain of the coordinate transformation
:type domain: `esys.escript.AbstractDomain`
:param reference: the reference system
:type reference: `ReferenceSystem`
"""
DIM=domain.getDim()
super(GeodeticCoordinateTransformation, self).__init__(domain, reference )
a=reference.getSemiMajorAxis()
f=reference.getFlattening()
f_a=reference.getAngularUnit()
f_h=reference.getHeightUnit()
x=esc.Function(domain).getX()
if DIM == 2:
phi=0.
else:
phi=x[1] * f_a
h=x[DIM-1] * f_h
e = esc.sqrt(2*f-f**2)
N = a/esc.sqrt(1 - e**2 * esc.sin(phi)**2 )
M = ( a*(1-e**2) ) /esc.sqrt(1 - e**2 * esc.sin(phi)**2 )**3
v_phi = f_a * (M + h)
v_lam = f_a * (N + h) * esc.cos(phi)
v_h = f_h
s= esc.Vector(1., esc.Function(domain))
if DIM == 2:
v= v_phi * v_h
s[0]=1/v_lam
s[1]=1/v_h
else:
v= v_phi * v_lam * v_h
s[0]=1/v_lam
s[1]=1/v_phi
s[2]=1/v_h
self._volumefactor=v
self._scaling_factors = s
def makeTransformation(domain, coordinates=None):
"""
returns a `SpatialCoordinateTransformation` for the given domain
:param domain: domain in the domain of the coordinate transformation
:type domain: `esys.escript.AbstractDomain`
:param coordinates: the reference system or spatial coordinate system.
:type coordinates: `ReferenceSystem` or `SpatialCoordinateTransformation`
:return: the spatial coordinate system for the given domain of the specified
reference system ``coordinates``. If ``coordinates`` is already spatial coordinate system based on the
riven domain ``coordinates`` is returned. Otherwise an appropriate spatial coordinate system
is created.
:rtype: `SpatialCoordinateTransformation`
"""
if coordinates == None:
return CartesianCoordinateTransformation(domain)
elif isinstance(coordinates, ReferenceSystem):
return coordinates.createTransformation(domain)
else:
if not coordinates.getDomain() == domain:
raise ValueError("Domain of spatial coordinate system and given domain don't match.")
else:
return coordinates
|