]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/FEM/R1.hs
f99b3dcace7be32ee7df3ba07a488dc012285ae2
[numerical-analysis.git] / src / FEM / R1.hs
1 {-# LANGUAGE GADTs #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3
4 -- | Finite Element Method (FEM) to solve the problem,
5 --
6 -- -(A(x)*u'(x))' + c(x)*u*(x) = f(x) on (a,b)
7 --
8 -- with one of the two types of boundary conditions,
9 --
10 -- * Dirichlet: u(a) = u(b) = 0
11 --
12 -- * A(a)u'(a) = alpha, A(b)u'(b) = beta
13 --
14 -- if c(x) = 0, then it is assumed that the boundary conditions are
15 -- Dirichlet.
16 --
17 -- The code creates a linear system,
18 --
19 -- (K + M)x = F
20 --
21 -- to be solved for @x@, where @K@ (\"big_K\") is the stiffness
22 -- matrix, @M@ (\"big_M\") is the mass matrix, and @F@ (\"big_F\")
23 -- is the load vector.
24 --
25 -- Warning: until PLU factorization is implemented, we can only
26 -- solve the resulting system if it's positive definite!
27 --
28 module FEM.R1
29 where
30
31 import Algebra.Absolute ( abs )
32 import qualified Algebra.Algebraic as Algebraic ( C )
33 import qualified Algebra.Field as Field ( C )
34 import qualified Algebra.RealField as RealField ( C )
35 import qualified Algebra.ToRational as ToRational ( C )
36 import Data.Vector.Fixed ( Arity, S )
37 import NumericPrelude hiding ( abs )
38 import qualified Prelude as P
39
40 import Integration.Gaussian ( gaussian )
41 import Linear.Matrix (
42 Col,
43 Mat(..),
44 Row,
45 (!!!),
46 construct,
47 dot,
48 element_sum2,
49 fromList,
50 ifoldl2,
51 map2,
52 nrows,
53 rows2,
54 set_idx,
55 toList,
56 transpose,
57 zip2,
58 zipwith2 )
59 import Linear.System ( solve_positive_definite )
60 import Piecewise ( Piecewise(..), evaluate', from_intervals )
61 import Polynomials.Orthogonal ( legendre )
62
63 -- | Dirichlet boundary conditions. Since u(a)=u(b)=0 are fixed,
64 -- there's no additional information conveyed by this type.
65 data Dirichlet a = Dirichlet { domain_dirichlet :: Interval a }
66
67 -- | Neumann boundary conditions. @alpha@ specifies A(a)u'(b) and
68 -- @beta@ specifies A(b)u'(b).
69 data Neumann a =
70 Neumann { domain_neumann :: Interval a,
71 alpha :: a,
72 beta :: a }
73
74 -- | Boundary conditions can be either Dirichlet or Neumann.
75 type BoundaryConditions a = Either (Dirichlet a) (Neumann a)
76
77 type Interval a = (a,a)
78
79 -- | The data for the PDE that we are attempting to solve.
80 data PDE a =
81 PDE {
82 -- | A(x)
83 big_A :: (a -> a),
84 -- | c(x)
85 c :: (a -> a),
86 -- | f(x)
87 f :: (a -> a),
88
89 -- | The boundary conditions. The domain also specifies the
90 -- boundary in R^1.
91 bdy :: BoundaryConditions a }
92
93
94
95 -- | Non-PDE parameters for the finite element method. The additional
96 -- type parameter @n@ should be a type-level representation of the
97 -- largest element in @max_degrees@. It needs to be known statically
98 -- for the dimensions of the pointer matrix. The parameter @l@ is
99 -- the number of global basis functions. It's equal to the number of
100 -- /internal/ mesh nodes (i.e. m-1), plus the sum of (p_i - 1) for
101 -- each p_i in max_degrees.
102 data Params m n l a =
103 Params {
104 -- | A partition of the domain.
105 mesh :: Col m (Interval a),
106
107 -- | A list of the highest-degree polynomial that we will use over
108 -- each interval in our mesh.
109 max_degrees :: Col m Int }
110
111
112 -- | The pointer matrix mapping local basis elements to global
113 -- ones. The (i,j)th entry of the matrix contains the index of the
114 -- global basis function corresponding to the jth local basis
115 -- function over element i.
116 --
117 -- TODO: This works for Dirichlet boundary conditions only.
118 --
119 -- Note that the number of columns in the matrix depends on the
120 --
121 -- Examples:
122 --
123 -- >>> import Data.Vector.Fixed ( N5, N6 )
124 -- >>> import Linear.Matrix ( Col5, fromList )
125 -- >>> import Naturals ( N19 )
126 --
127 -- >>> let p = fromList [[3],[3],[5],[4],[5]] :: Col5 Int
128 -- >>> let mesh = undefined :: Col5 (Int,Int)
129 -- >>> let params = Params mesh p :: Params N5 N5 N19 Int
130 -- >>> let row1 = [0,1,5,6,0,0] :: [Int]
131 -- >>> let row2 = [1,2,7,8,0,0] :: [Int]
132 -- >>> let row3 = [2,3,9,10,11,12] :: [Int]
133 -- >>> let row4 = [3,4,13,14,15,0] :: [Int]
134 -- >>> let row5 = [4,0,16,17,18,19] :: [Int]
135 -- >>> let expected = fromList [row1,row2,row3,row4,row5] :: Mat N5 N6 Int
136 -- >>> pointer params == expected
137 -- True
138 --
139 pointer :: (Arity m, Arity n, Arity l) => Params m n l a -> Mat m (S n) Int
140 pointer params =
141 construct lambda
142 where
143 -- | The number of polynomials in the local basis for element i.
144 numpolys :: Int -> Int
145 numpolys i = ((max_degrees params) !!! (i,0)) + 1
146
147 lambda i j
148
149 -- The first two (linear) basis functions are easy to do.
150 | j < 2 = if i + j >= (nrows $ mesh params) then 0 else i + j
151
152 -- No local basis function exists for this j.
153 | j >= (numpolys i) = 0
154
155 -- The first row we handle as a special case.
156 | i == 0 = (nrows $ mesh params) + j - 2
157
158 -- The first entry for this row not corresponding to one of the
159 -- linear polynomials (which we did in the first guard). This
160 -- grabs the biggest number in the previous row and begins counting
161 -- from there.
162 | j == 2 = (lambda (i-1) (numpolys (i-1) - 1)) + 1
163
164 -- If none of the other cases apply, we can compute our value by
165 -- looking to the left in the matrix.
166 | otherwise = (lambda i (j-1)) + 1
167
168
169
170 -- | An affine transform taking the interval [x1,x2] into [-1,1].
171 --
172 -- Examples:
173 --
174 -- >>> let phi = affine (-6,7)
175 -- >>> phi (-6)
176 -- -1.0
177 -- >>> phi 7
178 -- 1.0
179 --
180 affine :: Field.C a => (a,a) -> (a -> a)
181 affine (x1,x2) x = (fromInteger 2)*(x - x1)/(x2 - x1) - (fromInteger 1)
182
183 -- | The inverse of 'affine'. It should send [-1,1] into [x1,x2].
184 --
185 -- Examples:
186 --
187 -- >>> let phi = affine_inv (-6,7)
188 -- >>> phi (-1)
189 -- -6.0
190 -- >>> phi 1
191 -- 7.0
192 --
193 affine_inv :: Field.C a => (a,a) -> (a -> a)
194 affine_inv (x1,x2) x =
195 x*(x2 - x1)/two + (x1 + x2)/two
196 where
197 two = fromInteger 2
198
199
200 -- * Load vector
201
202 -- | Normalized integrals of orthogonal basis functions over
203 -- [-1,1]. The test case below comes from Sage where the
204 -- orthogonality of the polynomials' derivatives can easily be
205 -- tested.
206 --
207 -- Examples:
208 --
209 -- >>> import qualified Algebra.Absolute as Absolute ( abs )
210 --
211 -- >>> let expected = 2.99624907925257
212 -- >>> let actual = big_N 4 1.5 :: Double
213 -- >>> Absolute.abs (actual - expected) < 1e-12
214 -- True
215 --
216 big_N :: forall a. (Algebraic.C a, RealField.C a) => Integer -> a -> a
217 big_N k x
218 | k < 0 = error "requested a negative basis function"
219 | k == 0 = (one - x) / (fromInteger 2)
220 | k == 1 = (one + x) / (fromInteger 2)
221 | otherwise =
222 coeff * ( legendre k x - legendre (k-2) x )
223 where
224 two = fromInteger 2
225 four = fromInteger 4
226 coeff = one / (sqrt (four*(fromInteger k) - two)) :: a
227
228
229 -- | A matrix containing 'big_N' functions indexed by their
230 -- element/number. Each row in the matrix represents a finite element
231 -- (i.e. an interval in the mesh). Within row @i@, column @j@ contains
232 -- the @j@th 'big_N' basis function.
233 --
234 -- Any given 'big_N' will probably wind up in this matrix multiple
235 -- times; the basis functions don't change depending on the
236 -- interval. Only the /number/ of basis functions does. Computing
237 -- them this way allows us to easily construct a lookup \"table\" of
238 -- the proper dimensions.
239 --
240 -- The second example below relies on the fact that @big_N 3@ and
241 -- @big_N 6@ expand to Legendre polynomials (2,4) and (5,7)
242 -- respectively and so should be orthogonal over [-1,1].
243 --
244 -- Examples:
245 --
246 -- >>> import Data.Vector.Fixed ( N5, N6 )
247 -- >>> import Integration.Gaussian ( gaussian )
248 -- >>> import Linear.Matrix ( Col5, fromList )
249 -- >>> import Naturals ( N19 )
250 --
251 -- >>> let p = fromList [[3],[3],[5],[4],[5]] :: Col5 Int
252 -- >>> let mesh = undefined :: Col5 (Double,Double)
253 -- >>> let params = Params mesh p :: Params N5 N5 N19 Double
254 -- >>> let big_ns = big_N_matrix :: Mat N5 N6 (Double -> Double)
255 -- >>> let n1 = big_ns !!! (1,0)
256 -- >>> let n4 = big_ns !!! (4,0)
257 -- >>> n1 1.5 == n4 1.5
258 -- True
259 -- >>> let n1 = big_ns !!! (1,3)
260 -- >>> let n2 = big_ns !!! (2,4)
261 -- >>> gaussian (\x -> (n1 x) * (n2 x)) < 1e-12
262 -- True
263 --
264 big_N_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
265 => Mat m n (a -> a)
266 big_N_matrix =
267 construct lambda
268 where
269 lambda _ j x = big_N (toInteger j) x
270
271
272 -- | The matrix of (N_i * N_j) functions used in the integrand of
273 -- the mass matrices.
274 big_Ns_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
275 => Mat m n (a -> a)
276 big_Ns_matrix =
277 construct lambda
278 where
279 lambda i j x = (big_N (toInteger i) x) * (big_N (toInteger j) x)
280
281
282 -- | Compute the global load vector F.
283 --
284 -- Examples:
285 --
286 -- >>> import Linear.Matrix ( Col7, frobenius_norm )
287 -- >>> import FEM.R1.Example1 ( pde', params' )
288 --
289 -- >>> let f1 = [0.0418]
290 -- >>> let f2 = [0.0805]
291 -- >>> let f3 = [0.1007]
292 -- >>> let f4 = [-0.0045]
293 -- >>> let f5 = [-0.0332]
294 -- >>> let f6 = [-0.0054]
295 -- >>> let f7 = [-0.0267]
296 -- >>> let expected = fromList [f1,f2,f3,f4,f5,f6,f7] :: Col7 Double
297 -- >>> let actual = big_F pde' params'
298 -- >>> frobenius_norm (actual - expected) < 1e-4
299 -- True
300 --
301 big_F :: forall m n l a.
302 (Arity l, Arity m, Arity n,
303 Algebraic.C a, RealField.C a, ToRational.C a)
304 => PDE a
305 -> Params m n l a
306 -> Col l a
307 big_F pde params =
308 ifoldl2 accum zero (big_N_matrix :: Mat m (S n) (a -> a))
309 where
310 accum :: Int -> Int -> Col l a -> (a -> a) -> Col l a
311 accum i j prev_F this_N =
312 prev_F + this_F
313 where
314 two = fromInteger 2
315 (x1,x2) = (mesh params) !!! (i,0)
316 q = affine_inv (x1,x2)
317 integrand x = ((f pde) (q x)) * (this_N x)
318
319 -- The pointer matrix numbers from 1 so subtract one here to
320 -- get the right index.
321 k = ((pointer params) !!! (i,j)) - 1
322 integral = (gaussian integrand)*(x2 - x1) / two
323 this_F = set_idx zero (k,0) integral
324
325
326 -- * Stiffness matrix
327
328 -- | Derivatives of the 'big_N's, that is, orthogonal basis functions
329 -- over [-1,1]. The test case below comes from Sage where the
330 -- orthogonality of the polynomials' derivatives can easily be
331 -- tested. The indices are shifted by one so that k=0 is the first
332 -- basis function.
333 --
334 -- Examples:
335 --
336 -- >>> import qualified Algebra.Absolute as Absolute ( abs )
337 --
338 -- >>> let expected = 11.5757525403319
339 -- >>> let actual = big_N' 3 1.5 :: Double
340 -- >>> Absolute.abs (actual - expected) < 1e-10
341 -- True
342 --
343 big_N' :: forall a. (Algebraic.C a, RealField.C a) => Integer -> a -> a
344 big_N' k x
345 | k < 0 = error "requested a negative basis function"
346 | k == 0 = negate ( one / (fromInteger 2))
347 | k == 1 = one / (fromInteger 2)
348 | otherwise = coeff * ( legendre k x )
349 where
350 two = fromInteger 2
351 coeff = sqrt ((two*(fromInteger k) + one) / two) :: a
352
353
354 -- | The matrix of (N_i' * N_j') functions used in the integrand of
355 -- the stiffness matrix.
356 big_N's_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
357 => Mat m n (a -> a)
358 big_N's_matrix =
359 construct lambda
360 where
361 lambda i j x = (big_N' (toInteger i) x) * (big_N' (toInteger j) x)
362
363
364 big_K_elem :: forall m n l a b.
365 (Arity l, Arity m, Arity n,
366 Algebraic.C a, RealField.C a, ToRational.C a)
367 => PDE a
368 -> Params m n l a
369 -> Int
370 -> Int
371 -> Mat l l a
372 -> b
373 -> Mat l l a
374 big_K_elem pde params _ k cur_K _ =
375 ifoldl2 accum cur_K (big_N's_matrix :: Mat (S n) (S n) (a -> a))
376 where
377 accum :: Int -> Int -> Mat l l a -> (a -> a) -> Mat l l a
378 accum i j prev_K these_N's =
379 prev_K + this_K
380 where
381 two = fromInteger 2
382 (x1,x2) = (mesh params) !!! (k,0)
383 q = affine_inv (x1,x2)
384 integrand x = ((big_A pde) (q x)) * (these_N's x)
385 -- The pointer matrix numbers from 1 so subtract one below to
386 -- get the right index. The indices i,j have upper bounds
387 -- dependent on the element k. Since we statically create the
388 -- matrix of basis function derivatives, we have to check here
389 -- whether or not i,j exceed the max index.
390 row_idx = ((pointer params) !!! (k,i)) - 1
391 col_idx = ((pointer params) !!! (k,j)) - 1
392 integral = (two/(x2 - x1))* (gaussian integrand)
393 this_K = set_idx zero (row_idx, col_idx) integral
394
395
396
397 -- | Compute the \"big K\" stiffness matrix. There are three
398 -- parameters needed for K, namely i,j,k so a fold over a matrix will
399 -- not do. This little gimmick simulates a three-index fold by doing a
400 -- two-index fold over a row of the proper dimensions.
401 --
402 -- Examples:
403 --
404 -- >>> import Linear.Matrix ( Mat7, frobenius_norm )
405 -- >>> import FEM.R1.Example1 ( pde', params' )
406 --
407 -- >>> let k1 = [6, -3, 0, 0, 0, 0, 0] :: [Double]
408 -- >>> let k2 = [-3, 10.5, -7.5, 0, 0, 0, 0] :: [Double]
409 -- >>> let k3 = [0, -7.5, 12.5, 0, 0, 0, 0] :: [Double]
410 -- >>> let k4 = [0, 0, 0, 6, 0, 0, 0] :: [Double]
411 -- >>> let k5 = [0, 0, 0, 0, 6, 0, 0] :: [Double]
412 -- >>> let k6 = [0, 0, 0, 0, 0, 6, 0] :: [Double]
413 -- >>> let k7 = [0, 0, 0, 0, 0, 0, 15] :: [Double]
414 -- >>> let expected = fromList [k1,k2,k3,k4,k5,k6,k7] :: Mat7 Double
415 -- >>> let actual = big_K pde' params'
416 -- >>> frobenius_norm (actual - expected) < 1e-10
417 -- True
418 --
419 big_K :: forall m n l a.
420 (Arity l, Arity m, Arity n,
421 Algebraic.C a, RealField.C a, ToRational.C a)
422 => PDE a
423 -> Params m n l a
424 -> Mat l l a
425 big_K pde params =
426 ifoldl2 (big_K_elem pde params) zero col_idxs
427 where
428 col_idxs = fromList [map fromInteger [0..]] :: Row m a
429
430
431 -- * Mass matrix
432
433 big_M_elem :: forall m n l a b.
434 (Arity l, Arity m, Arity n,
435 Algebraic.C a, RealField.C a, ToRational.C a)
436 => PDE a
437 -> Params m n l a
438 -> Int
439 -> Int
440 -> Mat l l a
441 -> b
442 -> Mat l l a
443 big_M_elem pde params _ k cur_M _ =
444 ifoldl2 accum cur_M (big_Ns_matrix :: Mat (S n) (S n) (a -> a))
445 where
446 accum :: Int -> Int -> Mat l l a -> (a -> a) -> Mat l l a
447 accum i j prev_M these_Ns =
448 prev_M + this_M
449 where
450 two = fromInteger 2
451 (x1,x2) = (mesh params) !!! (k,0)
452 q = affine_inv (x1,x2)
453 integrand x = ((c pde) (q x)) * (these_Ns x)
454 -- The pointer matrix numbers from 1 so subtract one here to
455 -- get the right index.
456 row_idx = ((pointer params) !!! (k,i)) - 1
457 col_idx = ((pointer params) !!! (k,j)) - 1
458 integral = (x2 - x1)*(gaussian integrand) / two
459 this_M = set_idx zero (row_idx, col_idx) integral
460
461
462 -- | Compute the \"big M\" mass matrix. There are three
463 -- parameters needed for M, namely i,j,k so a fold over a matrix will
464 -- not do. This little gimmick simulates a three-index fold by doing a
465 -- two-index fold over a row of the proper dimensions.
466 --
467 -- Examples:
468 --
469 -- >>> import Linear.Matrix ( Mat7, frobenius_norm )
470 -- >>> import FEM.R1.Example1 ( pde', params' )
471 --
472 -- >>> let m1 = [0.0723,0.0266,0,-0.0135,-0.0305,0.0058,0] :: [Double]
473 -- >>> let m2 = [0.0266,0.0897,0.0149,0,-0.0345,-0.0109,-0.0179] :: [Double]
474 -- >>> let m3 = [0,0.0149,0.0809,0,0,0,-0.0185] :: [Double]
475 -- >>> let m4 = [-0.0135,0,0,0.0110,0,0,0] :: [Double]
476 -- >>> let m5 = [-0.0305,-0.0345,0,0,0.0319,0.0018,0] :: [Double]
477 -- >>> let m6 = [0.0058,-0.0109,0,0,0.0018,0.0076,0] :: [Double]
478 -- >>> let m7 = [0,-0.0179,-0.0185,0,0,0,0.0178] :: [Double]
479 --
480 -- >>> let expected = fromList [m1,m2,m3,m4,m5,m6,m7] :: Mat7 Double
481 -- >>> let actual = big_M pde' params'
482 -- >>> frobenius_norm (actual - expected) < 1e-3
483 -- True
484 --
485 big_M :: forall m n l a.
486 (Arity l, Arity m, Arity n,
487 Algebraic.C a, RealField.C a, ToRational.C a)
488 => PDE a
489 -> Params m n l a
490 -> Mat l l a
491 big_M pde params =
492 ifoldl2 (big_M_elem pde params) zero col_idxs
493 where
494 col_idxs = fromList [map fromInteger [0..]] :: Row m a
495
496
497
498 -- | Determine the coefficient vector @x@ from the system @(K + M)x = F@.
499 --
500 -- Examples:
501 --
502 -- >>> import Linear.Matrix ( Col7, frobenius_norm )
503 -- >>> import FEM.R1.Example1 ( pde', params' )
504 --
505 -- >>> let c1 = [0.02366220347687] :: [Double]
506 -- >>> let c2 = [0.03431630082636] :: [Double]
507 -- >>> let c3 = [0.02841800893264] :: [Double]
508 -- >>> let c4 = [-0.00069489654996] :: [Double]
509 -- >>> let c5 = [-0.00518637005151] :: [Double]
510 -- >>> let c6 = [-0.00085028505337] :: [Double]
511 -- >>> let c7 = [-0.00170478210110] :: [Double]
512 -- >>>
513 -- >>> let expected = fromList [c1,c2,c3,c4,c5,c6,c7] :: Col7 Double
514 -- >>> let actual = coefficients pde' params'
515 -- >>> frobenius_norm (actual - expected) < 1e-8
516 -- True
517 --
518 coefficients :: forall m n l a.
519 (Arity m, Arity n, Arity l,
520 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
521 => PDE a
522 -> Params m n (S l) a
523 -> Col (S l) a
524 coefficients pde params =
525 solve_positive_definite matrix b
526 where
527 matrix = (big_K pde params) + (big_M pde params)
528 b = big_F pde params
529
530
531 solution :: forall m n l a.
532 (Arity m, Arity n, Arity l,
533 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
534 => PDE a
535 -> Params m n (S l) a
536 -> Piecewise a
537 solution pde params =
538 from_intervals $ map head $ toList $ solved_column
539 where
540 global_coeffs :: Col (S l) a
541 global_coeffs = coefficients pde params
542
543 ptr :: Mat m (S n) Int
544 ptr = pointer params
545
546 -- Each mesh element has an associated row in the pointer
547 -- matrix. Stick them together.
548 mesh_with_ptr_rows :: Col m (Interval a, Row (S n) Int)
549 mesh_with_ptr_rows = zip2 (mesh params) (rows2 ptr)
550
551 make_local_coeffs :: (Interval a, Row (S n) Int) -> Row (S n) a
552 make_local_coeffs (_, ptr_row) =
553 construct lambda
554 where
555 lambda _ j = if (ptr_row !!! (0,j)) == zero
556 then zero
557 else global_coeffs !!! ((ptr_row !!! (0,j)) - 1, 0)
558
559 -- Create a column vector for each mesh element containing the global
560 -- coefficients corresponding to that element.
561 local_coeffs :: Col m (Row (S n) a)
562 local_coeffs = map2 make_local_coeffs mesh_with_ptr_rows
563
564 global_basis_functions :: Col (S n) (a -> a)
565 global_basis_functions =
566 construct lambda
567 where lambda i _ = big_N (toInteger i)
568
569 mesh_with_coeffs :: Col m (Interval a, Row (S n) a)
570 mesh_with_coeffs = zip2 (mesh params) local_coeffs
571
572 solved_column :: Col m (Interval a, (a -> a))
573 solved_column = map2 solve_piece $ mesh_with_coeffs
574
575 solve_piece :: (Interval a, Row (S n) a) -> (Interval a, (a -> a))
576 solve_piece (interval, coeffs_row) = (interval, g)
577 where
578 coeffs_col = transpose coeffs_row
579
580 g x = element_sum2 $ zipwith2 combine coeffs_col global_basis_functions
581 where
582 xi = (affine interval) x
583 combine ci ni = ci*(ni xi)
584
585
586 -- energy_true :: (Arity m, Arity n, Arity l,
587 -- Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
588 -- => PDE a
589 -- -> Params m n (S l) a
590 -- -> (a -> a) -- ^ True solution @u@
591 -- -> (a -> a) -- ^ Derivative of true solution @u'@
592 -- -> a
593 -- energy_true pde params u u' =
594 -- case (bdy pde) of
595 -- Left (Dirichlet (x1,x2)) ->
596 -- sqrt $ bilinear_form u u' u u'
597 -- where
598 -- two = fromInteger 2
599 -- q = affine_inv (x1,x2)
600 -- bilinear_form w w' v v' = (x2 - x1)*(gaussian integrand)/two
601 -- where
602 -- integrand x = ((big_A pde) (q x))*(w' (q x))*(v' (q x))
603 -- + ((c pde) (q x))*(w (q x))*(v (q x))
604
605 -- _ -> error "Neumann BCs not implemented."
606
607
608 energy_fem :: (Arity m, Arity n, Arity l,
609 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
610 => PDE a
611 -> Params m n (S l) a
612 -> a
613 energy_fem pde params =
614 (coefficients pde params) `dot` (big_F pde params)
615
616
617 relative_error :: forall m n l a.
618 (Arity m, Arity n, Arity l,
619 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
620 => PDE a
621 -> Params m n (S l) a
622 -> a -- ^ The energy norm of the true solution @u@
623 -> a
624 relative_error pde params energy_true =
625 cent * sqrt(energy_true - (energy_fem pde params)/energy_true)
626 where
627 cent = fromInteger 100
628
629
630
631 relative_error_pointwise :: forall m n l a.
632 (Arity m, Arity n, Arity l,
633 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
634 => PDE a
635 -> Params m n (S l) a
636 -> (a -> a) -- ^ The true solution @u@
637 -> a -- ^ The point @x@ at which to compute the error.
638 -> a
639 relative_error_pointwise pde params u x =
640 cent * ( u_exact - u_fem ) / u_exact
641 where
642 u_exact = abs $ u x
643 u_fem = evaluate' (solution pde params) x
644 cent = fromInteger 100