-
Notifications
You must be signed in to change notification settings - Fork 3
Meshfree Shallow Water on a Sphere #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AndreyAPopov
wants to merge
14
commits into
master
Choose a base branch
from
meshfree-shallow-water-sphere
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea7b64e
first commit of the meshfree shallow water on a sphere
AndreyAPopov b03be54
Added a more general approach to rbfs
AndreyAPopov 8348d67
Added a bunch of CSRBF functions
AndreyAPopov b0a239d
Modified the PhysicalConstants
AndreyAPopov ed87c83
Completely changed the RBF method to a cartesian one, so that it woul…
AndreyAPopov 0fb67d0
switched to 784 nodes
AndreyAPopov 1fd08f0
update distance function
AndreyAPopov d22bd67
update distance function 2
AndreyAPopov f881d62
better preset
AndreyAPopov 5a75ff9
small fixes
AndreyAPopov e991917
Changes
AndreyAPopov 4dce007
Switched to 100 nodes
AndreyAPopov 9175541
removed for now
AndreyAPopov e362c46
Continued work on MF Shallow water
AndreyAPopov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
classdef Canonical < otp.mfshallowwatersphere.MFShallowWaterSphereProblem | ||
methods | ||
function obj = Canonical(varargin) | ||
|
||
%load('nodes100.mat', 'x', 'y', 'z'); | ||
|
||
load('nodes250.mat', 'x', 'y', 'z'); | ||
|
||
%load('nodes784.mat', 'x', 'y', 'z'); | ||
|
||
%load('nodes500.mat', 'x', 'y', 'z'); | ||
|
||
|
||
% mean water height | ||
H = 5.768e4; | ||
% earth gravity | ||
g = otp.utils.PhysicalConstants.EarthGravity; | ||
% radius of the earth | ||
a = otp.utils.PhysicalConstants.EarthRadius; | ||
% initial velocity for the perturbation | ||
u0 = 20; | ||
%u0 = 1; | ||
% Angular velocity of the earth | ||
Omega = otp.utils.PhysicalConstants.EarthAngularVelocity; | ||
|
||
coriolisForce = 2*Omega*z; | ||
|
||
params.gravity = otp.utils.PhysicalConstants.EarthGravity; | ||
params.radius = a; | ||
params.angularSpeed = Omega; | ||
params.coriolisForce = coriolisForce; | ||
|
||
params.x = x; | ||
params.y = y; | ||
params.z = z; | ||
|
||
%params.rbfradius = 2.0; | ||
%params.rbf = @otp.utils.rbf.buhmann3; | ||
|
||
params.rbfradius = 2; | ||
params.rbf = @otp.utils.rbf.buhmann3; | ||
%params.rbf = @otp.utils.rbf.quadricspline; | ||
%params.rbf = @otp.utils.rbf.wendlandWeC2PD1; | ||
|
||
% convert from Cartesian to spherical coordinates | ||
theta = atan2(z, sqrt(x.^2 + y.^2)); | ||
lambda = atan2(y, x); | ||
|
||
%% Define the initial conditions | ||
% first get a stableish Rossby-Haurwitz wave with a wave number | ||
% of R = 7 | ||
R = 4; | ||
[h, zonalwind, meridionalwind] = getrossbyhaurwitzwave(x, y, z, Omega, a, g, R); | ||
|
||
% then, define perturbations to this wave in terms of the T-Z | ||
% initial condition. | ||
hpert = (1/g)*(H + 2*Omega*a*u0*( sin(theta).^3 ).*cos(theta).*sin(lambda)); | ||
zonalwindpert = (-3*u0*sin(theta).*( cos(theta).^2 ).*sin(lambda) + u0*( sin(theta).^3 ).*sin(lambda)); | ||
meridionalwindpert = (u0*( sin(theta).^2 ).*cos(lambda)); | ||
|
||
% remove the excess height and velocity | ||
%hpert = hpert - mean(hpert); | ||
%zonalwindpert = zonalwindpert - mean(zonalwindpert); | ||
%meridionalwindpert = meridionalwindpert - mean(meridionalwindpert); | ||
|
||
% mixing coefficient | ||
%alpha = 0.1; | ||
alpha = 0; | ||
|
||
% perturb the R-H wave | ||
h = alpha*h + (1 - alpha)*hpert; | ||
zonalwind = alpha*zonalwind + (1 - alpha)*zonalwindpert; | ||
meridionalwind = alpha*meridionalwind + (1 - alpha)*meridionalwindpert; | ||
|
||
% finally, convert to Cartesian coordinates | ||
[u, v, w] = velocitytocartesian(x, y, z, zonalwind, meridionalwind); | ||
|
||
huv0 = [h; u; v; w]; | ||
|
||
%% Do the rest | ||
|
||
oneday = 24*60*60; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use |
||
|
||
tspan = [0, oneday]; | ||
|
||
obj = [email protected](tspan, ... | ||
huv0, params); | ||
|
||
end | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
src/+otp/+mfshallowwatersphere/+presets/private/getrossbyhaurwitzwave.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
% See | ||
% Williamson, David L., John B. Drake, James J. Hack, Rüdiger Jakob, and Paul N. Swarztrauber. | ||
% "A standard test set for numerical approximations to the shallow water equations in spherical geometry." | ||
% Journal of Computational Physics 102, no. 1 (1992): 211-224. | ||
function [h, zonalwind, meridionalwind] = getrossbyhaurwitzwave(x, y, z, Omega, a, g, wavenumber) | ||
|
||
if nargin < 7 | ||
wavenumber = 4; | ||
end | ||
|
||
omega = 7.848e-6; | ||
K = omega; | ||
h0 = 8.0e3; | ||
R = wavenumber; | ||
|
||
% convert from Cartesian to spherical coordinates | ||
theta = atan2(z, sqrt(x.^2 + y.^2)); | ||
lambda = atan2(y, x); | ||
|
||
zonalwind = a*omega*cos(theta) + a*K*( cos(theta).^(R-1) ).*( R*(sin(theta).^2) - (cos(theta).^2)).*cos(R*lambda); | ||
meridionalwind = -a*K*R*( cos(theta).^(R-1) ).*sin(theta).*sin(R*lambda); | ||
|
||
At = (R + 1)*( cos(theta).^2 ) + (2*(R^2) - R - 2) - (2*(R^2))./( cos(theta).^2 ); | ||
A = (omega/2)*(2*Omega + omega)*( cos(theta).^2 ) + ((K^2)/4)*( cos(theta).^(2*R) ).*At; | ||
|
||
Bt = R^2 + 2*R + 2 - ((R + 1).^2).*( cos(theta).^2 ); | ||
B = (2*(Omega + omega)*K)/((R+1)*(R+2)).*( cos(theta).^R ).*Bt; | ||
|
||
C = ((K^2)/4)*( cos(theta).^(2*R) ).*( (R+1)*( cos(theta).^2 ) - (R+2)); | ||
|
||
h = (1/g)*(h0 + (a^2)*( A + B.*cos(R*lambda) + C.*cos(2*R*lambda) )); | ||
|
||
end |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions
12
src/+otp/+mfshallowwatersphere/+presets/private/velocitytocartesian.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function [u, v, w] = velocitytocartesian(x, y, z, zonalwind, meridionalwind) | ||
|
||
% convert from Cartesian to spherical coordinates | ||
theta = atan2(z, sqrt(x.^2 + y.^2)); | ||
lambda = atan2(y, x); | ||
|
||
% convert from spherical to Cartesian velocity | ||
u = (-zonalwind.*sin(lambda).*cos(theta) - meridionalwind.*cos(lambda).*sin(theta)); | ||
v = (zonalwind.*cos(lambda).*cos(theta) - meridionalwind.*sin(lambda).*sin(theta)); | ||
w = (meridionalwind.*cos(theta)); | ||
|
||
end |
210 changes: 210 additions & 0 deletions
210
src/+otp/+mfshallowwatersphere/MFShallowWaterSphereProblem.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
classdef MFShallowWaterSphereProblem < otp.Problem | ||
|
||
methods | ||
function obj = MFShallowWaterSphereProblem(timeSpan, y0, parameters) | ||
|
||
[email protected]('Meshfree Shallow Water on a Sphere', [], ... | ||
timeSpan, y0, parameters); | ||
|
||
end | ||
end | ||
|
||
properties (SetAccess = private) | ||
DistanceFunction | ||
Constraints | ||
ConstraintsDerivative | ||
end | ||
|
||
methods | ||
|
||
function plotSphere(obj, huv, projection) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent passing of axes to plotting functions |
||
if nargin < 3 | ||
%projection = 'eqaazim'; | ||
projection = 'eckert4'; | ||
end | ||
|
||
|
||
x = obj.Parameters.x; | ||
y = obj.Parameters.y; | ||
z = obj.Parameters.z; | ||
rbf = obj.Parameters.rbf; | ||
|
||
Nplot = 50; | ||
lambdaplot = linspace(-pi, pi, Nplot); | ||
thetaplot = linspace(-pi/2, pi/2, Nplot); | ||
[lambdainterpgrid, thetainterpgrid] = meshgrid(lambdaplot, thetaplot); | ||
|
||
% get the cartesian coordinates for the uniform mesh | ||
[x2, y2, z2] = sph2cart(lambdainterpgrid(:), thetainterpgrid(:), ones(numel(lambdainterpgrid), 1)); | ||
radiusplot = 0.5; | ||
|
||
% | ||
Winterp = rbfinterp(x, y, z, x2, y2, z2, radiusplot, rbf); | ||
Winterp = Winterp./sum(Winterp, 2); | ||
|
||
lon = 360*(lambdainterpgrid/pi + 1)/2; | ||
lat = 180*(thetainterpgrid/(pi/2))/2; | ||
|
||
|
||
load('coastlines', 'coastlat', 'coastlon'); | ||
|
||
n = size(huv, 1)/4; | ||
h = huv(1:n); | ||
%u = huv((n+1):(2*n)); | ||
%v = huv((2*n+1):end); | ||
|
||
Nplot = sqrt(size(Winterp, 1)); | ||
|
||
cmap = interp1([0; 0.5; 1], [1, 0, 0; 1, 1, 1; 0, 0.3, 0.8], linspace(0, 1, 500)); | ||
levels = 20; | ||
|
||
%colormap(cmap); | ||
|
||
%subplot(1, 3, 1); | ||
cla; | ||
axesm(projection); | ||
contourfm(lat, lon, reshape(Winterp*h, Nplot, Nplot), levels, 'LineStyle','none'); | ||
%colorbar; | ||
ax = gca; | ||
setm(ax,'FLineWidth', 3, 'Grid','on') | ||
l = plotm(coastlat, coastlon, '-k'); | ||
l.Color = [l.Color, 0.5]; | ||
|
||
% subplot(1, 3, 2); cla; | ||
% axesm(projection); | ||
% contourfm(lat, lon, reshape(Winterp*u, Nplot, Nplot), levels, 'LineStyle','none'); | ||
% colorbar; | ||
% ax = gca; | ||
% setm(ax,'FLineWidth', 3, 'Grid','on') | ||
% l = plotm(coastlat, coastlon, '-k'); | ||
% l.Color = [l.Color, 0.5]; | ||
% | ||
% subplot(1, 3, 3); cla; | ||
% axesm(projection); | ||
% contourfm(lat, lon, reshape(Winterp*v, Nplot, Nplot), levels, 'LineStyle','none'); | ||
% colorbar; | ||
% ax = gca; | ||
% setm(ax,'FLineWidth', 3, 'Grid','on') | ||
% l = plotm(coastlat, coastlon, '-k'); | ||
% l.Color = [l.Color, 0.5]; | ||
|
||
drawnow; | ||
|
||
|
||
end | ||
|
||
|
||
end | ||
|
||
|
||
methods (Access = protected) | ||
|
||
function onSettingsChanged(obj) | ||
x = obj.Parameters.x; | ||
y = obj.Parameters.y; | ||
z = obj.Parameters.z; | ||
g = obj.Parameters.gravity; | ||
a = obj.Parameters.radius; | ||
rbf = obj.Parameters.rbf; | ||
rbfradius = obj.Parameters.rbfradius; | ||
f = obj.Parameters.coriolisForce; | ||
|
||
% create the interpolation matrix and derivatives | ||
[W, dWdx, dWdy, dWdz, d2Wdx2, d2Wdy2, d2Wdz2] = rbfinterp(x, y, z, x, y, z, rbfradius, rbf); | ||
|
||
Bx = dWdx.*(1 - x.^2) + dWdy.*(-x.*y) + dWdz.*(-x.*z); | ||
By = dWdx.*(-x.*y) + dWdy.*(1 - y.^2) + dWdz.*(-y.*z); | ||
Bz = dWdx.*(-x.*z) + dWdy.*(-y.*z) + dWdz.*(1 - z.^2); | ||
|
||
|
||
Bx = Bx/a; | ||
By = By/a; | ||
Bz = Bz/a; | ||
|
||
Bx2 = d2Wdx2.*(1 - x.^2) + d2Wdy2.*(-x.*y) + d2Wdz2.*(-x.*z); | ||
By2 = d2Wdx2.*(-x.*y) + d2Wdy2.*(1 - y.^2) + d2Wdz2.*(-y.*z); | ||
Bz2 = d2Wdx2.*(-x.*z) + d2Wdy2.*(-y.*z) + d2Wdz2.*(1 - z.^2); | ||
|
||
Bx2 = Bx2/a; | ||
By2 = By2/a; | ||
Bz2 = Bz2/a; | ||
|
||
|
||
Wscale = full(sum(W, 2)); | ||
|
||
try | ||
Wdecomp = decomposition(W, 'chol'); | ||
catch | ||
error('The selected compination of RBF, radius, and nodes did not result in a SPD collocation matrix.') | ||
end | ||
|
||
|
||
|
||
n = size(obj.Y0, 1)/4; | ||
|
||
h = obj.Y0(1:n, :); | ||
|
||
sch0 = sum(Wdecomp\h); | ||
|
||
% build Shepard interpolation matrix | ||
S = W./sum(W, 2); | ||
|
||
% set the right hand side | ||
obj.Rhs = otp.Rhs(@(t, huvw) ... | ||
otp.mfshallowwatersphere.f(huvw, S, Wdecomp, Bx, By, Bz, Bx2, By2, Bz2, f, g, x, y, z, a, sch0)); | ||
|
||
|
||
|
||
H0Z0E0 = otp.mfshallowwatersphere.massenstrophyenergy(obj.Y0, S, Wdecomp, Bx, By, Bz, f, g, x, y, z, a, Wscale); | ||
|
||
% H0Z0E0(3) = 1; | ||
%eq = [1; 0; 0; 1]; | ||
|
||
obj.Constraints = @(t, huvw) ... | ||
otp.mfshallowwatersphere.massenstrophyenergy(huvw, S, Wdecomp, Bx, By, Bz, f, g, x, y, z, a, Wscale)./H0Z0E0 - 1; | ||
|
||
|
||
obj.ConstraintsDerivative = @(t, huvw) ... | ||
otp.mfshallowwatersphere.jacobianmassenstrophyenergy(huvw, S, Wdecomp, Bx, By, Bz, f, g, x, y, z, a, Wscale)./H0Z0E0; | ||
|
||
|
||
%% Distance function | ||
|
||
theta = atan2(z, sqrt(x.^2 + y.^2)); | ||
lambda = atan2(y, x); | ||
|
||
obj.DistanceFunction = @(t, huv, i, j) otp.mfshallowwatersphere.distfn(t, huv, i, j, theta, lambda); | ||
|
||
end | ||
|
||
function validateNewState(obj, newTimeSpan, newY0, newParameters) | ||
|
||
[email protected](obj, ... | ||
newTimeSpan, newY0, newParameters) | ||
|
||
%otp.utils.StructParser(newParameters) ... | ||
% .checkField('nx', 'finite', 'scalar', 'integer', 'positive') ... | ||
% .checkField('ny', 'finite', 'scalar', 'integer', 'positive') ... | ||
% .checkField('Re', 'finite', 'scalar', 'real', 'positive') ... | ||
% .checkField('Ro', 'finite', 'scalar', 'real'); | ||
|
||
end | ||
|
||
function label = internalIndex2label(obj, index) | ||
|
||
|
||
label = []; | ||
|
||
%[i, j] = ind2sub([obj.Parameters.nx, obj.Parameters.ny], index); | ||
|
||
%label = sprintf('(%d, %d)', i, j); | ||
|
||
end | ||
|
||
function sol = internalSolve(obj, varargin) | ||
% This really requires an SSP method | ||
sol = [email protected](obj, 'Method', @ode45, varargin{:}); | ||
end | ||
|
||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function d = distfn(~, ~, i, j, theta, lambda) | ||
|
||
n = numel(lambda); | ||
|
||
i = mod(i - 1, n) + 1; | ||
j = mod(j - 1, n) + 1; | ||
|
||
li = lambda(i); | ||
pi = theta(i); | ||
|
||
lj = lambda(j); | ||
pj = theta(j); | ||
|
||
dell = li - lj; | ||
|
||
nom = sqrt( ( cos(pj).*sin(dell) ).^2 + ( cos(pi).*sin(pj) - sin(pi).*cos(pj).*cos(dell) ).^2 ); | ||
den = sin(pi).*sin(pj) + cos(pi).*cos(pj).*cos(dell); | ||
d = atan2( nom, den ).'; | ||
|
||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use hypot function