-
Notifications
You must be signed in to change notification settings - Fork 147
/
surfactantConvectionVariable.py
executable file
·149 lines (114 loc) · 5.5 KB
/
surfactantConvectionVariable.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
#!/usr/bin/env python
##
# -*-Pyth-*-
# ###################################################################
# FiPy - Python-based finite volume PDE solver
#
# FILE: "surfactantConvectionVariable.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__ = ['SurfactantConvectionVariable']
from fipy.tools.numerix import MA
from fipy.tools import numerix
from fipy.tools import vector
from fipy.variables.faceVariable import FaceVariable
class SurfactantConvectionVariable(FaceVariable):
"""
Convection coefficient for the `ConservativeSurfactantEquation`.
The coeff only has a value for a negative `distanceVar`.
"""
def __init__(self, distanceVar):
"""
Simple one dimensional test:
>>> from fipy.variables.cellVariable import CellVariable
>>> from fipy.meshes import Grid2D
>>> mesh = Grid2D(nx = 3, ny = 1, dx = 1., dy = 1.)
>>> from fipy.variables.distanceVariable import DistanceVariable
>>> distanceVar = DistanceVariable(mesh, value = (-.5, .5, 1.5))
>>> ## answer = numerix.zeros((2, mesh.numberOfFaces),'d')
>>> answer = FaceVariable(mesh=mesh, rank=1, value=0.).globalValue
>>> answer[0,7] = -1
>>> print numerix.allclose(SurfactantConvectionVariable(distanceVar).globalValue, answer)
True
Change the dimensions:
>>> mesh = Grid2D(nx = 3, ny = 1, dx = .5, dy = .25)
>>> distanceVar = DistanceVariable(mesh, value = (-.25, .25, .75))
>>> answer[0,7] = -.5
>>> print numerix.allclose(SurfactantConvectionVariable(distanceVar).globalValue, answer)
True
Two dimensional example:
>>> mesh = Grid2D(nx = 2, ny = 2, dx = 1., dy = 1.)
>>> distanceVar = DistanceVariable(mesh, value = (-1.5, -.5, -.5, .5))
>>> answer = FaceVariable(mesh=mesh, rank=1, value=0.).globalValue
>>> answer[1,2] = -.5
>>> answer[1,3] = -1
>>> answer[0,7] = -.5
>>> answer[0,10] = -1
>>> print numerix.allclose(SurfactantConvectionVariable(distanceVar).globalValue, answer)
True
Larger grid:
>>> mesh = Grid2D(nx = 3, ny = 3, dx = 1., dy = 1.)
>>> distanceVar = DistanceVariable(mesh, value = (1.5, .5 , 1.5,
... .5 , -.5, .5 ,
... 1.5, .5 , 1.5))
>>> answer = FaceVariable(mesh=mesh, rank=1, value=0.).globalValue
>>> answer[1,4] = .25
>>> answer[1,7] = -.25
>>> answer[0,17] = .25
>>> answer[0,18] = -.25
>>> print numerix.allclose(SurfactantConvectionVariable(distanceVar).globalValue, answer)
True
"""
FaceVariable.__init__(self, mesh=distanceVar.mesh, name='surfactant convection', rank=1)
self.distanceVar = self._requires(distanceVar)
def _calcValue(self):
Nfaces = self.mesh.numberOfFaces
M = self.mesh._maxFacesPerCell
dim = self.mesh.dim
cellFaceIDs = self.mesh.cellFaceIDs
faceNormalAreas = self.distanceVar._levelSetNormals * self.mesh._faceAreas
cellFaceNormalAreas = numerix.array(MA.filled(numerix.take(faceNormalAreas, cellFaceIDs, axis=-1), 0))
norms = numerix.array(MA.filled(MA.array(self.mesh._cellNormals), 0))
alpha = numerix.dot(cellFaceNormalAreas, norms)
alpha = numerix.where(alpha > 0, alpha, 0)
alphasum = numerix.sum(alpha, axis=0)
alphasum += (alphasum < 1e-100) * 1.0
alpha = alpha / alphasum
phi = numerix.repeat(self.distanceVar[numerix.newaxis, ...], M, axis=0)
alpha = numerix.where(phi > 0., 0, alpha)
volumes = numerix.array(self.mesh.cellVolumes)
alpha = alpha * volumes * norms
value = numerix.zeros((dim, Nfaces),'d')
vector._putAdd(value, cellFaceIDs, alpha, mask=MA.getmask(MA.array(cellFaceIDs)))
## value = numerix.reshape(value, (dim, Nfaces, dim))
return -value / self.mesh._faceAreas
def _test():
import fipy.tests.doctestPlus
return fipy.tests.doctestPlus.testmod()
if __name__ == "__main__":
_test()