1 """Representations for given data, such as initial and boundary
2 conditions and source terms."""
3
4 __copyright__ = "Copyright (C) 2007 Andreas Kloeckner"
5
6 __license__ = """
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see U{http://www.gnu.org/licenses/}.
19 """
20
21
22
23
24 import hedge.mesh
33
34 @property
36 return self.value.shape
37
40
47 """Abstract interface for obtaining interpolants of I{time-independent}
48 functions.
49 """
50
52 """Return the volume interpolant of this function with respect to
53 the L{discretization.Discretization} C{discr}.
54 """
55 raise NotImplementedError
56
58 """Return the boundary interpolant of this function with respect to
59 the L{discretization.Discretization} discr at the boundary tagged with C{tag}.
60 """
61 raise NotImplementedError
62
67 """Abstract interface for obtaining interpolants of I{time-dependent}
68 functions.
69 """
70
72 """Return the volume interpolant of this function with respect to
73 the L{discretization.Discretization} discr at time {t}.
74 """
75 raise NotImplementedError
76
78 """Return the boundary interpolant of this function with respect to
79 the L{discretization.Discretization} discr at time C{t} at the boundary tagged with
80 C{tag}.
81 """
82 raise NotImplementedError
83
88 """Adapter for a function M{f(x)} into an L{IGivenFunction}.
89 """
91 """Initialize the caches and store the function C{f}.
92
93 @param f: a function mapping space to a scalar value.
94 If f.target_dimensions exists and equals M{n}, then f maps into an
95 M{n}-dimensional vector space instead.
96 """
97 from weakref import WeakKeyDictionary
98
99 self.f = f
100
101 self.volume_cache = WeakKeyDictionary()
102 self.boundary_cache = WeakKeyDictionary()
103
105 try:
106 return self.volume_cache[discr]
107 except KeyError:
108 result = discr.interpolate_volume_function(self.f)
109 self.volume_cache[discr] = result
110 return result
111
113 try:
114 return self.boundary_cache[discr][tag]
115 except KeyError:
116 tag_cache = self.boundary_cache.setdefault(discr, {})
117 result = discr.interpolate_boundary_function(self.f, tag)
118 tag_cache[tag] = result
119 return result
120
125 """A L{GivenFunction} that has a constant value on all space.
126 """
131
136 """A constant-valued L{GivenFunction}.
137 """
138 - def __init__(self, discr, interpolant):
139 self.discr = discr
140 self.interpolant = interpolant
141
143 if discr != self.discr:
144 raise ValueError, "cross-interpolation between discretizations not supported"
145 return self.interpolant
146
148 if discr != self.discr:
149 raise ValueError, "cross-interpolation between discretizations not supported"
150 return discr.boundarize_volume_field(self.interpolant, tag)
151
157 """Adapts a L{GivenFunction} to have a (formal) time-dependency, being constant
158 over all time.
159 """
162
165
168
175
180 """Adapts an L{IGivenFunction} to have a harmonic time-dependency.
181 """
182 - def __init__(self, gf, omega, phase=0):
183 self.gf = gf
184 self.omega = omega
185 self.phase = phase
186
188 from math import sin
189 return sin(self.omega*t+self.phase)\
190 *self.gf.volume_interpolant(discr)
191
193 from math import sin
194 return sin(self.omega*t+self.phase)\
195 *self.gf.boundary_interpolant(discr, tag)
196
202 """Adapts an L{IGivenFunction} to depend on time by "turning it on"
203 for the time interval [on_time, off_time), and having it be zero
204 the rest of the time.
205 """
206
207 - def __init__(self, gf, on_time=0, off_time=1):
208 self.gf = gf
209 self.on_time = on_time
210 self.off_time = off_time
211 assert on_time <= off_time
212
213
214
222
230
235 """Adapts a function M{f(x,t)} into the L{GivenFunction} framework.
236 """
239
242 """Adapt a function `f(x, el, t) in such a way that it can be fed to
243 `interpolate_*_function()`. In particular, preserve the `shape` attribute.
244 """
245 self.f = f
246 self.t = t
247
248 @property
251
253 return self.f(x, el, self.t)
254
257
260