comparison +grid/evalOn.m @ 717:8e4274ee6dd8 feature/utux2D

Merge with feature/poroelastic
author Martin Almquist <malmquist@stanford.edu>
date Sat, 03 Mar 2018 14:58:21 -0800
parents 0609a72dcdfe
children 94f0f0b0d721
comparison
equal deleted inserted replaced
666:2d85f17a8aec 717:8e4274ee6dd8
5 % func -- Function to evaluate. May be a function handle or a constant. If 5 % func -- Function to evaluate. May be a function handle or a constant. If
6 % it is a vector value it has to be provided as a column vector, 6 % it is a vector value it has to be provided as a column vector,
7 function gf = evalOn(g, func) 7 function gf = evalOn(g, func)
8 if ~isa(func, 'function_handle') 8 if ~isa(func, 'function_handle')
9 % We should have a constant. 9 % We should have a constant.
10 if size(func,2) ~= 1 10 assert(size(func,2) == 1,'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
11 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
12 end
13 11
14 gf = repmat(func,[g.N, 1]); 12 gf = repmat(func,[g.N, 1]);
15 return 13 return
16 end 14 end
17 % func should now be a function_handle 15 % func should now be a function_handle
16 assert(g.D == nargin(func),'grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
18 17
19 if g.D ~= nargin(func) 18 x = num2cell(g.points(),1);
20 g.D 19 k = numberOfComponents(func);
21 nargin(func)
22 error('grid:evalOn:WrongNumberOfInputs', 'The number of inputs of the function must match the dimension of the domain.')
23 end
24 20
21 gf = func(x{:});
22 gf = reorderComponents(gf, k);
23 end
25 24
26 % Get coordinates 25 % Find the number of vector components of func
27 x = g.points(); 26 function k = numberOfComponents(func)
27 x0 = num2cell(ones(1,nargin(func)));
28 f0 = func(x0{:});
29 assert(size(f0,2) == 1, 'grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector');
30 k = length(f0);
31 end
28 32
29 % Find the number of components 33 % Reorder the components of the function to sit together
30 if size(x,1) ~= 0 34 function gf = reorderComponents(a, k)
31 x0 = x(1,:); 35 N = length(a)/k;
32 else 36 gf = zeros(N*k, 1);
33 x0 = num2cell(ones(1,size(x,2)));
34 end
35
36 dim = length(x0);
37 % Evaluate f0 = func(x0(1),x0(2),...,x0(dim));
38 if(dim == 1)
39 f0 = func(x0);
40 else
41 eval_str = 'f0 = func(x0(1)';
42 for i = 2:dim
43 eval_str = [eval_str, sprintf(',x0(%d)',i)];
44 end
45 eval_str = [eval_str, ');'];
46 eval(eval_str);
47 end
48
49 % k = number of components
50 k = length(f0);
51
52 if size(f0,2) ~= 1
53 error('grid:evalOn:VectorValuedWrongDim', 'A vector valued function must be given as a column vector')
54 end
55
56 % Evaluate gf = func(x(:,1),x(:,2),...,x(:,dim));
57 if(dim == 1)
58 gf = func(x);
59 else
60 eval_str = 'gf = func(x(:,1)';
61 for i = 2:dim
62 eval_str = [eval_str, sprintf(',x(:,%d)',i)];
63 end
64 eval_str = [eval_str, ');'];
65 eval(eval_str);
66 end
67
68 % Reorganize gf
69 gf_temp = gf;
70 gf = zeros(g.N*k, 1);
71 for i = 1:k 37 for i = 1:k
72 gf(i:k:end) = gf_temp((i-1)*g.N + 1 : i*g.N); 38 gf(i:k:end) = a((i-1)*N + 1 : i*N);
73 end 39 end
74 end 40 end