Mercurial > repos > public > sbplib
comparison +grid/Cartesian.m @ 154:c7b2f645101f feature/grids
Added classes and functions for Cartesian and equidistant grids.
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 18 Feb 2016 16:46:02 +0100 |
parents | |
children | cf748f33cd5b |
comparison
equal
deleted
inserted
replaced
153:7aee9eba3bb8 | 154:c7b2f645101f |
---|---|
1 classdef Cartesian < grid.Grid | |
2 properties | |
3 n % Number of points in the grid | |
4 d % Number of dimensions | |
5 m % Number of points in each direction | |
6 x % Cell array of vectors with node placement for each dimension. | |
7 end | |
8 | |
9 % General d dimensional grid with n points | |
10 methods | |
11 % Creates a cartesian grid given vectors conatining the coordinates | |
12 % in each direction | |
13 function obj = Cartesian(varargin) | |
14 obj.d = length(varargin); | |
15 for i = 1:obj.d | |
16 obj.x{i} = varargin{i}; | |
17 obj.m(i) = length(varargin{i}); | |
18 end | |
19 obj.n = prod(obj.m); | |
20 if obj.n == 0 | |
21 error('grid:Cartesian:EmptyGrid','Input parameter gives an empty grid.') | |
22 end | |
23 end | |
24 % n returns the number of points in the grid | |
25 function o = N(obj) | |
26 o = obj.n; | |
27 end | |
28 | |
29 % d returns the spatial dimension of the grid | |
30 function o = D(obj) | |
31 o = obj.d; | |
32 end | |
33 | |
34 % points returns a n x d matrix containing the coordianets for all points. | |
35 % points are ordered according to the kronecker product with X*Y*Z | |
36 function X = points(obj) | |
37 X = zeros(obj.n, obj.d); | |
38 | |
39 for i = 1:obj.d | |
40 if iscolumn(obj.x{i}) | |
41 c = obj.x{i}; | |
42 else | |
43 c = obj.x{i}'; | |
44 end | |
45 | |
46 m_before = prod(obj.m(1:i-1)); | |
47 m_after = prod(obj.m(i+1:end)); | |
48 | |
49 X(:,i) = kr(ones(m_before,1),c,ones(m_after,1)); | |
50 end | |
51 end | |
52 | |
53 % matrices returns a cell array with coordinates in matrix form. | |
54 % For 2d case these will have to be transposed to work with plotting routines. | |
55 function X = matrices(obj) | |
56 | |
57 if obj.d == 1 % There is no 1d matrix data type in matlab, handle special case | |
58 X{1} = reshape(obj.x{1}, [obj.m 1]); | |
59 return | |
60 end | |
61 | |
62 X = cell(1,obj.d); | |
63 for i = 1:obj.d | |
64 s = ones(1,obj.d); | |
65 s(i) = obj.m(i); | |
66 | |
67 t = reshape(obj.x{i},s); | |
68 | |
69 s = obj.m; | |
70 s(i) = 1; | |
71 X{i} = repmat(t,s); | |
72 end | |
73 end | |
74 | |
75 % coordVectors() | |
76 % coordMatrices() | |
77 end | |
78 end |