Mercurial > repos > public > sbplib
comparison +scheme/+bc/forcingSetup.m @ 1033:037f203b9bf5 feature/burgers1d
Merge with branch feature/advectioRV to utilize the +rv package
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 17 Jan 2019 10:44:12 +0100 |
parents | ba10f24bf476 |
children | a52033540dd9 |
comparison
equal
deleted
inserted
replaced
854:18162a0a5bb5 | 1033:037f203b9bf5 |
---|---|
1 % Setup the forcing function for the given boundary conditions and data. | |
2 % Each bc is a struct with the fields | |
3 % * type -- Type of boundary condition | |
4 % * boundary -- Boundary identifier | |
5 % * data -- A function_handle for a function which provides boundary data.(see below) | |
6 % S_sign allows changing the sign of the function to put on different sides in the system of ODEs. | |
7 % default is 1, which the same side as the diffOp. | |
8 % Returns a forcing function S. | |
9 % | |
10 % The boundary data function can either be a function of time or a function of time and space coordinates. | |
11 % In the case where it only depends on time it should return the data as grid function for the boundary. | |
12 % In the case where it also takes space coordinates the number of space coordinates should match the number of dimensions of the problem domain. | |
13 % For example in the 2D case: f(t,x,y). | |
14 | |
15 function S = forcingSetup(diffOp, penalties, bcs, S_sign) | |
16 default_arg('S_sign', 1); | |
17 | |
18 assertType(bcs, 'cell'); | |
19 assertIsMember(S_sign, [1, -1]); | |
20 | |
21 scheme.bc.verifyFormat(bcs, diffOp); | |
22 | |
23 [gridData, symbolicData] = parseAndSortData(bcs, penalties, diffOp); | |
24 | |
25 % Setup penalty function | |
26 O = spzeros(size(diffOp),1); | |
27 function v = S_fun(t) | |
28 v = O; | |
29 for i = 1:length(gridData) | |
30 v = v + gridData{i}.penalty*gridData{i}.func(t); | |
31 end | |
32 | |
33 for i = 1:length(symbolicData) | |
34 v = v + symbolicData{i}.penalty*symbolicData{i}.func(t, symbolicData{i}.coords{:}); | |
35 end | |
36 | |
37 v = S_sign * v; | |
38 end | |
39 S = @S_fun; | |
40 end | |
41 | |
42 % Go through a cell array of boundary condition specifications and return cell arrays | |
43 % of structs for grid and symbolic data. | |
44 function [gridData, symbolicData] = parseAndSortData(bcs, penalties, diffOp) | |
45 gridData = {}; | |
46 symbolicData = {}; | |
47 for i = 1:length(bcs) | |
48 [ok, isSymbolic, data] = parseData(bcs{i}, penalties{i}, diffOp.grid); | |
49 | |
50 if ~ok | |
51 continue % There was no data | |
52 end | |
53 | |
54 if isSymbolic | |
55 symbolicData{end+1} = data; | |
56 else | |
57 gridData{end+1} = data; | |
58 end | |
59 end | |
60 end | |
61 | |
62 function [ok, isSymbolic, dataStruct] = parseData(bc, penalty, grid) | |
63 if ~isfield(bc,'data') || isempty(bc.data) | |
64 isSymbolic = []; | |
65 dataStruct = struct(); | |
66 ok = false; | |
67 return | |
68 end | |
69 ok = true; | |
70 | |
71 nArg = nargin(bc.data); | |
72 | |
73 if nArg > 1 | |
74 % Symbolic data | |
75 isSymbolic = true; | |
76 coord = grid.getBoundary(bc.boundary); | |
77 dataStruct.penalty = penalty; | |
78 dataStruct.func = bc.data; | |
79 dataStruct.coords = num2cell(coord, 1); | |
80 else | |
81 % Grid data | |
82 isSymbolic = false; | |
83 dataStruct.penalty = penalty; | |
84 dataStruct.func = bc.data; | |
85 end | |
86 end |