changeset 161:73bc43c7379e feature/grids

Added function for reshaping grid functions. Added size() method to structured grid.
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 22 Feb 2016 13:20:55 +0100
parents c700b26ad304
children c75c03f692b3
files +grid/Structured.m +grid/funcToMatrix.m +grid/funcToMatrixTest.m +grid/funcToPlotMatrix.m +grid/funcToPlotMatrixTest.m
diffstat 5 files changed, 178 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/+grid/Structured.m	Mon Feb 22 12:27:31 2016 +0100
+++ b/+grid/Structured.m	Mon Feb 22 13:20:55 2016 +0100
@@ -1,7 +1,6 @@
 classdef Structured < grid.Grid
-    % General multiblock grid
     methods (Abstract)
-        % get matrices of coords
-        % turn gridfunctions to matrices
+        % Returns the size of the grid in each dimension m = [mx my mz ...]
+        m = size(obj)
     end
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToMatrix.m	Mon Feb 22 13:20:55 2016 +0100
@@ -0,0 +1,13 @@
+% Takes a grid function and reshapes it into a matrix of shape m.
+% Called by class methods.
+function F = funcToMatrix(gf, m)
+    D = length(m);
+
+    if D == 1
+        F = gf;
+        return
+    end
+
+    % Reshape and reverse order of indecies
+    F = permute(reshape(gf, rot90(m,2)), D:-1:1);
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToMatrixTest.m	Mon Feb 22 13:20:55 2016 +0100
@@ -0,0 +1,95 @@
+function tests = funcToMatrixTest()
+    tests = functiontests(localfunctions);
+end
+
+function test1D(testCase)
+    inGf = [1 2 3 4 5]';
+    inM = 5;
+    out = [1 2 3 4 5]';
+    testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out);
+end
+
+function test2D(testCase)
+    inGf = [11; 12; 21; 22];
+    inM = [2, 2];
+
+    out(1,1) = 11;
+    out(1,2) = 12;
+    out(2,1) = 21;
+    out(2,2) = 22;
+
+    testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out);
+end
+
+function test3D(testCase)
+    inGf = [111; 112; 121; 122; 211; 212; 221; 222];
+    inM = [2, 2, 2];
+
+    out(1,1,1) = 111;
+    out(1,1,2) = 112;
+    out(1,2,1) = 121;
+    out(1,2,2) = 122;
+    out(2,1,1) = 211;
+    out(2,1,2) = 212;
+    out(2,2,1) = 221;
+    out(2,2,2) = 222;
+
+    testCase.verifyEqual(grid.funcToMatrix(inGf, inM),out);
+end
+
+function testNonSquare(testCase)
+    inGf = [
+        111;
+        112;
+        113;
+        114;
+        121;
+        122;
+        123;
+        124;
+        131;
+        132;
+        133;
+        134;
+        211;
+        212;
+        213;
+        214;
+        221;
+        222;
+        223;
+        224;
+        231;
+        232;
+        233;
+        234;
+    ];
+    inM = [2, 3, 4];
+
+    out(1,1,1) = 111;
+    out(1,1,2) = 112;
+    out(1,1,3) = 113;
+    out(1,1,4) = 114;
+    out(1,2,1) = 121;
+    out(1,2,2) = 122;
+    out(1,2,3) = 123;
+    out(1,2,4) = 124;
+    out(1,3,1) = 131;
+    out(1,3,2) = 132;
+    out(1,3,3) = 133;
+    out(1,3,4) = 134;
+    out(2,1,1) = 211;
+    out(2,1,2) = 212;
+    out(2,1,3) = 213;
+    out(2,1,4) = 214;
+    out(2,2,1) = 221;
+    out(2,2,2) = 222;
+    out(2,2,3) = 223;
+    out(2,2,4) = 224;
+    out(2,3,1) = 231;
+    out(2,3,2) = 232;
+    out(2,3,3) = 233;
+    out(2,3,4) = 234;
+
+    testCase.verifyEqual(grid.funcToMatrix(inGf, inM), out);
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToPlotMatrix.m	Mon Feb 22 13:20:55 2016 +0100
@@ -0,0 +1,20 @@
+% Takes a grid function and reshapes it into a matrix of shape m for plotting.
+% Called by class methods.
+function F = funcToPlotMatrix(gf, m)
+    D = length(m);
+
+
+
+    switch D
+        case 1
+            F = gf;
+        case 2
+            F = reshape(gf, rot90(m,2));
+        case 3
+            % After the reshape the indecies will be M(z,y,x). Plot need them to be M(y,x,z)
+            p = [2 3 1]; % Permuation
+            F = permute(reshape(gf,rot90(m,2)), p);
+        otherwise
+            error('grid:funcToMatrix:NotImplemented','Grid function to matrix is not implemented for dimension = %d', length(m));
+    end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/+grid/funcToPlotMatrixTest.m	Mon Feb 22 13:20:55 2016 +0100
@@ -0,0 +1,48 @@
+function tests = funcToPlotMatrixTest()
+    tests = functiontests(localfunctions);
+end
+
+function test1D(testCase)
+    inGf = [1 2 3 4 5]';
+    inM = 5;
+    out = [1 2 3 4 5]';
+    testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out);
+end
+
+function test2D(testCase)
+    x = 1:2;
+    y = 1:3;
+
+    f = @(x,y) x + y*10;
+
+    xx = [1; 1; 1; 2; 2; 2];
+    yy = [1; 2; 3; 1; 2; 3];
+    inGf = f(xx,yy);
+
+    [X,Y] = meshgrid(x,y);
+    out = f(X,Y);
+
+    inM = [2, 3];
+
+    testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out);
+end
+
+function test3D(testCase)
+    x = 1:2;
+    y = 1:3;
+    z = 1:4;
+
+    f = @(x,y,z) x + y*10 + z*100;
+
+    xx = [repmat(1, [12, 1]); repmat(2, [12, 1])];
+    yy = repmat([1; 1; 1; 1; 2; 2; 2; 2; 3; 3; 3; 3], [2, 1]);
+    zz = repmat([1; 2; 3; 4], [6, 1]);
+    inGf = f(xx,yy,zz);
+
+    [X,Y,Z] = meshgrid(x,y,z);
+    out = f(X,Y,Z);
+
+    inM = [2, 3, 4];
+
+    testCase.verifyEqual(grid.funcToPlotMatrix(inGf, inM),out);
+end
\ No newline at end of file