close all
clear all


ElementSize = 1;  %Inches
Theta = 20;  %degrees

filenm = sprintf('CompliantPlate-th%d-d%d',Theta,ElementSize);
title = filenm;
depth_offset = -100;


RunConditions.depth = -1;  %Infinite Depth
RunConditions.WaveAmp = 1;  %Wave Amp
RunConditions.ISym = 2;  %Two Planes of Symmetry
RunConditions.IType = 1;  %Freely floating body
RunConditions.IPrint = 0;  %Don't print velocity potential
RunConditions.WaveNumbers = [1000.0];  %WaveNumbers to compute solution at
RunConditions.Angle = 0.0;  %Wave Angle.


a1 = 69.75*.0254;
b1 = .5*.0254;
b2 = 6*.0254;
b3 = 24.5*.0254;
b4 = (31-5.5)*.0254;
b5 = 31*.0254;
b6 = b4+24*.0254;

c = +.04;

d1 = 5.5*.0254;
d2 = 18.5*.0254; 

ElementSize = 0.0254*ElementSize;


n = 1;    %1
BaseEl{n}.Type = 1;  %Four Point Quadrilateral
BaseEl{n}.CoordSys = 1;  %Cartesian CS
BaseEl{n}.x = [0 a1 a1 0];
BaseEl{n}.y = [-d1 -d1 d2 d2];
BaseEl{n}.z = [0 0 0 0];


n = n+1;  %2
BaseEl{n}.Type = 1;  %Four Point Quadrilateral
BaseEl{n}.CoordSys = 1;  %Cartesian CS
BaseEl{n}.x = [0 a1 a1 0];
BaseEl{n}.y = [0 0 0 0];
BaseEl{n}.z = [-c/2 -c/2 c/2 c/2];

n = n+1;  %3
BaseEl{n}.Type = 1;  %Four Point Quadrilateral
BaseEl{n}.CoordSys = 1;  %Cartesian CS
BaseEl{n}.x = [0 0 0 0 ];
BaseEl{n}.y = [-d1 d2 d2 -d1];
BaseEl{n}.z = [-c/2 -c/2 c/2 c/2];




n = 1;  %#1
SE{n}.BaseElNum = 1;
SE{n}.Offset = [0 b2 c/2];
SE{n}.Normal = 1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil((d1+d2)/ElementSize);
SE{n+1} = SE{n};
SE{n+1}.Offset(3) = -SE{n}.Offset(3);
SE{n+1}.Normal = -1;
n = n+2;  %#2

%Vertical SuperElements Start here
%2-3-2
SE{n}.BaseElNum = 2;
SE{n}.Offset = [0 b1 0];
SE{n}.Normal = 1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;  

SE{n}.BaseElNum = 3;
SE{n}.Offset = [a1 b2 0];
SE{n}.Normal = 1;
SE{n}.DivX = ceil((d1+d2)/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;  

SE{n}.BaseElNum = 2;
SE{n}.Offset = [0 b3 0];
SE{n}.Normal = -1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;



SE{n}.BaseElNum = 1;
SE{n}.Offset = [0 b5 c/2];
SE{n}.Normal = 1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil((d1+d2)/ElementSize);
SE{n+1} = SE{n};
SE{n+1}.Offset(3) = -SE{n}.Offset(3);
SE{n+1}.Normal = -1;
n = n+2;

%2-3-2
SE{n}.BaseElNum = 2;
SE{n}.Offset = [0 b4 0];
SE{n}.Normal = 1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;  

SE{n}.BaseElNum = 3;
SE{n}.Offset = [a1 b5 0];
SE{n}.Normal = 1;
SE{n}.DivX = ceil((d1+d2)/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;  

SE{n}.BaseElNum = 2;
SE{n}.Offset = [0 b6 0];
SE{n}.Normal = -1;
SE{n}.DivX = ceil(a1/ElementSize);
SE{n}.DivY = ceil(c/ElementSize);
n = n+1;

%Translate all boundary elements to global coordinates for subsequent rotations
for n = 1:length(SE)
    NewBaseEl{n}.Type = BaseEl{SE{n}.BaseElNum}.Type;
    NewBaseEl{n}.CoordSys = BaseEl{SE{n}.BaseElNum}.CoordSys;
    NewBaseEl{n}.x = SE{n}.Offset(1)+BaseEl{SE{n}.BaseElNum}.x;
    NewBaseEl{n}.y = SE{n}.Offset(2)+BaseEl{SE{n}.BaseElNum}.y;
    NewBaseEl{n}.z = SE{n}.Offset(3)+BaseEl{SE{n}.BaseElNum}.z;
    SE{n}.Offset = [0 0 0];
    SE{n}.BaseElNum = n;       
end

 for n = 1:length(NewBaseEl)/2
     NewBaseEl{n}.y = sin(Theta*pi/180)*NewBaseEl{n}.z+b2+cos(Theta*pi/180)*(NewBaseEl{n}.y-b2);
     NewBaseEl{n}.z = cos(Theta*pi/180)*NewBaseEl{n}.z-sin(Theta*pi/180)*(NewBaseEl{n}.y-b2);
 end
 for n = length(NewBaseEl)/2+1:length(NewBaseEl)
     NewBaseEl{n}.y = sin(Theta*pi/180)*NewBaseEl{n}.z+b5+cos(Theta*pi/180)*(NewBaseEl{n}.y-b5);
     NewBaseEl{n}.z = cos(Theta*pi/180)*NewBaseEl{n}.z-sin(Theta*pi/180)*(NewBaseEl{n}.y-b5);
 end


%Rotate both plates seperately about the pivot point by the specified
%angle.

WriteMGEN(filenm,title,depth_offset,RunConditions,NewBaseEl,SE)


