-
Notifications
You must be signed in to change notification settings - Fork 222
/
constant.py
111 lines (91 loc) · 2.98 KB
/
constant.py
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
import numpy as np
from devito.exceptions import InvalidArgument
from devito.logger import warning
from devito.types.args import ArgProvider
from devito.types.basic import DataSymbol
__all__ = ['Constant']
class Constant(DataSymbol, ArgProvider):
"""
Symbol representing a constant, scalar value in symbolic equations.
A Constant carries a scalar value.
Parameters
----------
name : str
Name of the symbol.
dtype : data-type, optional
Any object that can be interpreted as a numpy data type. Defaults
to ``np.float32``.
Examples
--------
>>> from devito import Constant
>>> c = Constant(name='c')
>>> c
c
>>> c.data
0.0
>>> c.data = 4
>>> c.data
4.0
Notes
-----
The parameters must always be given as keyword arguments, since SymPy
uses ``*args`` to (re-)create the dimension arguments of the symbolic object.
"""
is_Input = True
is_Constant = True
is_Scalar = True
def __init_finalize__(self, *args, **kwargs):
self._value = kwargs.get('value', 0)
@classmethod
def __dtype_setup__(cls, **kwargs):
return kwargs.get('dtype', np.float32)
@property
def is_const(self):
return True
@property
def data(self):
"""The value of the data object, as a scalar (int, float, ...)."""
return self.dtype(self._value)
@data.setter
def data(self, val):
self._value = val
@property
def _arg_names(self):
"""Tuple of argument names introduced by this symbol."""
return (self.name,)
def _arg_defaults(self, alias=None):
"""A map of default argument values defined by this symbol."""
key = alias or self
return {key.name: self.data}
def _arg_values(self, **kwargs):
"""
Produce a map of argument values after evaluating user input. If no
user input is provided, return a default value.
Parameters
----------
**kwargs
Dictionary of user-provided argument overrides.
"""
if self.name in kwargs:
new = kwargs.pop(self.name)
if isinstance(new, Constant):
return new._arg_defaults(alias=self)
else:
return {self.name: new}
else:
return self._arg_defaults()
def _arg_check(self, args, intervals):
"""
Check that ``args`` contains legal runtime values bound to ``self``.
"""
if self.name not in args:
raise InvalidArgument("No runtime value for %s" % self.name)
key = args[self.name]
try:
# Might be a plain number, w/o a dtype field
if key.dtype != self.dtype:
warning("Data type %s of runtime value `%s` does not match the "
"Constant data type %s" % (key.dtype, self.name, self.dtype))
except AttributeError:
pass
_pickle_kwargs = DataSymbol._pickle_kwargs + ['_value']