/usr/lib/python3/dist-packages/ufl/differentiation.py is in python3-ufl 2016.2.0-2.
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 | # -*- coding: utf-8 -*-
"Differential operators."
# Copyright (C) 2008-2016 Martin Sandve Alnæs
#
# This file is part of UFL.
#
# UFL is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with UFL. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Anders Logg, 2009.
from ufl.log import error
from ufl.utils.py23 import as_native_strings
from ufl.core.expr import Expr
from ufl.core.terminal import Terminal
from ufl.core.operator import Operator
from ufl.core.ufl_type import ufl_type
from ufl.exprcontainers import ExprList, ExprMapping
from ufl.constantvalue import Zero
from ufl.coefficient import Coefficient
from ufl.variable import Variable
from ufl.precedence import parstr
from ufl.domain import find_geometric_dimension
from ufl.checks import is_cellwise_constant
# --- Basic differentiation objects ---
@ufl_type(is_abstract=True,
is_differential=True)
class Derivative(Operator):
"Base class for all derivative types."
__slots__ = ()
def __init__(self, operands):
Operator.__init__(self, operands)
@ufl_type(num_ops=4, inherit_shape_from_operand=0,
inherit_indices_from_operand=0)
class CoefficientDerivative(Derivative):
"""Derivative of the integrand of a form w.r.t. the
degrees of freedom in a discrete Coefficient."""
__slots__ = ()
def __new__(cls, integrand, coefficients, arguments,
coefficient_derivatives):
if not isinstance(coefficients, ExprList):
error("Expecting ExprList instance with Coefficients.")
if not isinstance(arguments, ExprList):
error("Expecting ExprList instance with Arguments.")
if not isinstance(coefficient_derivatives, ExprMapping):
error("Expecting ExprMapping for coefficient derivatives.")
if isinstance(integrand, Zero):
return integrand
return Derivative.__new__(cls)
def __init__(self, integrand, coefficients, arguments,
coefficient_derivatives):
if not isinstance(coefficient_derivatives, ExprMapping):
coefficient_derivatives = ExprMapping(coefficient_derivatives)
Derivative.__init__(self, (integrand, coefficients, arguments,
coefficient_derivatives))
def __str__(self):
return "d/dfj { %s }, with fh=%s, dfh/dfj = %s, and coefficient derivatives %s"\
% (self.ufl_operands[0], self.ufl_operands[1],
self.ufl_operands[2], self.ufl_operands[3])
@ufl_type(num_ops=2)
class VariableDerivative(Derivative):
__slots__ = as_native_strings((
"ufl_shape",
"ufl_free_indices",
"ufl_index_dimensions",
))
def __new__(cls, f, v):
# Checks
if not isinstance(f, Expr):
error("Expecting an Expr in VariableDerivative.")
if not isinstance(v, (Variable, Coefficient)):
error("Expecting a Variable in VariableDerivative.")
if v.ufl_free_indices:
error("Differentiation variable cannot have free indices.")
# Simplification
# Return zero if expression is trivially independent of variable
if f._ufl_is_terminal_ and f != v:
return Zero(f.ufl_shape + v.ufl_shape, f.ufl_free_indices,
f.ufl_index_dimensions)
# Construction
return Derivative.__new__(cls)
def __init__(self, f, v):
Derivative.__init__(self, (f, v))
self.ufl_free_indices = f.ufl_free_indices
self.ufl_index_dimensions = f.ufl_index_dimensions
self.ufl_shape = f.ufl_shape + v.ufl_shape
def __str__(self):
if isinstance(self.ufl_operands[0], Terminal):
return "d%s/d[%s]" % (self.ufl_operands[0], self.ufl_operands[1])
return "d/d[%s] %s" % (self.ufl_operands[1],
parstr(self.ufl_operands[0], self))
# --- Compound differentiation objects ---
@ufl_type(is_abstract=True)
class CompoundDerivative(Derivative):
"Base class for all compound derivative types."
__slots__ = ()
def __init__(self, operands):
Derivative.__init__(self, operands)
@ufl_type(num_ops=1, inherit_indices_from_operand=0, is_terminal_modifier=True)
class Grad(CompoundDerivative):
__slots__ = as_native_strings(("_dim",))
def __new__(cls, f):
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
dim = find_geometric_dimension(f)
return Zero(f.ufl_shape + (dim,), f.ufl_free_indices,
f.ufl_index_dimensions)
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
self._dim = find_geometric_dimension(f)
def _ufl_expr_reconstruct_(self, op):
"Return a new object of the same type with new operands."
if is_cellwise_constant(op):
if op.ufl_shape != self.ufl_operands[0].ufl_shape:
error("Operand shape mismatch in Grad reconstruct.")
if self.ufl_operands[0].ufl_free_indices != op.ufl_free_indices:
error("Free index mismatch in Grad reconstruct.")
return Zero(self.ufl_shape, self.ufl_free_indices,
self.ufl_index_dimensions)
return self._ufl_class_(op)
def evaluate(self, x, mapping, component, index_values, derivatives=()):
"Get child from mapping and return the component asked for."
component, i = component[:-1], component[-1]
derivatives = derivatives + (i,)
result = self.ufl_operands[0].evaluate(x, mapping, component,
index_values,
derivatives=derivatives)
return result
@property
def ufl_shape(self):
return self.ufl_operands[0].ufl_shape + (self._dim,)
def __str__(self):
return "grad(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0, is_terminal_modifier=True,
is_in_reference_frame=True)
class ReferenceGrad(CompoundDerivative):
__slots__ = as_native_strings(("_dim",))
def __new__(cls, f):
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
dim = f.ufl_domain().topological_dimension()
return Zero(f.ufl_shape + (dim,), f.ufl_free_indices,
f.ufl_index_dimensions)
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
self._dim = f.ufl_domain().topological_dimension()
def _ufl_expr_reconstruct_(self, op):
"Return a new object of the same type with new operands."
if is_cellwise_constant(op):
if op.ufl_shape != self.ufl_operands[0].ufl_shape:
error("Operand shape mismatch in ReferenceGrad reconstruct.")
if self.ufl_operands[0].ufl_free_indices != op.ufl_free_indices:
error("Free index mismatch in ReferenceGrad reconstruct.")
return Zero(self.ufl_shape, self.ufl_free_indices,
self.ufl_index_dimensions)
return self._ufl_class_(op)
def evaluate(self, x, mapping, component, index_values, derivatives=()):
"Get child from mapping and return the component asked for."
component, i = component[:-1], component[-1]
derivatives = derivatives + (i,)
result = self.ufl_operands[0].evaluate(x, mapping, component,
index_values,
derivatives=derivatives)
return result
@property
def ufl_shape(self):
return self.ufl_operands[0].ufl_shape + (self._dim,)
def __str__(self):
return "reference_grad(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0, is_terminal_modifier=True)
class Div(CompoundDerivative):
__slots__ = ()
def __new__(cls, f):
if f.ufl_free_indices:
error("Free indices in the divergence argument is not allowed.")
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
return Zero(f.ufl_shape[:-1]) # No free indices asserted above
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
@property
def ufl_shape(self):
return self.ufl_operands[0].ufl_shape[:-1]
def __str__(self):
return "div(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0, is_terminal_modifier=True,
is_in_reference_frame=True)
class ReferenceDiv(CompoundDerivative):
__slots__ = ()
def __new__(cls, f):
if f.ufl_free_indices:
error("Free indices in the divergence argument is not allowed.")
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
return Zero(f.ufl_shape[:-1]) # No free indices asserted above
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
@property
def ufl_shape(self):
return self.ufl_operands[0].ufl_shape[:-1]
def __str__(self):
return "reference_div(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0)
class NablaGrad(CompoundDerivative):
__slots__ = as_native_strings(("_dim",))
def __new__(cls, f):
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
dim = find_geometric_dimension(f)
return Zero((dim,) + f.ufl_shape, f.ufl_free_indices,
f.ufl_index_dimensions)
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
self._dim = find_geometric_dimension(f)
def _ufl_expr_reconstruct_(self, op):
"Return a new object of the same type with new operands."
if is_cellwise_constant(op):
if op.ufl_shape != self.ufl_operands[0].ufl_shape:
error("Operand shape mismatch in NablaGrad reconstruct.")
if self.ufl_operands[0].ufl_free_indices != op.ufl_free_indices:
error("Free index mismatch in NablaGrad reconstruct.")
return Zero(self.ufl_shape, self.ufl_free_indices,
self.ufl_index_dimensions)
return self._ufl_class_(op)
@property
def ufl_shape(self):
return (self._dim,) + self.ufl_operands[0].ufl_shape
def __str__(self):
return "nabla_grad(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0)
class NablaDiv(CompoundDerivative):
__slots__ = ()
def __new__(cls, f):
if f.ufl_free_indices:
error("Free indices in the divergence argument is not allowed.")
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
return Zero(f.ufl_shape[1:]) # No free indices asserted above
return CompoundDerivative.__new__(cls)
def __init__(self, f):
CompoundDerivative.__init__(self, (f,))
@property
def ufl_shape(self):
return self.ufl_operands[0].ufl_shape[1:]
def __str__(self):
return "nabla_div(%s)" % self.ufl_operands[0]
_curl_shapes = {(): (2,), (2,): (), (3,): (3,)}
@ufl_type(num_ops=1, inherit_indices_from_operand=0, is_terminal_modifier=True)
class Curl(CompoundDerivative):
__slots__ = as_native_strings(("ufl_shape",))
def __new__(cls, f):
# Validate input
sh = f.ufl_shape
if f.ufl_shape not in ((), (2,), (3,)):
error("Expecting a scalar, 2D vector or 3D vector.")
if f.ufl_free_indices:
error("Free indices in the curl argument is not allowed.")
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
sh = {(): (2,), (2,): (), (3,): (3,)}[sh]
return Zero(sh) # No free indices asserted above
return CompoundDerivative.__new__(cls)
def __init__(self, f):
global _curl_shapes
CompoundDerivative.__init__(self, (f,))
self.ufl_shape = _curl_shapes[f.ufl_shape]
def __str__(self):
return "curl(%s)" % self.ufl_operands[0]
@ufl_type(num_ops=1, inherit_indices_from_operand=0,
is_terminal_modifier=True, is_in_reference_frame=True)
class ReferenceCurl(CompoundDerivative):
__slots__ = as_native_strings(("ufl_shape",))
def __new__(cls, f):
# Validate input
sh = f.ufl_shape
if f.ufl_shape not in ((), (2,), (3,)):
error("Expecting a scalar, 2D vector or 3D vector.")
if f.ufl_free_indices:
error("Free indices in the curl argument is not allowed.")
# Return zero if expression is trivially constant
if is_cellwise_constant(f):
sh = {(): (2,), (2,): (), (3,): (3,)}[sh]
return Zero(sh) # No free indices asserted above
return CompoundDerivative.__new__(cls)
def __init__(self, f):
global _curl_shapes
CompoundDerivative.__init__(self, (f,))
self.ufl_shape = _curl_shapes[f.ufl_shape]
def __str__(self):
return "reference_curl(%s)" % self.ufl_operands[0]
|