Mercurial > repos > public > sbplib
comparison +scheme/bcSetup.m @ 619:9dd49d622a4c feature/grids
Add a function for easily applying many boundary conditions at the same time
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 20 Oct 2017 16:01:43 +0200 |
parents | |
children | facb8ffb5c34 eac5fb4b63db |
comparison
equal
deleted
inserted
replaced
618:c360bbecf260 | 619:9dd49d622a4c |
---|---|
1 % function [closure, S] = bcSetup(diffOp, bc) | |
2 % Takes a diffOp and a cell array of boundary condition definitions. | |
3 % Each bc is a struct with the fields | |
4 % * type -- Type of boundary condition | |
5 % * boundary -- Boundary identifier | |
6 % * data -- A function_handle with time and space coordinates as a parameters, for example f(t,x,y) for a 2D problem | |
7 % Also takes S_sign which modifies the sign of S, [-1,1] | |
8 % Returns a closure matrix and a forcing function S | |
9 function [closure, S] = bcSetup(diffOp, bc, S_sign) | |
10 default_arg('S_sign', 1); | |
11 assertType(bc, 'cell'); | |
12 assert(S_sign == 1 || S_sign == -1, 'S_sign must be either 1 or -1'); | |
13 | |
14 | |
15 closure = spzeros(size(diffOp)); | |
16 penalties = {}; | |
17 dataFunctions = {}; | |
18 dataParams = {}; | |
19 | |
20 for i = 1:length(bc) | |
21 assertType(bc{i}, 'struct'); | |
22 [localClosure, penalty] = diffOp.boundary_condition(bc{i}.boundary, bc{i}.type); | |
23 closure = closure + localClosure; | |
24 | |
25 if isempty(bc{i}.data) | |
26 continue | |
27 end | |
28 assertType(bc{i}.data, 'function_handle'); | |
29 | |
30 coord = diffOp.grid.getBoundary(bc{i}.boundary); | |
31 assertNumberOfArguments(bc{i}.data, 1+size(coord,2)); | |
32 | |
33 penalties{end+1} = penalty; | |
34 dataFunctions{end+1} = bc{i}.data; | |
35 dataParams{end+1} = num2cell(coord ,1); | |
36 end | |
37 | |
38 O = spzeros(size(diffOp),1); | |
39 function v = S_fun(t) | |
40 v = O; | |
41 for i = 1:length(dataFunctions) | |
42 v = v + penalties{i}*dataFunctions{i}(t, dataParams{i}{:}); | |
43 end | |
44 | |
45 v = S_sign * v; | |
46 end | |
47 S = @S_fun; | |
48 end |