2 {-# LANGUAGE ScopedTypeVariables #-}
4 -- | Finite Element Method (FEM) to solve the problem,
6 -- -(A(x)*u'(x))' + c(x)*u*(x) = f(x) on (a,b)
8 -- with one of the two types of boundary conditions,
10 -- * Dirichlet: u(a) = u(b) = 0
12 -- * A(a)u'(a) = alpha, A(b)u'(b) = beta
14 -- if c(x) = 0, then it is assumed that the boundary conditions are
17 -- The code creates a linear system,
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.
25 -- Warning: until PLU factorization is implemented, we can only
26 -- solve the resulting system if it's positive definite!
31 import qualified Algebra.Algebraic as Algebraic ( C )
32 import qualified Algebra.Field as Field ( C )
33 import qualified Algebra.RealField as RealField ( C )
34 import qualified Algebra.ToRational as ToRational ( C )
35 import Data.Vector.Fixed ( Arity, S )
37 import qualified Prelude as P
39 import Integration.Gaussian ( gaussian )
40 import Linear.Matrix (
57 import Linear.System ( solve_positive_definite )
58 import Piecewise ( Piecewise(..), from_intervals )
59 import Polynomials.Orthogonal ( legendre )
61 -- | Dirichlet boundary conditions. Since u(a)=u(b)=0 are fixed,
62 -- there's no additional information conveyed by this type.
63 data Dirichlet a = Dirichlet { domain_dirichlet :: Interval a }
65 -- | Neumann boundary conditions. @alpha@ specifies A(a)u'(b) and
66 -- @beta@ specifies A(b)u'(b).
68 Neumann { domain_neumann :: Interval a,
72 -- | Boundary conditions can be either Dirichlet or Neumann.
73 type BoundaryConditions a = Either (Dirichlet a) (Neumann a)
75 type Interval a = (a,a)
77 -- | The data for the PDE that we are attempting to solve.
87 -- | The boundary conditions. The domain also specifies the
89 bdy :: BoundaryConditions a }
93 -- | Non-PDE parameters for the finite element method. The additional
94 -- type parameter @n@ should be a type-level representation of the
95 -- largest element in @max_degrees@. It needs to be known statically
96 -- for the dimensions of the pointer matrix. The parameter @l@ is
97 -- the number of global basis functions. It's equal to the number of
98 -- /internal/ mesh nodes (i.e. m-1), plus the sum of (p_i - 1) for
99 -- each p_i in max_degrees.
100 data Params m n l a =
102 -- | A partition of the domain.
103 mesh :: Col m (Interval a),
105 -- | A list of the highest-degree polynomial that we will use over
106 -- each interval in our mesh.
107 max_degrees :: Col m Int }
110 -- | The pointer matrix mapping local basis elements to global
111 -- ones. The (i,j)th entry of the matrix contains the index of the
112 -- global basis function corresponding to the jth local basis
113 -- function over element i.
115 -- TODO: This works for Dirichlet boundary conditions only.
117 -- Note that the number of columns in the matrix depends on the
121 -- >>> import Data.Vector.Fixed ( N5, N6 )
122 -- >>> import Linear.Matrix ( Col5, fromList )
123 -- >>> import Naturals ( N19 )
125 -- >>> let p = fromList [[3],[3],[5],[4],[5]] :: Col5 Int
126 -- >>> let mesh = undefined :: Col5 (Int,Int)
127 -- >>> let params = Params mesh p :: Params N5 N5 N19 Int
128 -- >>> let row1 = [0,1,5,6,0,0] :: [Int]
129 -- >>> let row2 = [1,2,7,8,0,0] :: [Int]
130 -- >>> let row3 = [2,3,9,10,11,12] :: [Int]
131 -- >>> let row4 = [3,4,13,14,15,0] :: [Int]
132 -- >>> let row5 = [4,0,16,17,18,19] :: [Int]
133 -- >>> let expected = fromList [row1,row2,row3,row4,row5] :: Mat N5 N6 Int
134 -- >>> pointer params == expected
137 pointer :: (Arity m, Arity n, Arity l) => Params m n l a -> Mat m (S n) Int
141 -- | The number of polynomials in the local basis for element i.
142 numpolys :: Int -> Int
143 numpolys i = ((max_degrees params) !!! (i,0)) + 1
147 -- The first two (linear) basis functions are easy to do.
148 | j < 2 = if i + j >= (nrows $ mesh params) then 0 else i + j
150 -- No local basis function exists for this j.
151 | j >= (numpolys i) = 0
153 -- The first row we handle as a special case.
154 | i == 0 = (nrows $ mesh params) + j - 2
156 -- The first entry for this row not corresponding to one of the
157 -- linear polynomials (which we did in the first guard). This
158 -- grabs the biggest number in the previous row and begins counting
160 | j == 2 = (lambda (i-1) (numpolys (i-1) - 1)) + 1
162 -- If none of the other cases apply, we can compute our value by
163 -- looking to the left in the matrix.
164 | otherwise = (lambda i (j-1)) + 1
168 -- | An affine transform taking the interval [x1,x2] into [-1,1].
172 -- >>> let phi = affine (-6,7)
178 affine :: Field.C a => (a,a) -> (a -> a)
179 affine (x1,x2) x = (fromInteger 2)*(x - x1)/(x2 - x1) - (fromInteger 1)
181 -- | The inverse of 'affine'. It should send [-1,1] into [x1,x2].
185 -- >>> let phi = affine_inv (-6,7)
191 affine_inv :: Field.C a => (a,a) -> (a -> a)
192 affine_inv (x1,x2) x =
193 x*(x2 - x1)/two + (x1 + x2)/two
200 -- | Normalized integrals of orthogonal basis functions over
201 -- [-1,1]. The test case below comes from Sage where the
202 -- orthogonality of the polynomials' derivatives can easily be
207 -- >>> import qualified Algebra.Absolute as Absolute ( abs )
209 -- >>> let expected = 2.99624907925257
210 -- >>> let actual = big_N 4 1.5 :: Double
211 -- >>> Absolute.abs (actual - expected) < 1e-12
214 big_N :: forall a. (Algebraic.C a, RealField.C a) => Integer -> a -> a
216 | k < 0 = error "requested a negative basis function"
217 | k == 0 = (one - x) / (fromInteger 2)
218 | k == 1 = (one + x) / (fromInteger 2)
220 coeff * ( legendre k x - legendre (k-2) x )
224 coeff = one / (sqrt (four*(fromInteger k) - two)) :: a
227 -- | A matrix containing 'big_N' functions indexed by their
228 -- element/number. Each row in the matrix represents a finite element
229 -- (i.e. an interval in the mesh). Within row @i@, column @j@ contains
230 -- the @j@th 'big_N' basis function.
232 -- Any given 'big_N' will probably wind up in this matrix multiple
233 -- times; the basis functions don't change depending on the
234 -- interval. Only the /number/ of basis functions does. Computing
235 -- them this way allows us to easily construct a lookup \"table\" of
236 -- the proper dimensions.
238 -- The second example below relies on the fact that @big_N 3@ and
239 -- @big_N 6@ expand to Legendre polynomials (2,4) and (5,7)
240 -- respectively and so should be orthogonal over [-1,1].
244 -- >>> import Data.Vector.Fixed ( N5, N6 )
245 -- >>> import Integration.Gaussian ( gaussian )
246 -- >>> import Linear.Matrix ( Col5, fromList )
247 -- >>> import Naturals ( N19 )
249 -- >>> let p = fromList [[3],[3],[5],[4],[5]] :: Col5 Int
250 -- >>> let mesh = undefined :: Col5 (Double,Double)
251 -- >>> let params = Params mesh p :: Params N5 N5 N19 Double
252 -- >>> let big_ns = big_N_matrix :: Mat N5 N6 (Double -> Double)
253 -- >>> let n1 = big_ns !!! (1,0)
254 -- >>> let n4 = big_ns !!! (4,0)
255 -- >>> n1 1.5 == n4 1.5
257 -- >>> let n1 = big_ns !!! (1,3)
258 -- >>> let n2 = big_ns !!! (2,4)
259 -- >>> gaussian (\x -> (n1 x) * (n2 x)) < 1e-12
262 big_N_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
267 lambda _ j x = big_N (toInteger j) x
270 -- | The matrix of (N_i * N_j) functions used in the integrand of
271 -- the mass matrices.
272 big_Ns_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
277 lambda i j x = (big_N (toInteger i) x) * (big_N (toInteger j) x)
280 -- | Compute the global load vector F.
284 -- >>> import Linear.Matrix ( Col7, frobenius_norm )
285 -- >>> import FEM.R1.Example1 ( pde', params' )
287 -- >>> let f1 = [0.0418]
288 -- >>> let f2 = [0.0805]
289 -- >>> let f3 = [0.1007]
290 -- >>> let f4 = [-0.0045]
291 -- >>> let f5 = [-0.0332]
292 -- >>> let f6 = [-0.0054]
293 -- >>> let f7 = [-0.0267]
294 -- >>> let expected = fromList [f1,f2,f3,f4,f5,f6,f7] :: Col7 Double
295 -- >>> let actual = big_F pde' params'
296 -- >>> frobenius_norm (actual - expected) < 1e-4
299 big_F :: forall m n l a.
300 (Arity l, Arity m, Arity n,
301 Algebraic.C a, RealField.C a, ToRational.C a)
306 ifoldl2 accum zero (big_N_matrix :: Mat m (S n) (a -> a))
308 accum :: Int -> Int -> Col l a -> (a -> a) -> Col l a
309 accum i j prev_F this_N =
313 (x1,x2) = (mesh params) !!! (i,0)
314 q = affine_inv (x1,x2)
315 integrand x = ((f pde) (q x)) * (this_N x)
317 -- The pointer matrix numbers from 1 so subtract one here to
318 -- get the right index.
319 k = ((pointer params) !!! (i,j)) - 1
320 integral = (gaussian integrand)*(x2 - x1) / two
321 this_F = set_idx zero (k,0) integral
324 -- * Stiffness matrix
326 -- | Derivatives of the 'big_N's, that is, orthogonal basis functions
327 -- over [-1,1]. The test case below comes from Sage where the
328 -- orthogonality of the polynomials' derivatives can easily be
329 -- tested. The indices are shifted by one so that k=0 is the first
334 -- >>> import qualified Algebra.Absolute as Absolute ( abs )
336 -- >>> let expected = 11.5757525403319
337 -- >>> let actual = big_N' 3 1.5 :: Double
338 -- >>> Absolute.abs (actual - expected) < 1e-10
341 big_N' :: forall a. (Algebraic.C a, RealField.C a) => Integer -> a -> a
343 | k < 0 = error "requested a negative basis function"
344 | k == 0 = negate ( one / (fromInteger 2))
345 | k == 1 = one / (fromInteger 2)
346 | otherwise = coeff * ( legendre k x )
349 coeff = sqrt ((two*(fromInteger k) + one) / two) :: a
352 -- | The matrix of (N_i' * N_j') functions used in the integrand of
353 -- the stiffness matrix.
354 big_N's_matrix :: (Arity m, Arity n, Algebraic.C a, RealField.C a)
359 lambda i j x = (big_N' (toInteger i) x) * (big_N' (toInteger j) x)
362 big_K_elem :: forall m n l a b.
363 (Arity l, Arity m, Arity n,
364 Algebraic.C a, RealField.C a, ToRational.C a)
372 big_K_elem pde params _ k cur_K _ =
373 ifoldl2 accum cur_K (big_N's_matrix :: Mat m (S n) (a -> a))
375 accum :: Int -> Int -> Mat l l a -> (a -> a) -> Mat l l a
376 accum i j prev_K these_N's =
380 (x1,x2) = (mesh params) !!! (k,0)
381 q = affine_inv (x1,x2)
382 integrand x = ((big_A pde) (q x)) * (these_N's x)
383 -- The pointer matrix numbers from 1 so subtract one here to
384 -- get the right index.
385 row_idx = ((pointer params) !!! (k,i)) - 1
386 col_idx = ((pointer params) !!! (k,j)) - 1
387 integral = (two/(x2 - x1))* (gaussian integrand)
388 this_K = set_idx zero (row_idx, col_idx) integral
392 -- | Compute the \"big K\" stiffness matrix. There are three
393 -- parameters needed for K, namely i,j,k so a fold over a matrix will
394 -- not do. This little gimmick simulates a three-index fold by doing a
395 -- two-index fold over a row of the proper dimensions.
399 -- >>> import Linear.Matrix ( Mat7, frobenius_norm )
400 -- >>> import FEM.R1.Example1 ( pde', params' )
402 -- >>> let k1 = [6, -3, 0, 0, 0, 0, 0] :: [Double]
403 -- >>> let k2 = [-3, 10.5, -7.5, 0, 0, 0, 0] :: [Double]
404 -- >>> let k3 = [0, -7.5, 12.5, 0, 0, 0, 0] :: [Double]
405 -- >>> let k4 = [0, 0, 0, 6, 0, 0, 0] :: [Double]
406 -- >>> let k5 = [0, 0, 0, 0, 6, 0, 0] :: [Double]
407 -- >>> let k6 = [0, 0, 0, 0, 0, 6, 0] :: [Double]
408 -- >>> let k7 = [0, 0, 0, 0, 0, 0, 15] :: [Double]
409 -- >>> let expected = fromList [k1,k2,k3,k4,k5,k6,k7] :: Mat7 Double
410 -- >>> let actual = big_K pde' params'
411 -- >>> frobenius_norm (actual - expected) < 1e-10
414 big_K :: forall m n l a.
415 (Arity l, Arity m, Arity n,
416 Algebraic.C a, RealField.C a, ToRational.C a)
421 ifoldl2 (big_K_elem pde params) zero col_idxs
423 col_idxs = fromList [map fromInteger [0..]] :: Row m a
428 big_M_elem :: forall m n l a b.
429 (Arity l, Arity m, Arity n,
430 Algebraic.C a, RealField.C a, ToRational.C a)
438 big_M_elem pde params _ k cur_M _ =
439 ifoldl2 accum cur_M (big_Ns_matrix :: Mat m (S n) (a -> a))
441 accum :: Int -> Int -> Mat l l a -> (a -> a) -> Mat l l a
442 accum i j prev_M these_Ns =
446 (x1,x2) = (mesh params) !!! (k,0)
447 q = affine_inv (x1,x2)
448 integrand x = ((c pde) (q x)) * (these_Ns x)
449 -- The pointer matrix numbers from 1 so subtract one here to
450 -- get the right index.
451 row_idx = ((pointer params) !!! (k,i)) - 1
452 col_idx = ((pointer params) !!! (k,j)) - 1
453 integral = (x2 - x1)*(gaussian integrand) / two
454 this_M = set_idx zero (row_idx, col_idx) integral
457 -- | Compute the \"big M\" mass matrix. There are three
458 -- parameters needed for M, namely i,j,k so a fold over a matrix will
459 -- not do. This little gimmick simulates a three-index fold by doing a
460 -- two-index fold over a row of the proper dimensions.
464 -- >>> import Linear.Matrix ( Mat7, frobenius_norm )
465 -- >>> import FEM.R1.Example1 ( pde', params' )
467 -- >>> let m1 = [0.0723,0.0266,0,-0.0135,-0.0305,0.0058,0] :: [Double]
468 -- >>> let m2 = [0.0266,0.0897,0.0149,0,-0.0345,-0.0109,-0.0179] :: [Double]
469 -- >>> let m3 = [0,0.0149,0.0809,0,0,0,-0.0185] :: [Double]
470 -- >>> let m4 = [-0.0135,0,0,0.0110,0,0,0] :: [Double]
471 -- >>> let m5 = [-0.0305,-0.0345,0,0,0.0319,0.0018,0] :: [Double]
472 -- >>> let m6 = [0.0058,-0.0109,0,0,0.0018,0.0076,0] :: [Double]
473 -- >>> let m7 = [0,-0.0179,-0.0185,0,0,0,0.0178] :: [Double]
475 -- >>> let expected = fromList [m1,m2,m3,m4,m5,m6,m7] :: Mat7 Double
476 -- >>> let actual = big_M pde' params'
477 -- >>> frobenius_norm (actual - expected) < 1e-3
480 big_M :: forall m n l a.
481 (Arity l, Arity m, Arity n,
482 Algebraic.C a, RealField.C a, ToRational.C a)
487 ifoldl2 (big_M_elem pde params) zero col_idxs
489 col_idxs = fromList [map fromInteger [0..]] :: Row m a
493 -- | Determine the coefficient vector @x@ from the system @(K + M)x = F@.
497 -- >>> import Linear.Matrix ( Col7, frobenius_norm )
498 -- >>> import FEM.R1.Example1 ( pde', params' )
500 -- >>> let c1 = [0.02366220347687] :: [Double]
501 -- >>> let c2 = [0.03431630082636] :: [Double]
502 -- >>> let c3 = [0.02841800893264] :: [Double]
503 -- >>> let c4 = [-0.00069489654996] :: [Double]
504 -- >>> let c5 = [-0.00518637005151] :: [Double]
505 -- >>> let c6 = [-0.00085028505337] :: [Double]
506 -- >>> let c7 = [-0.00170478210110] :: [Double]
508 -- >>> let expected = fromList [c1,c2,c3,c4,c5,c6,c7] :: Col7 Double
509 -- >>> let actual = coefficients pde' params'
510 -- >>> frobenius_norm (actual - expected) < 1e-8
513 coefficients :: forall m n l a.
514 (Arity m, Arity n, Arity l,
515 Algebraic.C a, Eq a, RealField.C a, ToRational.C a)
517 -> Params m n (S l) a
519 coefficients pde params =
520 solve_positive_definite matrix b
522 matrix = (big_K pde params) + (big_M pde params)
526 solution :: forall m n l a.
527 (Arity m, Arity n, Arity l,
528 Algebraic.C a, Eq a, RealField.C a, ToRational.C a, Show a)
530 -> Params m n (S l) a
532 solution pde params =
533 from_intervals $ map head $ toList $ solved_column
535 global_coeffs :: Col (S l) a
536 global_coeffs = coefficients pde params
538 ptr :: Mat m (S n) Int
541 -- Each mesh element has an associated row in the pointer
542 -- matrix. Stick them together.
543 mesh_with_ptr_rows :: Col m (Interval a, Row (S n) Int)
544 mesh_with_ptr_rows = zip2 (mesh params) (rows2 ptr)
546 make_local_coeffs :: (Interval a, Row (S n) Int) -> Row (S n) a
547 make_local_coeffs (_, ptr_row) =
550 lambda _ j = if (ptr_row !!! (0,j)) == zero
552 else global_coeffs !!! ((ptr_row !!! (0,j)) - 1, 0)
554 -- Create a column vector for each mesh element containing the global
555 -- coefficients corresponding to that element.
556 local_coeffs :: Col m (Row (S n) a)
557 local_coeffs = map2 make_local_coeffs mesh_with_ptr_rows
559 global_basis_functions :: Col (S n) (a -> a)
560 global_basis_functions =
562 where lambda i _ = big_N (toInteger i)
564 mesh_with_coeffs :: Col m (Interval a, Row (S n) a)
565 mesh_with_coeffs = zip2 (mesh params) local_coeffs
567 solved_column :: Col m (Interval a, (a -> a))
568 solved_column = map2 solve_piece $ mesh_with_coeffs
570 solve_piece :: (Interval a, Row (S n) a) -> (Interval a, (a -> a))
571 solve_piece (interval, coeffs_row) = (interval, g)
573 coeffs_col = transpose coeffs_row
575 g x = element_sum2 $ zipwith2 combine coeffs_col global_basis_functions
577 xi = (affine interval) x
578 combine ci ni = ci*(ni xi)