-
Notifications
You must be signed in to change notification settings - Fork 147
/
coupledCellVariable.py
171 lines (136 loc) · 5.16 KB
/
coupledCellVariable.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
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
#!/usr/bin/env python
## -*-Pyth-*-
# ###################################################################
# FiPy - Python-based finite volume PDE solver
#
# FILE: "coupledCellVariable.py"
#
# Author: Jonathan Guyer <guyer@nist.gov>
# Author: Daniel Wheeler <daniel.wheeler@nist.gov>
# Author: James Warren <jwarren@nist.gov>
# mail: NIST
# www: http://www.ctcms.nist.gov/fipy/
#
# ========================================================================
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. FiPy is an experimental
# system. NIST assumes no responsibility whatsoever for its use by
# other parties, and makes no guarantees, expressed or implied, about
# its quality, reliability, or any other characteristic. We would
# appreciate acknowledgement if the software is used.
#
# This software can be redistributed and/or modified freely
# provided that any derivative works bear some notice that they are
# derived from it, and any modified versions bear some notice that
# they have been modified.
# ========================================================================
#
# ###################################################################
##
__docformat__ = 'restructuredtext'
__all__ = []
from fipy.tools import numerix
from fipy.tools.decorators import getsetDeprecated
class _CoupledCellVariable(object):
def __init__(self, vars):
self.vars = vars
@property
def shape(self):
return (len(self.vars) * self.mesh.numberOfCells,)
@property
def rank(self):
return self.vars[0].rank
def __repr__(self):
return "(" + ", ".join([repr(var) for var in self.vars]) + ")"
@getsetDeprecated
def getMesh(self):
return self.mesh
@property
def mesh(self):
meshes = list(set([var.mesh for var in self.vars]))
if len(meshes) == 0:
raise Exception("There are no Meshes defined")
elif len(meshes) > 1:
raise Exception("All Variables must be defined on the same Mesh")
else:
return meshes[0]
def __getitem__(self, index):
return numerix.concatenate([numerix.array(var[index]) for var in self.vars])
def __setitem__(self, index, value):
N = self.mesh.numberOfCells
for i, var in enumerate(self.vars):
if numerix.shape(value) == ():
var[index] = value
else:
var[index] = value[i * N:(i + 1) * N]
@getsetDeprecated
def getValue(self):
return self.value
@getsetDeprecated
def setValue(self, value):
self.value = value
def _getValue(self):
return numerix.concatenate([numerix.array(var.value) for var in self.vars])
def _setValue(self, value):
self[:] = value
value = property(_getValue, _setValue)
@getsetDeprecated
def getGlobalValue(self):
return self.globalValue
@property
def globalValue(self):
return numerix.concatenate([numerix.array(var.globalValue) for var in self.vars])
@getsetDeprecated
def getNumericValue(self):
return self.numericValue
@property
def numericValue(self):
return numerix.concatenate([var.numericValue for var in self.vars])
@getsetDeprecated
def getUnit(self):
return self.unit
@property
def unit(self):
from fipy.tools.dimensions import physicalField
return physicalField._unity
def __array__(self, t=None):
"""
Attempt to convert the `_CoupledCellVariable` to a numerix `array` object
>>> from fipy import *
>>> mesh = Grid1D(nx=2)
>>> v1 = CellVariable(mesh=mesh, value=[2, 3])
>>> v2 = CellVariable(mesh=mesh, value=[4, 5])
>>> v = _CoupledCellVariable(vars=(v1, v2))
>>> print numerix.allequal([2,3,4,5], numerix.array(v)) # doctest: +PROCESSOR_0
True
>>> v[:] = (6,7,8,9)
>>> print v1
[6 7]
>>> print v2
[8 9]
>>> v.getsctype() == numerix.NUMERIX.obj2sctype(numerix.array(1))
True
"""
return numerix.array(self.value, t)
def __neg__(self):
return _CoupledCellVariable([-var for var in self.vars])
def __abs__(self):
return _CoupledCellVariable([abs(var) for var in self.vars])
def __iter__(self):
return iter(self.value)
def getsctype(self, default=None):
if not hasattr(self, 'typecode'):
self.typecode = numerix.obj2sctype(rep=self.numericValue, default=default)
return self.typecode
def ravel(self):
return self.value.ravel()
def copy(self):
return self.__class__(vars=[var.copy() for var in self.vars])
def _test():
import fipy.tests.doctestPlus
return fipy.tests.doctestPlus.testmod()
if __name__ == "__main__":
_test()