Mercurial > repos > public > sbplib
changeset 1331:60c875c18de3 feature/D2_boundary_opt
Merge with feature/poroelastic for Elastic schemes
author | Vidar Stiernström <vidar.stiernstrom@it.uu.se> |
---|---|
date | Thu, 10 Mar 2022 16:54:26 +0100 |
parents | 855871e0b852 (current diff) 412b8ceafbc6 (diff) |
children | 8e9df030a0a5 |
files | +multiblock/DefCurvilinear.m +multiblock/DiffOp.m +parametrization/Curve.m +parametrization/dataSpline.m +sbp/D2VariableCompatible.m +scheme/Elastic2dCurvilinear.m +scheme/Elastic2dVariable.m diracDiscr.m diracDiscrTest.m |
diffstat | 49 files changed, 9192 insertions(+), 245 deletions(-) [+] |
line wrap: on
line diff
diff -r 855871e0b852 -r 60c875c18de3 +grid/Staggered.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/Staggered.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,112 @@ +classdef Staggered < grid.Structured + properties + gridGroups % Cell array of grid groups, each group is a cell array + nGroups % Number of grid groups + h % Interior grid spacing + d % Number of dimensions + logic % Grid in logical domain, if any. + end + + methods + + % Accepts multiple grids and combines them into a staggered grid + % Each grid entry is a cell array of grids that store the same field + function obj = Staggered(d, varargin) + default_arg('d', 2); + + obj.d = d; + + obj.nGroups = length(varargin); + obj.gridGroups = cell(obj.nGroups, 1); + for i = 1:obj.nGroups + obj.gridGroups{i} = varargin{i}; + end + + obj.h = []; + obj.logic = []; + end + + % N returns the number of points in the first grid group + function o = N(obj) + o = 0; + gs = obj.gridGroups{1}; + for i = 1:length(gs) + o = o+gs{i}.N(); + end + end + + % D returns the spatial dimension of the grid + function o = D(obj) + o = obj.d; + end + + % size returns a reference size + function m = size(obj) + m = obj.gridGroups{1}{1}; + end + + % points returns an n x 1 vector containing the coordinates for the first grid group. + function X = points(obj) + X = []; + gs = obj.gridGroups{1}; + for i = 1:length(gs) + X = [X; gs{i}.points()]; + end + end + + % matrices returns a cell array with coordinates in matrix form. + % For 2d case these will have to be transposed to work with plotting routines. + function X = matrices(obj) + error('grid:Staggered1d:matrices', 'Not implemented') + end + + function h = scaling(obj) + if isempty(obj.h) + error('grid:Staggered1d:NoScalingSet', 'No scaling set') + end + + h = obj.h; + end + + % Restricts the grid function gf on obj to the subgrid g. + % Only works for even multiples + function gf = restrictFunc(obj, gf, g) + error('grid:Staggered1d:NotImplemented','This method does not exist yet') + end + + % Projects the grid function gf on obj to the grid g. + function gf = projectFunc(obj, gf, g) + error('grid:Staggered1d:NotImplemented','This method does not exist yet') + end + + % Return the names of all boundaries in this grid. + function bs = getBoundaryNames(obj) + switch obj.d() + case 1 + bs = {'l', 'r'}; + case 2 + bs = {'w', 'e', 's', 'n'}; + case 3 + bs = {'w', 'e', 's', 'n', 'd', 'u'}; + otherwise + error('not implemented'); + end + end + + % Return coordinates for the given boundary + % gridGroup (scalar) - grid group to return coordinates for + % subGrids (array) - specifies which grids in the grid group to include (default: all grids in the grid group) + function X = getBoundary(obj, name, gridGroup, subGrids) + + default_arg('gridGroup' , 1); + grids = obj.gridGroups{gridGroup}; + default_arg('subGrids' , 1:numel(grids)); + + X = []; + for i = subGrids + X = [X; grids{i}.getBoundary(name)]; + end + end + + end +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/evalOnStaggered.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/evalOnStaggered.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,29 @@ +function gf = evalOnStaggered(g, f, gridGroup, subGrids) + + default_arg('gridGroup', 1); + + gf = []; + + if isa(g, 'multiblock.Grid') + + nSubGrids = numel(g.grids{1}.gridGroups{gridGroup}); + default_arg('subGrids', 1:nSubGrids ); + + for i = 1:g.nBlocks() + for j = subGrids + gf = [gf; grid.evalOn(g.grids{i}.gridGroups{gridGroup}{j}, f)]; + end + end + + else + + nSubGrids = numel(g.gridGroups{gridGroup}); + default_arg('subGrids', 1:nSubGrids ); + + for j = subGrids + gf = [gf; grid.evalOn(g.grids{i}.gridGroups{gridGroup}{j}, f)]; + end + + end + +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/funcToMatrix.m --- a/+grid/funcToMatrix.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+grid/funcToMatrix.m Thu Mar 10 16:54:26 2022 +0100 @@ -1,5 +1,5 @@ % Converts a gridfunction to a matrix -% Takes a grid function and and a structured grid. +% Takes a grid function and a structured grid. function F = funcToMatrix(g, gf) F = reshapeRowMaj(gf, g.size()); end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/funcToPlotMatrix.m --- a/+grid/funcToPlotMatrix.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+grid/funcToPlotMatrix.m Thu Mar 10 16:54:26 2022 +0100 @@ -1,5 +1,5 @@ % Converts a gridfunction to a plot matrix -% Takes a grid function and and a structured grid. +% Takes a grid function and a structured grid. function F = funcToPlotMatrix(g, gf) F = reshapeToPlotMatrix(gf, g.size()); end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/lebedev2d.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/lebedev2d.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,57 @@ +% Creates a 2D staggered grid of Lebedev (checkerboard) type +% Primal grid: equidistant with m points. +% Dual grid: m + 1 points, h/2 spacing first point. +% First grid line is "primal", 2nd is "dual", etc. +% +% Examples +% g = grid.Lebedev2d(m, xlims, ylims) +% g = grid.Lebedev2d([21, 31], {0,2}, {0,3}) +function g = lebedev2d(m, xlims, ylims, opSet) + + default_arg('opSet', @(m,lim) sbp.D1StaggeredUpwind(m,lim,2)); + + if ~iscell(xlims) || numel(xlims) ~= 2 + error('grid:lebedev2D:InvalidLimits','The limits should be cell arrays with 2 elements.'); + end + + if ~iscell(ylims) || numel(ylims) ~= 2 + error('grid:lebedev2D:InvalidLimits','The limits should be cell arrays with 2 elements.'); + end + + if xlims{1} > xlims{2} + error('grid:lebedev2D:InvalidLimits','The elements of the limit must be increasing.'); + end + + if ylims{1} > ylims{2} + error('grid:lebedev2D:InvalidLimits','The elements of the limit must be increasing.'); + end + + opsX = opSet(m(1), xlims); + xp = opsX.x_primal; + xd = opsX.x_dual; + + opsY = opSet(m(2), ylims); + yp = opsY.x_primal; + yd = opsY.x_dual; + + % 4 Cartesian grids with spacing h + % 2 grids for displacements (u) + % 2 grids for stresses (sigma) + % Density needs to be evaluated on the u grids + % The stiffness tensor is evaluated on the sigma grids + + gu1 = grid.Cartesian(xp, yp); + gu2 = grid.Cartesian(xd, yd); + gs1 = grid.Cartesian(xd, yp); + gs2 = grid.Cartesian(xp, yd); + + gu = {gu1, gu2}; + gs = {gs1, gs2}; + + dim = 2; + g = grid.Staggered(dim, gu, gs); + +end + + +
diff -r 855871e0b852 -r 60c875c18de3 +grid/lebedev2dCurvilinear.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/lebedev2dCurvilinear.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,46 @@ +% Creates a curvilinear 2d lebedev2d grid +% over the logical domain xi_lim, eta_lim, ... +% If all limits are ommited they are set to {0,1}. +% Examples: +% g = grid.lebedev2dCurvilinear(mapping, [m_xi, m_eta]) +% g = grid.lebedev2dCurvilinear(mapping, [m_xi, m_eta], xi_lim, eta_lim) +% g = grid.lebedev2dCurvilinear(mapping, [10, 15], {0,1}, {0,1}) +function g = lebedev2dCurvilinear(mapping, m, varargin) + if isempty(varargin) + varargin = repmat({{0,1}}, [1 length(m)]); + end + + if length(m) ~= length(varargin) + error('grid:lebedev2d:NonMatchingParameters','The number of provided dimensions do not match.') + end + + for i = 1:length(m) + if ~iscell(varargin{i}) || numel(varargin{i}) ~= 2 + error('grid:lebedev2d:InvalidLimits','The limits should be cell arrays with 2 elements.'); + end + + if varargin{i}{1} > varargin{i}{2} + error('grid:lebedev2d:InvalidLimits','The elements of the limit must be increasing.'); + end + end + + g_logic = grid.lebedev2d(m, varargin{:}); + + gu1_logic = g_logic.gridGroups{1}{1}; + gu2_logic = g_logic.gridGroups{1}{2}; + gs1_logic = g_logic.gridGroups{2}{1}; + gs2_logic = g_logic.gridGroups{2}{2}; + + gu1 = grid.Curvilinear(mapping, gu1_logic.x{1}, gu1_logic.x{2}); + gu2 = grid.Curvilinear(mapping, gu2_logic.x{1}, gu2_logic.x{2}); + gs1 = grid.Curvilinear(mapping, gs1_logic.x{1}, gs1_logic.x{2}); + gs2 = grid.Curvilinear(mapping, gs2_logic.x{1}, gs2_logic.x{2}); + + gu = {gu1, gu2}; + gs = {gs1, gs2}; + + dim = 2; + g = grid.Staggered(dim, gu, gs); + + g.logic = g_logic; +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/primalDual1D.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/primalDual1D.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,30 @@ +% Creates a 1D staggered grid of dimension length(m). +% over the interval xlims +% Primal grid: equidistant with m points. +% Dual grid: m + 1 points, h/2 spacing first point. +% Examples +% g = grid.primal_dual_1D(m, xlim) +% g = grid.primal_dual_1D(11, {0,1}) +function [g_primal, g_dual] = primalDual1D(m, xlims) + + if ~iscell(xlims) || numel(xlims) ~= 2 + error('grid:primalDual1D:InvalidLimits','The limits should be cell arrays with 2 elements.'); + end + + if xlims{1} > xlims{2} + error('grid:primalDual1D:InvalidLimits','The elements of the limit must be increasing.'); + end + + xl = xlims{1}; + xr = xlims{2}; + h = (xr-xl)/(m-1); + + % Primal grid + g_primal = grid.equidistant(m, xlims); + g_primal.h = h; + + % Dual grid + x = [xl; linspace(xl+h/2, xr-h/2, m-1)'; xr]; + g_dual = grid.Cartesian(x); + g_dual.h = h; +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +grid/primalDual1DTest.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+grid/primalDual1DTest.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,32 @@ +function tests = primalDual1DTest() + tests = functiontests(localfunctions); +end + + +function testErrorInvalidLimits(testCase) + in = { + {10,{1}}, + {10,[0,1]}, + {10,{1,0}}, + }; + + for i = 1:length(in) + testCase.verifyError(@()grid.primalDual1D(in{i}{:}),'grid:primalDual1D:InvalidLimits',sprintf('in(%d) = %s',i,toString(in{i}))); + end +end + +function testCompiles(testCase) + in = { + {5, {0,1}}, + }; + + out = { + {[0; 0.25; 0.5; 0.75; 1], [0; 0.125; 0.375; 0.625; 0.875; 1]}, + }; + + for i = 1:length(in) + [gp, gd] = grid.primalDual1D(in{i}{:}); + testCase.verifyEqual(gp.points(),out{i}{1}); + testCase.verifyEqual(gd.points(),out{i}{2}); + end +end
diff -r 855871e0b852 -r 60c875c18de3 +multiblock/DefCurvilinear.m --- a/+multiblock/DefCurvilinear.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+multiblock/DefCurvilinear.m Thu Mar 10 16:54:26 2022 +0100 @@ -74,6 +74,18 @@ g = multiblock.Grid(grids, obj.connections, obj.boundaryGroups); end + function g = getLebedevGrid(obj, varargin) + ms = obj.getGridSizes(varargin{:}); + + grids = cell(1, obj.nBlocks); + for i = 1:obj.nBlocks + % grids{i} = grid.equidistantCurvilinear(obj.blockMaps{i}.S, ms{i}); + grids{i} = grid.lebedev2dCurvilinear(obj.blockMaps{i}.S, ms{i}); + end + + g = multiblock.Grid(grids, obj.connections, obj.boundaryGroups); + end + function h = show(obj, label, gridLines, varargin) default_arg('label', 'name') default_arg('gridLines', false);
diff -r 855871e0b852 -r 60c875c18de3 +multiblock/DiffOp.m --- a/+multiblock/DiffOp.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+multiblock/DiffOp.m Thu Mar 10 16:54:26 2022 +0100 @@ -129,19 +129,20 @@ % Get a boundary operator specified by opName for the given boundary/BoundaryGroup function op = getBoundaryOperator(obj, opName, boundary) + blockmatrixDiv = obj.blockmatrixDiv{1}; switch class(boundary) case 'cell' blockId = boundary{1}; localOp = obj.diffOps{blockId}.getBoundaryOperator(opName, boundary{2}); - div = {obj.blockmatrixDiv{1}, size(localOp,2)}; + div = {blockmatrixDiv, size(localOp,2)}; blockOp = blockmatrix.zero(div); blockOp{blockId,1} = localOp; op = blockmatrix.toMatrix(blockOp); return case 'multiblock.BoundaryGroup' - op = sparse(size(obj.D,1),0); + op = sparse(sum(blockmatrixDiv),0); for i = 1:length(boundary) op = [op, obj.getBoundaryOperator(opName, boundary{i})]; end @@ -208,6 +209,57 @@ error('not implemented') end + function penalties = interfaceForcing(obj, boundary, neighbour_boundary, type) + default_arg('type', []); + switch class(boundary) + case 'cell' + penalties = obj.singleInterfaceForcing(boundary, neighbour_boundary, type); + case 'multiblock.BoundaryGroup' + [n,m] = size(obj.D); + penaltyZero = sparse(n,0); + + for i = 1:length(boundary) + penaltyParts = obj.interfaceForcing(boundary{i}, neighbour_boundary{i}, type); + + % Initalize if this is the first boundary + if i==1 + penalties = cell(numel(penaltyParts), 1); + for j = 1:numel(penaltyParts) + penalties{j} = penaltyZero; + end + end + + for j = 1:numel(penaltyParts) + penalties{j} = [penalties{j}, penaltyParts{j}]; + end + + end + otherwise + error('Unknown boundary indentifier') + end + + end + + function penalties = singleInterfaceForcing(obj, boundary, neighbour_boundary, type) + I = boundary{1}; + b1 = boundary{2}; + + J = neighbour_boundary{1}; + b2 = neighbour_boundary{2}; + + % Get local penalties + [~, ~, blockPenalties] = obj.diffOps{I}.interface(b1, obj.diffOps{J}, b2, type); + % [~, ~, blockPenalties2] = obj.diffOps{J}.interface(b2, b1, obj.diffOps{I}); + + % Expand to matrices for full domain. + n = numel(blockPenalties); + penalties = cell(n, 1); + for i = 1:n + penalties{i} = multiblock.local2globalPenalty(blockPenalties{i}, obj.blockmatrixDiv, I); + % + multiblock.local2globalPenalty(blockPenalties2{i}, obj.blockmatrixDiv, J); + end + end + % Size returns the number of degrees of freedom function N = size(obj) N = 0;
diff -r 855871e0b852 -r 60c875c18de3 +multiblock/Grid.m --- a/+multiblock/Grid.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+multiblock/Grid.m Thu Mar 10 16:54:26 2022 +0100 @@ -132,6 +132,27 @@ end + % Pads a grid function that lives on a subgrid with + % zeros and gives it the size that mathces obj. + function gf = expandFunc(obj, gfSub, subGridId) + nComponents = length(gfSub)/obj.grids{subGridId}.N(); + nBlocks = numel(obj.grids); + + % Create sparse block matrix + gfs = cell(nBlocks,1); + for i = 1:nBlocks + N = obj.grids{i}.N()*nComponents; + gfs{i} = sparse(N, 1); + end + + % Insert local gf + gfs{subGridId} = gfSub; + + % Convert cell to vector + gf = blockmatrix.toMatrix(gfs); + + end + % Find all non interface boundaries of all blocks. % Return their grid.boundaryIdentifiers in a cell array. function bs = getBoundaryNames(obj) @@ -155,16 +176,16 @@ end % Return coordinates for the given boundary/boundaryGroup - function b = getBoundary(obj, boundary) + function b = getBoundary(obj, boundary, varargin) switch class(boundary) case 'cell' I = boundary{1}; name = boundary{2}; - b = obj.grids{I}.getBoundary(name); + b = obj.grids{I}.getBoundary(name, varargin{:}); case 'multiblock.BoundaryGroup' b = sparse(0,obj.D()); for i = 1:length(boundary) - b = [b; obj.getBoundary(boundary{i})]; + b = [b; obj.getBoundary(boundary{i}, varargin{:})]; end otherwise error('Unknown boundary indentifier')
diff -r 855871e0b852 -r 60c875c18de3 +multiblock/StaggeredSurface.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+multiblock/StaggeredSurface.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,97 @@ +classdef StaggeredSurface < handle + properties + grid + surfs + gridGroup + subGrid + + ZData + CData + + end + + methods + function obj = StaggeredSurface(g, gf, gridGroup, subGrid) + + default_arg('gridGroup', 1); + default_arg('subGrid', 1); + + obj.grid = g; + obj.gridGroup = gridGroup; + obj.subGrid = subGrid; + + % coords = obj.grid.points(); + % X = obj.grid.funcToPlotMatrices(coords(:,1)); + % Y = obj.grid.funcToPlotMatrices(coords(:,2)); + % V = obj.grid.funcToPlotMatrices(gf); + X = {}; + Y = {}; + V = {}; + + holdState = ishold(); + hold on + + surfs = cell(1, obj.grid.nBlocks); + gfIndex = 1; + for i = 1:g.nBlocks() + + gi = g.grids{i}.gridGroups{gridGroup}{subGrid}; + + X{i} = grid.funcToPlotMatrix(gi, gi.coords(:,1)); + Y{i} = grid.funcToPlotMatrix(gi, gi.coords(:,2)); + + Ni = gi.N(); + gf_i = gf(gfIndex:gfIndex+Ni-1); + V{i} = grid.funcToPlotMatrix(gi, gf_i); + + surfs{i} = surf(X{i}, Y{i}, V{i}); + gfIndex = gfIndex + Ni; + end + + if holdState == false + hold off + end + + obj.surfs = [surfs{:}]; + + obj.ZData = gf; + obj.CData = gf; + end + + function set(obj, propertyName, propertyValue) + set(obj.surfs, propertyName, propertyValue); + end + + function obj = set.ZData(obj, gf) + obj.ZData = gf; + + % V = obj.grid.funcToPlotMatrices(gf); + gfIndex = 1; + for i = 1:obj.grid.nBlocks() + gi = obj.grid.grids{i}.gridGroups{obj.gridGroup}{obj.subGrid}; + Ni = gi.N(); + gf_i = gf(gfIndex:gfIndex+Ni-1); + Vi = grid.funcToPlotMatrix(gi, gf_i); + obj.surfs(i).ZData = Vi; + + gfIndex = gfIndex + Ni; + end + end + + function obj = set.CData(obj, gf) + obj.CData = gf; + + % V = obj.grid.funcToPlotMatrices(gf); + gfIndex = 1; + for i = 1:obj.grid.nBlocks() + gi = obj.grid.grids{i}.gridGroups{obj.gridGroup}{obj.subGrid}; + Ni = gi.N(); + gf_i = gf(gfIndex:gfIndex+Ni-1); + Vi = grid.funcToPlotMatrix(gi, gf_i); + obj.surfs(i).CData = Vi; + + gfIndex = gfIndex + Ni; + end + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/coeffs_d2_variable_2.mat Binary file +sbp/+implementations/coeffs_d2_variable_2.mat has changed
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/coeffs_d2_variable_4.mat Binary file +sbp/+implementations/coeffs_d2_variable_4.mat has changed
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/coeffs_d2_variable_6.mat Binary file +sbp/+implementations/coeffs_d2_variable_6.mat has changed
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d1_staggered_upwind_2.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d1_staggered_upwind_2.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,73 @@ +function [xp,xm,Hp,Hm,HIp,HIm,Dp,Dm,Dpp,Dpm,Dmp,Dmm] = d1_staggered_upwind_2(n,L) + +assert(n-1 >= 6,'Not enough grid points'); + +np=n; +nm=n+1; +h = L/(n-1); + + +%H- norm +Hm_U=[0.88829e5 / 0.599299e6 0 0; 0 0.8056187e7 / 0.9588784e7 0; 0 0 0.9700117e7 / 0.9588784e7;]; +%H+ norm +Hp_U=[0.7489557e7 / 0.19177568e8 0 0; 0 0.1386089e7 / 0.1198598e7 0; 0 0 0.18276939e8 / 0.19177568e8;]; +% Upper part of Qpp. Notice that the B matrix is not included here. +% Qpp+Qpp^T=S/2,Qpp+Qpm^T=0 +Qpp_U=[-0.1e1 / 0.32e2 0.12886609e8 / 0.19177568e8 -0.1349263e7 / 0.9588784e7; -0.10489413e8 / 0.19177568e8 -0.3e1 / 0.16e2 0.16482403e8 / 0.19177568e8; 0.187491e6 / 0.2397196e7 -0.9290815e7 / 0.19177568e8 -0.11e2 / 0.32e2;]; +% Upper part of Qmp. Notice that the B matrix is not included here. +% Qmp+Qmp^T=S/2,Qmp+Qmm^T=0 +Qmp_U=[-0.2e1 / 0.21e2 0.12495263e8 / 0.16780372e8 -0.7520839e7 / 0.50341116e8; -0.7700871e7 / 0.16780372e8 -0.31e2 / 0.112e3 0.57771939e8 / 0.67121488e8; 0.2726447e7 / 0.50341116e8 -0.31402783e8 / 0.67121488e8 -0.113e3 / 0.336e3;]; +% The staggered + operator, upper part. Notice that the B matrix is not included here.Qp+Qm^T=0 +Qp_U=[-0.801195e6 / 0.2397196e7 0.16507959e8 / 0.19177568e8 -0.509615e6 / 0.19177568e8; -0.219745e6 / 0.1198598e7 -0.2112943e7 / 0.2397196e7 0.2552433e7 / 0.2397196e7; 0.42087e5 / 0.2397196e7 0.395585e6 / 0.19177568e8 -0.19909849e8 / 0.19177568e8;]; +Hp=spdiags(ones(np,1),0,np,np); +Hp(1:3,1:3)=Hp_U; +Hp(np-2:np,np-2:np)=fliplr(flipud(Hp_U)); +Hp=Hp*h; +HIp=inv(Hp); + +Hm=spdiags(ones(nm,1),0,nm,nm); +Hm(1:3,1:3)=Hm_U; +Hm(nm-2:nm,nm-2:nm)=fliplr(flipud(Hm_U)); +Hm=Hm*h; +HIm=inv(Hm); + +Qpp=spdiags(repmat([-0.3e1 / 0.8e1 -0.3e1 / 0.8e1 0.7e1 / 0.8e1 -0.1e1 / 0.8e1;],[np,1]),-1:2,np,np); +Qpp(1:3,1:3)=Qpp_U; +Qpp(np-2:np,np-2:np)=flipud( fliplr(Qpp_U(1:3,1:3) ) )'; + +Qpm=-Qpp'; + +Qmp=spdiags(repmat([-0.3e1 / 0.8e1 -0.3e1 / 0.8e1 0.7e1 / 0.8e1 -0.1e1 / 0.8e1;],[nm,1]),-1:2,nm,nm); +Qmp(1:3,1:3)=Qmp_U; +Qmp(nm-2:nm,nm-2:nm)=flipud( fliplr(Qmp_U(1:3,1:3) ) )'; + +Qmm=-Qmp'; + + +Bpp=spalloc(np,np,2);Bpp(1,1)=-1;Bpp(np,np)=1; +Bmp=spalloc(nm,nm,2);Bmp(1,1)=-1;Bmp(nm,nm)=1; + +Dpp=HIp*(Qpp+1/2*Bpp) ; +Dpm=HIp*(Qpm+1/2*Bpp) ; + + +Dmp=HIm*(Qmp+1/2*Bmp) ; +Dmm=HIm*(Qmm+1/2*Bmp) ; + + +%%% Start with the staggered +Qp=spdiags(repmat([-1 1],[np,1]),0:1,np,nm); +Qp(1:3,1:3)=Qp_U; +Qp(np-2:np,nm-2:nm)=flipud( fliplr(-Qp_U(1:3,1:3) ) ); +Qm=-Qp'; + +Bp=spalloc(np,nm,2);Bp(1,1)=-1;Bp(np,nm)=1; +Bm=Bp'; + +Dp=HIp*(Qp+1/2*Bp) ; + +Dm=HIm*(Qm+1/2*Bm) ; + +% grids +xp = h*[0:n]'; +xm = h*[0 1/2+0:n n]'; \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d1_staggered_upwind_4.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d1_staggered_upwind_4.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,76 @@ +function [xp,xm,Hp,Hm,HIp,HIm,Dp,Dm,Dpp,Dpm,Dmp,Dmm] = d1_staggered_upwind_4(n,L) + +assert(n-1 >= 8,'Not enough grid points'); + +np=n; +nm=n+1; +h = L/(n-1); + + +%H- norm +Hm_U=[0.91e2 / 0.720e3 0 0 0; 0 0.325e3 / 0.384e3 0 0; 0 0 0.595e3 / 0.576e3 0; 0 0 0 0.1909e4 / 0.1920e4;]; +%H+ norm +Hp_U=[0.805e3 / 0.2304e4 0 0 0; 0 0.955e3 / 0.768e3 0 0; 0 0 0.677e3 / 0.768e3 0; 0 0 0 0.2363e4 / 0.2304e4;]; +% Upper part of Qpp. Notice that the B matrix is not included here. +% Qpp+Qpp^T=S/2,Qpp+Qpm^T=0 +Qpp_U=[-0.11e2 / 0.1536e4 0.1493e4 / 0.2304e4 -0.571e3 / 0.4608e4 -0.13e2 / 0.768e3; -0.697e3 / 0.1152e4 -0.121e3 / 0.1536e4 0.97e2 / 0.128e3 -0.473e3 / 0.4608e4; 0.373e3 / 0.4608e4 -0.139e3 / 0.256e3 -0.319e3 / 0.1536e4 0.1999e4 / 0.2304e4; 0.1e1 / 0.32e2 -0.121e3 / 0.4608e4 -0.277e3 / 0.576e3 -0.143e3 / 0.512e3;]; + +% Upper part of Qmp. Notice that the B matrix is not included here. +% Qmp+Qmp^T=S/2,Qmp+Qmm^T=0 +Qmp_U=[-0.209e3 / 0.5970e4 0.13439e5 / 0.17910e5 -0.13831e5 / 0.47760e5 0.10637e5 / 0.143280e6; -0.44351e5 / 0.71640e5 -0.20999e5 / 0.152832e6 0.230347e6 / 0.229248e6 -0.70547e5 / 0.254720e6; 0.3217e4 / 0.15920e5 -0.86513e5 / 0.114624e6 -0.15125e5 / 0.76416e5 0.1087241e7 / 0.1146240e7; -0.1375e4 / 0.28656e5 0.36117e5 / 0.254720e6 -0.655601e6 / 0.1146240e7 -0.211717e6 / 0.764160e6;]; + +% The staggered + operator, upper part. Notice that the B matrix is not included here.Qp+Qm^T=0 +Qp_U=[-0.338527e6 / 0.1004160e7 0.4197343e7 / 0.4819968e7 0.1423e4 / 0.803328e6 -0.854837e6 / 0.24099840e8; -0.520117e6 / 0.3012480e7 -0.492581e6 / 0.535552e6 0.2476673e7 / 0.2409984e7 0.520117e6 / 0.8033280e7; -0.50999e5 / 0.3012480e7 0.117943e6 / 0.1606656e7 -0.2476673e7 / 0.2409984e7 0.2712193e7 / 0.2677760e7; 0.26819e5 / 0.1004160e7 -0.117943e6 / 0.4819968e7 -0.1423e4 / 0.803328e6 -0.26119411e8 / 0.24099840e8;]; + +Hp=spdiags(ones(np,1),0,np,np); +Hp(1:4,1:4)=Hp_U; +Hp(np-3:np,np-3:np)=fliplr(flipud(Hp_U)); +Hp=Hp*h; +HIp=inv(Hp); + +Hm=spdiags(ones(nm,1),0,nm,nm); +Hm(1:4,1:4)=Hm_U; +Hm(nm-3:nm,nm-3:nm)=fliplr(flipud(Hm_U)); +Hm=Hm*h; +HIm=inv(Hm); + +Qpp=spdiags(repmat([0.7e1 / 0.128e3 -0.67e2 / 0.128e3 -0.55e2 / 0.192e3 0.61e2 / 0.64e2 -0.29e2 / 0.128e3 0.11e2 / 0.384e3;],[np,1]),-2:3,np,np); +Qpp(1:4,1:4)=Qpp_U; +Qpp(np-3:np,np-3:np)=flipud( fliplr(Qpp_U(1:4,1:4) ) )'; + +Qpm=-Qpp'; + +Qmp=spdiags(repmat([0.7e1 / 0.128e3 -0.67e2 / 0.128e3 -0.55e2 / 0.192e3 0.61e2 / 0.64e2 -0.29e2 / 0.128e3 0.11e2 / 0.384e3;],[nm,1]),-2:3,nm,nm); +Qmp(1:4,1:4)=Qmp_U; +Qmp(nm-3:nm,nm-3:nm)=flipud( fliplr(Qmp_U(1:4,1:4) ) )'; + +Qmm=-Qmp'; + + +Bpp=spalloc(np,np,2);Bpp(1,1)=-1;Bpp(np,np)=1; +Bmp=spalloc(nm,nm,2);Bmp(1,1)=-1;Bmp(nm,nm)=1; + +Dpp=HIp*(Qpp+1/2*Bpp) ; +Dpm=HIp*(Qpm+1/2*Bpp) ; + + +Dmp=HIm*(Qmp+1/2*Bmp) ; +Dmm=HIm*(Qmm+1/2*Bmp) ; + + +%%% Start with the staggered +Qp=spdiags(repmat([1/24 -9/8 9/8 -1/24],[np,1]),-1:2,np,nm); +Qp(1:4,1:4)=Qp_U; +Qp(np-3:np,nm-3:nm)=flipud( fliplr(-Qp_U(1:4,1:4) ) ); +Qm=-Qp'; + +Bp=spalloc(np,nm,2);Bp(1,1)=-1;Bp(np,nm)=1; +Bm=Bp'; + +Dp=HIp*(Qp+1/2*Bp) ; + +Dm=HIm*(Qm+1/2*Bm) ; + +% grids +xp = h*[0:n]'; +xm = h*[0 1/2+0:n n]'; \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d1_staggered_upwind_6.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d1_staggered_upwind_6.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,78 @@ +function [xp,xm,Hp,Hm,HIp,HIm,Dp,Dm,Dpp,Dpm,Dmp,Dmm] = d1_staggered_upwind_6(n,L) + +assert(n-1 >= 12,'Not enough grid points'); + +np=n; +nm=n+1; +h = L/(n-1); + +bp=6; % Number of boundary points + + +%H- norm +Hm_U=[0.18373e5 / 0.136080e6 0 0 0 0 0; 0 0.228247e6 / 0.276480e6 0 0 0 0; 0 0 0.219557e6 / 0.207360e6 0 0 0; 0 0 0 0.44867e5 / 0.46080e5 0 0; 0 0 0 0 0.487757e6 / 0.483840e6 0; 0 0 0 0 0 0.2485447e7 / 0.2488320e7;]; +%H+ norm +Hp_U=[0.174529e6 / 0.552960e6 0 0 0 0 0; 0 0.769723e6 / 0.552960e6 0 0 0 0; 0 0 0.172613e6 / 0.276480e6 0 0 0; 0 0 0 0.343867e6 / 0.276480e6 0 0; 0 0 0 0 0.503237e6 / 0.552960e6 0; 0 0 0 0 0 0.560831e6 / 0.552960e6;]; +% Upper part of Qpp. Notice that the B matrix is not included here. +% Qpp+Qpp^T=S/2,Qpp+Qpm^T=0 +Qpp_U=[-0.11e2 / 0.12288e5 0.3248629e7 / 0.4976640e7 -0.742121e6 / 0.9953280e7 -0.173089e6 / 0.1658880e7 0.100627e6 / 0.9953280e7 0.263e3 / 0.15552e5; -0.3212989e7 / 0.4976640e7 -0.341e3 / 0.20480e5 0.488429e6 / 0.995328e6 0.2108717e7 / 0.9953280e7 0.5623e4 / 0.829440e6 -0.468779e6 / 0.9953280e7; 0.635201e6 / 0.9953280e7 -0.2135641e7 / 0.4976640e7 -0.2233e4 / 0.30720e5 0.298757e6 / 0.497664e6 -0.2148901e7 / 0.9953280e7 0.99581e5 / 0.1658880e7; 0.184969e6 / 0.1658880e7 -0.2671829e7 / 0.9953280e7 -0.1044721e7 / 0.2488320e7 -0.4697e4 / 0.30720e5 0.882599e6 / 0.995328e6 -0.2114063e7 / 0.9953280e7; -0.118447e6 / 0.9953280e7 0.15761e5 / 0.829440e6 0.915757e6 / 0.9953280e7 -0.2923243e7 / 0.4976640e7 -0.4279e4 / 0.20480e5 0.4614511e7 / 0.4976640e7; -0.263e3 / 0.15552e5 0.422447e6 / 0.9953280e7 -0.5185e4 / 0.331776e6 0.424727e6 / 0.9953280e7 -0.570779e6 / 0.995328e6 -0.2761e4 / 0.12288e5;]; + +% Upper part of Qmp. Notice that the B matrix is not included here. +% Qmp+Qmp^T=S/2,Qmp+Qmm^T=0 +Qmp_U=[-0.6660404399e10 / 0.975680535935e12 0.42802131970831759e17 / 0.60695134779444480e17 -0.27241603626152813e17 / 0.91042702169166720e17 0.148772145985039e15 / 0.1264481974571760e16 -0.386865438537449e15 / 0.30347567389722240e17 -0.739491571084877e15 / 0.182085404338333440e18; -0.40937739835891039e17 / 0.60695134779444480e17 -0.20934998251893e14 / 0.570912496455680e15 0.3069347264824655e16 / 0.3082927480860672e16 -0.531249064089227e15 / 0.1541463740430336e16 0.6343721417240047e16 / 0.107902461830123520e18 0.194756943144697e15 / 0.138731736638730240e18; 0.24212381292051773e17 / 0.91042702169166720e17 -0.13932096926615587e17 / 0.15414637404303360e17 -0.22149140293797e14 / 0.285456248227840e15 0.443549615179363e15 / 0.481707418884480e15 -0.1531771257444041e16 / 0.5994581212784640e16 0.23579222779798361e17 / 0.416195209916190720e18; -0.119651391006031e15 / 0.1264481974571760e16 0.2061363806050549e16 / 0.7707318702151680e16 -0.355212104634871e15 / 0.481707418884480e15 -0.42909037900311e14 / 0.285456248227840e15 0.25466291778687943e17 / 0.26975615457530880e17 -0.3289076301679109e16 / 0.11560978053227520e17; 0.153994229603129e15 / 0.30347567389722240e17 -0.2655886084488631e16 / 0.107902461830123520e18 0.782300684927837e15 / 0.5994581212784640e16 -0.17511430871269903e17 / 0.26975615457530880e17 -0.413193098349471e15 / 0.1998193737594880e16 0.189367309285289755e18 / 0.194224431294222336e18; 0.894580992211517e15 / 0.182085404338333440e18 -0.209441083772219e15 / 0.27746347327746048e17 -0.4946149632449393e16 / 0.416195209916190720e18 0.334964642443661e15 / 0.2890244513306880e16 -0.604469352802317407e18 / 0.971122156471111680e18 -0.128172128502407e15 / 0.570912496455680e15;]; + +% The staggered + operator, upper part. Notice that the B matrix is not included here.Qp+Qm^T=0 +Qp_U=[-0.34660470729017653729e20 / 0.113641961250214656000e21 0.351671379135966469961e21 / 0.415604886857927884800e21 0.1819680091728191503e19 / 0.103901221714481971200e21 -0.18252344147469061739e20 / 0.346337405714939904000e21 -0.18145368485798816351e20 / 0.727308552001373798400e21 0.2627410615589536403e19 / 0.138534962285975961600e21; -0.1606450873889019037e19 / 0.7576130750014310400e19 -0.8503979509850519441e19 / 0.9235664152398397440e19 0.2208731907526094393e19 / 0.2308916038099599360e19 0.1143962309827873891e19 / 0.7696386793665331200e19 0.1263616990270014071e19 / 0.16162412266697195520e20 -0.1402288892096389187e19 / 0.27706992457195192320e20; -0.502728075208147729e18 / 0.11364196125021465600e20 0.5831273443201206481e19 / 0.41560488685792788480e20 -0.9031420599281409001e19 / 0.10390122171448197120e20 0.29977986617775158621e20 / 0.34633740571493990400e20 -0.7995649008389734663e19 / 0.72730855200137379840e20 0.728315692435313537e18 / 0.41560488685792788480e20; 0.710308100786581369e18 / 0.11364196125021465600e20 -0.2317346723533341809e19 / 0.41560488685792788480e20 -0.1357359577229545879e19 / 0.10390122171448197120e20 -0.35124499190079631261e20 / 0.34633740571493990400e20 0.81675241511291974823e20 / 0.72730855200137379840e20 0.144034831596315317e18 / 0.13853496228597596160e20; 0.13360631165154733e17 / 0.841792305557145600e18 -0.875389186128426797e18 / 0.27706992457195192320e20 0.95493318392786453e17 / 0.6926748114298798080e19 0.1714625642820850967e19 / 0.23089160380995993600e20 -0.53371483072841696197e20 / 0.48487236800091586560e20 0.30168063964639488547e20 / 0.27706992457195192320e20; -0.1943232250834614071e19 / 0.113641961250214656000e21 0.8999269402554660119e19 / 0.415604886857927884800e21 0.1242786058815312817e19 / 0.103901221714481971200e21 -0.7480218714053301461e19 / 0.346337405714939904000e21 0.28468183824757664191e20 / 0.727308552001373798400e21 -0.476082521721490837529e21 / 0.415604886857927884800e21;]; + +Hp=spdiags(ones(np,1),0,np,np); +Hp(1:bp,1:bp)=Hp_U; +Hp(np-bp+1:np,np-bp+1:np)=fliplr(flipud(Hp_U)); +Hp=Hp*h; +HIp=inv(Hp); + +Hm=spdiags(ones(nm,1),0,nm,nm); +Hm(1:bp,1:bp)=Hm_U; +Hm(nm-bp+1:nm,nm-bp+1:nm)=fliplr(flipud(Hm_U)); +Hm=Hm*h; +HIm=inv(Hm); + +Qpp=spdiags(repmat([-0.157e3 / 0.15360e5 0.537e3 / 0.5120e4 -0.3147e4 / 0.5120e4 -0.231e3 / 0.1024e4 0.999e3 / 0.1024e4 -0.1461e4 / 0.5120e4 0.949e3 / 0.15360e5 -0.33e2 / 0.5120e4;],[np,1]),-3:4,np,np); +Qpp(1:bp,1:bp)=Qpp_U; +Qpp(np-bp+1:np,np-bp+1:np)=flipud( fliplr(Qpp_U ) )'; + +Qpm=-Qpp'; + +Qmp=spdiags(repmat([-0.157e3 / 0.15360e5 0.537e3 / 0.5120e4 -0.3147e4 / 0.5120e4 -0.231e3 / 0.1024e4 0.999e3 / 0.1024e4 -0.1461e4 / 0.5120e4 0.949e3 / 0.15360e5 -0.33e2 / 0.5120e4;],[nm,1]),-3:4,nm,nm); +Qmp(1:bp,1:bp)=Qmp_U; +Qmp(nm-bp+1:nm,nm-bp+1:nm)=flipud( fliplr(Qmp_U ) )'; + +Qmm=-Qmp'; + +Bpp=spalloc(np,np,2);Bpp(1,1)=-1;Bpp(np,np)=1; +Bmp=spalloc(nm,nm,2);Bmp(1,1)=-1;Bmp(nm,nm)=1; + + +Dpp=HIp*(Qpp+1/2*Bpp) ; +Dpm=HIp*(Qpm+1/2*Bpp) ; + + +Dmp=HIm*(Qmp+1/2*Bmp) ; +Dmm=HIm*(Qmm+1/2*Bmp) ; + + +%%% Start with the staggered +Qp=spdiags(repmat([-0.3e1 / 0.640e3 0.25e2 / 0.384e3 -0.75e2 / 0.64e2 0.75e2 / 0.64e2 -0.25e2 / 0.384e3 0.3e1 / 0.640e3],[np,1]),-2:3,np,nm); +Qp(1:bp,1:bp)=Qp_U; +Qp(np-bp+1:np,nm-bp+1:nm)=flipud( fliplr(-Qp_U ) ); +Qm=-Qp'; + +Bp=spalloc(np,nm,2);Bp(1,1)=-1;Bp(np,nm)=1; +Bm=Bp'; + +Dp=HIp*(Qp+1/2*Bp) ; + +Dm=HIm*(Qm+1/2*Bm) ; + +% grids +xp = h*[0:n]'; +xm = h*[0 1/2+0:n n]'; \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d1_staggered_upwind_8.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d1_staggered_upwind_8.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,82 @@ +function [xp,xm,Hp,Hm,HIp,HIm,Dp,Dm,Dpp,Dpm,Dmp,Dmm] = d1_staggered_upwind_8(n, L) + +assert(n-1 >= 12,'Not enough grid points'); + +np=n; +nm=n+1; +h = L/(n-1); + +bp=8; % Number of boundary points + + +%H- norm +Hm_U=[0.27058177e8 / 0.194594400e9 0 0 0 0 0 0 0; 0 0.378196601e9 / 0.464486400e9 0 0 0 0 0 0; 0 0 0.250683799e9 / 0.232243200e9 0 0 0 0 0; 0 0 0 0.48820787e8 / 0.51609600e8 0 0 0 0; 0 0 0 0 0.23953873e8 / 0.23224320e8 0 0 0; 0 0 0 0 0 0.55018021e8 / 0.55738368e8 0 0; 0 0 0 0 0 0 0.284772253e9 / 0.283852800e9 0; 0 0 0 0 0 0 0 0.6036097241e10 / 0.6038323200e10;]; + +%H+ norm +Hp_U=[0.273925927e9 / 0.928972800e9 0 0 0 0 0 0 0; 0 0.1417489391e10 / 0.928972800e9 0 0 0 0 0 0; 0 0 0.26528603e8 / 0.103219200e9 0 0 0 0 0; 0 0 0 0.66843235e8 / 0.37158912e8 0 0 0 0; 0 0 0 0 0.76542481e8 / 0.185794560e9 0 0 0; 0 0 0 0 0 0.132009637e9 / 0.103219200e9 0 0; 0 0 0 0 0 0 0.857580049e9 / 0.928972800e9 0; 0 0 0 0 0 0 0 0.937663193e9 / 0.928972800e9;]; + +% Upper part of Qpp. Notice that the B matrix is not included here. +% Qpp+Qpp^T=S/2,Qpp+Qpm^T=0 +Qpp_U=[-0.5053e4 / 0.43253760e8 0.299126491231e12 / 0.442810368000e12 -0.562745123e9 / 0.8856207360e10 -0.59616938177e11 / 0.398529331200e12 -0.5489464381e10 / 0.177124147200e12 0.6994759151e10 / 0.88562073600e11 0.712609553e9 / 0.181149696000e12 -0.14167e5 / 0.998400e6; -0.74652297589e11 / 0.110702592000e12 -0.995441e6 / 0.302776320e9 0.662285893e9 / 0.2236416000e10 0.2364992837e10 / 0.5535129600e10 0.8893339297e10 / 0.49816166400e11 -0.2166314077e10 / 0.8945664000e10 -0.78813191e8 / 0.2952069120e10 0.88810243397e11 / 0.1992646656000e13; 0.16939159e8 / 0.276756480e9 -0.2302477691e10 / 0.8200192000e10 -0.197067e6 / 0.9175040e7 0.46740527413e11 / 0.88562073600e11 -0.4827794723e10 / 0.11070259200e11 0.155404199e9 / 0.1230028800e10 0.15110006581e11 / 0.295206912000e12 -0.2491856531e10 / 0.88562073600e11; 0.7568509969e10 / 0.49816166400e11 -0.19762404121e11 / 0.44281036800e11 -0.2554553593e10 / 0.5535129600e10 -0.19550057e8 / 0.302776320e9 0.145135201e9 / 0.210862080e9 0.1596117533e10 / 0.11070259200e11 0.104983477e9 / 0.3558297600e10 -0.1087937837e10 / 0.25303449600e11; 0.5282544031e10 / 0.177124147200e12 -0.131784830977e12 / 0.797058662400e12 0.8310607171e10 / 0.22140518400e11 -0.56309573e8 / 0.105431040e9 -0.36477607e8 / 0.302776320e9 0.57443289107e11 / 0.88562073600e11 -0.220576549e9 / 0.691891200e9 0.39942520697e11 / 0.398529331200e12; -0.1743516779e10 / 0.22140518400e11 0.7784403199e10 / 0.32800768000e11 -0.459036521e9 / 0.4920115200e10 -0.5749179391e10 / 0.22140518400e11 -0.562460513e9 / 0.1383782400e10 -0.1500741e7 / 0.9175040e7 0.70986504791e11 / 0.73801728000e11 -0.930160589e9 / 0.3406233600e10; -0.712609553e9 / 0.181149696000e12 0.180761e6 / 0.6589440e7 -0.18016744831e11 / 0.295206912000e12 0.18651112477e11 / 0.797058662400e12 0.7155507361e10 / 0.44281036800e11 -0.49358401541e11 / 0.73801728000e11 -0.55032223e8 / 0.302776320e9 0.38275518139e11 / 0.40255488000e11; 0.14167e5 / 0.998400e6 -0.88810243397e11 / 0.1992646656000e13 0.650307179e9 / 0.22140518400e11 0.474654619e9 / 0.16102195200e11 -0.7267828861e10 / 0.199264665600e12 0.42227833e8 / 0.425779200e9 -0.71245142351e11 / 0.110702592000e12 -0.7998899e7 / 0.43253760e8;]; + +% Upper part of Qmp. Notice that the B matrix is not included here. +% Qmp+Qmp^T=S/2,Qmp+Qmm^T=0 +Qmp_U=[-0.92025012754706822244637e23 / 0.73350939131274317328275670e26 0.930302337374620084855601123690977e33 / 0.1359398284732080668181467336976000e34 -0.727851704787797291000113057457371e33 / 0.2718796569464161336362934673952000e34 0.7360201789777281105403766444579e31 / 0.90626552315472044545431155798400e32 0.13716157770579047943179985700303e32 / 0.543759313892832267272586934790400e33 -0.553550686983724599329589830113e30 / 0.21750372555713290690903477391616e32 0.39889728754596585193681401053e29 / 0.20596943708061828305779808136000e32 0.4597826214543803453588826224861e31 / 0.2718796569464161336362934673952000e34; -0.460817194517403953445488602736801e33 / 0.679699142366040334090733668488000e33 -0.2251237342833501172927156567e28 / 0.267062619272621870023659683840e30 0.190175037040902421814031988319053e33 / 0.202800676510147232549216572416000e33 -0.280264041304585424221475951435491e33 / 0.1081603608054118573595821719552000e34 -0.2948540748840639854083293613843e31 / 0.81120270604058893019686628966400e32 0.10373234521893519136055741251159e32 / 0.194688649449741343247247909519360e33 -0.391946441371315598560753809131e30 / 0.59488198442976521547770194575360e32 -0.15471439859462263133402977429037e32 / 0.6026077244872946338605292437504000e34; 0.702728186606917922841148808013121e33 / 0.2718796569464161336362934673952000e34 -0.738964213741742941634289388463587e33 / 0.811202706040588930196866289664000e33 -0.6938440904692701484464706797e28 / 0.267062619272621870023659683840e30 0.13587935221144065448123508546711e32 / 0.16900056375845602712434714368000e32 -0.398581023027193812136121833051e30 / 0.3244810824162355720787465158656e31 -0.4390487184318690815376655433561e31 / 0.486721623624353358118119773798400e33 0.39375020562052974775725982794643e32 / 0.5948819844297652154777019457536000e34 -0.337431206994896862016299739723e30 / 0.1054563517852765609255926176563200e34; -0.1626484714285090813572964936931e31 / 0.22656638078868011136357788949600e32 0.247081446636583523913555235517491e33 / 0.1081603608054118573595821719552000e34 -0.98606689532786656855690277893813e32 / 0.135200451006764821699477714944000e33 -0.54412910204947725598668603049e29 / 0.801187857817865610070979051520e30 0.44527159063104584858762191285733e32 / 0.54080180402705928679791085977600e32 -0.4170461618635408764831166558231e31 / 0.18541776138070604118785515192320e32 0.2733053052508781643445069010989e31 / 0.55081665224978260692379809792000e32 -0.62697761224771731704532896739409e32 / 0.7030423452351770728372841177088000e34; -0.16732447706243527499842078942603e32 / 0.543759313892832267272586934790400e33 0.166114324967929244010272382781e30 / 0.2897152521573531893560236748800e31 0.45101984053799299547057123965e29 / 0.811202706040588930196866289664e30 -0.17968772701532140224971787578029e32 / 0.27040090201352964339895542988800e32 -0.48635228792591105226576208151e29 / 0.400593928908932805035489525760e30 0.89480251714879473157102672858403e32 / 0.97344324724870671623623954759680e32 -0.42143863277048975845235502012773e32 / 0.148720496107441303869425486438400e33 0.4389611982769118812600028148913e31 / 0.52728175892638280462796308828160e32; 0.590475930631333482244268674627e30 / 0.21750372555713290690903477391616e32 -0.1702402353852012839500776925027e31 / 0.27812664207105906178178272788480e32 0.5538920825569170589993365115709e31 / 0.121680405906088339529529943449600e33 0.13866428236712167588433125984777e32 / 0.129792432966494228831498606346240e33 -0.16470253483258635888811378530287e32 / 0.24336081181217667905905988689920e32 -0.391812951622286986994545412081e30 / 0.2403563573453596830212937154560e31 0.345131812155051195787190432571781e33 / 0.356929190657859129286621167452160e33 -0.8126635967231661246920267342728147e34 / 0.25309524428466374622142228237516800e35; -0.88170259036818455465427409231e29 / 0.41193887416123656611559616272000e32 0.943459254925453305858100463659e30 / 0.118976396885953043095540389150720e33 -0.104249970715879362499981962934393e33 / 0.5948819844297652154777019457536000e34 0.78160374875076232344526852587e29 / 0.18360555074992753564126603264000e32 0.37536035579237342566706229296521e32 / 0.297440992214882607738850972876800e33 -0.240812609144054591374890622842541e33 / 0.356929190657859129286621167452160e33 -0.145390363516271219220036634153e30 / 0.801187857817865610070979051520e30 0.38093040928245760105862644889779897e35 / 0.38667328987934739006050626473984000e35; -0.4596580943680048141920105029111e31 / 0.2718796569464161336362934673952000e34 0.15262190991038620305994595494037e32 / 0.6026077244872946338605292437504000e34 0.1782508142749448850000523441843e31 / 0.1054563517852765609255926176563200e34 -0.33510338065544202178903991034341e32 / 0.7030423452351770728372841177088000e34 -0.8226959851063633384855147175189e31 / 0.421825407141106243702370470625280e33 0.3729744974003821244717412110773747e34 / 0.25309524428466374622142228237516800e35 -0.6554525408845613610278033864711443e34 / 0.9666832246983684751512656618496000e34 -0.49383212966905764275823531781e29 / 0.267062619272621870023659683840e30;]; + +% The staggered + operator, upper part. Notice that the B matrix is not included here.Qp+Qm^T=0 +Qp_U=[-0.6661444046602902130086192779621746771e37 / 0.23342839720855518078613888837079040000e38 0.12367666586033683088530161144944922123649e41 / 0.14797137255429936031548004199961722880000e41 0.4395151478839780503265910147432452357e37 / 0.186518536833150454179176523528929280000e39 -0.87963490365651280757669626389462038003e38 / 0.1345194295948176002868000381814702080000e40 -0.224187853266917333808369723332131873619e39 / 0.5178998039400477611041801469986603008000e40 0.55314264508663245255306547445101748003e38 / 0.2959427451085987206309600839992344576000e40 0.232303691019191510116997121080032335473e39 / 0.7398568627714968015774002099980861440000e40 -0.2816079756494683118054172509551114287e37 / 0.182680706857159704093185237036564480000e39; -0.10353150194859080046224306922028068797e38 / 0.43350988053017390717425793554575360000e38 -0.41810120772068966379630018024826564868789e41 / 0.44391411766289808094644012599885168640000e41 0.14548989645937048285445132077600344947e38 / 0.16118885899161150361163403267932160000e38 0.1353060140543056426043612771323929599939e40 / 0.6341630252327115442092001799983595520000e40 0.52584560489710238297754746193308329991e38 / 0.317081512616355772104600089999179776000e39 -0.322880558646947920788817445027309369183e39 / 0.8878282353257961618928802519977033728000e40 -0.2523050363123545986203815545725359199053e40 / 0.22195705883144904047322006299942584320000e41 0.2171073153815036601208579021528549178587e40 / 0.44391411766289808094644012599885168640000e41; -0.891108828407068615176254357157902263e36 / 0.14450329351005796905808597851525120000e38 0.9207480733237809022786174965361787199767e40 / 0.44391411766289808094644012599885168640000e41 -0.42541158717297365539935073107511004261e38 / 0.62172845611050151393058841176309760000e38 0.3752589973871193536179209598104238527889e40 / 0.4932379085143312010516001399987240960000e40 -0.503978909830206767329311700636686423011e39 / 0.2219570588314490404732200629994258432000e40 -0.25729240076595732139825342259542295873e38 / 0.328825272342887467367733426665816064000e39 0.283553624900223723890865927423934504751e39 / 0.2466189542571656005258000699993620480000e40 -0.129077863120107719239717930927898530811e39 / 0.4035582887844528008604001145444106240000e40; 0.150348887575956035991597139943595613e36 / 0.2000814833216187263881190471749632000e37 -0.20271017027971837857434511014725441049e38 / 0.422775350155141029472800119998906368000e39 -0.28593401740189393456434271848012721991e38 / 0.87041983855470211950282377646833664000e38 -0.3107901953269564253446821657614993689969e40 / 0.2959427451085987206309600839992344576000e40 0.163990240717967340033770840655582198979e39 / 0.147971372554299360315480041999617228800e39 0.1282390462809880153203810439898877805771e40 / 0.5326969411954776971357281511986220236800e40 0.55264142141446425784998699697993554089e38 / 0.1479713725542993603154800419996172288000e40 -0.11464469241412665723941542481946601159e38 / 0.328825272342887467367733426665816064000e39; 0.523088046329055388999216632374482217e36 / 0.8670197610603478143485158710915072000e37 -0.1060078188350823496391913491020744512571e40 / 0.8878282353257961618928802519977033728000e40 0.2489758378222482566046953325470732791e37 / 0.87041983855470211950282377646833664000e38 0.2223322627542429729644405110485179851147e40 / 0.8878282353257961618928802519977033728000e40 -0.405229856535104846314030690968355556777e39 / 0.443914117662898080946440125998851686400e39 0.1490365162783652350291746570529147907183e40 / 0.1775656470651592323785760503995406745600e40 -0.768972334347761129534143069144613163467e39 / 0.4439141176628980809464401259988516864000e40 0.34866718651627637034229483614924933299e38 / 0.1268326050465423088418400359996719104000e40; -0.522920230014765612739540250860177277e36 / 0.14450329351005796905808597851525120000e38 0.1453599225900263767517831305438030179313e40 / 0.44391411766289808094644012599885168640000e41 0.39466073050115777575909309244731209107e38 / 0.435209919277351059751411888234168320000e39 -0.501532616891797921922210650058733239369e39 / 0.4932379085143312010516001399987240960000e40 -0.276688880738967796756224778348832938429e39 / 0.2219570588314490404732200629994258432000e40 -0.346414321340583244146983258114429082007e39 / 0.328825272342887467367733426665816064000e39 0.12835585968364300662881260070481636919e38 / 0.10676145205937904784666669696942080000e38 -0.823464330534329517579044232163467356639e39 / 0.44391411766289808094644012599885168640000e41; -0.52802811745049309885720368328150617e35 / 0.1688999534533145092886719229399040000e37 0.306081878756402097710283710502974342821e39 / 0.4932379085143312010516001399987240960000e40 -0.43281676756908448328630499995327205907e38 / 0.1305629757832053179254235664702504960000e40 -0.136777517026276610492513752852976312597e39 / 0.4932379085143312010516001399987240960000e40 0.7290267185112688983569547959866321207e37 / 0.246618954257165600525800069999362048000e39 0.351639553804386998900032061955116226307e39 / 0.3804978151396269265255201079990157312000e40 -0.2840607921448362125113163437018746783083e40 / 0.2466189542571656005258000699993620480000e40 0.1859197240206073478058984196606793676119e40 / 0.1644126361714437336838667133329080320000e40; 0.5412884950805861225701675068383948563e37 / 0.303456916371121735021980554882027520000e39 -0.1279848124286614098666833963684894093627e40 / 0.44391411766289808094644012599885168640000e41 0.17218770500911534891873213313315117e35 / 0.39564538116122823613764717112197120000e38 0.904771800018341402297740826650985365019e39 / 0.44391411766289808094644012599885168640000e41 0.65377500469171784914136353007658339737e38 / 0.15536994118201432833125404409959809024000e41 -0.211014619053462658139013021424372225649e39 / 0.8878282353257961618928802519977033728000e40 0.195325945159072852492812627815477159003e39 / 0.3170815126163557721046000899991797760000e40 -0.52260858238454846311625894178508086247499e41 / 0.44391411766289808094644012599885168640000e41;]; + +Hp=spdiags(ones(np,1),0,np,np); +Hp(1:bp,1:bp)=Hp_U; +Hp(np-bp+1:np,np-bp+1:np)=fliplr(flipud(Hp_U)); +Hp=Hp*h; +HIp=inv(Hp); + +Hm=spdiags(ones(nm,1),0,nm,nm); +Hm(1:bp,1:bp)=Hm_U; +Hm(nm-bp+1:nm,nm-bp+1:nm)=fliplr(flipud(Hm_U)); +Hm=Hm*h; +HIm=inv(Hm); + +tt=[0.1447e4 / 0.688128e6 -0.17119e5 / 0.688128e6 0.8437e4 / 0.57344e5 -0.5543e4 / 0.8192e4 -0.15159e5 / 0.81920e5 0.16139e5 / 0.16384e5 -0.2649e4 / 0.8192e4 0.15649e5 / 0.172032e6 -0.3851e4 / 0.229376e6 0.5053e4 / 0.3440640e7;]; + +Qpp=spdiags(repmat(tt,[np,1]),-4:5,np,np); +Qpp(1:bp,1:bp)=Qpp_U; +Qpp(np-bp+1:np,np-bp+1:np)=flipud( fliplr(Qpp_U ) )'; + +Qpm=-Qpp'; + +Qmp=spdiags(repmat(tt,[nm,1]),-4:5,nm,nm); +Qmp(1:bp,1:bp)=Qmp_U; +Qmp(nm-bp+1:nm,nm-bp+1:nm)=flipud( fliplr(Qmp_U ) )'; + +Qmm=-Qmp'; + + +Bpp=spalloc(np,np,2);Bpp(1,1)=-1;Bpp(np,np)=1; +Bmp=spalloc(nm,nm,2);Bmp(1,1)=-1;Bmp(nm,nm)=1; + +Dpp=HIp*(Qpp+1/2*Bpp) ; +Dpm=HIp*(Qpm+1/2*Bpp) ; + + +Dmp=HIm*(Qmp+1/2*Bmp) ; +Dmm=HIm*(Qmm+1/2*Bmp) ; + + +%%% Start with the staggered +Qp=spdiags(repmat([0.5e1 / 0.7168e4 -0.49e2 / 0.5120e4 0.245e3 / 0.3072e4 -0.1225e4 / 0.1024e4 0.1225e4 / 0.1024e4 -0.245e3 / 0.3072e4 0.49e2 / 0.5120e4 -0.5e1 / 0.7168e4;],[np,1]),-3:4,np,nm); +Qp(1:bp,1:bp)=Qp_U; +Qp(np-bp+1:np,nm-bp+1:nm)=flipud( fliplr(-Qp_U ) ); +Qm=-Qp'; + +Bp=spalloc(np,nm,2);Bp(1,1)=-1;Bp(np,nm)=1; +Bm=Bp'; + +Dp=HIp*(Qp+1/2*Bp) ; + +Dm=HIm*(Qm+1/2*Bm) ; + +% grids +xp = h*[0:n]'; +xm = h*[0 1/2+0:n n]'; \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d2_variable_2.m --- a/+sbp/+implementations/d2_variable_2.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+sbp/+implementations/d2_variable_2.m Thu Mar 10 16:54:26 2022 +0100 @@ -27,7 +27,7 @@ diags = -1:1; stencil = [-1/2 0 1/2]; D1 = stripeMatrix(stencil, diags, m); - + D1(1,1)=-1;D1(1,2)=1;D1(m,m-1)=-1;D1(m,m)=1; D1(m,m-1)=-1;D1(m,m)=1; D1=D1/h; @@ -40,7 +40,7 @@ scheme_radius = (scheme_width-1)/2; r = (1+scheme_radius):(m-scheme_radius); - function D2 = D2_fun(c) + function [D2, B] = D2_fun(c) Mm1 = -c(r-1)/2 - c(r)/2; M0 = c(r-1)/2 + c(r) + c(r+1)/2; @@ -54,6 +54,8 @@ M=M/h; D2=HI*(-M-c(1)*e_l*d1_l'+c(m)*e_r*d1_r'); + B = HI*M; end D2 = @D2_fun; + end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d2_variable_4.m --- a/+sbp/+implementations/d2_variable_4.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+sbp/+implementations/d2_variable_4.m Thu Mar 10 16:54:26 2022 +0100 @@ -49,7 +49,7 @@ N = m; - function D2 = D2_fun(c) + function [D2, B] = D2_fun(c) M = 78+(N-12)*5; %h = 1/(N-1); @@ -131,6 +131,8 @@ cols(40+(i-7)*5:44+(i-7)*5) = [i-2;i-1;i;i+1;i+2]; end D2 = sparse(rows,cols,D2); + + B = HI*( c(end)*e_r*d1_r' - c(1)*e_l*d1_l') - D2; end D2 = @D2_fun; end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d2_variable_hollow_2.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d2_variable_hollow_2.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,56 @@ +function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_hollow_2(m,h) + + BP = 1; + if(m<2*BP) + error(['Operator requires at least ' num2str(2*BP) ' grid points']); + end + + % Norm + Hv = ones(m,1); + Hv(1) = 1/2; + Hv(m:m) = 1/2; + Hv = h*Hv; + H = spdiag(Hv, 0); + HI = spdiag(1./Hv, 0); + + + % Boundary operators + e_l = sparse(m,1); + e_l(1) = 1; + e_r = rot90(e_l, 2); + + d1_l = sparse(m,1); + d1_l(1:3) = 1/h*[-3/2 2 -1/2]; + d1_r = -rot90(d1_l, 2); + + % D1 operator + diags = -1:1; + stencil = [-1/2 0 1/2]; + D1 = stripeMatrix(stencil, diags, m); + + D1(1,1)=-1;D1(1,2)=1;D1(m,m-1)=-1;D1(m,m)=1; + D1(m,m-1)=-1;D1(m,m)=1; + D1=D1/h; + %Q=H*D1 + 1/2*(e_1*e_1') - 1/2*(e_m*e_m'); + + + nBP = 2; + M = sparse(m,m); + coeffs = load('sbplib/+sbp/+implementations/coeffs_d2_variable_2.mat'); + + function D2 = D2_fun(c) + M_l = zeros(nBP, coeffs.nBPC); + M_r = zeros(nBP, coeffs.nBPC); + + for i=1:coeffs.nBPC + M_l = M_l + coeffs.C_l{i}*c(i); + M_r = M_r + coeffs.C_r{i}*c(m-coeffs.nBPC+i); + end + + M(1:nBP, 1:coeffs.nBPC) = M_l; + M(m-nBP+1:m, m-coeffs.nBPC+1:m) = M_r; + + D2 = M/h^2; + end + D2 = @D2_fun; +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d2_variable_hollow_4.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d2_variable_hollow_4.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,72 @@ +function [H, HI, D1, D2, e_l, e_r, d1_l, d1_r] = d2_variable_hollow_4(m,h) + + BP = 6; + if(m<2*BP) + error(['Operator requires at least ' num2str(2*BP) ' grid points']); + end + + + + + % Norm + Hv = ones(m,1); + Hv(1:4) = [17/48 59/48 43/48 49/48]; + Hv(m-3:m) = rot90(Hv(1:4),2); + Hv = h*Hv; + H = spdiag(Hv, 0); + HI = spdiag(1./Hv, 0); + + + % Boundary operators + e_l = sparse(m,1); + e_l(1) = 1; + e_r = rot90(e_l, 2); + + d1_l = sparse(m,1); + d1_l(1:4) = 1/h*[-11/6 3 -3/2 1/3]; + d1_r = -rot90(d1_l, 2); + + + + + S = d1_l*d1_l' + d1_r*d1_r'; + + stencil = [1/12 -2/3 0 2/3 -1/12]; + diags = -2:2; + + Q_U = [ + 0 0.59e2/0.96e2 -0.1e1/0.12e2 -0.1e1/0.32e2; + -0.59e2/0.96e2 0 0.59e2/0.96e2 0; + 0.1e1/0.12e2 -0.59e2/0.96e2 0 0.59e2/0.96e2; + 0.1e1/0.32e2 0 -0.59e2/0.96e2 0; + ]; + + Q = stripeMatrix(stencil, diags, m); + Q(1:4,1:4) = Q_U; + Q(m-3:m,m-3:m) = -rot90(Q_U, 2); + + D1 = HI*(Q - 1/2*e_l*e_l' + 1/2*e_r*e_r'); + + + % Second derivative + nBP = 6; + M = sparse(m,m); + coeffs = load('sbplib/+sbp/+implementations/coeffs_d2_variable_4.mat'); + + function D2 = D2_fun(c) + M_l = zeros(nBP, coeffs.nBPC); + M_r = zeros(nBP, coeffs.nBPC); + + for i=1:coeffs.nBPC + M_l = M_l + coeffs.C_l{i}*c(i); + M_r = M_r + coeffs.C_r{i}*c(m-coeffs.nBPC+i); + end + + M(1:nBP, 1:coeffs.nBPC) = M_l; + M(m-nBP+1:m, m-coeffs.nBPC+1:m) = M_r; + + D2 = M/h^2; + end + D2 = @D2_fun; + +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d4_variable_6.m --- a/+sbp/+implementations/d4_variable_6.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+sbp/+implementations/d4_variable_6.m Thu Mar 10 16:54:26 2022 +0100 @@ -85,7 +85,7 @@ scheme_radius = (scheme_width-1)/2; r = (1+scheme_radius):(m-scheme_radius); - function D2 = D2_fun(c) + function [D2, B] = D2_fun(c) Mm3 = c(r-2)/0.40e2 + c(r-1)/0.40e2 - 0.11e2/0.360e3 * c(r-3) - 0.11e2/0.360e3 * c(r); Mm2 = c(r-3)/0.20e2 - 0.3e1/0.10e2 * c(r-1) + c(r+1)/0.20e2 + 0.7e1/0.40e2 * c(r) + 0.7e1/0.40e2 * c(r-2); @@ -128,6 +128,7 @@ M=M/h; D2 = HI*(-M - c(1)*e_l*d1_l' + c(m)*e_r*d1_r'); + B = HI*M; end D2 = @D2_fun;
diff -r 855871e0b852 -r 60c875c18de3 +sbp/+implementations/d4_variable_hollow_6.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/+implementations/d4_variable_hollow_6.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,126 @@ +function [H, HI, D1, D2, D4, e_l, e_r, M4, d2_l, d2_r, d3_l, d3_r, d1_l, d1_r] = d4_variable_hollow_6(m,h) + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %%% 6:te ordn. SBP Finita differens %%% + %%% operatorer med diagonal norm %%% + %%% Extension to variable koeff %%% + %%% %%% + %%% H (Normen) %%% + %%% D1=H^(-1)Q (approx f?rsta derivatan) %%% + %%% D2 (approx andra derivatan) %%% + %%% D2=HI*(R+C*D*S %%% + %%% %%% + %%% R=-D1'*H*C*D1-RR %%% + %%% %%% + %%% RR ?r dissipation) %%% + %%% Dissipationen uppbyggd av D4: %%% + %%% DI=D4*B*H*D4 %%% + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + BP = 9; + if(m<2*BP) + error(['Operator requires at least ' num2str(2*BP) ' grid points']); + end + + % Norm + Hv = ones(m,1); + Hv(1:6) = [13649/43200,12013/8640,2711/4320,5359/4320,7877/8640, 43801/43200]; + Hv(m-5:m) = rot90(Hv(1:6),2); + Hv = h*Hv; + H = spdiag(Hv, 0); + HI = spdiag(1./Hv, 0); + + + % Boundary operators + e_l = sparse(m,1); + e_l(1) = 1; + e_r = rot90(e_l, 2); + + d1_l = sparse(m,1); + d1_l(1:5) = [-25/12, 4, -3, 4/3, -1/4]/h; + d1_r = -rot90(d1_l, 2); + + d2_l = sparse(m,1); + d2_l(1:5) = [0.35e2/0.12e2 -0.26e2/0.3e1 0.19e2/0.2e1 -0.14e2/0.3e1 0.11e2/0.12e2;]/h^2; + d2_r = rot90(d2_l, 2); + + d3_l = sparse(m,1); + d3_l(1:5) = [-5/2 9 -12 7 -3/2]/h^3; + d3_r = -rot90(d3_l, 2); + + + % First derivtive + x1=0.70127127127127; + + + D1=(1/60*diag(ones(m-3,1),3)-9/60*diag(ones(m-2,1),2)+45/60*diag(ones(m-1,1),1)-45/60*diag(ones(m-1,1),-1)+9/60*diag(ones(m-2,1),-2)-1/60*diag(ones(m-3,1),-3)); + + + + D1(1:6,1:9)=[-21600/13649, 43200/13649*x1-7624/40947, -172800/13649*x1+ ... + 715489/81894, 259200/13649*x1-187917/13649, -172800/13649* ... + x1+735635/81894, 43200/13649*x1-89387/40947, 0, 0, 0; ... + -8640/12013*x1+7624/180195, 0, 86400/12013*x1-57139/12013, ... + -172800/12013*x1+745733/72078, 129600/12013*x1-91715/12013, ... + -34560/12013*x1+240569/120130, 0, 0, 0; ... + 17280/2711*x1-715489/162660, -43200/2711*x1+57139/5422, 0, ... + 86400/2711*x1-176839/8133, -86400/2711*x1+242111/10844, ... + 25920/2711*x1-182261/27110, 0, 0, 0; ... + -25920/5359*x1+187917/53590, 86400/5359*x1-745733/64308, ... + -86400/5359*x1+176839/16077, 0, 43200/5359*x1-165041/32154, ... + -17280/5359*x1+710473/321540, 72/5359, 0, 0; ... + 34560/7877*x1-147127/47262, -129600/7877*x1+91715/7877, ... + 172800/7877*x1-242111/15754, -86400/7877*x1+165041/23631, ... + 0, 8640/7877*x1, -1296/7877, 144/7877, 0; ... + -43200/43801*x1+89387/131403, 172800/43801*x1-240569/87602,... + -259200/43801*x1+182261/43801, 172800/43801*x1-710473/262806, ... + -43200/43801*x1, 0, 32400/43801, -6480/43801, 720/43801]; + D1(m-5:m,m-8:m)=rot90( -D1(1:6,1:9),2); + D1=D1/h; + + + % Second derivative + nBP = 9; + M = sparse(m,m); + coeffs = load('sbplib/+sbp/+implementations/coeffs_d2_variable_6.mat'); + + function D2 = D2_fun(c) + M_l = zeros(nBP, coeffs.nBPC); + M_r = zeros(nBP, coeffs.nBPC); + + for i=1:coeffs.nBPC + M_l = M_l + coeffs.C_l{i}*c(i); + M_r = M_r + coeffs.C_r{i}*c(m-coeffs.nBPC+i); + end + + M(1:nBP, 1:coeffs.nBPC) = M_l; + M(m-nBP+1:m, m-coeffs.nBPC+1:m) = M_r; + + D2 = M/h^2; + end + D2 = @D2_fun; + + % Fourth derivative, 1th order accurate at first 8 boundary points (still + % yield 5th order convergence if stable: for example u_tt=-u_xxxx + stencil = [7/240, -2/5, 169/60, -122/15, 91/8, -122/15, 169/60, -2/5, 7/240]; + diags = -4:4; + M4 = stripeMatrix(stencil, diags, m); + + M4_U = [ + 0.1394226315049e13/0.367201486080e12 -0.1137054563243e13/0.114750464400e12 0.16614189027367e14/0.1836007430400e13 -0.1104821700277e13/0.306001238400e12 0.1355771086763e13/0.1836007430400e13 -0.27818686453e11/0.459001857600e12 -0.40671054239e11/0.1836007430400e13 0.5442887371e10/0.306001238400e12; + -0.1137054563243e13/0.114750464400e12 0.70616795535409e14/0.2570410402560e13 -0.173266854731041e15/0.6426026006400e13 0.28938615291031e14/0.2570410402560e13 -0.146167361863e12/0.71400288960e11 0.2793470836571e13/0.12852052012800e14 0.6219558097e10/0.428401733760e12 -0.7313844559e10/0.166909766400e12; + 0.16614189027367e14/0.1836007430400e13 -0.173266854731041e15/0.6426026006400e13 0.378613061504779e15/0.12852052012800e14 -0.9117069604217e13/0.642602600640e12 0.632177582849e12/0.233673672960e12 -0.1057776382577e13/0.6426026006400e13 0.443019868399e12/0.4284017337600e13 -0.3707981e7/0.2318191200e10; + -0.1104821700277e13/0.306001238400e12 0.28938615291031e14/0.2570410402560e13 -0.9117069604217e13/0.642602600640e12 0.5029150721885e13/0.514082080512e12 -0.5209119714341e13/0.1285205201280e13 0.12235427457469e14/0.12852052012800e14 -0.13731270505e11/0.64260260064e11 0.2933596129e10/0.40800165120e11; + 0.1355771086763e13/0.1836007430400e13 -0.146167361863e12/0.71400288960e11 0.632177582849e12/0.233673672960e12 -0.5209119714341e13/0.1285205201280e13 0.14871726798559e14/0.2570410402560e13 -0.7504337615347e13/0.1606506501600e13 0.310830296467e12/0.171360693504e12 -0.55284274391e11/0.183600743040e12; + -0.27818686453e11/0.459001857600e12 0.2793470836571e13/0.12852052012800e14 -0.1057776382577e13/0.6426026006400e13 0.12235427457469e14/0.12852052012800e14 -0.7504337615347e13/0.1606506501600e13 0.106318657014853e15/0.12852052012800e14 -0.14432772918527e14/0.2142008668800e13 0.58102695589e11/0.22666758400e11; + -0.40671054239e11/0.1836007430400e13 0.6219558097e10/0.428401733760e12 0.443019868399e12/0.4284017337600e13 -0.13731270505e11/0.64260260064e11 0.310830296467e12/0.171360693504e12 -0.14432772918527e14/0.2142008668800e13 0.27102479467823e14/0.2570410402560e13 -0.1216032192203e13/0.153000619200e12; + 0.5442887371e10/0.306001238400e12 -0.7313844559e10/0.166909766400e12 -0.3707981e7/0.2318191200e10 0.2933596129e10/0.40800165120e11 -0.55284274391e11/0.183600743040e12 0.58102695589e11/0.22666758400e11 -0.1216032192203e13/0.153000619200e12 0.20799922829107e14/0.1836007430400e13; + ]; + + M4(1:8,1:8) = M4_U; + M4(m-7:m,m-7:m) = rot90( M4_U ,2 ); + M4 = M4/h^3; + + + + D4=HI*(M4 - e_l*d3_l'+e_r*d3_r' + d1_l*d2_l'-d1_r*d2_r'); +end
diff -r 855871e0b852 -r 60c875c18de3 +sbp/D1StaggeredUpwind.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/D1StaggeredUpwind.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,99 @@ +classdef D1StaggeredUpwind < sbp.OpSet + % Compatible staggered and upwind operators by Ken Mattsson and Ossian O'reilly + properties + % x_primal: "primal" grid with m points. Equidistant. Called Plus grid in Ossian's paper. + % x_dual: "dual" grid with m+1 points. Called Minus grid in Ossian's paper. + + % D1_primal takes FROM dual grid TO primal grid + % D1_dual takes FROM primal grid TO dual grid + + D1_primal % SBP operator approximating first derivative + D1_dual % SBP operator approximating first derivative + + Dplus_primal % Upwind operator on primal grid + Dminus_primal % Upwind operator on primal grid + Dplus_dual % Upwind operator on dual grid + Dminus_dual % Upwind operator on dual grid + + H_primal % Norm matrix + H_dual % Norm matrix + H_primalI % H^-1 + H_dualI % H^-1 + e_primal_l % Left boundary operator + e_dual_l % Left boundary operator + e_primal_r % Right boundary operator + e_dual_r % Right boundary operator + m % Number of grid points. + m_primal % Number of grid points. + m_dual % Number of grid points. + h % Step size + x_primal % grid + x_dual % grid + x + borrowing % Struct with borrowing limits for different norm matrices + end + + methods + function obj = D1StaggeredUpwind(m,lim,order) + + xl = lim{1}; + xr = lim{2}; + L = xr-xl; + h = L/(m-1); + + m_primal = m; + m_dual = m+1; + + switch order + case 2 + [~, ~, obj.H_primal, obj.H_dual,... + obj.H_primalI, obj.H_dualI,... + obj.D1_primal, obj.D1_dual, obj.Dplus_primal, obj.Dminus_primal,... + obj.Dplus_dual, obj.Dminus_dual] = sbp.implementations.d1_staggered_upwind_2(m, L); + case 4 + [~, ~, obj.H_primal, obj.H_dual,... + obj.H_primalI, obj.H_dualI,... + obj.D1_primal, obj.D1_dual, obj.Dplus_primal, obj.Dminus_primal,... + obj.Dplus_dual, obj.Dminus_dual] = sbp.implementations.d1_staggered_upwind_4(m, L); + case 6 + [~, ~, obj.H_primal, obj.H_dual,... + obj.H_primalI, obj.H_dualI,... + obj.D1_primal, obj.D1_dual, obj.Dplus_primal, obj.Dminus_primal,... + obj.Dplus_dual, obj.Dminus_dual] = sbp.implementations.d1_staggered_upwind_6(m, L); + case 8 + [~, ~, obj.H_primal, obj.H_dual,... + obj.H_primalI, obj.H_dualI,... + obj.D1_primal, obj.D1_dual, obj.Dplus_primal, obj.Dminus_primal,... + obj.Dplus_dual, obj.Dminus_dual] = sbp.implementations.d1_staggered_upwind_8(m, L); + otherwise + error('Invalid operator order %d.',order); + end + + obj.m = m; + obj.m_primal = m_primal; + obj.m_dual = m_dual; + obj.h = h; + + obj.x_primal = linspace(xl, xr, m)'; + obj.x_dual = [xl, linspace(xl+h/2, xr-h/2, m-1), xr]'; + + obj.e_primal_l = sparse(m_primal,1); + obj.e_primal_r = sparse(m_primal,1); + obj.e_primal_l(1) = 1; + obj.e_primal_r(m_primal) = 1; + + obj.e_dual_l = sparse(m_dual,1); + obj.e_dual_r = sparse(m_dual,1); + obj.e_dual_l(1) = 1; + obj.e_dual_r(m_dual) = 1; + + obj.borrowing = []; + obj.x = []; + + end + + function str = string(obj) + str = [class(obj) '_' num2str(obj.order)]; + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +sbp/D1UpwindCompatible.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/D1UpwindCompatible.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,64 @@ +classdef D1UpwindCompatible < sbp.OpSet + properties + Dp, Dm % SBP operator approximating first derivative + H % Norm matrix + HI % H^-1 + e_l % Left boundary operator + e_r % Right boundary operator + m % Number of grid points. + h % Step size + x % grid + borrowing % Struct with borrowing limits for different norm matrices + end + + methods + function obj = D1UpwindCompatible(m,lim,order) + + x_l = lim{1}; + x_r = lim{2}; + L = x_r-x_l; + obj.h = L/(m-1); + obj.x = linspace(x_l,x_r,m)'; + + ops = sbp.D2Standard(m, lim, order); + D1 = ops.D1; + H = ops.H; + + obj.H = H; + obj.HI = inv(H); + obj.e_l = ops.e_l; + obj.e_r = ops.e_r; + + switch order + case 2 + ops = sbp.D2Standard(m, lim, 2); + Dp = D1 + obj.h^1*1/2*(H\ops.M); + Dm = D1 - obj.h^1*1/2*(H\ops.M); + case 4 + ops = sbp.D4Variable(m, lim, 2); + Dp = D1 - obj.h^3*1/12*(H\ops.M4); + Dm = D1 + obj.h^3*1/12*(H\ops.M4); + otherwise + error('Invalid operator order %d.',order); + end + + obj.Dp = Dp; + obj.Dm = Dm; + + obj.m = m; + obj.borrowing = []; + + end + + function str = string(obj) + str = [class(obj) '_' num2str(obj.order)]; + end + end + + +end + + + + +
diff -r 855871e0b852 -r 60c875c18de3 +sbp/D2VariableCompatibleHollow.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+sbp/D2VariableCompatibleHollow.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,81 @@ +classdef D2VariableCompatibleHollow < sbp.OpSet + properties + D1 % SBP operator approximating first derivative + H % Norm matrix + HI % H^-1 + Q % Skew-symmetric matrix + e_l % Left boundary operator + e_r % Right boundary operator + D2 % SBP operator for second derivative + M % Norm matrix, second derivative + d1_l % Left boundary first derivative + d1_r % Right boundary first derivative + m % Number of grid points. + h % Step size + x % grid + borrowing % Struct with borrowing limits for different norm matrices + end + + methods + function obj = D2VariableCompatibleHollow(m,lim,order) + + x_l = lim{1}; + x_r = lim{2}; + L = x_r-x_l; + obj.h = L/(m-1); + obj.x = linspace(x_l,x_r,m)'; + + switch order + + case 6 + + [obj.H, obj.HI, obj.D1, D2, ... + ~, obj.e_l, obj.e_r, ~, ~, ~, ~, ~,... + d1_l, d1_r] = ... + sbp.implementations.d4_variable_hollow_6(m, obj.h); + + case 4 + [obj.H, obj.HI, obj.D1, D2, obj.e_l,... + obj.e_r, d1_l, d1_r] = ... + sbp.implementations.d2_variable_hollow_4(m,obj.h); + case 2 + [obj.H, obj.HI, obj.D1, D2, obj.e_l,... + obj.e_r, d1_l, d1_r] = ... + sbp.implementations.d2_variable_hollow_2(m,obj.h); + + otherwise + error('Invalid operator order %d.',order); + end + obj.borrowing.H11 = obj.H(1,1)/obj.h; % First element in H/h, + obj.borrowing.M.d1 = obj.H(1,1)/obj.h; % First element in H/h is borrowing also for M + obj.borrowing.R.delta_D = inf; + obj.m = m; + obj.M = []; + + + D1 = obj.D1; + e_r = obj.e_r; + e_l = obj.e_l; + + % D2 = Hinv * (-M + br*er*d1r^T - bl*el*d1l^T); + % Replace d1' by e'*D1 in D2. + % D2_compatible = @(b) D2(b) - obj.HI*(b(m)*e_r*d1_r' - b(m)*e_r*e_r'*D1) ... + % + obj.HI*(b(1)*e_l*d1_l' - b(1)*e_l*e_l'*D1); + + obj.D2 = D2; + obj.d1_l = (e_l'*D1)'; + obj.d1_r = (e_r'*D1)'; + + end + function str = string(obj) + str = [class(obj) '_' num2str(obj.order)]; + end + end + + +end + + + + +
diff -r 855871e0b852 -r 60c875c18de3 +scheme/+bc/forcingSetup.m --- a/+scheme/+bc/forcingSetup.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+scheme/+bc/forcingSetup.m Thu Mar 10 16:54:26 2022 +0100 @@ -22,8 +22,14 @@ [gridData, symbolicData] = parseAndSortData(bcs, penalties, diffOp); - % Setup penalty function - O = spzeros(size(diffOp),1); + if length(gridData) + length(symbolicData) == 0 + S = []; + else + % Setup penalty function + O = spzeros(size(diffOp),1); + S = @S_fun; + end + function v = S_fun(t) v = O; for i = 1:length(gridData) @@ -36,7 +42,6 @@ v = S_sign * v; end - S = @S_fun; end % Go through a cell array of boundary condition specifications and return cell arrays
diff -r 855871e0b852 -r 60c875c18de3 +scheme/+bc/forcingSetupStaggered.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/+bc/forcingSetupStaggered.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,97 @@ +% Setup the forcing function for the given boundary conditions and data. +% Each bc is a struct with the fields +% * type -- Type of boundary condition +% * boundary -- Boundary identifier +% * data -- A function_handle for a function which provides boundary data.(see below) +% S_sign allows changing the sign of the function to put on different sides in the system of ODEs. +% default is 1, which the same side as the diffOp. +% Returns a forcing function S. +% +% The boundary data function can either be a function of time or a function of time and space coordinates. +% In the case where it only depends on time it should return the data as grid function for the boundary. +% In the case where it also takes space coordinates the number of space coordinates should match the number of dimensions of the problem domain. +% For example in the 2D case: f(t,x,y). + +function S = forcingSetupStaggered(diffOp, penalties, bcs, S_sign) + default_arg('S_sign', 1); + + assertType(bcs, 'cell'); + assertIsMember(S_sign, [1, -1]); + + scheme.bc.verifyFormat(bcs, diffOp); + + [gridData, symbolicData] = parseAndSortData(bcs, penalties, diffOp); + + if length(gridData) + length(symbolicData) == 0 + S = []; + else + % Setup penalty function + O = spzeros(size(diffOp),1); + S = @S_fun; + end + + function v = S_fun(t) + v = O; + for i = 1:length(gridData) + v = v + gridData{i}.penalty*gridData{i}.func(t); + end + + for i = 1:length(symbolicData) + v = v + symbolicData{i}.penalty*symbolicData{i}.func(t, symbolicData{i}.coords{:}); + end + + v = S_sign * v; + end +end + +% Go through a cell array of boundary condition specifications and return cell arrays +% of structs for grid and symbolic data. +function [gridData, symbolicData] = parseAndSortData(bcs, penalties, diffOp) + gridData = {}; + symbolicData = {}; + for i = 1:length(bcs) + [ok, isSymbolic, data] = parseData(bcs{i}, penalties{i}, diffOp.grid); + + if ~ok + continue % There was no data + end + + if isSymbolic + symbolicData{end+1} = data; + else + gridData{end+1} = data; + end + end +end + +function [ok, isSymbolic, dataStruct] = parseData(bc, penalty, grid) + if ~isfield(bc,'data') || isempty(bc.data) + isSymbolic = []; + dataStruct = struct(); + ok = false; + return + end + ok = true; + + nArg = nargin(bc.data); + + if nArg > 1 + % Symbolic data + isSymbolic = true; + + switch bc.type{2} + case {'F','f','Free','free','traction','Traction','t','T'} + coord = grid.getBoundary(bc.boundary, 2); + otherwise + coord = grid.getBoundary(bc.boundary, 1); + end + dataStruct.penalty = penalty; + dataStruct.func = bc.data; + dataStruct.coords = num2cell(coord, 1); + else + % Grid data + isSymbolic = false; + dataStruct.penalty = penalty; + dataStruct.func = bc.data; + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Divergence.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Divergence.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,108 @@ +classdef Divergence < scheme.Scheme + +% Approximates the divergence +% Interface and boundary condition methods are just dummies + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + D + D1 + H + end + + methods + + function obj = Divergence(g, order, opSet) + default_arg('opSet',{@sbp.D2Variable, @sbp.D2Variable}); + + dim = 2; + + m = g.size(); + m_tot = g.N(); + + h = g.scaling(); + lim = g.lim; + if isempty(lim) + x = g.x; + lim = cell(length(x),1); + for i = 1:length(x) + lim{i} = {min(x{i}), max(x{i})}; + end + end + + % 1D operators + ops = cell(dim,1); + for i = 1:dim + ops{i} = opSet{i}(m(i), lim{i}, order); + end + + I = cell(dim,1); + D1 = cell(dim,1); + + for i = 1:dim + I{i} = speye(m(i)); + D1{i} = ops{i}.D1; + end + + %====== Assemble full operators ======== + + % D1 + obj.D1{1} = kron(D1{1},I{2}); + obj.D1{2} = kron(I{1},D1{2}); + + I_dim = speye(dim, dim); + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + + Div = sparse(m_tot, dim*m_tot); + for i = 1:dim + Div = Div + obj.D1{i}*E{i}'; + end + obj.D = Div; + obj.H = []; + %=========================================%' + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + function [closure, penalty] = boundary_condition(obj, boundary, bc) + error('Not implemented') + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.2 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + error('Not implemented') + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dCurvilinear.m --- a/+scheme/Elastic2dCurvilinear.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+scheme/Elastic2dCurvilinear.m Thu Mar 10 16:54:26 2022 +0100 @@ -156,8 +156,8 @@ b = cell(dim,dim); b{1,1} = y_eta./J; - b{1,2} = -x_eta./J; - b{2,1} = -y_xi./J; + b{2,1} = -x_eta./J; + b{1,2} = -y_xi./J; b{2,2} = x_xi./J; % Scale factors for boundary integrals
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dCurvilinearAnisotropic.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dCurvilinearAnisotropic.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,818 @@ +classdef Elastic2dCurvilinearAnisotropic < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% in curvilinear coordinates. +% opSet should be cell array of opSets, one per dimension. This +% is useful if we have periodic BC in one direction. +% Assumes fully compatible operators. + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + J, Ji + RHO % Density + C % Elastic stiffness tensor + + D % Total operator + + K % Transformation gradient + Dx, Dy % Physical derivatives + sigma % Cell matrix of physical stress operators + n_w, n_e, n_s, n_n % Physical normals + tangent_w, tangent_e, tangent_s, tangent_n % Physical tangents + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + tau_n_w, tau_n_e, tau_n_s, tau_n_n % Return scalar field + tau_t_w, tau_t_e, tau_t_s, tau_t_n % Return scalar field + + % Inner products + H + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Surface Jacobian vectors + s_w, s_e, s_s, s_n + + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + en_w, en_e, en_s, en_n % Act on vector field, return normal component + et_w, et_e, et_s, et_n % Act on vector field, return tangential component + + % E{i}^T picks out component i + E + + % Elastic2dVariableAnisotropic object for reference domain + refObj + end + + methods + + % The coefficients can either be function handles or grid functions + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dCurvilinearAnisotropic(g, order, rho, C, opSet, optFlag, hollow) + default_arg('hollow', false); + default_arg('rho', @(x,y) 0*x+1); + default_arg('opSet',{@sbp.D2VariableCompatible, @sbp.D2VariableCompatible}); + default_arg('optFlag', false); + dim = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x ; + end + end + end + end + default_arg('C', C_default); + + assert(isa(g, 'grid.Curvilinear')); + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + C_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C{i,j,k,l}, 'function_handle') + C{i,j,k,l} = grid.evalOn(g, C{i,j,k,l}); + end + C_mat{i,j,k,l} = spdiag(C{i,j,k,l}); + end + end + end + end + obj.C = C_mat; + + m = g.size(); + m_tot = g.N(); + + % 1D operators + m_u = m(1); + m_v = m(2); + ops_u = opSet{1}(m_u, {0, 1}, order); + ops_v = opSet{2}(m_v, {0, 1}, order); + + h_u = ops_u.h; + h_v = ops_v.h; + + I_u = speye(m_u); + I_v = speye(m_v); + + D1_u = ops_u.D1; + H_u = ops_u.H; + Hi_u = ops_u.HI; + e_l_u = ops_u.e_l; + e_r_u = ops_u.e_r; + d1_l_u = ops_u.d1_l; + d1_r_u = ops_u.d1_r; + + D1_v = ops_v.D1; + H_v = ops_v.H; + Hi_v = ops_v.HI; + e_l_v = ops_v.e_l; + e_r_v = ops_v.e_r; + d1_l_v = ops_v.d1_l; + d1_r_v = ops_v.d1_r; + + + % Logical operators + Du = kr(D1_u,I_v); + Dv = kr(I_u,D1_v); + + e_w = kr(e_l_u,I_v); + e_e = kr(e_r_u,I_v); + e_s = kr(I_u,e_l_v); + e_n = kr(I_u,e_r_v); + + % Metric coefficients + coords = g.points(); + x = coords(:,1); + y = coords(:,2); + + x_u = Du*x; + x_v = Dv*x; + y_u = Du*y; + y_v = Dv*y; + + J = x_u.*y_v - x_v.*y_u; + + K = cell(dim, dim); + K{1,1} = y_v./J; + K{1,2} = -y_u./J; + K{2,1} = -x_v./J; + K{2,2} = x_u./J; + obj.K = K; + + % Physical derivatives + obj.Dx = spdiag( y_v./J)*Du + spdiag(-y_u./J)*Dv; + obj.Dy = spdiag(-x_v./J)*Du + spdiag( x_u./J)*Dv; + + % Wrap around Aniosotropic Cartesian + rho_tilde = J.*rho; + + PHI = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + PHI{i,j,k,l} = 0*C{i,j,k,l}; + for m = 1:dim + for n = 1:dim + PHI{i,j,k,l} = PHI{i,j,k,l} + J.*K{m,i}.*C{m,j,n,l}.*K{n,k}; + end + end + end + end + end + end + + gRef = grid.equidistant([m_u, m_v], {0,1}, {0,1}); + refObj = scheme.Elastic2dVariableAnisotropic(gRef, order, rho_tilde, PHI, opSet, [], hollow); + + %---- Set object properties ------ + obj.RHO = spdiag(rho); + + % Volume quadrature + obj.J = spdiag(J); + obj.Ji = spdiag(1./J); + obj.H = obj.J*kr(H_u,H_v); + + % Boundary quadratures + s_w = sqrt((e_w'*x_v).^2 + (e_w'*y_v).^2); + s_e = sqrt((e_e'*x_v).^2 + (e_e'*y_v).^2); + s_s = sqrt((e_s'*x_u).^2 + (e_s'*y_u).^2); + s_n = sqrt((e_n'*x_u).^2 + (e_n'*y_u).^2); + obj.s_w = s_w; + obj.s_e = s_e; + obj.s_s = s_s; + obj.s_n = s_n; + + obj.H_w = H_v*spdiag(s_w); + obj.H_e = H_v*spdiag(s_e); + obj.H_s = H_u*spdiag(s_s); + obj.H_n = H_u*spdiag(s_n); + + % Restriction operators + obj.e_w = refObj.e_w; + obj.e_e = refObj.e_e; + obj.e_s = refObj.e_s; + obj.e_n = refObj.e_n; + + % Adapt things from reference object + obj.D = refObj.D; + obj.E = refObj.E; + + obj.e1_w = refObj.e1_w; + obj.e1_e = refObj.e1_e; + obj.e1_s = refObj.e1_s; + obj.e1_n = refObj.e1_n; + + obj.e2_w = refObj.e2_w; + obj.e2_e = refObj.e2_e; + obj.e2_s = refObj.e2_s; + obj.e2_n = refObj.e2_n; + + obj.e_scalar_w = refObj.e_scalar_w; + obj.e_scalar_e = refObj.e_scalar_e; + obj.e_scalar_s = refObj.e_scalar_s; + obj.e_scalar_n = refObj.e_scalar_n; + + e1_w = obj.e1_w; + e1_e = obj.e1_e; + e1_s = obj.e1_s; + e1_n = obj.e1_n; + + e2_w = obj.e2_w; + e2_e = obj.e2_e; + e2_s = obj.e2_s; + e2_n = obj.e2_n; + + obj.tau1_w = (spdiag(1./s_w)*refObj.tau1_w')'; + obj.tau1_e = (spdiag(1./s_e)*refObj.tau1_e')'; + obj.tau1_s = (spdiag(1./s_s)*refObj.tau1_s')'; + obj.tau1_n = (spdiag(1./s_n)*refObj.tau1_n')'; + + obj.tau2_w = (spdiag(1./s_w)*refObj.tau2_w')'; + obj.tau2_e = (spdiag(1./s_e)*refObj.tau2_e')'; + obj.tau2_s = (spdiag(1./s_s)*refObj.tau2_s')'; + obj.tau2_n = (spdiag(1./s_n)*refObj.tau2_n')'; + + obj.tau_w = (refObj.e_w'*obj.e1_w*obj.tau1_w')' + (refObj.e_w'*obj.e2_w*obj.tau2_w')'; + obj.tau_e = (refObj.e_e'*obj.e1_e*obj.tau1_e')' + (refObj.e_e'*obj.e2_e*obj.tau2_e')'; + obj.tau_s = (refObj.e_s'*obj.e1_s*obj.tau1_s')' + (refObj.e_s'*obj.e2_s*obj.tau2_s')'; + obj.tau_n = (refObj.e_n'*obj.e1_n*obj.tau1_n')' + (refObj.e_n'*obj.e2_n*obj.tau2_n')'; + + % Physical normals + e_w = obj.e_scalar_w; + e_e = obj.e_scalar_e; + e_s = obj.e_scalar_s; + e_n = obj.e_scalar_n; + + e_w_vec = obj.e_w; + e_e_vec = obj.e_e; + e_s_vec = obj.e_s; + e_n_vec = obj.e_n; + + nu_w = [-1,0]; + nu_e = [1,0]; + nu_s = [0,-1]; + nu_n = [0,1]; + + obj.n_w = cell(2,1); + obj.n_e = cell(2,1); + obj.n_s = cell(2,1); + obj.n_n = cell(2,1); + + % Compute normal and rotate (exactly!) 90 degrees counter-clockwise to get tangent + n_w_1 = (1./s_w).*e_w'*(J.*(K{1,1}*nu_w(1) + K{1,2}*nu_w(2))); + n_w_2 = (1./s_w).*e_w'*(J.*(K{2,1}*nu_w(1) + K{2,2}*nu_w(2))); + obj.n_w{1} = spdiag(n_w_1); + obj.n_w{2} = spdiag(n_w_2); + obj.tangent_w = {-obj.n_w{2}, obj.n_w{1}}; + + n_e_1 = (1./s_e).*e_e'*(J.*(K{1,1}*nu_e(1) + K{1,2}*nu_e(2))); + n_e_2 = (1./s_e).*e_e'*(J.*(K{2,1}*nu_e(1) + K{2,2}*nu_e(2))); + obj.n_e{1} = spdiag(n_e_1); + obj.n_e{2} = spdiag(n_e_2); + obj.tangent_e = {-obj.n_e{2}, obj.n_e{1}}; + + n_s_1 = (1./s_s).*e_s'*(J.*(K{1,1}*nu_s(1) + K{1,2}*nu_s(2))); + n_s_2 = (1./s_s).*e_s'*(J.*(K{2,1}*nu_s(1) + K{2,2}*nu_s(2))); + obj.n_s{1} = spdiag(n_s_1); + obj.n_s{2} = spdiag(n_s_2); + obj.tangent_s = {-obj.n_s{2}, obj.n_s{1}}; + + n_n_1 = (1./s_n).*e_n'*(J.*(K{1,1}*nu_n(1) + K{1,2}*nu_n(2))); + n_n_2 = (1./s_n).*e_n'*(J.*(K{2,1}*nu_n(1) + K{2,2}*nu_n(2))); + obj.n_n{1} = spdiag(n_n_1); + obj.n_n{2} = spdiag(n_n_2); + obj.tangent_n = {-obj.n_n{2}, obj.n_n{1}}; + + % Operators that extract the normal component + obj.en_w = (obj.n_w{1}*obj.e1_w' + obj.n_w{2}*obj.e2_w')'; + obj.en_e = (obj.n_e{1}*obj.e1_e' + obj.n_e{2}*obj.e2_e')'; + obj.en_s = (obj.n_s{1}*obj.e1_s' + obj.n_s{2}*obj.e2_s')'; + obj.en_n = (obj.n_n{1}*obj.e1_n' + obj.n_n{2}*obj.e2_n')'; + + % Operators that extract the tangential component + obj.et_w = (obj.tangent_w{1}*obj.e1_w' + obj.tangent_w{2}*obj.e2_w')'; + obj.et_e = (obj.tangent_e{1}*obj.e1_e' + obj.tangent_e{2}*obj.e2_e')'; + obj.et_s = (obj.tangent_s{1}*obj.e1_s' + obj.tangent_s{2}*obj.e2_s')'; + obj.et_n = (obj.tangent_n{1}*obj.e1_n' + obj.tangent_n{2}*obj.e2_n')'; + + obj.tau_n_w = (obj.n_w{1}*obj.tau1_w' + obj.n_w{2}*obj.tau2_w')'; + obj.tau_n_e = (obj.n_e{1}*obj.tau1_e' + obj.n_e{2}*obj.tau2_e')'; + obj.tau_n_s = (obj.n_s{1}*obj.tau1_s' + obj.n_s{2}*obj.tau2_s')'; + obj.tau_n_n = (obj.n_n{1}*obj.tau1_n' + obj.n_n{2}*obj.tau2_n')'; + + obj.tau_t_w = (obj.tangent_w{1}*obj.tau1_w' + obj.tangent_w{2}*obj.tau2_w')'; + obj.tau_t_e = (obj.tangent_e{1}*obj.tau1_e' + obj.tangent_e{2}*obj.tau2_e')'; + obj.tau_t_s = (obj.tangent_s{1}*obj.tau1_s' + obj.tangent_s{2}*obj.tau2_s')'; + obj.tau_t_n = (obj.tangent_n{1}*obj.tau1_n' + obj.tangent_n{2}*obj.tau2_n')'; + + % Stress operators + sigma = cell(dim, dim); + D1 = {obj.Dx, obj.Dy}; + E = obj.E; + N = length(obj.RHO); + for i = 1:dim + for j = 1:dim + sigma{i,j} = sparse(N,2*N); + for k = 1:dim + for l = 1:dim + sigma{i,j} = sigma{i,j} + obj.C{i,j,k,l}*D1{k}*E{l}'; + end + end + end + end + obj.sigma = sigma; + + % Misc. + obj.refObj = refObj; + obj.m = refObj.m; + obj.h = refObj.h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + + component = bc{1}; + type = bc{2}; + + switch component + + % If conditions on Cartesian components + case {1,2} + [closure, penalty] = obj.refObj.boundary_condition(boundary, bc, tuning); + + % If conditions on normal or tangential components + case {'n', 't'} + + switch component + case 'n' + en = obj.getBoundaryOperator('en', boundary); + case 't' + en = obj.getBoundaryOperator('et', boundary); + end + e1 = obj.getBoundaryOperator('e1', boundary); + + bc1 = {1, type}; + [c1, p1] = obj.refObj.boundary_condition(boundary, bc1, tuning); + bc2 = {2, type}; + c2 = obj.refObj.boundary_condition(boundary, bc2, tuning); + + switch type + case {'F','f','Free','free','traction','Traction','t','T'} + closure = en*en'*(c1+c2); + penalty = en*e1'*p1; + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + [closure, penalty] = obj.displacementBCNormalTangential(boundary, bc, tuning); + end + + end + + switch type + case {'F','f','Free','free','traction','Traction','t','T'} + + s = obj.(['s_' boundary]); + s = spdiag(s); + penalty = penalty*s; + + end + end + + function [closure, penalty] = displacementBCNormalTangential(obj, boundary, bc, tuning) + u = obj; + + component = bc{1}; + type = bc{2}; + + switch component + case 'n' + en = u.getBoundaryOperator('en', boundary); + tau_n = u.getBoundaryOperator('tau_n', boundary); + N = u.getNormal(boundary); + case 't' + en = u.getBoundaryOperator('et', boundary); + tau_n = u.getBoundaryOperator('tau_t', boundary); + N = u.getTangent(boundary); + end + + % Operators + e = u.getBoundaryOperatorForScalarField('e', boundary); + h11 = u.getBorrowing(boundary); + n = u.getNormal(boundary); + + C = u.C; + Ji = u.Ji; + s = spdiag(u.(['s_' boundary])); + m_tot = u.grid.N(); + + Hi = u.E{1}*inv(u.H)*u.E{1}' + u.E{2}*inv(u.H)*u.E{2}'; + RHOi = u.E{1}*inv(u.RHO)*u.E{1}' + u.E{2}*inv(u.RHO)*u.E{2}'; + + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot, dim*m_tot); + penalty = sparse(dim*m_tot, m_int); + + % Term 1: The symmetric term + Z = sparse(m_int, m_int); + for i = 1:dim + for j = 1:dim + for l = 1:dim + for k = 1:dim + Z = Z + n{i}*N{j}*e'*Ji*C{i,j,k,l}*e*n{k}*N{l}; + end + end + end + end + + Z = -tuning*dim*1/h11*s*Z; + closure = closure + en*H_gamma*Z*en'; + penalty = penalty - en*H_gamma*Z; + + % Term 2: The symmetrizing term + closure = closure + tau_n*H_gamma*en'; + penalty = penalty - tau_n*H_gamma; + + % Multiply all terms by inverse of density x quadraure + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty, forcingPenalties] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + defaultType.type = 'standard'; + default_struct('type', defaultType); + + forcingPenalties = []; + + switch type.type + case 'standard' + [closure, penalty] = obj.refObj.interface(boundary,neighbour_scheme.refObj,neighbour_boundary,type); + case 'normalTangential' + [closure, penalty, forcingPenalties] = obj.interfaceNormalTangential(boundary,neighbour_scheme,neighbour_boundary,type); + case 'frictionalFault' + [closure, penalty, forcingPenalties] = obj.interfaceFrictionalFault(boundary,neighbour_scheme,neighbour_boundary,type); + end + + end + + function [closure, penalty, forcingPenalties] = interfaceFrictionalFault(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + forcingPenalties = cell(1, 1); + u = obj; + v = neighbour_scheme; + + % Operators, u side + e_u = u.getBoundaryOperatorForScalarField('e', boundary); + en_u = u.getBoundaryOperator('en', boundary); + tau_n_u = u.getBoundaryOperator('tau_n', boundary); + h11_u = u.getBorrowing(boundary); + n_u = u.getNormal(boundary); + + C_u = u.C; + Ji_u = u.Ji; + s_u = spdiag(u.(['s_' boundary])); + m_tot_u = u.grid.N(); + + % Operators, v side + e_v = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + en_v = v.getBoundaryOperator('en', neighbour_boundary); + tau_n_v = v.getBoundaryOperator('tau_n', neighbour_boundary); + h11_v = v.getBorrowing(neighbour_boundary); + n_v = v.getNormal(neighbour_boundary); + + C_v = v.C; + Ji_v = v.Ji; + s_v = spdiag(v.(['s_' neighbour_boundary])); + m_tot_v = v.grid.N(); + + % Operators that are only required for own domain + Hi = u.E{1}*inv(u.H)*u.E{1}' + u.E{2}*inv(u.H)*u.E{2}'; + RHOi = u.E{1}*inv(u.RHO)*u.E{1}' + u.E{2}*inv(u.RHO)*u.E{2}'; + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot_u, dim*m_tot_u); + penalty = sparse(dim*m_tot_u, dim*m_tot_v); + + % Continuity of normal displacement, term 1: The symmetric term + Z_u = sparse(m_int, m_int); + Z_v = sparse(m_int, m_int); + for i = 1:dim + for j = 1:dim + for l = 1:dim + for k = 1:dim + Z_u = Z_u + n_u{i}*n_u{j}*e_u'*Ji_u*C_u{i,j,k,l}*e_u*n_u{k}*n_u{l}; + Z_v = Z_v + n_v{i}*n_v{j}*e_v'*Ji_v*C_v{i,j,k,l}*e_v*n_v{k}*n_v{l}; + end + end + end + end + + Z = -tuning*dim*( 1/(4*h11_u)*s_u*Z_u + 1/(4*h11_v)*s_v*Z_v ); + closure = closure + en_u*H_gamma*Z*en_u'; + penalty = penalty + en_u*H_gamma*Z*en_v'; + + % Continuity of normal displacement, term 2: The symmetrizing term + closure = closure + 1/2*tau_n_u*H_gamma*en_u'; + penalty = penalty + 1/2*tau_n_u*H_gamma*en_v'; + + % Continuity of normal traction + closure = closure - 1/2*en_u*H_gamma*tau_n_u'; + penalty = penalty + 1/2*en_u*H_gamma*tau_n_v'; + forcing_tau_n = 1/2*en_u*H_gamma; + + % Multiply all normal component terms by inverse of density x quadraure + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + forcing_tau_n = RHOi*Hi*forcing_tau_n; + + % ---- Tangential tractions are imposed just like traction BC ------ + closure = closure + obj.boundary_condition(boundary, {'t', 't'}); + + forcingPenalties{1} = forcing_tau_n; + + end + + % Same interface conditions as in interfaceStandard, but imposed in the normal-tangential + % coordinate system. For the isotropic case, the components decouple nicely. + % The resulting scheme is not identical to that of interfaceStandard. This appears to be better. + function [closure, penalty, forcingPenalties, Zt] = interfaceNormalTangential(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + forcingPenalties = cell(2, 1); + u = obj; + v = neighbour_scheme; + + % Operators, u side + e_u = u.getBoundaryOperatorForScalarField('e', boundary); + en_u = u.getBoundaryOperator('en', boundary); + et_u = u.getBoundaryOperator('et', boundary); + tau_n_u = u.getBoundaryOperator('tau_n', boundary); + tau_t_u = u.getBoundaryOperator('tau_t', boundary); + h11_u = u.getBorrowing(boundary); + n_u = u.getNormal(boundary); + t_u = u.getTangent(boundary); + + C_u = u.C; + Ji_u = u.Ji; + s_u = spdiag(u.(['s_' boundary])); + m_tot_u = u.grid.N(); + + % Operators, v side + e_v = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + en_v = v.getBoundaryOperator('en', neighbour_boundary); + et_v = v.getBoundaryOperator('et', neighbour_boundary); + tau_n_v = v.getBoundaryOperator('tau_n', neighbour_boundary); + tau_t_v = v.getBoundaryOperator('tau_t', neighbour_boundary); + h11_v = v.getBorrowing(neighbour_boundary); + n_v = v.getNormal(neighbour_boundary); + t_v = v.getTangent(neighbour_boundary); + + C_v = v.C; + Ji_v = v.Ji; + s_v = spdiag(v.(['s_' neighbour_boundary])); + m_tot_v = v.grid.N(); + + % Operators that are only required for own domain + Hi = u.E{1}*inv(u.H)*u.E{1}' + u.E{2}*inv(u.H)*u.E{2}'; + RHOi = u.E{1}*inv(u.RHO)*u.E{1}' + u.E{2}*inv(u.RHO)*u.E{2}'; + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot_u, dim*m_tot_u); + penalty = sparse(dim*m_tot_u, dim*m_tot_v); + + % -- Continuity of displacement, term 1: The symmetric term --- + Zn_u = sparse(m_int, m_int); + Zn_v = sparse(m_int, m_int); + Zt_u = sparse(m_int, m_int); + Zt_v = sparse(m_int, m_int); + for i = 1:dim + for j = 1:dim + for l = 1:dim + for k = 1:dim + % Penalty strength for normal component + Zn_u = Zn_u + n_u{i}*n_u{j}*e_u'*Ji_u*C_u{i,j,k,l}*e_u*n_u{k}*n_u{l}; + Zn_v = Zn_v + n_v{i}*n_v{j}*e_v'*Ji_v*C_v{i,j,k,l}*e_v*n_v{k}*n_v{l}; + + % Penalty strength for tangential component + Zt_u = Zt_u + n_u{i}*t_u{j}*e_u'*Ji_u*C_u{i,j,k,l}*e_u*n_u{k}*t_u{l}; + Zt_v = Zt_v + n_v{i}*t_v{j}*e_v'*Ji_v*C_v{i,j,k,l}*e_v*n_v{k}*t_v{l}; + end + end + end + end + + Zn = -tuning*dim*( 1/(4*h11_u)*s_u*Zn_u + 1/(4*h11_v)*s_v*Zn_v ); + Zt = -tuning*dim*( 1/(4*h11_u)*s_u*Zt_u + 1/(4*h11_v)*s_v*Zt_v ); + + % Continuity of normal component + closure = closure + en_u*H_gamma*Zn*en_u'; + penalty = penalty + en_u*H_gamma*Zn*en_v'; + forcing_u_n = -en_u*H_gamma*Zn; + + % Continuity of tangential component + closure = closure + et_u*H_gamma*Zt*et_u'; + penalty = penalty + et_u*H_gamma*Zt*et_v'; + forcing_u_t = -et_u*H_gamma*Zt; + %------------------------------------------------------------------ + + % --- Continuity of displacement, term 2: The symmetrizing term + + % Continuity of normal displacement + closure = closure + 1/2*tau_n_u*H_gamma*en_u'; + penalty = penalty + 1/2*tau_n_u*H_gamma*en_v'; + forcing_u_n = forcing_u_n - 1/2*tau_n_u*H_gamma; + + % Continuity of tangential displacement + closure = closure + 1/2*tau_t_u*H_gamma*et_u'; + penalty = penalty + 1/2*tau_t_u*H_gamma*et_v'; + forcing_u_t = forcing_u_t - 1/2*tau_t_u*H_gamma; + % ------------------------------------------------------------------ + + % --- Continuity of tractions ----------------------------- + + % Continuity of normal traction + closure = closure - 1/2*en_u*H_gamma*tau_n_u'; + penalty = penalty + 1/2*en_u*H_gamma*tau_n_v'; + + % Continuity of tangential traction + closure = closure - 1/2*et_u*H_gamma*tau_t_u'; + penalty = penalty + 1/2*et_u*H_gamma*tau_t_v'; + %-------------------------------------------------------------------- + + % Multiply all terms by inverse of density x quadraure + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + forcing_u_n = RHOi*Hi*forcing_u_n; + forcing_u_t = RHOi*Hi*forcing_u_t; + + forcingPenalties{1} = forcing_u_n; + forcingPenalties{2} = forcing_u_t; + + end + + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.refObj.h11{1}; + case {'s', 'n'} + h11 = obj.refObj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + % n is a cell of diagonal matrices for each normal component, n{1} = n_1, n{2} = n_2. + function n = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + n = obj.(['n_' boundary]); + end + + % Returns the unit tangent vector for the boundary specified by the string boundary. + % t is a cell of diagonal matrices for each normal component, t{1} = t_1, t{2} = t_2. + function t = getTangent(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + t = obj.(['tangent_' boundary]); + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2', 'en', 'et', 'tau_n', 'tau_t'}) + + o = obj.([op, '_', boundary]); + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dCurvilinearAnisotropicUpwind.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dCurvilinearAnisotropicUpwind.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,464 @@ +classdef Elastic2dCurvilinearAnisotropicUpwind < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% in curvilinear coordinates. +% opSet should be cell array of opSets, one per dimension. This +% is useful if we have periodic BC in one direction. +% Assumes fully compatible operators. + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + J, Ji + RHO % Density + C % Elastic stiffness tensor + + D % Total operator + + Dx, Dy % Physical derivatives + sigma % Cell matrix of physical stress operators + n_w, n_e, n_s, n_n % Physical normals + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + + % Inner products + H + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Surface Jacobian vectors + s_w, s_e, s_s, s_n + + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + en_w, en_e, en_s, en_n % Act on vector field, return normal component + + % E{i}^T picks out component i + E + + % Elastic2dVariableAnisotropic object for reference domain + refObj + end + + methods + + % The coefficients can either be function handles or grid functions + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dCurvilinearAnisotropicUpwind(g, order, rho, C, opSet, optFlag) + default_arg('rho', @(x,y) 0*x+1); + default_arg('opSet',{@sbp.D1Upwind, @sbp.D1Upwind}); + default_arg('optFlag', false); + dim = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x; + end + end + end + end + default_arg('C', C_default); + + assert(isa(g, 'grid.Curvilinear')); + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + C_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C{i,j,k,l}, 'function_handle') + C{i,j,k,l} = grid.evalOn(g, C{i,j,k,l}); + end + C_mat{i,j,k,l} = spdiag(C{i,j,k,l}); + end + end + end + end + obj.C = C_mat; + + m = g.size(); + m_tot = g.N(); + + % 1D operators + opSetMetric = {@sbp.D2VariableCompatible, @sbp.D2VariableCompatible}; + orderMetric = ceil(order/2)*2; + m_u = m(1); + m_v = m(2); + ops_u = opSetMetric{1}(m_u, {0, 1}, orderMetric); + ops_v = opSetMetric{2}(m_v, {0, 1}, orderMetric); + + h_u = ops_u.h; + h_v = ops_v.h; + + I_u = speye(m_u); + I_v = speye(m_v); + + D1_u = ops_u.D1; + H_u = ops_u.H; + Hi_u = ops_u.HI; + e_l_u = ops_u.e_l; + e_r_u = ops_u.e_r; + d1_l_u = ops_u.d1_l; + d1_r_u = ops_u.d1_r; + + D1_v = ops_v.D1; + H_v = ops_v.H; + Hi_v = ops_v.HI; + e_l_v = ops_v.e_l; + e_r_v = ops_v.e_r; + d1_l_v = ops_v.d1_l; + d1_r_v = ops_v.d1_r; + + + % Logical operators + Du = kr(D1_u,I_v); + Dv = kr(I_u,D1_v); + + e_w = kr(e_l_u,I_v); + e_e = kr(e_r_u,I_v); + e_s = kr(I_u,e_l_v); + e_n = kr(I_u,e_r_v); + + % Metric coefficients + coords = g.points(); + x = coords(:,1); + y = coords(:,2); + + x_u = Du*x; + x_v = Dv*x; + y_u = Du*y; + y_v = Dv*y; + + J = x_u.*y_v - x_v.*y_u; + + K = cell(dim, dim); + K{1,1} = y_v./J; + K{1,2} = -y_u./J; + K{2,1} = -x_v./J; + K{2,2} = x_u./J; + + % Physical derivatives + obj.Dx = spdiag( y_v./J)*Du + spdiag(-y_u./J)*Dv; + obj.Dy = spdiag(-x_v./J)*Du + spdiag( x_u./J)*Dv; + + % Wrap around Aniosotropic Cartesian + rho_tilde = J.*rho; + + PHI = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + PHI{i,j,k,l} = 0*C{i,j,k,l}; + for m = 1:dim + for n = 1:dim + PHI{i,j,k,l} = PHI{i,j,k,l} + J.*K{m,i}.*C{m,j,n,l}.*K{n,k}; + end + end + end + end + end + end + + gRef = grid.equidistant([m_u, m_v], {0,1}, {0,1}); + refObj = scheme.Elastic2dVariableAnisotropicUpwind(gRef, order, rho_tilde, PHI, opSet); + + %---- Set object properties ------ + obj.RHO = spdiag(rho); + + % Volume quadrature + obj.J = spdiag(J); + obj.Ji = spdiag(1./J); + obj.H = obj.J*refObj.H; + + % Boundary quadratures + s_w = sqrt((e_w'*x_v).^2 + (e_w'*y_v).^2); + s_e = sqrt((e_e'*x_v).^2 + (e_e'*y_v).^2); + s_s = sqrt((e_s'*x_u).^2 + (e_s'*y_u).^2); + s_n = sqrt((e_n'*x_u).^2 + (e_n'*y_u).^2); + obj.s_w = s_w; + obj.s_e = s_e; + obj.s_s = s_s; + obj.s_n = s_n; + + obj.H_w = H_v*spdiag(s_w); + obj.H_e = H_v*spdiag(s_e); + obj.H_s = H_u*spdiag(s_s); + obj.H_n = H_u*spdiag(s_n); + + % Restriction operators + obj.e_w = refObj.e_w; + obj.e_e = refObj.e_e; + obj.e_s = refObj.e_s; + obj.e_n = refObj.e_n; + + % Adapt things from reference object + obj.D = refObj.D; + obj.E = refObj.E; + + obj.e1_w = refObj.e1_w; + obj.e1_e = refObj.e1_e; + obj.e1_s = refObj.e1_s; + obj.e1_n = refObj.e1_n; + + obj.e2_w = refObj.e2_w; + obj.e2_e = refObj.e2_e; + obj.e2_s = refObj.e2_s; + obj.e2_n = refObj.e2_n; + + obj.e_scalar_w = refObj.e_scalar_w; + obj.e_scalar_e = refObj.e_scalar_e; + obj.e_scalar_s = refObj.e_scalar_s; + obj.e_scalar_n = refObj.e_scalar_n; + + e1_w = obj.e1_w; + e1_e = obj.e1_e; + e1_s = obj.e1_s; + e1_n = obj.e1_n; + + e2_w = obj.e2_w; + e2_e = obj.e2_e; + e2_s = obj.e2_s; + e2_n = obj.e2_n; + + obj.tau1_w = (spdiag(1./s_w)*refObj.tau1_w')'; + obj.tau1_e = (spdiag(1./s_e)*refObj.tau1_e')'; + obj.tau1_s = (spdiag(1./s_s)*refObj.tau1_s')'; + obj.tau1_n = (spdiag(1./s_n)*refObj.tau1_n')'; + + obj.tau2_w = (spdiag(1./s_w)*refObj.tau2_w')'; + obj.tau2_e = (spdiag(1./s_e)*refObj.tau2_e')'; + obj.tau2_s = (spdiag(1./s_s)*refObj.tau2_s')'; + obj.tau2_n = (spdiag(1./s_n)*refObj.tau2_n')'; + + obj.tau_w = (refObj.e_w'*obj.e1_w*obj.tau1_w')' + (refObj.e_w'*obj.e2_w*obj.tau2_w')'; + obj.tau_e = (refObj.e_e'*obj.e1_e*obj.tau1_e')' + (refObj.e_e'*obj.e2_e*obj.tau2_e')'; + obj.tau_s = (refObj.e_s'*obj.e1_s*obj.tau1_s')' + (refObj.e_s'*obj.e2_s*obj.tau2_s')'; + obj.tau_n = (refObj.e_n'*obj.e1_n*obj.tau1_n')' + (refObj.e_n'*obj.e2_n*obj.tau2_n')'; + + % Physical normals + e_w = obj.e_scalar_w; + e_e = obj.e_scalar_e; + e_s = obj.e_scalar_s; + e_n = obj.e_scalar_n; + + e_w_vec = obj.e_w; + e_e_vec = obj.e_e; + e_s_vec = obj.e_s; + e_n_vec = obj.e_n; + + nu_w = [-1,0]; + nu_e = [1,0]; + nu_s = [0,-1]; + nu_n = [0,1]; + + obj.n_w = cell(2,1); + obj.n_e = cell(2,1); + obj.n_s = cell(2,1); + obj.n_n = cell(2,1); + + n_w_1 = (1./s_w).*e_w'*(J.*(K{1,1}*nu_w(1) + K{1,2}*nu_w(2))); + n_w_2 = (1./s_w).*e_w'*(J.*(K{2,1}*nu_w(1) + K{2,2}*nu_w(2))); + obj.n_w{1} = spdiag(n_w_1); + obj.n_w{2} = spdiag(n_w_2); + + n_e_1 = (1./s_e).*e_e'*(J.*(K{1,1}*nu_e(1) + K{1,2}*nu_e(2))); + n_e_2 = (1./s_e).*e_e'*(J.*(K{2,1}*nu_e(1) + K{2,2}*nu_e(2))); + obj.n_e{1} = spdiag(n_e_1); + obj.n_e{2} = spdiag(n_e_2); + + n_s_1 = (1./s_s).*e_s'*(J.*(K{1,1}*nu_s(1) + K{1,2}*nu_s(2))); + n_s_2 = (1./s_s).*e_s'*(J.*(K{2,1}*nu_s(1) + K{2,2}*nu_s(2))); + obj.n_s{1} = spdiag(n_s_1); + obj.n_s{2} = spdiag(n_s_2); + + n_n_1 = (1./s_n).*e_n'*(J.*(K{1,1}*nu_n(1) + K{1,2}*nu_n(2))); + n_n_2 = (1./s_n).*e_n'*(J.*(K{2,1}*nu_n(1) + K{2,2}*nu_n(2))); + obj.n_n{1} = spdiag(n_n_1); + obj.n_n{2} = spdiag(n_n_2); + + % Operators that extract the normal component + obj.en_w = (obj.n_w{1}*obj.e1_w' + obj.n_w{2}*obj.e2_w')'; + obj.en_e = (obj.n_e{1}*obj.e1_e' + obj.n_e{2}*obj.e2_e')'; + obj.en_s = (obj.n_s{1}*obj.e1_s' + obj.n_s{2}*obj.e2_s')'; + obj.en_n = (obj.n_n{1}*obj.e1_n' + obj.n_n{2}*obj.e2_n')'; + + % Stress operators + sigma = cell(dim, dim); + D1 = {obj.Dx, obj.Dy}; + E = obj.E; + N = length(obj.RHO); + for i = 1:dim + for j = 1:dim + sigma{i,j} = sparse(N,2*N); + for k = 1:dim + for l = 1:dim + sigma{i,j} = sigma{i,j} + obj.C{i,j,k,l}*D1{k}*E{l}'; + end + end + end + end + obj.sigma = sigma; + + % Misc. + obj.refObj = refObj; + obj.m = refObj.m; + obj.h = refObj.h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + + [closure, penalty] = obj.refObj.boundary_condition(boundary, bc, tuning); + + type = bc{2}; + + switch type + case {'F','f','Free','free','traction','Traction','t','T'} + s = obj.(['s_' boundary]); + s = spdiag(s); + penalty = penalty*s; + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + [closure, penalty] = obj.refObj.interface(boundary,neighbour_scheme.refObj,neighbour_boundary,type); + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.refObj.h11{1}; + case {'s', 'n'} + h11 = obj.refObj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + % n is a cell of diagonal matrices for each normal component, n{1} = n_1, n{2} = n_2. + function n = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + n = obj.(['n_' boundary]); + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2', 'en'}) + + o = obj.([op, '_', boundary]); + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dStaggeredAnisotropic.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dStaggeredAnisotropic.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,904 @@ +classdef Elastic2dStaggeredAnisotropic < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% Uses a staggered Lebedev grid +% The solution (displacement) is stored on g_u +% Stresses (and hance tractions) appear on g_s +% Density is evaluated on g_u +% The stiffness tensor is evaluated on g_s + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + nGrids + N % Total number of unknowns stored (2 displacement components on 2 grids) + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + RHO % Density + C % Elastic stiffness tensor + + D % Total operator + + % Boundary operators in cell format, used for BC + % T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return scalar field + T_w, T_e, T_s, T_n % Act on scalar, return scalar + + % Inner products + H, H_u, H_s + % , Hi, Hi_kron, H_1D + + % Boundary inner products (for scalar field) + H_w_u, H_e_u, H_s_u, H_n_u + H_w_s, H_e_s, H_s_s, H_n_s + + % Boundary restriction operators + e_w_u, e_e_u, e_s_u, e_n_u % Act on scalar field, return scalar field at boundary + e_w_s, e_e_s, e_s_s, e_n_s % Act on scalar field, return scalar field at boundary + + % U{i}^T picks out component i + U + + % G{i}^T picks out displacement grid i + G + + % Borrowing constants of the form gamma*h, where gamma is a dimensionless constant. + h11 % First entry in norm matrix + + end + + methods + + % The coefficients can either be function handles or grid functions + function obj = Elastic2dStaggeredAnisotropic(g, order, rho, C) + default_arg('rho', @(x,y) 0*x+1); + dim = 2; + nGrids = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x + 1; + end + end + end + end + default_arg('C', C_default); + assert(isa(g, 'grid.Staggered')) + + g_u = g.gridGroups{1}; + g_s = g.gridGroups{2}; + + m_u = {g_u{1}.size(), g_u{2}.size()}; + m_s = {g_s{1}.size(), g_s{2}.size()}; + + if isa(rho, 'function_handle') + rho_vec = cell(nGrids, 1); + for i = 1:nGrids + rho_vec{i} = grid.evalOn(g_u{i}, rho); + end + rho = rho_vec; + end + for i = 1:nGrids + RHO{i} = spdiag(rho{i}); + end + obj.RHO = RHO; + + C_mat = cell(nGrids, 1); + for a = 1:nGrids + C_mat{a} = cell(dim,dim,dim,dim); + end + for a = 1:nGrids + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if numel(C) == nGrids + C_mat{a}{i,j,k,l} = spdiag(C{a}{i,j,k,l}); + else + C_mat{a}{i,j,k,l} = spdiag(grid.evalOn(g_s{a}, C{i,j,k,l})); + end + end + end + end + end + end + C = C_mat; + obj.C = C; + + % Reference m for primal grid + m = g_u{1}.size(); + X = g_u{1}.points; + lim = cell(dim, 1); + for i = 1:dim + lim{i} = {min(X(:,i)), max(X(:,i))}; + end + + % 1D operators + ops = cell(dim,1); + D1p = cell(dim, 1); + D1d = cell(dim, 1); + mp = cell(dim, 1); + md = cell(dim, 1); + Ip = cell(dim, 1); + Id = cell(dim, 1); + Hp = cell(dim, 1); + Hd = cell(dim, 1); + + opSet = @sbp.D1StaggeredUpwind; + for i = 1:dim + ops{i} = opSet(m(i), lim{i}, order); + D1p{i} = ops{i}.D1_dual; + D1d{i} = ops{i}.D1_primal; + mp{i} = length(ops{i}.x_primal); + md{i} = length(ops{i}.x_dual); + Ip{i} = speye(mp{i}, mp{i}); + Id{i} = speye(md{i}, md{i}); + Hp{i} = ops{i}.H_primal; + Hd{i} = ops{i}.H_dual; + ep_l{i} = ops{i}.e_primal_l; + ep_r{i} = ops{i}.e_primal_r; + ed_l{i} = ops{i}.e_dual_l; + ed_r{i} = ops{i}.e_dual_r; + end + + % Borrowing constants + % for i = 1:dim + % obj.h11{i} = ops{i}.H_dual(1,1); + % end + obj.h11{1}{1} = ops{1}.H_dual(1,1); + obj.h11{1}{2} = ops{2}.H_primal(1,1); + obj.h11{2}{1} = ops{1}.H_primal(1,1); + obj.h11{2}{2} = ops{2}.H_dual(1,1); + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + % Quadratures + obj.H_u = cell(nGrids, 1); + obj.H_s = cell(nGrids, 1); + obj.H_u{1} = kron(Hp{1}, Hp{2}); + obj.H_u{2} = kron(Hd{1}, Hd{2}); + obj.H_s{1} = kron(Hd{1}, Hp{2}); + obj.H_s{2} = kron(Hp{1}, Hd{2}); + + obj.H_w_s = cell(nGrids, 1); + obj.H_e_s = cell(nGrids, 1); + obj.H_s_s = cell(nGrids, 1); + obj.H_n_s = cell(nGrids, 1); + + obj.H_w_s{1} = Hp{2}; + obj.H_w_s{2} = Hd{2}; + obj.H_e_s{1} = Hp{2}; + obj.H_e_s{2} = Hd{2}; + + obj.H_s_s{1} = Hd{1}; + obj.H_s_s{2} = Hp{1}; + obj.H_n_s{1} = Hd{1}; + obj.H_n_s{2} = Hp{1}; + + % Boundary restriction ops + e_w_u = cell(nGrids, 1); + e_s_u = cell(nGrids, 1); + e_e_u = cell(nGrids, 1); + e_n_u = cell(nGrids, 1); + + e_w_s = cell(nGrids, 1); + e_s_s = cell(nGrids, 1); + e_e_s = cell(nGrids, 1); + e_n_s = cell(nGrids, 1); + + e_w_u{1} = kron(ep_l{1}, Ip{2}); + e_e_u{1} = kron(ep_r{1}, Ip{2}); + e_s_u{1} = kron(Ip{1}, ep_l{2}); + e_n_u{1} = kron(Ip{1}, ep_r{2}); + + e_w_u{2} = kron(ed_l{1}, Id{2}); + e_e_u{2} = kron(ed_r{1}, Id{2}); + e_s_u{2} = kron(Id{1}, ed_l{2}); + e_n_u{2} = kron(Id{1}, ed_r{2}); + + e_w_s{1} = kron(ed_l{1}, Ip{2}); + e_e_s{1} = kron(ed_r{1}, Ip{2}); + e_s_s{1} = kron(Id{1}, ep_l{2}); + e_n_s{1} = kron(Id{1}, ep_r{2}); + + e_w_s{2} = kron(ep_l{1}, Id{2}); + e_e_s{2} = kron(ep_r{1}, Id{2}); + e_s_s{2} = kron(Ip{1}, ed_l{2}); + e_n_s{2} = kron(Ip{1}, ed_r{2}); + + obj.e_w_u = e_w_u; + obj.e_e_u = e_e_u; + obj.e_s_u = e_s_u; + obj.e_n_u = e_n_u; + + obj.e_w_s = e_w_s; + obj.e_e_s = e_e_s; + obj.e_s_s = e_s_s; + obj.e_n_s = e_n_s; + + + % D1_u2s{a, b}{i} approximates ddi and + % takes from u grid number b to s grid number a + % Some of D1_x2y{a, b} are 0. + D1_u2s = cell(nGrids, nGrids); + D1_s2u = cell(nGrids, nGrids); + + N_u = cell(nGrids, 1); + N_s = cell(nGrids, 1); + for a = 1:nGrids + N_u{a} = g_u{a}.N(); + N_s{a} = g_s{a}.N(); + end + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + D1_u2s{1,1}{1} = kron(D1p{1}, Ip{2}); + D1_s2u{1,1}{1} = kron(D1d{1}, Ip{2}); + + D1_u2s{1,2}{2} = kron(Id{1}, D1d{2}); + D1_u2s{2,1}{2} = kron(Ip{1}, D1p{2}); + + D1_s2u{1,2}{2} = kron(Ip{1}, D1d{2}); + D1_s2u{2,1}{2} = kron(Id{1}, D1p{2}); + + D1_u2s{2,2}{1} = kron(D1d{1}, Id{2}); + D1_s2u{2,2}{1} = kron(D1p{1}, Id{2}); + + D1_u2s{1,1}{2} = sparse(N_s{1}, N_u{1}); + D1_s2u{1,1}{2} = sparse(N_u{1}, N_s{1}); + + D1_u2s{2,2}{2} = sparse(N_s{2}, N_u{2}); + D1_s2u{2,2}{2} = sparse(N_u{2}, N_s{2}); + + D1_u2s{1,2}{1} = sparse(N_s{1}, N_u{2}); + D1_s2u{1,2}{1} = sparse(N_u{1}, N_s{2}); + + D1_u2s{2,1}{1} = sparse(N_s{2}, N_u{1}); + D1_s2u{2,1}{1} = sparse(N_u{2}, N_s{1}); + + + %---- Combine grids and components ----- + + % U{a}{i}^T picks out u component i on grid a + U = cell(nGrids, 1); + for a = 1:2 + U{a} = cell(dim, 1); + I = speye(N_u{a}, N_u{a}); + for i = 1:dim + E = sparse(dim,1); + E(i) = 1; + U{a}{i} = kron(I, E); + end + end + obj.U = U; + + % Order grids + % u1, u2 + Iu1 = speye(dim*N_u{1}, dim*N_u{1}); + Iu2 = speye(dim*N_u{2}, dim*N_u{2}); + + Gu1 = cell2mat( {Iu1; sparse(dim*N_u{2}, dim*N_u{1})} ); + Gu2 = cell2mat( {sparse(dim*N_u{1}, dim*N_u{2}); Iu2} ); + + G = {Gu1; Gu2}; + obj.G = G; + + obj.H = G{1}*(U{1}{1}*obj.H_u{1}*U{1}{1}' + U{1}{2}*obj.H_u{1}*U{1}{2}')*G{1}'... + + G{2}*(U{2}{1}*obj.H_u{2}*U{2}{1}' + U{2}{2}*obj.H_u{2}*U{2}{2}')*G{2}'; + + % e1_w = (e_scalar_w'*E{1}')'; + % e1_e = (e_scalar_e'*E{1}')'; + % e1_s = (e_scalar_s'*E{1}')'; + % e1_n = (e_scalar_n'*E{1}')'; + + % e2_w = (e_scalar_w'*E{2}')'; + % e2_e = (e_scalar_e'*E{2}')'; + % e2_s = (e_scalar_s'*E{2}')'; + % e2_n = (e_scalar_n'*E{2}')'; + + stencilWidth = order; + % Differentiation matrix D (without SAT) + N = dim*(N_u{1} + N_u{2}); + D = spalloc(N, N, stencilWidth^2*N); + for a = 1:nGrids + for b = 1:nGrids + for c = 1:nGrids + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + D = D + (G{a}*U{a}{j})*(RHO{a}\(D1_s2u{a,b}{i}*C{b}{i,j,k,l}*D1_u2s{b,c}{k}*U{c}{l}'*G{c}')); + end + end + end + end + end + end + end + obj.D = D; + clear D; + obj.N = N; + %=========================================%' + + % Numerical traction operators for BC. + % + % Formula at boundary j: % tau^{j}_i = sum_l T^{j}_{il} u_l + % + + n_w = obj.getNormal('w'); + n_e = obj.getNormal('e'); + n_s = obj.getNormal('s'); + n_n = obj.getNormal('n'); + + tau_w = cell(nGrids, 1); + tau_e = cell(nGrids, 1); + tau_s = cell(nGrids, 1); + tau_n = cell(nGrids, 1); + + T_w = cell(nGrids, nGrids); + T_e = cell(nGrids, nGrids); + T_s = cell(nGrids, nGrids); + T_n = cell(nGrids, nGrids); + for b = 1:nGrids + [~, m_we] = size(e_w_s{b}); + [~, m_sn] = size(e_s_s{b}); + for c = 1:nGrids + T_w{b,c} = cell(dim, dim); + T_e{b,c} = cell(dim, dim); + T_s{b,c} = cell(dim, dim); + T_n{b,c} = cell(dim, dim); + + for i = 1:dim + for j = 1:dim + T_w{b,c}{i,j} = sparse(N_u{c}, m_we); + T_e{b,c}{i,j} = sparse(N_u{c}, m_we); + T_s{b,c}{i,j} = sparse(N_u{c}, m_sn); + T_n{b,c}{i,j} = sparse(N_u{c}, m_sn); + end + end + end + end + + for b = 1:nGrids + tau_w{b} = cell(dim, 1); + tau_e{b} = cell(dim, 1); + tau_s{b} = cell(dim, 1); + tau_n{b} = cell(dim, 1); + + for j = 1:dim + tau_w{b}{j} = sparse(N, m_s{b}(2)); + tau_e{b}{j} = sparse(N, m_s{b}(2)); + tau_s{b}{j} = sparse(N, m_s{b}(1)); + tau_n{b}{j} = sparse(N, m_s{b}(1)); + end + + for c = 1:nGrids + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + sigma_b_ij = C{b}{i,j,k,l}*D1_u2s{b,c}{k}*U{c}{l}'*G{c}'; + + tau_w{b}{j} = tau_w{b}{j} + (e_w_s{b}'*n_w(i)*sigma_b_ij)'; + tau_e{b}{j} = tau_e{b}{j} + (e_e_s{b}'*n_e(i)*sigma_b_ij)'; + tau_s{b}{j} = tau_s{b}{j} + (e_s_s{b}'*n_s(i)*sigma_b_ij)'; + tau_n{b}{j} = tau_n{b}{j} + (e_n_s{b}'*n_n(i)*sigma_b_ij)'; + + S_bc_ijl = C{b}{i,j,k,l}*D1_u2s{b,c}{k}; + + T_w{b,c}{j,l} = T_w{b,c}{j,l} + (e_w_s{b}'*n_w(i)*S_bc_ijl)'; + T_e{b,c}{j,l} = T_e{b,c}{j,l} + (e_e_s{b}'*n_e(i)*S_bc_ijl)'; + T_s{b,c}{j,l} = T_s{b,c}{j,l} + (e_s_s{b}'*n_s(i)*S_bc_ijl)'; + T_n{b,c}{j,l} = T_n{b,c}{j,l} + (e_n_s{b}'*n_n(i)*S_bc_ijl)'; + end + end + end + end + end + end + + obj.tau_w = tau_w; + obj.tau_e = tau_e; + obj.tau_s = tau_s; + obj.tau_n = tau_n; + + obj.T_w = T_w; + obj.T_e = T_e; + obj.T_s = T_s; + obj.T_n = T_n; + + % D1 = obj.D1; + + % Traction tensors, T_ij + % obj.T_w = T_l{1}; + % obj.T_e = T_r{1}; + % obj.T_s = T_l{2}; + % obj.T_n = T_r{2}; + + % Restriction operators + % obj.e_w = e_w; + % obj.e_e = e_e; + % obj.e_s = e_s; + % obj.e_n = e_n; + + % obj.e1_w = e1_w; + % obj.e1_e = e1_e; + % obj.e1_s = e1_s; + % obj.e1_n = e1_n; + + % obj.e2_w = e2_w; + % obj.e2_e = e2_e; + % obj.e2_s = e2_s; + % obj.e2_n = e2_n; + + % obj.e_scalar_w = e_scalar_w; + % obj.e_scalar_e = e_scalar_e; + % obj.e_scalar_s = e_scalar_s; + % obj.e_scalar_n = e_scalar_n; + + % % First component of traction + % obj.tau1_w = tau_l{1}{1}; + % obj.tau1_e = tau_r{1}{1}; + % obj.tau1_s = tau_l{2}{1}; + % obj.tau1_n = tau_r{2}{1}; + + % % Second component of traction + % obj.tau2_w = tau_l{1}{2}; + % obj.tau2_e = tau_r{1}{2}; + % obj.tau2_s = tau_l{2}{2}; + % obj.tau2_n = tau_r{2}{2}; + + % % Traction vectors + % obj.tau_w = (e_w'*e1_w*obj.tau1_w')' + (e_w'*e2_w*obj.tau2_w')'; + % obj.tau_e = (e_e'*e1_e*obj.tau1_e')' + (e_e'*e2_e*obj.tau2_e')'; + % obj.tau_s = (e_s'*e1_s*obj.tau1_s')' + (e_s'*e2_s*obj.tau2_s')'; + % obj.tau_n = (e_n'*e1_n*obj.tau1_n')' + (e_n'*e2_n*obj.tau2_n')'; + + % Misc. + obj.m = m; + obj.h = []; + obj.order = order; + obj.grid = g; + obj.dim = dim; + obj.nGrids = nGrids; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + comp = bc{1}; + type = bc{2}; + if ischar(comp) + comp = obj.getComponent(comp, boundary); + end + + e_u = obj.getBoundaryOperatorForScalarField('e_u', boundary); + e_s = obj.getBoundaryOperatorForScalarField('e_s', boundary); + tau = obj.getBoundaryOperator('tau', boundary); + T = obj.getBoundaryTractionOperator(boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); + nu = obj.getNormal(boundary); + + U = obj.U; + G = obj.G; + H = obj.H_u; + RHO = obj.RHO; + C = obj.C; + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + switch boundary + case {'w', 'e'} + gridCombos = {{1,1}, {2,2}}; + case {'s', 'n'} + gridCombos = {{2,1}, {1,2}}; + end + + dim = obj.dim; + nGrids = obj.nGrids; + + m_tot = obj.N; + + % Preallocate + [~, col] = size(tau{1}{1}); + closure = sparse(m_tot, m_tot); + penalty = cell(1, nGrids); + for a = 1:nGrids + [~, col] = size(e_u{a}); + penalty{a} = sparse(m_tot, col); + end + + j = comp; + switch type + + % Dirichlet boundary condition + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + + if numel(bc) >= 3 + dComps = bc{3}; + else + dComps = 1:dim; + end + + % Loops over components that Dirichlet penalties end up on + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Nonsymmetric part goes on all components to + % yield traction in discrete energy rate + for c = 1:nGrids + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + + for i = 1:dim + Y = T{a,c}{j,i}'; + closure = closure + G{c}*U{c}{i}*((RHO{c}*H{c})\(Y'*H_gamma{a}*(e_u{b}'*U{b}{j}'*G{b}') )); + penalty{b} = penalty{b} - G{c}*U{c}{i}*((RHO{c}*H{c})\(Y'*H_gamma{a}) ); + end + end + end + + % Symmetric part only required on components with displacement BC. + % (Otherwise it's not symmetric.) + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + + h11 = obj.getBorrowing(b, boundary); + + for i = dComps + Z = 0*C{b}{1,1,1,1}; + for l = 1:dim + for k = 1:dim + Z = Z + nu(l)*C{b}{l,i,k,j}*nu(k); + end + end + Z = -tuning*dim/h11*Z; + X = e_s{b}'*Z*e_s{b}; + closure = closure + G{a}*U{a}{i}*((RHO{a}*H{a})\(e_u{a}*X'*H_gamma{b}*(e_u{a}'*U{a}{j}'*G{a}' ) )); + penalty{a} = penalty{a} - G{a}*U{a}{i}*((RHO{a}*H{a})\(e_u{a}*X'*H_gamma{b} )); + end + end + + % Free boundary condition + case {'F','f','Free','free','traction','Traction','t','T'} + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + closure = closure - G{a}*U{a}{j}*(RHO{a}\(H{a}\(e_u{a}*H_gamma{b}*tau{b}{j}'))); + penalty{b} = G{a}*U{a}{j}*(RHO{a}\(H{a}\(e_u{a}*H_gamma{b}))); + end + + % Unknown boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + + penalty = cell2mat(penalty); + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + u = obj; + v = neighbour_scheme; + + % Operators, u side + eu_u = u.getBoundaryOperatorForScalarField('e_u', boundary); + es_u = u.getBoundaryOperatorForScalarField('e_s', boundary); + tau_u = u.getBoundaryOperator('tau', boundary); + nu_u = u.getNormal(boundary); + + G_u = u.G; + U_u = u.U; + C_u = u.C; + m_tot_u = u.N; + + % Operators, v side + eu_v = v.getBoundaryOperatorForScalarField('e_u', neighbour_boundary); + es_v = v.getBoundaryOperatorForScalarField('e_s', neighbour_boundary); + tau_v = v.getBoundaryOperator('tau', neighbour_boundary); + nu_v = v.getNormal(neighbour_boundary); + + G_v = v.G; + U_v = v.U; + C_v = v.C; + m_tot_v = v.N; + + % Operators that are only required for own domain + % Hi = u.Hi_kron; + % RHOi = u.RHOi_kron; + % e_kron = u.getBoundaryOperator('e', boundary); + H = u.H_u; + RHO = u.RHO; + T_u = u.getBoundaryTractionOperator(boundary); + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + % H_gamma_kron = u.getBoundaryQuadrature(boundary); + dim = u.dim; + nGrids = obj.nGrids; + + % Preallocate + % [~, m_int] = size(H_gamma); + closure = sparse(m_tot_u, m_tot_u); + penalty = sparse(m_tot_u, m_tot_v); + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + switch boundary + case {'w', 'e'} + switch neighbour_boundary + case {'w', 'e'} + gridCombos = {{1,1,1}, {2,2,2}}; + case {'s', 'n'} + gridCombos = {{1,1,2}, {2,2,1}}; + end + case {'s', 'n'} + switch neighbour_boundary + case {'s', 'n'} + gridCombos = {{2,1,1}, {1,2,2}}; + case {'w', 'e'} + gridCombos = {{2,1,2}, {1,2,1}}; + end + end + + % Symmetrizing part + for c = 1:nGrids + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + + for i = 1:dim + for j = 1:dim + Y = 1/2*T_u{a,c}{j,i}'; + closure = closure + G_u{c}*U_u{c}{i}*((RHO{c}*H{c})\(Y'*H_gamma{a}*(eu_u{b}'*U_u{b}{j}'*G_u{b}') )); + penalty = penalty - G_u{c}*U_u{c}{i}*((RHO{c}*H{c})\(Y'*H_gamma{a}*(eu_v{b}'*U_v{b}{j}'*G_v{b}') )); + end + end + end + end + + % Symmetric part + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + bv = gc{3}; + + h11_u = u.getBorrowing(b, boundary); + h11_v = v.getBorrowing(bv, neighbour_boundary); + + for i = 1:dim + for j = 1:dim + Z_u = 0*es_u{b}'*es_u{b}; + Z_v = 0*es_v{bv}'*es_v{bv}; + for l = 1:dim + for k = 1:dim + Z_u = Z_u + es_u{b}'*nu_u(l)*C_u{b}{l,i,k,j}*nu_u(k)*es_u{b}; + Z_v = Z_v + es_v{bv}'*nu_v(l)*C_v{bv}{l,i,k,j}*nu_v(k)*es_v{bv}; + end + end + Z = -tuning*dim*( 1/(4*h11_u)*Z_u + 1/(4*h11_v)*Z_v ); + % X = es_u{b}'*Z*es_u{b}; + % X = Z; + closure = closure + G_u{a}*U_u{a}{i}*((RHO{a}*H{a})\(eu_u{a}*Z'*H_gamma{b}*(eu_u{a}'*U_u{a}{j}'*G_u{a}' ) )); + penalty = penalty - G_u{a}*U_u{a}{i}*((RHO{a}*H{a})\(eu_u{a}*Z'*H_gamma{b}*(eu_v{a}'*U_v{a}{j}'*G_v{a}' ) )); + end + end + end + + % Continuity of traction + for j = 1:dim + for m = 1:numel(gridCombos) + gc = gridCombos{m}; + a = gc{1}; + b = gc{2}; + bv = gc{3}; + closure = closure - 1/2*G_u{a}*U_u{a}{j}*(RHO{a}\(H{a}\(eu_u{a}*H_gamma{b}*tau_u{b}{j}'))); + penalty = penalty - 1/2*G_u{a}*U_u{a}{j}*(RHO{a}\(H{a}\(eu_u{a}*H_gamma{b}*tau_v{bv}{j}'))); + end + end + + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + error('Non-conforming interfaces not implemented yet.'); + end + + % Returns the component number that is the tangential/normal component + % at the specified boundary + function comp = getComponent(obj, comp_str, boundary) + assertIsMember(comp_str, {'normal', 'tangential'}); + assertIsMember(boundary, {'w', 'e', 's', 'n'}); + + switch boundary + case {'w', 'e'} + switch comp_str + case 'normal' + comp = 1; + case 'tangential' + comp = 2; + end + case {'s', 'n'} + switch comp_str + case 'normal' + comp = 2; + case 'tangential' + comp = 1; + end + end + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, stressGrid, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.h11{stressGrid}{1}; + case {'s', 'n'} + h11 = obj.h11{stressGrid}{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + function nu = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case 'w' + nu = [-1,0]; + case 'e' + nu = [1,0]; + case 's' + nu = [0,-1]; + case 'n' + nu = [0,1]; + end + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'}) + + switch op + case {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'} + o = obj.([op, '_', boundary]); + end + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e_u', 'e_s'}) + + switch op + case 'e_u' + o = obj.(['e_', boundary, '_u']); + case 'e_s' + o = obj.(['e_', boundary, '_s']); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary, '_s']); + end + + function N = size(obj) + N = length(obj.D); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dStaggeredCurvilinearAnisotropic.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dStaggeredCurvilinearAnisotropic.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,694 @@ +classdef Elastic2dStaggeredCurvilinearAnisotropic < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% in curvilinear coordinates, using Lebedev staggered grids + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + nGrids + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + J, Ji + RHO % Density + C % Elastic stiffness tensor + + D % Total operator + + % Dx, Dy % Physical derivatives + n_w, n_e, n_s, n_n % Physical normals + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + % tau_w, tau_e, tau_s, tau_n % Return vector field + % tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + % tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + + % Inner products + H + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Surface Jacobian vectors + s_w, s_e, s_s, s_n + + % Boundary restriction operators + e_w_u, e_e_u, e_s_u, e_n_u % Act on scalar field, return scalar field at boundary + e_w_s, e_e_s, e_s_s, e_n_s % Act on scalar field, return scalar field at boundary + % e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + % e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + % e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + % en_w, en_e, en_s, en_n % Act on vector field, return normal component + + % U{i}^T picks out component i + U + + % G{i}^T picks out displacement grid i + G + + % Elastic2dVariableAnisotropic object for reference domain + refObj + end + + methods + + % The coefficients can either be function handles or grid functions + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dStaggeredCurvilinearAnisotropic(g, order, rho, C) + default_arg('rho', @(x,y) 0*x+1); + + opSet = @sbp.D1StaggeredUpwind; + dim = 2; + nGrids = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x; + end + end + end + end + default_arg('C', C_default); + assert(isa(g, 'grid.Staggered')); + + g_u = g.gridGroups{1}; + g_s = g.gridGroups{2}; + + m_u = {g_u{1}.size(), g_u{2}.size()}; + m_s = {g_s{1}.size(), g_s{2}.size()}; + + if isa(rho, 'function_handle') + rho_vec = cell(nGrids, 1); + for a = 1:nGrids + rho_vec{a} = grid.evalOn(g_u{a}, rho); + end + rho = rho_vec; + end + for a = 1:nGrids + RHO{a} = spdiag(rho{a}); + end + obj.RHO = RHO; + + C_mat = cell(nGrids, 1); + for a = 1:nGrids + C_mat{a} = cell(dim,dim,dim,dim); + end + for a = 1:nGrids + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if numel(C) == dim + C_mat{a}{i,j,k,l} = spdiag(C{a}{i,j,k,l}); + else + C_mat{a}{i,j,k,l} = spdiag(grid.evalOn(g_s{a}, C{i,j,k,l})); + end + end + end + end + end + end + obj.C = C_mat; + + C = cell(nGrids, 1); + for a = 1:nGrids + C{a} = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C{a}{i,j,k,l} = diag(C_mat{a}{i,j,k,l}); + end + end + end + end + end + + % Reference m for primal grid + m = g_u{1}.size(); + + % 1D operators + ops = cell(dim,1); + D1p = cell(dim, 1); + D1d = cell(dim, 1); + mp = cell(dim, 1); + md = cell(dim, 1); + Ip = cell(dim, 1); + Id = cell(dim, 1); + Hp = cell(dim, 1); + Hd = cell(dim, 1); + + opSet = @sbp.D1StaggeredUpwind; + for i = 1:dim + ops{i} = opSet(m(i), {0,1}, order); + D1p{i} = ops{i}.D1_dual; + D1d{i} = ops{i}.D1_primal; + mp{i} = length(ops{i}.x_primal); + md{i} = length(ops{i}.x_dual); + Ip{i} = speye(mp{i}, mp{i}); + Id{i} = speye(md{i}, md{i}); + Hp{i} = ops{i}.H_primal; + Hd{i} = ops{i}.H_dual; + ep_l{i} = ops{i}.e_primal_l; + ep_r{i} = ops{i}.e_primal_r; + ed_l{i} = ops{i}.e_dual_l; + ed_r{i} = ops{i}.e_dual_r; + end + + % D1_u2s{a, b}{i} approximates ddi and + % takes from u grid number b to s grid number a + % Some of D1_x2y{a, b} are 0. + D1_u2s = cell(nGrids, nGrids); + D1_s2u = cell(nGrids, nGrids); + + N_u = cell(nGrids, 1); + N_s = cell(nGrids, 1); + for a = 1:nGrids + N_u{a} = g_u{a}.N(); + N_s{a} = g_s{a}.N(); + end + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + % Logical operators + D1_u2s{1,1}{1} = kron(D1p{1}, Ip{2}); + D1_s2u{1,1}{1} = kron(D1d{1}, Ip{2}); + + D1_u2s{1,2}{2} = kron(Id{1}, D1d{2}); + D1_u2s{2,1}{2} = kron(Ip{1}, D1p{2}); + + D1_s2u{1,2}{2} = kron(Ip{1}, D1d{2}); + D1_s2u{2,1}{2} = kron(Id{1}, D1p{2}); + + D1_u2s{2,2}{1} = kron(D1d{1}, Id{2}); + D1_s2u{2,2}{1} = kron(D1p{1}, Id{2}); + + D1_u2s{1,1}{2} = sparse(N_s{1}, N_u{1}); + D1_s2u{1,1}{2} = sparse(N_u{1}, N_s{1}); + + D1_u2s{2,2}{2} = sparse(N_s{2}, N_u{2}); + D1_s2u{2,2}{2} = sparse(N_u{2}, N_s{2}); + + D1_u2s{1,2}{1} = sparse(N_s{1}, N_u{2}); + D1_s2u{1,2}{1} = sparse(N_u{1}, N_s{2}); + + D1_u2s{2,1}{1} = sparse(N_s{2}, N_u{1}); + D1_s2u{2,1}{1} = sparse(N_u{2}, N_s{1}); + + + %---- Combine grids and components ----- + + % U{a}{i}^T picks out u component i on grid a + U = cell(nGrids, 1); + for a = 1:nGrids + U{a} = cell(dim, 1); + I = speye(N_u{a}, N_u{a}); + for i = 1:dim + E = sparse(dim,1); + E(i) = 1; + U{a}{i} = kron(I, E); + end + end + obj.U = U; + + % Order grids + % u1, u2 + Iu1 = speye(dim*N_u{1}, dim*N_u{1}); + Iu2 = speye(dim*N_u{2}, dim*N_u{2}); + + Gu1 = cell2mat( {Iu1; sparse(dim*N_u{2}, dim*N_u{1})} ); + Gu2 = cell2mat( {sparse(dim*N_u{1}, dim*N_u{2}); Iu2} ); + + G = {Gu1; Gu2}; + + %---- Grid layout ------- + % gu1 = xp o yp; + % gu2 = xd o yd; + % gs1 = xd o yp; + % gs2 = xp o yd; + %------------------------ + + % Boundary restriction ops + e_w_u = cell(nGrids, 1); + e_s_u = cell(nGrids, 1); + e_e_u = cell(nGrids, 1); + e_n_u = cell(nGrids, 1); + + e_w_s = cell(nGrids, 1); + e_s_s = cell(nGrids, 1); + e_e_s = cell(nGrids, 1); + e_n_s = cell(nGrids, 1); + + e_w_u{1} = kron(ep_l{1}, Ip{2}); + e_e_u{1} = kron(ep_r{1}, Ip{2}); + e_s_u{1} = kron(Ip{1}, ep_l{2}); + e_n_u{1} = kron(Ip{1}, ep_r{2}); + + e_w_u{2} = kron(ed_l{1}, Id{2}); + e_e_u{2} = kron(ed_r{1}, Id{2}); + e_s_u{2} = kron(Id{1}, ed_l{2}); + e_n_u{2} = kron(Id{1}, ed_r{2}); + + e_w_s{1} = kron(ed_l{1}, Ip{2}); + e_e_s{1} = kron(ed_r{1}, Ip{2}); + e_s_s{1} = kron(Id{1}, ep_l{2}); + e_n_s{1} = kron(Id{1}, ep_r{2}); + + e_w_s{2} = kron(ep_l{1}, Id{2}); + e_e_s{2} = kron(ep_r{1}, Id{2}); + e_s_s{2} = kron(Ip{1}, ed_l{2}); + e_n_s{2} = kron(Ip{1}, ed_r{2}); + + % --- Metric coefficients on stress grids ------- + x = cell(nGrids, 1); + y = cell(nGrids, 1); + J = cell(nGrids, 1); + x_xi = cell(nGrids, 1); + x_eta = cell(nGrids, 1); + y_xi = cell(nGrids, 1); + y_eta = cell(nGrids, 1); + + for a = 1:nGrids + coords = g_u{a}.points(); + x{a} = coords(:,1); + y{a} = coords(:,2); + end + + for a = 1:nGrids + x_xi{a} = zeros(N_s{a}, 1); + y_xi{a} = zeros(N_s{a}, 1); + x_eta{a} = zeros(N_s{a}, 1); + y_eta{a} = zeros(N_s{a}, 1); + + for b = 1:nGrids + x_xi{a} = x_xi{a} + D1_u2s{a,b}{1}*x{b}; + y_xi{a} = y_xi{a} + D1_u2s{a,b}{1}*y{b}; + x_eta{a} = x_eta{a} + D1_u2s{a,b}{2}*x{b}; + y_eta{a} = y_eta{a} + D1_u2s{a,b}{2}*y{b}; + end + end + + for a = 1:nGrids + J{a} = x_xi{a}.*y_eta{a} - x_eta{a}.*y_xi{a}; + end + + K = cell(nGrids, 1); + for a = 1:nGrids + K{a} = cell(dim, dim); + K{a}{1,1} = y_eta{a}./J{a}; + K{a}{1,2} = -y_xi{a}./J{a}; + K{a}{2,1} = -x_eta{a}./J{a}; + K{a}{2,2} = x_xi{a}./J{a}; + end + % ---------------------------------------------- + + % --- Metric coefficients on displacement grids ------- + x_s = cell(nGrids, 1); + y_s = cell(nGrids, 1); + J_u = cell(nGrids, 1); + x_xi_u = cell(nGrids, 1); + x_eta_u = cell(nGrids, 1); + y_xi_u = cell(nGrids, 1); + y_eta_u = cell(nGrids, 1); + + for a = 1:nGrids + coords = g_s{a}.points(); + x_s{a} = coords(:,1); + y_s{a} = coords(:,2); + end + + for a = 1:nGrids + x_xi_u{a} = zeros(N_u{a}, 1); + y_xi_u{a} = zeros(N_u{a}, 1); + x_eta_u{a} = zeros(N_u{a}, 1); + y_eta_u{a} = zeros(N_u{a}, 1); + + for b = 1:nGrids + x_xi_u{a} = x_xi_u{a} + D1_s2u{a,b}{1}*x_s{b}; + y_xi_u{a} = y_xi_u{a} + D1_s2u{a,b}{1}*y_s{b}; + x_eta_u{a} = x_eta_u{a} + D1_s2u{a,b}{2}*x_s{b}; + y_eta_u{a} = y_eta_u{a} + D1_s2u{a,b}{2}*y_s{b}; + end + end + + for a = 1:nGrids + J_u{a} = x_xi_u{a}.*y_eta_u{a} - x_eta_u{a}.*y_xi_u{a}; + end + % ---------------------------------------------- + + % x_u = Du*x; + % x_v = Dv*x; + % y_u = Du*y; + % y_v = Dv*y; + + % J = x_u.*y_v - x_v.*y_u; + + % K = cell(dim, dim); + % K{1,1} = y_v./J; + % K{1,2} = -y_u./J; + % K{2,1} = -x_v./J; + % K{2,2} = x_u./J; + + % Physical derivatives + % obj.Dx = spdiag( y_v./J)*Du + spdiag(-y_u./J)*Dv; + % obj.Dy = spdiag(-x_v./J)*Du + spdiag( x_u./J)*Dv; + + % Wrap around Aniosotropic Cartesian. Transformed density and stiffness + rho_tilde = cell(nGrids, 1); + PHI = cell(nGrids, 1); + + for a = 1:nGrids + rho_tilde{a} = J_u{a}.*rho{a}; + end + + for a = 1:nGrids + PHI{a} = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + PHI{a}{i,j,k,l} = 0*C{a}{i,j,k,l}; + for m = 1:dim + for n = 1:dim + PHI{a}{i,j,k,l} = PHI{a}{i,j,k,l} + J{a}.*K{a}{m,i}.*C{a}{m,j,n,l}.*K{a}{n,k}; + end + end + end + end + end + end + end + + refObj = scheme.Elastic2dStaggeredAnisotropic(g.logic, order, rho_tilde, PHI); + + G = refObj.G; + U = refObj.U; + H_u = refObj.H_u; + + % Volume quadrature + [m, n] = size(refObj.H); + obj.H = sparse(m, n); + obj.J = sparse(m, n); + for a = 1:nGrids + for i = 1:dim + obj.H = obj.H + G{a}*U{a}{i}*spdiag(J_u{a})*refObj.H_u{a}*U{a}{i}'*G{a}'; + obj.J = obj.J + G{a}*U{a}{i}*spdiag(J_u{a})*U{a}{i}'*G{a}'; + end + end + obj.Ji = inv(obj.J); + + % Boundary quadratures on stress grids + s_w = cell(nGrids, 1); + s_e = cell(nGrids, 1); + s_s = cell(nGrids, 1); + s_n = cell(nGrids, 1); + + % e_w_u = refObj.e_w_u; + % e_e_u = refObj.e_e_u; + % e_s_u = refObj.e_s_u; + % e_n_u = refObj.e_n_u; + + e_w_s = refObj.e_w_s; + e_e_s = refObj.e_e_s; + e_s_s = refObj.e_s_s; + e_n_s = refObj.e_n_s; + + for a = 1:nGrids + s_w{a} = sqrt((e_w_s{a}'*x_eta{a}).^2 + (e_w_s{a}'*y_eta{a}).^2); + s_e{a} = sqrt((e_e_s{a}'*x_eta{a}).^2 + (e_e_s{a}'*y_eta{a}).^2); + s_s{a} = sqrt((e_s_s{a}'*x_xi{a}).^2 + (e_s_s{a}'*y_xi{a}).^2); + s_n{a} = sqrt((e_n_s{a}'*x_xi{a}).^2 + (e_n_s{a}'*y_xi{a}).^2); + end + + obj.s_w = s_w; + obj.s_e = s_e; + obj.s_s = s_s; + obj.s_n = s_n; + + % for a = 1:nGrids + % obj.H_w_s{a} = refObj.H_w_s{a}*spdiag(s_w{a}); + % obj.H_e_s{a} = refObj.H_e_s{a}*spdiag(s_e{a}); + % obj.H_s_s{a} = refObj.H_s_s{a}*spdiag(s_s{a}); + % obj.H_n_s{a} = refObj.H_n_s{a}*spdiag(s_n{a}); + % end + + % Restriction operators + obj.e_w_u = refObj.e_w_u; + obj.e_e_u = refObj.e_e_u; + obj.e_s_u = refObj.e_s_u; + obj.e_n_u = refObj.e_n_u; + + obj.e_w_s = refObj.e_w_s; + obj.e_e_s = refObj.e_e_s; + obj.e_s_s = refObj.e_s_s; + obj.e_n_s = refObj.e_n_s; + + % Adapt things from reference object + obj.D = refObj.D; + obj.U = refObj.U; + obj.G = refObj.G; + + % obj.e1_w = refObj.e1_w; + % obj.e1_e = refObj.e1_e; + % obj.e1_s = refObj.e1_s; + % obj.e1_n = refObj.e1_n; + + % obj.e2_w = refObj.e2_w; + % obj.e2_e = refObj.e2_e; + % obj.e2_s = refObj.e2_s; + % obj.e2_n = refObj.e2_n; + + % obj.e_scalar_w = refObj.e_scalar_w; + % obj.e_scalar_e = refObj.e_scalar_e; + % obj.e_scalar_s = refObj.e_scalar_s; + % obj.e_scalar_n = refObj.e_scalar_n; + + % e1_w = obj.e1_w; + % e1_e = obj.e1_e; + % e1_s = obj.e1_s; + % e1_n = obj.e1_n; + + % e2_w = obj.e2_w; + % e2_e = obj.e2_e; + % e2_s = obj.e2_s; + % e2_n = obj.e2_n; + + % obj.tau1_w = (spdiag(1./s_w)*refObj.tau1_w')'; + % obj.tau1_e = (spdiag(1./s_e)*refObj.tau1_e')'; + % obj.tau1_s = (spdiag(1./s_s)*refObj.tau1_s')'; + % obj.tau1_n = (spdiag(1./s_n)*refObj.tau1_n')'; + + % obj.tau2_w = (spdiag(1./s_w)*refObj.tau2_w')'; + % obj.tau2_e = (spdiag(1./s_e)*refObj.tau2_e')'; + % obj.tau2_s = (spdiag(1./s_s)*refObj.tau2_s')'; + % obj.tau2_n = (spdiag(1./s_n)*refObj.tau2_n')'; + + % obj.tau_w = (refObj.e_w'*obj.e1_w*obj.tau1_w')' + (refObj.e_w'*obj.e2_w*obj.tau2_w')'; + % obj.tau_e = (refObj.e_e'*obj.e1_e*obj.tau1_e')' + (refObj.e_e'*obj.e2_e*obj.tau2_e')'; + % obj.tau_s = (refObj.e_s'*obj.e1_s*obj.tau1_s')' + (refObj.e_s'*obj.e2_s*obj.tau2_s')'; + % obj.tau_n = (refObj.e_n'*obj.e1_n*obj.tau1_n')' + (refObj.e_n'*obj.e2_n*obj.tau2_n')'; + + % % Physical normals + % e_w = obj.e_scalar_w; + % e_e = obj.e_scalar_e; + % e_s = obj.e_scalar_s; + % e_n = obj.e_scalar_n; + + % e_w_vec = obj.e_w; + % e_e_vec = obj.e_e; + % e_s_vec = obj.e_s; + % e_n_vec = obj.e_n; + + % nu_w = [-1,0]; + % nu_e = [1,0]; + % nu_s = [0,-1]; + % nu_n = [0,1]; + + % obj.n_w = cell(2,1); + % obj.n_e = cell(2,1); + % obj.n_s = cell(2,1); + % obj.n_n = cell(2,1); + + % n_w_1 = (1./s_w).*e_w'*(J.*(K{1,1}*nu_w(1) + K{1,2}*nu_w(2))); + % n_w_2 = (1./s_w).*e_w'*(J.*(K{2,1}*nu_w(1) + K{2,2}*nu_w(2))); + % obj.n_w{1} = spdiag(n_w_1); + % obj.n_w{2} = spdiag(n_w_2); + + % n_e_1 = (1./s_e).*e_e'*(J.*(K{1,1}*nu_e(1) + K{1,2}*nu_e(2))); + % n_e_2 = (1./s_e).*e_e'*(J.*(K{2,1}*nu_e(1) + K{2,2}*nu_e(2))); + % obj.n_e{1} = spdiag(n_e_1); + % obj.n_e{2} = spdiag(n_e_2); + + % n_s_1 = (1./s_s).*e_s'*(J.*(K{1,1}*nu_s(1) + K{1,2}*nu_s(2))); + % n_s_2 = (1./s_s).*e_s'*(J.*(K{2,1}*nu_s(1) + K{2,2}*nu_s(2))); + % obj.n_s{1} = spdiag(n_s_1); + % obj.n_s{2} = spdiag(n_s_2); + + % n_n_1 = (1./s_n).*e_n'*(J.*(K{1,1}*nu_n(1) + K{1,2}*nu_n(2))); + % n_n_2 = (1./s_n).*e_n'*(J.*(K{2,1}*nu_n(1) + K{2,2}*nu_n(2))); + % obj.n_n{1} = spdiag(n_n_1); + % obj.n_n{2} = spdiag(n_n_2); + + % % Operators that extract the normal component + % obj.en_w = (obj.n_w{1}*obj.e1_w' + obj.n_w{2}*obj.e2_w')'; + % obj.en_e = (obj.n_e{1}*obj.e1_e' + obj.n_e{2}*obj.e2_e')'; + % obj.en_s = (obj.n_s{1}*obj.e1_s' + obj.n_s{2}*obj.e2_s')'; + % obj.en_n = (obj.n_n{1}*obj.e1_n' + obj.n_n{2}*obj.e2_n')'; + + % Misc. + obj.refObj = refObj; + obj.m = refObj.m; + obj.h = refObj.h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + obj.nGrids = nGrids; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + + [closure, penalty] = obj.refObj.boundary_condition(boundary, bc, tuning); + + type = bc{2}; + + switch type + case {'F','f','Free','free','traction','Traction','t','T'} + s = obj.(['s_' boundary]); + s = spdiag(cell2mat(s)); + penalty = penalty*s; + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + [closure, penalty] = obj.refObj.interface(boundary,neighbour_scheme.refObj,neighbour_boundary,type); + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.refObj.h11{1}; + case {'s', 'n'} + h11 = obj.refObj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + % n is a cell of diagonal matrices for each normal component, n{1} = n_1, n{2} = n_2. + function n = getNormal(obj, boundary) + error('Not implemented'); + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + n = obj.(['n_' boundary]); + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2', 'en'}) + + o = obj.([op, '_', boundary]); + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e_u', 'e_s'}) + + switch op + case 'e_u' + o = obj.(['e_', boundary, '_u']); + case 'e_s' + o = obj.(['e_', boundary, '_s']); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = length(obj.D); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dVariable.m --- a/+scheme/Elastic2dVariable.m Thu Feb 17 18:55:11 2022 +0100 +++ b/+scheme/Elastic2dVariable.m Thu Mar 10 16:54:26 2022 +0100 @@ -14,10 +14,10 @@ order % Order of accuracy for the approximation - % Diagonal matrices for varible coefficients - LAMBDA % Variable coefficient, related to dilation - MU % Shear modulus, variable coefficient - RHO, RHOi % Density, variable + % Diagonal matrices for variable coefficients + LAMBDA % Lame's first parameter, related to dilation + MU % Shear modulus + RHO, RHOi, RHOi_kron % Density D % Total operator D1 % First derivatives @@ -26,22 +26,28 @@ D2_lambda D2_mu - % Traction operators used for BC - T_l, T_r - tau_l, tau_r + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n - H, Hi, H_1D % Inner products - e_l, e_r + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + % Inner products + H, Hi, Hi_kron, H_1D - d1_l, d1_r % Normal derivatives at the boundary - E % E{i}^T picks out component i + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n - H_boundary % Boundary inner products + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field - % Kroneckered norms and coefficients - RHOi_kron - Hi_kron + % E{i}^T picks out component i + E % Borrowing constants of the form gamma*h, where gamma is a dimensionless constant. theta_R % Borrowing (d1- D1)^2 from R @@ -55,11 +61,13 @@ methods % The coefficients can either be function handles or grid functions - function obj = Elastic2dVariable(g ,order, lambda, mu, rho, opSet) + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dVariable(g ,order, lambda, mu, rho, opSet, optFlag) default_arg('opSet',{@sbp.D2Variable, @sbp.D2Variable}); default_arg('lambda', @(x,y) 0*x+1); default_arg('mu', @(x,y) 0*x+1); default_arg('rho', @(x,y) 0*x+1); + default_arg('optFlag', false); dim = 2; assert(isa(g, 'grid.Cartesian')) @@ -105,10 +113,10 @@ D2 = cell(dim,1); H = cell(dim,1); Hi = cell(dim,1); - e_l = cell(dim,1); - e_r = cell(dim,1); - d1_l = cell(dim,1); - d1_r = cell(dim,1); + e_0 = cell(dim,1); + e_m = cell(dim,1); + d1_0 = cell(dim,1); + d1_m = cell(dim,1); for i = 1:dim I{i} = speye(m(i)); @@ -116,10 +124,10 @@ D2{i} = ops{i}.D2; H{i} = ops{i}.H; Hi{i} = ops{i}.HI; - e_l{i} = ops{i}.e_l; - e_r{i} = ops{i}.e_r; - d1_l{i} = ops{i}.d1_l; - d1_r{i} = ops{i}.d1_r; + e_0{i} = ops{i}.e_l; + e_m{i} = ops{i}.e_r; + d1_0{i} = ops{i}.d1_l; + d1_m{i} = ops{i}.d1_r; end %====== Assemble full operators ======== @@ -134,30 +142,64 @@ obj.D1 = cell(dim,1); obj.D2_lambda = cell(dim,1); obj.D2_mu = cell(dim,1); - obj.e_l = cell(dim,1); - obj.e_r = cell(dim,1); - obj.d1_l = cell(dim,1); - obj.d1_r = cell(dim,1); % D1 obj.D1{1} = kron(D1{1},I{2}); obj.D1{2} = kron(I{1},D1{2}); - % Boundary operators - obj.e_l{1} = kron(e_l{1},I{2}); - obj.e_l{2} = kron(I{1},e_l{2}); - obj.e_r{1} = kron(e_r{1},I{2}); - obj.e_r{2} = kron(I{1},e_r{2}); + % Boundary restriction operators + e_l = cell(dim,1); + e_r = cell(dim,1); + e_l{1} = kron(e_0{1}, I{2}); + e_l{2} = kron(I{1}, e_0{2}); + e_r{1} = kron(e_m{1}, I{2}); + e_r{2} = kron(I{1}, e_m{2}); + + e_scalar_w = e_l{1}; + e_scalar_e = e_r{1}; + e_scalar_s = e_l{2}; + e_scalar_n = e_r{2}; + + I_dim = speye(dim, dim); + e_w = kron(e_scalar_w, I_dim); + e_e = kron(e_scalar_e, I_dim); + e_s = kron(e_scalar_s, I_dim); + e_n = kron(e_scalar_n, I_dim); - obj.d1_l{1} = kron(d1_l{1},I{2}); - obj.d1_l{2} = kron(I{1},d1_l{2}); - obj.d1_r{1} = kron(d1_r{1},I{2}); - obj.d1_r{2} = kron(I{1},d1_r{2}); + % Boundary derivatives + d1_l = cell(dim,1); + d1_r = cell(dim,1); + d1_l{1} = kron(d1_0{1}, I{2}); + d1_l{2} = kron(I{1}, d1_0{2}); + d1_r{1} = kron(d1_m{1}, I{2}); + d1_r{2} = kron(I{1}, d1_m{2}); + + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + obj.E = E; + + e1_w = (e_scalar_w'*E{1}')'; + e1_e = (e_scalar_e'*E{1}')'; + e1_s = (e_scalar_s'*E{1}')'; + e1_n = (e_scalar_n'*E{1}')'; + + e2_w = (e_scalar_w'*E{2}')'; + e2_e = (e_scalar_e'*E{2}')'; + e2_s = (e_scalar_s'*E{2}')'; + e2_n = (e_scalar_n'*E{2}')'; + % D2 for i = 1:dim - obj.D2_lambda{i} = sparse(m_tot); - obj.D2_mu{i} = sparse(m_tot); + obj.D2_lambda{i} = sparse(m_tot, m_tot); + obj.D2_mu{i} = sparse(m_tot, m_tot); end ind = grid.funcToMatrix(g, 1:m_tot); @@ -182,21 +224,12 @@ % Quadratures obj.H = kron(H{1},H{2}); obj.Hi = inv(obj.H); - obj.H_boundary = cell(dim,1); - obj.H_boundary{1} = H{2}; - obj.H_boundary{2} = H{1}; + obj.H_w = H{2}; + obj.H_e = H{2}; + obj.H_s = H{1}; + obj.H_n = H{1}; obj.H_1D = {H{1}, H{2}}; - % E{i}^T picks out component i. - E = cell(dim,1); - I = speye(m_tot,m_tot); - for i = 1:dim - e = sparse(dim,1); - e(i) = 1; - E{i} = kron(I,e); - end - obj.E = E; - % Differentiation matrix D (without SAT) D2_lambda = obj.D2_lambda; D2_mu = obj.D2_mu; @@ -221,16 +254,14 @@ % Numerical traction operators for BC. % Because d1 =/= e0^T*D1, the numerical tractions are different % at every boundary. + % + % Formula at boundary j: % tau^{j}_i = sum_k T^{j}_{ik} u_k + % T_l = cell(dim,1); T_r = cell(dim,1); tau_l = cell(dim,1); tau_r = cell(dim,1); - % tau^{j}_i = sum_k T^{j}_{ik} u_k - d1_l = obj.d1_l; - d1_r = obj.d1_r; - e_l = obj.e_l; - e_r = obj.e_r; D1 = obj.D1; % Loop over boundaries @@ -250,45 +281,72 @@ % Loop over components for i = 1:dim - tau_l{j}{i} = sparse(n_l, dim*m_tot); - tau_r{j}{i} = sparse(n_r, dim*m_tot); + tau_l{j}{i} = sparse(dim*m_tot, n_l); + tau_r{j}{i} = sparse(dim*m_tot, n_r); for k = 1:dim T_l{j}{i,k} = ... - -d(i,j)*LAMBDA_l*(d(i,k)*d1_l{j}' + db(i,k)*e_l{j}'*D1{k})... - -d(j,k)*MU_l*(d(i,j)*d1_l{j}' + db(i,j)*e_l{j}'*D1{i})... - -d(i,k)*MU_l*d1_l{j}'; + (-d(i,j)*LAMBDA_l*(d(i,k)*d1_l{j}' + db(i,k)*e_l{j}'*D1{k})... + -d(j,k)*MU_l*(d(i,j)*d1_l{j}' + db(i,j)*e_l{j}'*D1{i})... + -d(i,k)*MU_l*d1_l{j}')'; T_r{j}{i,k} = ... - d(i,j)*LAMBDA_r*(d(i,k)*d1_r{j}' + db(i,k)*e_r{j}'*D1{k})... + (d(i,j)*LAMBDA_r*(d(i,k)*d1_r{j}' + db(i,k)*e_r{j}'*D1{k})... +d(j,k)*MU_r*(d(i,j)*d1_r{j}' + db(i,j)*e_r{j}'*D1{i})... - +d(i,k)*MU_r*d1_r{j}'; + +d(i,k)*MU_r*d1_r{j}')'; - tau_l{j}{i} = tau_l{j}{i} + T_l{j}{i,k}*E{k}'; - tau_r{j}{i} = tau_r{j}{i} + T_r{j}{i,k}*E{k}'; + tau_l{j}{i} = tau_l{j}{i} + (T_l{j}{i,k}'*E{k}')'; + tau_r{j}{i} = tau_r{j}{i} + (T_r{j}{i,k}'*E{k}')'; end end end - % Transpose T and tau to match boundary operator convention - for i = 1:dim - for j = 1:dim - tau_l{i}{j} = transpose(tau_l{i}{j}); - tau_r{i}{j} = transpose(tau_r{i}{j}); - for k = 1:dim - T_l{i}{j,k} = transpose(T_l{i}{j,k}); - T_r{i}{j,k} = transpose(T_r{i}{j,k}); - end - end - end + % Traction tensors, T_ij + obj.T_w = T_l{1}; + obj.T_e = T_r{1}; + obj.T_s = T_l{2}; + obj.T_n = T_r{2}; + + % Restriction operators + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + obj.e1_w = e1_w; + obj.e1_e = e1_e; + obj.e1_s = e1_s; + obj.e1_n = e1_n; + + obj.e2_w = e2_w; + obj.e2_e = e2_e; + obj.e2_s = e2_s; + obj.e2_n = e2_n; - obj.T_l = T_l; - obj.T_r = T_r; - obj.tau_l = tau_l; - obj.tau_r = tau_r; + obj.e_scalar_w = e_scalar_w; + obj.e_scalar_e = e_scalar_e; + obj.e_scalar_s = e_scalar_s; + obj.e_scalar_n = e_scalar_n; + + % First component of traction + obj.tau1_w = tau_l{1}{1}; + obj.tau1_e = tau_r{1}{1}; + obj.tau1_s = tau_l{2}{1}; + obj.tau1_n = tau_r{2}{1}; + + % Second component of traction + obj.tau2_w = tau_l{1}{2}; + obj.tau2_e = tau_r{1}{2}; + obj.tau2_s = tau_l{2}{2}; + obj.tau2_n = tau_r{2}{2}; + + % Traction vectors + obj.tau_w = (e_w'*e1_w*obj.tau1_w')' + (e_w'*e2_w*obj.tau2_w')'; + obj.tau_e = (e_e'*e1_e*obj.tau1_e')' + (e_e'*e2_e*obj.tau2_e')'; + obj.tau_s = (e_s'*e1_s*obj.tau1_s')' + (e_s'*e2_s*obj.tau2_s')'; + obj.tau_n = (e_n'*e1_n*obj.tau1_n')' + (e_n'*e2_n*obj.tau2_n')'; % Kroneckered norms and coefficients - I_dim = speye(dim); obj.RHOi_kron = kron(obj.RHOi, I_dim); obj.Hi_kron = kron(obj.Hi, I_dim); @@ -300,42 +358,46 @@ obj.dim = dim; % B, used for adjoint optimization - B = cell(dim, 1); - for i = 1:dim - B{i} = cell(m_tot, 1); - end + B = []; + if optFlag + B = cell(dim, 1); + for i = 1:dim + B{i} = cell(m_tot, 1); + end + + B0 = sparse(m_tot, m_tot); + for i = 1:dim + for j = 1:m_tot + B{i}{j} = B0; + end + end + + ind = grid.funcToMatrix(g, 1:m_tot); - for i = 1:dim - for j = 1:m_tot - B{i}{j} = sparse(m_tot, m_tot); + % Direction 1 + for k = 1:m(1) + c = sparse(m(1),1); + c(k) = 1; + [~, B_1D] = ops{1}.D2(c); + for l = 1:m(2) + p = ind(:,l); + B{1}{(k-1)*m(2) + l}(p, p) = B_1D; + end + end + + % Direction 2 + for k = 1:m(2) + c = sparse(m(2),1); + c(k) = 1; + [~, B_1D] = ops{2}.D2(c); + for l = 1:m(1) + p = ind(l,:); + B{2}{(l-1)*m(2) + k}(p, p) = B_1D; + end end end - - ind = grid.funcToMatrix(g, 1:m_tot); + obj.B = B; - % Direction 1 - for k = 1:m(1) - c = sparse(m(1),1); - c(k) = 1; - [~, B_1D] = ops{1}.D2(c); - for l = 1:m(2) - p = ind(:,l); - B{1}{(k-1)*m(2) + l}(p, p) = B_1D; - end - end - - % Direction 2 - for k = 1:m(2) - c = sparse(m(2),1); - c(k) = 1; - [~, B_1D] = ops{2}.D2(c); - for l = 1:m(1) - p = ind(l,:); - B{2}{(l-1)*m(2) + k}(p, p) = B_1D; - end - end - - obj.B = B; end @@ -344,7 +406,9 @@ % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition - % on the first component. + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. % data is a function returning the data that should be applied at the boundary. % neighbour_scheme is an instance of Scheme that should be interfaced to. % neighbour_boundary is a string specifying which boundary to interface to. @@ -354,11 +418,15 @@ assert( iscell(bc), 'The BC type must be a 2x1 cell array' ); comp = bc{1}; type = bc{2}; + if ischar(comp) + comp = obj.getComponent(comp, boundary); + end - % j is the coordinate direction of the boundary - j = obj.get_boundary_number(boundary); - [e, T, tau, H_gamma] = obj.getBoundaryOperator({'e','T','tau','H'}, boundary); - + e = obj.getBoundaryOperatorForScalarField('e', boundary); + tau = obj.getBoundaryOperator(['tau' num2str(comp)], boundary); + T = obj.getBoundaryTractionOperator(boundary); + alpha = obj.getBoundaryOperatorForScalarField('alpha', boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); E = obj.E; Hi = obj.Hi; @@ -370,8 +438,9 @@ m_tot = obj.grid.N(); % Preallocate + [~, col] = size(tau); closure = sparse(dim*m_tot, dim*m_tot); - penalty = sparse(dim*m_tot, m_tot/obj.m(j)); + penalty = sparse(dim*m_tot, col); k = comp; switch type @@ -379,8 +448,6 @@ % Dirichlet boundary condition case {'D','d','dirichlet','Dirichlet'} - alpha = obj.getBoundaryOperator('alpha', boundary); - % Loop over components that Dirichlet penalties end up on for i = 1:dim C = transpose(T{k,i}); @@ -392,7 +459,7 @@ % Free boundary condition case {'F','f','Free','free','traction','Traction','t','T'} - closure = closure - E{k}*RHOi*Hi*e*H_gamma*tau{k}'; + closure = closure - E{k}*RHOi*Hi*e*H_gamma*tau'; penalty = penalty + E{k}*RHOi*Hi*e*H_gamma; % Unknown boundary condition @@ -429,11 +496,11 @@ % Operators without subscripts are from the own domain. % Get boundary operators - e = obj.getBoundaryOperator('e_tot', boundary); - tau = obj.getBoundaryOperator('tau_tot', boundary); + e = obj.getBoundaryOperator('e', boundary); + tau = obj.getBoundaryOperator('tau', boundary); - e_v = neighbour_scheme.getBoundaryOperator('e_tot', neighbour_boundary); - tau_v = neighbour_scheme.getBoundaryOperator('tau_tot', neighbour_boundary); + e_v = neighbour_scheme.getBoundaryOperator('e', neighbour_boundary); + tau_v = neighbour_scheme.getBoundaryOperator('tau', neighbour_boundary); H_gamma = obj.getBoundaryQuadrature(boundary); @@ -442,8 +509,8 @@ RHOi = obj.RHOi_kron; % Penalty strength operators - alpha_u = 1/4*tuning*obj.getBoundaryOperator('alpha_tot', boundary); - alpha_v = 1/4*tuning*neighbour_scheme.getBoundaryOperator('alpha_tot', neighbour_boundary); + alpha_u = 1/4*tuning*obj.getBoundaryOperator('alpha', boundary); + alpha_v = 1/4*tuning*neighbour_scheme.getBoundaryOperator('alpha', neighbour_boundary); closure = -RHOi*Hi*e*H_gamma*(alpha_u' + alpha_v'*e_v*e'); penalty = RHOi*Hi*e*H_gamma*(alpha_u'*e*e_v' + alpha_v'); @@ -460,103 +527,108 @@ error('Non-conforming interfaces not implemented yet.'); end - % Returns the coordinate number and outward normal component for the boundary specified by the string boundary. - function [j, nj] = get_boundary_number(obj, boundary) - assertIsMember(boundary, {'w', 'e', 's', 'n'}) + % Returns the component number that is the tangential/normal component + % at the specified boundary + function comp = getComponent(obj, comp_str, boundary) + assertIsMember(comp_str, {'normal', 'tangential'}); + assertIsMember(boundary, {'w', 'e', 's', 'n'}); switch boundary - case {'w', 'e'} - j = 1; - case {'s', 'n'} - j = 2; - end - - switch boundary - case {'w', 's'} - nj = -1; - case {'e', 'n'} - nj = 1; + case {'w', 'e'} + switch comp_str + case 'normal' + comp = 1; + case 'tangential' + comp = 2; + end + case {'s', 'n'} + switch comp_str + case 'normal' + comp = 2; + case 'tangential' + comp = 1; + end end end % Returns the boundary operator op for the boundary specified by the string boundary. % op -- string - % Only operators with name *_tot can be used with multiblock.DiffOp.getBoundaryOperator() - function [varargout] = getBoundaryOperator(obj, op, boundary) + function o = getBoundaryOperator(obj, op, boundary) assertIsMember(boundary, {'w', 'e', 's', 'n'}) - assertIsMember(op, {'e', 'e_tot', 'd', 'T', 'tau', 'tau_tot', 'H', 'alpha', 'alpha_tot'}) - - switch boundary - case {'w', 'e'} - j = 1; - case {'s', 'n'} - j = 2; - end + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2', 'alpha', 'alpha1', 'alpha2'}) switch op - case 'e' - switch boundary - case {'w', 's'} - o = obj.e_l{j}; - case {'e', 'n'} - o = obj.e_r{j}; - end + + case {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'} + o = obj.([op, '_', boundary]); - case 'e_tot' - e = obj.getBoundaryOperator('e', boundary); - I_dim = speye(obj.dim, obj.dim); - o = kron(e, I_dim); + % Yields vector-valued penalty strength given displacement BC on all components + case 'alpha' + e = obj.getBoundaryOperator('e', boundary); + e_scalar = obj.getBoundaryOperatorForScalarField('e', boundary); + alpha_scalar = obj.getBoundaryOperatorForScalarField('alpha', boundary); + E = obj.E; + [m, n] = size(alpha_scalar{1,1}); + alpha = sparse(m*obj.dim, n*obj.dim); + for i = 1:obj.dim + for l = 1:obj.dim + alpha = alpha + (e'*E{i}*e_scalar*alpha_scalar{i,l}'*E{l}')'; + end + end + o = alpha; - case 'd' - switch boundary - case {'w', 's'} - o = obj.d1_l{j}; - case {'e', 'n'} - o = obj.d1_r{j}; - end + % Yields penalty strength for component 1 given displacement BC on all components + case 'alpha1' + alpha = obj.getBoundaryOperator('alpha', boundary); + e = obj.getBoundaryOperator('e', boundary); + e1 = obj.getBoundaryOperator('e1', boundary); - case 'T' - switch boundary - case {'w', 's'} - o = obj.T_l{j}; - case {'e', 'n'} - o = obj.T_r{j}; - end + alpha1 = (e1'*e*alpha')'; + o = alpha1; + + % Yields penalty strength for component 2 given displacement BC on all components + case 'alpha2' + alpha = obj.getBoundaryOperator('alpha', boundary); + e = obj.getBoundaryOperator('e', boundary); + e2 = obj.getBoundaryOperator('e2', boundary); + + alpha2 = (e2'*e*alpha')'; + o = alpha2; + end - case 'tau' - switch boundary - case {'w', 's'} - o = obj.tau_l{j}; - case {'e', 'n'} - o = obj.tau_r{j}; - end - - case 'tau_tot' - [e, tau] = obj.getBoundaryOperator({'e', 'tau'}, boundary); + end - I_dim = speye(obj.dim, obj.dim); - e_tot = kron(e, I_dim); - E = obj.E; - tau_tot = (e_tot'*E{1}*e*tau{1}')'; - for i = 2:obj.dim - tau_tot = tau_tot + (e_tot'*E{i}*e*tau{i}')'; - end - o = tau_tot; + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'alpha'}) - case 'H' - o = obj.H_boundary{j}; + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); case 'alpha' - % alpha = alpha(i,j) is the penalty strength for displacement BC. - e = obj.getBoundaryOperator('e', boundary); + + % alpha{i,j} is the penalty strength on component i due to + % displacement BC for component j. + e = obj.getBoundaryOperatorForScalarField('e', boundary); LAMBDA = obj.LAMBDA; MU = obj.MU; + dim = obj.dim; - dim = obj.dim; - theta_R = obj.theta_R{j}; - theta_H = obj.theta_H{j}; - theta_M = obj.theta_M{j}; + switch boundary + case {'w', 'e'} + k = 1; + case {'s', 'n'} + k = 2; + end + + theta_R = obj.theta_R{k}; + theta_H = obj.theta_H{k}; + theta_M = obj.theta_M{k}; a_lambda = dim/theta_H + 1/theta_R; a_mu_i = 2/theta_M; @@ -564,53 +636,53 @@ d = @kroneckerDelta; % Kronecker delta db = @(i,j) 1-d(i,j); % Logical not of Kronecker delta - alpha = cell(obj.dim, obj.dim); alpha_func = @(i,j) d(i,j)* a_lambda*LAMBDA ... + d(i,j)* a_mu_i*MU ... + db(i,j)*a_mu_ij*MU; + + alpha = cell(obj.dim, obj.dim); for i = 1:obj.dim - for l = 1:obj.dim - alpha{i,l} = d(i,l)*alpha_func(i,j)*e; + for j = 1:obj.dim + alpha{i,j} = d(i,j)*alpha_func(i,k)*e; end end - o = alpha; - - case 'alpha_tot' - % alpha = alpha(i,j) is the penalty strength for displacement BC. - [e, e_tot, alpha] = obj.getBoundaryOperator({'e', 'e_tot', 'alpha'}, boundary); - E = obj.E; - [m, n] = size(alpha{1,1}); - alpha_tot = sparse(m*obj.dim, n*obj.dim); - for i = 1:obj.dim - for l = 1:obj.dim - alpha_tot = alpha_tot + (e_tot'*E{i}*e*alpha{i,l}'*E{l}')'; - end - end - o = alpha_tot; end end + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + % Returns square boundary quadrature matrix, of dimension - % corresponding to the number of boundary points + % corresponding to the number of boundary unknowns % % boundary -- string function H = getBoundaryQuadrature(obj, boundary) assertIsMember(boundary, {'w', 'e', 's', 'n'}) - switch boundary - case {'w','e'} - j = 1; - case {'s','n'} - j = 2; - end - H = obj.H_boundary{j}; + H = obj.getBoundaryQuadratureForScalarField(boundary); I_dim = speye(obj.dim, obj.dim); H = kron(H, I_dim); end + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + function N = size(obj) N = obj.dim*prod(obj.m); end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dVariableAnisotropic.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dVariableAnisotropic.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,774 @@ +classdef Elastic2dVariableAnisotropic < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% opSet should be cell array of opSets, one per dimension. This +% is useful if we have periodic BC in one direction. +% Assumes fully compatible operators + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + RHO, RHOi, RHOi_kron % Density + C % Elastic stiffness tensor + + D % Total operator + D1 % First derivatives + % D2 % Second derivatives + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + + % Inner products + H, Hi, Hi_kron, H_1D + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + + % E{i}^T picks out component i + E + + % Borrowing constants of the form gamma*h, where gamma is a dimensionless constant. + h11 % First entry in norm matrix + + end + + methods + + % The coefficients can either be function handles or grid functions + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dVariableAnisotropic(g, order, rho, C, opSet, optFlag, hollow) + default_arg('hollow', false); + default_arg('rho', @(x,y) 0*x+1); + default_arg('opSet',{@sbp.D2VariableCompatible, @sbp.D2VariableCompatible}); + default_arg('optFlag', false); + dim = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x + 1; + end + end + end + end + default_arg('C', C_default); + assert(isa(g, 'grid.Cartesian')) + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + C_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C{i,j,k,l}, 'function_handle') + C{i,j,k,l} = grid.evalOn(g, C{i,j,k,l}); + end + C_mat{i,j,k,l} = spdiag(C{i,j,k,l}); + end + end + end + end + obj.C = C_mat; + + m = g.size(); + m_tot = g.N(); + lim = g.lim; + if isempty(lim) + x = g.x; + lim = cell(length(x),1); + for i = 1:length(x) + lim{i} = {min(x{i}), max(x{i})}; + end + end + + % 1D operators + ops = cell(dim,1); + opsHollow = cell(dim,1); + h = zeros(dim,1); + for i = 1:dim + ops{i} = opSet{i}(m(i), lim{i}, order); + h(i) = ops{i}.h; + if hollow + opsHollow{i} = sbp.D2VariableCompatibleHollow(m(i), lim{i}, order); + end + end + + % Borrowing constants + for i = 1:dim + obj.h11{i} = h(i)*ops{i}.borrowing.H11; + end + + I = cell(dim,1); + D1 = cell(dim,1); + D2 = cell(dim,1); + D2Hollow = cell(dim,1); + H = cell(dim,1); + Hi = cell(dim,1); + e_0 = cell(dim,1); + e_m = cell(dim,1); + d1_0 = cell(dim,1); + d1_m = cell(dim,1); + + for i = 1:dim + I{i} = speye(m(i)); + D1{i} = ops{i}.D1; + if hollow + D2Hollow{i} = opsHollow{i}.D2; + end + D2{i} = ops{i}.D2; + H{i} = ops{i}.H; + Hi{i} = ops{i}.HI; + e_0{i} = ops{i}.e_l; + e_m{i} = ops{i}.e_r; + d1_0{i} = ops{i}.d1_l; + d1_m{i} = ops{i}.d1_r; + end + + %====== Assemble full operators ======== + I_dim = speye(dim, dim); + RHO = spdiag(rho); + obj.RHO = RHO; + obj.RHOi = inv(RHO); + obj.RHOi_kron = kron(obj.RHOi, I_dim); + + obj.D1 = cell(dim,1); + D2_temp = cell(dim,dim,dim); + + % D1 + obj.D1{1} = kron(D1{1},I{2}); + obj.D1{2} = kron(I{1},D1{2}); + + % Boundary restriction operators + e_l = cell(dim,1); + e_r = cell(dim,1); + e_l{1} = kron(e_0{1}, I{2}); + e_l{2} = kron(I{1}, e_0{2}); + e_r{1} = kron(e_m{1}, I{2}); + e_r{2} = kron(I{1}, e_m{2}); + + e_scalar_w = e_l{1}; + e_scalar_e = e_r{1}; + e_scalar_s = e_l{2}; + e_scalar_n = e_r{2}; + + e_w = kron(e_scalar_w, I_dim); + e_e = kron(e_scalar_e, I_dim); + e_s = kron(e_scalar_s, I_dim); + e_n = kron(e_scalar_n, I_dim); + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + obj.E = E; + + e1_w = (e_scalar_w'*E{1}')'; + e1_e = (e_scalar_e'*E{1}')'; + e1_s = (e_scalar_s'*E{1}')'; + e1_n = (e_scalar_n'*E{1}')'; + + e2_w = (e_scalar_w'*E{2}')'; + e2_e = (e_scalar_e'*E{2}')'; + e2_s = (e_scalar_s'*E{2}')'; + e2_n = (e_scalar_n'*E{2}')'; + + + % D2 + switch order + case 2 + width = 3; + nBP = 2; + case 4 + width = 5; + nBP = 6; + case 6 + width = 7; + nBP = 9; + end + for j = 1:dim + for k = 1:dim + for l = 1:dim + if hollow + D2_temp{j,k,l} = sparse(m_tot, m_tot); + else + D2_temp{j,k,l} = spalloc(m_tot, m_tot, width*m_tot); + end + end + end + end + ind = grid.funcToMatrix(g, 1:m_tot); + + k = 1; + if hollow + mask = sparse(m(1), m(1)); + mask(1:nBP, 1:nBP) = speye(nBP, nBP); + mask(end-nBP+1:end, end-nBP+1:end) = speye(nBP, nBP); + maskXSmall = kron(mask, speye(m(2), m(2))); + maskX = E{1}*maskXSmall*E{1}' + E{2}*maskXSmall*E{2}'; + end + for r = 1:m(2) + p = ind(:,r); + for j = 1:dim + for l = 1:dim + coeff = C{k,j,k,l}; + if hollow && r > nBP && r < m(2) - nBP + 1 + D_kk = D2Hollow{1}(coeff(p)); + else + D_kk = D2{1}(coeff(p)); + end + D2_temp{j,k,l}(p,p) = D_kk; + end + end + end + + k = 2; + if hollow + mask = sparse(m(2), m(2)); + mask(1:nBP, 1:nBP) = speye(nBP, nBP); + mask(end-nBP+1:end, end-nBP+1:end) = speye(nBP, nBP); + maskYSmall = kron(speye(m(1), m(1)), mask); + + maskY = E{1}*maskYSmall*E{1}' + E{2}*maskYSmall*E{2}'; + mask = maskX + maskY; + mask = mask>0; + + maskSmall = maskXSmall + maskYSmall; + maskSmall = maskSmall>0; + end + for r = 1:m(1) + p = ind(r,:); + for j = 1:dim + for l = 1:dim + coeff = C{k,j,k,l}; + if hollow && r > nBP && r < m(1) - nBP + 1 + D_kk = D2Hollow{2}(coeff(p)); + else + D_kk = D2{2}(coeff(p)); + end + D2_temp{j,k,l}(p,p) = D_kk; + end + end + end + + % Quadratures + obj.H = kron(H{1},H{2}); + obj.Hi = inv(obj.H); + obj.H_w = H{2}; + obj.H_e = H{2}; + obj.H_s = H{1}; + obj.H_n = H{1}; + obj.H_1D = {H{1}, H{2}}; + + % Differentiation matrix D (without SAT) + D1 = obj.D1; + D = sparse(dim*m_tot,dim*m_tot); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if i == k + D = D + E{j}*D2_temp{j,k,l}*E{l}'; + D2_temp{j,k,l} = []; + else + if hollow + D = D + E{j}*(maskSmall*D1{i})*C_mat{i,j,k,l}*D1{k}*E{l}'; + else + D = D + E{j}*(D1{i})*C_mat{i,j,k,l}*D1{k}*E{l}'; + end + end + end + end + end + end + clear D2_temp; + if hollow + mask = maskX + maskY; + mask = mask>0; + D = mask*D; + end + D = obj.RHOi_kron*D; + obj.D = D; + clear D; + %=========================================%' + + % Numerical traction operators for BC. + % + % Formula at boundary j: % tau^{j}_i = sum_l T^{j}_{il} u_l + % + T_l = cell(dim,1); + T_r = cell(dim,1); + tau_l = cell(dim,1); + tau_r = cell(dim,1); + + D1 = obj.D1; + + % Boundary j + for j = 1:dim + T_l{j} = cell(dim,dim); + T_r{j} = cell(dim,dim); + tau_l{j} = cell(dim,1); + tau_r{j} = cell(dim,1); + + [~, n_l] = size(e_l{j}); + [~, n_r] = size(e_r{j}); + + % Traction component i + for i = 1:dim + tau_l{j}{i} = sparse(dim*m_tot, n_l); + tau_r{j}{i} = sparse(dim*m_tot, n_r); + + % Displacement component l + for l = 1:dim + T_l{j}{i,l} = sparse(m_tot, n_l); + T_r{j}{i,l} = sparse(m_tot, n_r); + + % Derivative direction k + for k = 1:dim + T_l{j}{i,l} = T_l{j}{i,l} ... + - (e_l{j}'*C_mat{j,i,k,l}*D1{k})'; + T_r{j}{i,l} = T_r{j}{i,l} ... + + (e_r{j}'*C_mat{j,i,k,l}*D1{k})'; + end + tau_l{j}{i} = tau_l{j}{i} + (T_l{j}{i,l}'*E{l}')'; + tau_r{j}{i} = tau_r{j}{i} + (T_r{j}{i,l}'*E{l}')'; + end + end + end + + % Traction tensors, T_ij + obj.T_w = T_l{1}; + obj.T_e = T_r{1}; + obj.T_s = T_l{2}; + obj.T_n = T_r{2}; + + % Restriction operators + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + obj.e1_w = e1_w; + obj.e1_e = e1_e; + obj.e1_s = e1_s; + obj.e1_n = e1_n; + + obj.e2_w = e2_w; + obj.e2_e = e2_e; + obj.e2_s = e2_s; + obj.e2_n = e2_n; + + obj.e_scalar_w = e_scalar_w; + obj.e_scalar_e = e_scalar_e; + obj.e_scalar_s = e_scalar_s; + obj.e_scalar_n = e_scalar_n; + + % First component of traction + obj.tau1_w = tau_l{1}{1}; + obj.tau1_e = tau_r{1}{1}; + obj.tau1_s = tau_l{2}{1}; + obj.tau1_n = tau_r{2}{1}; + + % Second component of traction + obj.tau2_w = tau_l{1}{2}; + obj.tau2_e = tau_r{1}{2}; + obj.tau2_s = tau_l{2}{2}; + obj.tau2_n = tau_r{2}{2}; + + % Traction vectors + obj.tau_w = (e_w'*e1_w*obj.tau1_w')' + (e_w'*e2_w*obj.tau2_w')'; + obj.tau_e = (e_e'*e1_e*obj.tau1_e')' + (e_e'*e2_e*obj.tau2_e')'; + obj.tau_s = (e_s'*e1_s*obj.tau1_s')' + (e_s'*e2_s*obj.tau2_s')'; + obj.tau_n = (e_n'*e1_n*obj.tau1_n')' + (e_n'*e2_n*obj.tau2_n')'; + + % Kroneckered norms and coefficients + obj.Hi_kron = kron(obj.Hi, I_dim); + + % Misc. + obj.m = m; + obj.h = h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + comp = bc{1}; + type = bc{2}; + if ischar(comp) + comp = obj.getComponent(comp, boundary); + end + + e = obj.getBoundaryOperatorForScalarField('e', boundary); + tau = obj.getBoundaryOperator(['tau' num2str(comp)], boundary); + T = obj.getBoundaryTractionOperator(boundary); + h11 = obj.getBorrowing(boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); + nu = obj.getNormal(boundary); + + E = obj.E; + Hi = obj.Hi; + RHOi = obj.RHOi; + C = obj.C; + + dim = obj.dim; + m_tot = obj.grid.N(); + + % Preallocate + [~, col] = size(tau); + closure = sparse(dim*m_tot, dim*m_tot); + penalty = sparse(dim*m_tot, col); + + j = comp; + switch type + + % Dirichlet boundary condition + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + + if numel(bc) >= 3 + dComps = bc{3}; + else + dComps = 1:dim; + end + + % Loops over components that Dirichlet penalties end up on + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Nonsymmetric part goes on all components to + % yield traction in discrete energy rate + for i = 1:dim + Y = T{j,i}'; + X = e*Y; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Symmetric part only required on components with displacement BC. + % (Otherwise it's not symmetric.) + for i = dComps + Z = sparse(m_tot, m_tot); + for l = 1:dim + for k = 1:dim + Z = Z + nu(l)*C{l,i,k,j}*nu(k); + end + end + Z = -tuning*dim/h11*Z; + X = Z; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Free boundary condition + case {'F','f','Free','free','traction','Traction','t','T'} + closure = closure - E{j}*RHOi*Hi*e*H_gamma*tau'; + penalty = penalty + E{j}*RHOi*Hi*e*H_gamma; + + % Unknown boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + u = obj; + v = neighbour_scheme; + + % Operators, u side + e_u = u.getBoundaryOperatorForScalarField('e', boundary); + tau_u = u.getBoundaryOperator('tau', boundary); + h11_u = u.getBorrowing(boundary); + nu_u = u.getNormal(boundary); + + E_u = u.E; + C_u = u.C; + m_tot_u = u.grid.N(); + + % Operators, v side + e_v = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + tau_v = v.getBoundaryOperator('tau', neighbour_boundary); + h11_v = v.getBorrowing(neighbour_boundary); + nu_v = v.getNormal(neighbour_boundary); + + E_v = v.E; + C_v = v.C; + m_tot_v = v.grid.N(); + + % Fix {'e', 's'}, {'w', 'n'}, and {'x','x'} couplings + flipFlag = false; + e_v_flip = e_v; + if (strcmp(boundary,'s') && strcmp(neighbour_boundary,'e')) || ... + (strcmp(boundary,'e') && strcmp(neighbour_boundary,'s')) || ... + (strcmp(boundary,'w') && strcmp(neighbour_boundary,'n')) || ... + (strcmp(boundary,'n') && strcmp(neighbour_boundary,'w')) || ... + (strcmp(boundary,'s') && strcmp(neighbour_boundary,'s')) || ... + (strcmp(boundary,'n') && strcmp(neighbour_boundary,'n')) || ... + (strcmp(boundary,'w') && strcmp(neighbour_boundary,'w')) || ... + (strcmp(boundary,'e') && strcmp(neighbour_boundary,'e')) + + flipFlag = true; + e_v_flip = fliplr(e_v); + + t1 = tau_v(:,1:2:end-1); + t2 = tau_v(:,2:2:end); + + t1 = fliplr(t1); + t2 = fliplr(t2); + + tau_v(:,1:2:end-1) = t1; + tau_v(:,2:2:end) = t2; + end + + % Operators that are only required for own domain + Hi = u.Hi_kron; + RHOi = u.RHOi_kron; + e_kron = u.getBoundaryOperator('e', boundary); + T_u = u.getBoundaryTractionOperator(boundary); + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + H_gamma_kron = u.getBoundaryQuadrature(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot_u, dim*m_tot_u); + penalty = sparse(dim*m_tot_u, dim*m_tot_v); + + % ---- Continuity of displacement ------ + + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Loop over components to couple across interface + for j = 1:dim + + % Loop over components that penalties end up on + for i = 1:dim + Y = 1/2*T_u{j,i}'; + Z_u = sparse(m_int, m_int); + Z_v = sparse(m_int, m_int); + for l = 1:dim + for k = 1:dim + Z_u = Z_u + e_u'*nu_u(l)*C_u{l,i,k,j}*nu_u(k)*e_u; + Z_v = Z_v + e_v'*nu_v(l)*C_v{l,i,k,j}*nu_v(k)*e_v; + end + end + + if flipFlag + Z_v = rot90(Z_v,2); + end + + Z = -tuning*dim*( 1/(4*h11_u)*Z_u + 1/(4*h11_v)*Z_v ); + X = Y + Z*e_u'; + closure = closure + E_u{i}*X'*H_gamma*e_u'*E_u{j}'; + penalty = penalty - E_u{i}*X'*H_gamma*e_v_flip'*E_v{j}'; + + end + end + + % ---- Continuity of traction ------ + closure = closure - 1/2*e_kron*H_gamma_kron*tau_u'; + penalty = penalty - 1/2*e_kron*H_gamma_kron*tau_v'; + + % ---- Multiply by inverse of density x quadraure ---- + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + error('Non-conforming interfaces not implemented yet.'); + end + + % Returns the component number that is the tangential/normal component + % at the specified boundary + function comp = getComponent(obj, comp_str, boundary) + assertIsMember(comp_str, {'normal', 'tangential'}); + assertIsMember(boundary, {'w', 'e', 's', 'n'}); + + switch boundary + case {'w', 'e'} + switch comp_str + case 'normal' + comp = 1; + case 'tangential' + comp = 2; + end + case {'s', 'n'} + switch comp_str + case 'normal' + comp = 2; + case 'tangential' + comp = 1; + end + end + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.h11{1}; + case {'s', 'n'} + h11 = obj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + function nu = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case 'w' + nu = [-1,0]; + case 'e' + nu = [1,0]; + case 's' + nu = [0,-1]; + case 'n' + nu = [0,1]; + end + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'}) + + switch op + case {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'} + o = obj.([op, '_', boundary]); + end + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dVariableAnisotropicMixedStencil.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dVariableAnisotropicMixedStencil.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,735 @@ +classdef Elastic2dVariableAnisotropicMixedStencil < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% opSet should be cell array of opSets, one per dimension. This +% is useful if we have periodic BC in one direction. +% Assumes fully compatible operators + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + RHO, RHOi, RHOi_kron % Density + C, C_D1, C_D2 % Elastic stiffness tensor, C = C_D1 + C_D2. + + D % Total operator + D1 % First derivatives + % D2 % Second derivatives + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + + % Inner products + H, Hi, Hi_kron, H_1D + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + + % E{i}^T picks out component i + E + + % Borrowing constants of the form gamma*h, where gamma is a dimensionless constant. + h11 % First entry in norm matrix + + end + + methods + + % Uses D1*D1 for the C_D1 part of the stiffness tensor C + % Uses narrow D2 whenever possible for the C_D2 part of C + % The coefficients can either be function handles or grid functions + function obj = Elastic2dVariableAnisotropicMixedStencil(g, order, rho, C_D1, C_D2, opSet) + default_arg('rho', @(x,y) 0*x+1); + default_arg('opSet',{@sbp.D2VariableCompatible, @sbp.D2VariableCompatible}); + default_arg('optFlag', false); + dim = 2; + + C_D1_default = cell(dim,dim,dim,dim); + C_D2_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_D1_default{i,j,k,l} = @(x,y) 0*x + 0; + C_D2_default{i,j,k,l} = @(x,y) 0*x + 1; + end + end + end + end + default_arg('C_D1', C_D1_default); + default_arg('C_D2', C_D2_default); + assert(isa(g, 'grid.Cartesian')) + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + C_mat = cell(dim,dim,dim,dim); + C_D1_mat = cell(dim,dim,dim,dim); + C_D2_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C_D1{i,j,k,l}, 'function_handle') + C_D1{i,j,k,l} = grid.evalOn(g, C_D1{i,j,k,l}); + end + if isa(C_D2{i,j,k,l}, 'function_handle') + C_D2{i,j,k,l} = grid.evalOn(g, C_D2{i,j,k,l}); + end + C_D1_mat{i,j,k,l} = spdiag(C_D1{i,j,k,l}); + C_D2_mat{i,j,k,l} = spdiag(C_D2{i,j,k,l}); + C_mat{i,j,k,l} = C_D1_mat{i,j,k,l} + C_D2_mat{i,j,k,l}; + end + end + end + end + obj.C = C_mat; + obj.C_D1 = C_D1_mat; + obj.C_D2 = C_D2_mat; + + m = g.size(); + m_tot = g.N(); + lim = g.lim; + if isempty(lim) + x = g.x; + lim = cell(length(x),1); + for i = 1:length(x) + lim{i} = {min(x{i}), max(x{i})}; + end + end + + % 1D operators + ops = cell(dim,1); + h = zeros(dim,1); + for i = 1:dim + ops{i} = opSet{i}(m(i), lim{i}, order); + h(i) = ops{i}.h; + end + + % Borrowing constants + for i = 1:dim + obj.h11{i} = h(i)*ops{i}.borrowing.H11; + end + + I = cell(dim,1); + D1 = cell(dim,1); + D2 = cell(dim,1); + H = cell(dim,1); + Hi = cell(dim,1); + e_0 = cell(dim,1); + e_m = cell(dim,1); + d1_0 = cell(dim,1); + d1_m = cell(dim,1); + + for i = 1:dim + I{i} = speye(m(i)); + D1{i} = ops{i}.D1; + D2{i} = ops{i}.D2; + H{i} = ops{i}.H; + Hi{i} = ops{i}.HI; + e_0{i} = ops{i}.e_l; + e_m{i} = ops{i}.e_r; + d1_0{i} = ops{i}.d1_l; + d1_m{i} = ops{i}.d1_r; + end + + %====== Assemble full operators ======== + I_dim = speye(dim, dim); + RHO = spdiag(rho); + obj.RHO = RHO; + obj.RHOi = inv(RHO); + obj.RHOi_kron = kron(obj.RHOi, I_dim); + + obj.D1 = cell(dim,1); + D2_temp = cell(dim,dim,dim); + + % D1 + obj.D1{1} = kron(D1{1},I{2}); + obj.D1{2} = kron(I{1},D1{2}); + + % Boundary restriction operators + e_l = cell(dim,1); + e_r = cell(dim,1); + e_l{1} = kron(e_0{1}, I{2}); + e_l{2} = kron(I{1}, e_0{2}); + e_r{1} = kron(e_m{1}, I{2}); + e_r{2} = kron(I{1}, e_m{2}); + + e_scalar_w = e_l{1}; + e_scalar_e = e_r{1}; + e_scalar_s = e_l{2}; + e_scalar_n = e_r{2}; + + e_w = kron(e_scalar_w, I_dim); + e_e = kron(e_scalar_e, I_dim); + e_s = kron(e_scalar_s, I_dim); + e_n = kron(e_scalar_n, I_dim); + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + obj.E = E; + + e1_w = (e_scalar_w'*E{1}')'; + e1_e = (e_scalar_e'*E{1}')'; + e1_s = (e_scalar_s'*E{1}')'; + e1_n = (e_scalar_n'*E{1}')'; + + e2_w = (e_scalar_w'*E{2}')'; + e2_e = (e_scalar_e'*E{2}')'; + e2_s = (e_scalar_s'*E{2}')'; + e2_n = (e_scalar_n'*E{2}')'; + + + % D2 + switch order + case 2 + width = 3; + case 4 + width = 5; + case 6 + width = 7; + end + for j = 1:dim + for k = 1:dim + for l = 1:dim + D2_temp{j,k,l} = spalloc(m_tot, m_tot, width*m_tot); + end + end + end + ind = grid.funcToMatrix(g, 1:m_tot); + + k = 1; + for r = 1:m(2) + p = ind(:,r); + for j = 1:dim + for l = 1:dim + coeff = C_D2{k,j,k,l}; + D_kk = D2{1}(coeff(p)); + D2_temp{j,k,l}(p,p) = D_kk; + end + end + end + + k = 2; + for r = 1:m(1) + p = ind(r,:); + for j = 1:dim + for l = 1:dim + coeff = C_D2{k,j,k,l}; + D_kk = D2{2}(coeff(p)); + D2_temp{j,k,l}(p,p) = D_kk; + end + end + end + + % Quadratures + obj.H = kron(H{1},H{2}); + obj.Hi = inv(obj.H); + obj.H_w = H{2}; + obj.H_e = H{2}; + obj.H_s = H{1}; + obj.H_n = H{1}; + obj.H_1D = {H{1}, H{2}}; + + % Differentiation matrix D (without SAT) + D1 = obj.D1; + D = sparse(dim*m_tot,dim*m_tot); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if i == k + D = D + E{j}*D2_temp{j,k,l}*E{l}'; + D2_temp{j,k,l} = []; + else + D = D + E{j}*D1{i}*C_D2_mat{i,j,k,l}*D1{k}*E{l}'; + end + D = D + E{j}*D1{i}*C_D1_mat{i,j,k,l}*D1{k}*E{l}'; + end + end + end + end + clear D2_temp; + D = obj.RHOi_kron*D; + obj.D = D; + clear D; + %=========================================%' + + % Numerical traction operators for BC. + % + % Formula at boundary j: % tau^{j}_i = sum_l T^{j}_{il} u_l + % + T_l = cell(dim,1); + T_r = cell(dim,1); + tau_l = cell(dim,1); + tau_r = cell(dim,1); + + D1 = obj.D1; + + % Boundary j + for j = 1:dim + T_l{j} = cell(dim,dim); + T_r{j} = cell(dim,dim); + tau_l{j} = cell(dim,1); + tau_r{j} = cell(dim,1); + + [~, n_l] = size(e_l{j}); + [~, n_r] = size(e_r{j}); + + % Traction component i + for i = 1:dim + tau_l{j}{i} = sparse(dim*m_tot, n_l); + tau_r{j}{i} = sparse(dim*m_tot, n_r); + + % Displacement component l + for l = 1:dim + T_l{j}{i,l} = sparse(m_tot, n_l); + T_r{j}{i,l} = sparse(m_tot, n_r); + + % Derivative direction k + for k = 1:dim + T_l{j}{i,l} = T_l{j}{i,l} ... + - (e_l{j}'*C_mat{j,i,k,l}*D1{k})'; + T_r{j}{i,l} = T_r{j}{i,l} ... + + (e_r{j}'*C_mat{j,i,k,l}*D1{k})'; + end + tau_l{j}{i} = tau_l{j}{i} + (T_l{j}{i,l}'*E{l}')'; + tau_r{j}{i} = tau_r{j}{i} + (T_r{j}{i,l}'*E{l}')'; + end + end + end + + % Traction tensors, T_ij + obj.T_w = T_l{1}; + obj.T_e = T_r{1}; + obj.T_s = T_l{2}; + obj.T_n = T_r{2}; + + % Restriction operators + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + obj.e1_w = e1_w; + obj.e1_e = e1_e; + obj.e1_s = e1_s; + obj.e1_n = e1_n; + + obj.e2_w = e2_w; + obj.e2_e = e2_e; + obj.e2_s = e2_s; + obj.e2_n = e2_n; + + obj.e_scalar_w = e_scalar_w; + obj.e_scalar_e = e_scalar_e; + obj.e_scalar_s = e_scalar_s; + obj.e_scalar_n = e_scalar_n; + + % First component of traction + obj.tau1_w = tau_l{1}{1}; + obj.tau1_e = tau_r{1}{1}; + obj.tau1_s = tau_l{2}{1}; + obj.tau1_n = tau_r{2}{1}; + + % Second component of traction + obj.tau2_w = tau_l{1}{2}; + obj.tau2_e = tau_r{1}{2}; + obj.tau2_s = tau_l{2}{2}; + obj.tau2_n = tau_r{2}{2}; + + % Traction vectors + obj.tau_w = (e_w'*e1_w*obj.tau1_w')' + (e_w'*e2_w*obj.tau2_w')'; + obj.tau_e = (e_e'*e1_e*obj.tau1_e')' + (e_e'*e2_e*obj.tau2_e')'; + obj.tau_s = (e_s'*e1_s*obj.tau1_s')' + (e_s'*e2_s*obj.tau2_s')'; + obj.tau_n = (e_n'*e1_n*obj.tau1_n')' + (e_n'*e2_n*obj.tau2_n')'; + + % Kroneckered norms and coefficients + obj.Hi_kron = kron(obj.Hi, I_dim); + + % Misc. + obj.m = m; + obj.h = h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + comp = bc{1}; + type = bc{2}; + if ischar(comp) + comp = obj.getComponent(comp, boundary); + end + + e = obj.getBoundaryOperatorForScalarField('e', boundary); + tau = obj.getBoundaryOperator(['tau' num2str(comp)], boundary); + T = obj.getBoundaryTractionOperator(boundary); + h11 = obj.getBorrowing(boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); + nu = obj.getNormal(boundary); + + E = obj.E; + Hi = obj.Hi; + RHOi = obj.RHOi; + C = obj.C; + + dim = obj.dim; + m_tot = obj.grid.N(); + + % Preallocate + [~, col] = size(tau); + closure = sparse(dim*m_tot, dim*m_tot); + penalty = sparse(dim*m_tot, col); + + j = comp; + switch type + + % Dirichlet boundary condition + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + + if numel(bc) >= 3 + dComps = bc{3}; + else + dComps = 1:dim; + end + + % Loops over components that Dirichlet penalties end up on + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Nonsymmetric part goes on all components to + % yield traction in discrete energy rate + for i = 1:dim + Y = T{j,i}'; + X = e*Y; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Symmetric part only required on components with displacement BC. + % (Otherwise it's not symmetric.) + for i = dComps + Z = sparse(m_tot, m_tot); + for l = 1:dim + for k = 1:dim + Z = Z + nu(l)*C{l,i,k,j}*nu(k); + end + end + Z = -tuning*dim/h11*Z; + X = Z; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Free boundary condition + case {'F','f','Free','free','traction','Traction','t','T'} + closure = closure - E{j}*RHOi*Hi*e*H_gamma*tau'; + penalty = penalty + E{j}*RHOi*Hi*e*H_gamma; + + % Unknown boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + u = obj; + v = neighbour_scheme; + + % Operators, u side + e_u = u.getBoundaryOperatorForScalarField('e', boundary); + tau_u = u.getBoundaryOperator('tau', boundary); + h11_u = u.getBorrowing(boundary); + nu_u = u.getNormal(boundary); + + E_u = u.E; + C_u = u.C; + m_tot_u = u.grid.N(); + + % Operators, v side + e_v = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + tau_v = v.getBoundaryOperator('tau', neighbour_boundary); + h11_v = v.getBorrowing(neighbour_boundary); + nu_v = v.getNormal(neighbour_boundary); + + E_v = v.E; + C_v = v.C; + m_tot_v = v.grid.N(); + + % Fix {'e', 's'}, {'w', 'n'}, and {'x','x'} couplings + flipFlag = false; + e_v_flip = e_v; + if (strcmp(boundary,'s') && strcmp(neighbour_boundary,'e')) || ... + (strcmp(boundary,'e') && strcmp(neighbour_boundary,'s')) || ... + (strcmp(boundary,'w') && strcmp(neighbour_boundary,'n')) || ... + (strcmp(boundary,'n') && strcmp(neighbour_boundary,'w')) || ... + (strcmp(boundary,'s') && strcmp(neighbour_boundary,'s')) || ... + (strcmp(boundary,'n') && strcmp(neighbour_boundary,'n')) || ... + (strcmp(boundary,'w') && strcmp(neighbour_boundary,'w')) || ... + (strcmp(boundary,'e') && strcmp(neighbour_boundary,'e')) + + flipFlag = true; + e_v_flip = fliplr(e_v); + + t1 = tau_v(:,1:2:end-1); + t2 = tau_v(:,2:2:end); + + t1 = fliplr(t1); + t2 = fliplr(t2); + + tau_v(:,1:2:end-1) = t1; + tau_v(:,2:2:end) = t2; + end + + % Operators that are only required for own domain + Hi = u.Hi_kron; + RHOi = u.RHOi_kron; + e_kron = u.getBoundaryOperator('e', boundary); + T_u = u.getBoundaryTractionOperator(boundary); + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + H_gamma_kron = u.getBoundaryQuadrature(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot_u, dim*m_tot_u); + penalty = sparse(dim*m_tot_u, dim*m_tot_v); + + % ---- Continuity of displacement ------ + + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Loop over components to couple across interface + for j = 1:dim + + % Loop over components that penalties end up on + for i = 1:dim + Y = 1/2*T_u{j,i}'; + Z_u = sparse(m_int, m_int); + Z_v = sparse(m_int, m_int); + for l = 1:dim + for k = 1:dim + Z_u = Z_u + e_u'*nu_u(l)*C_u{l,i,k,j}*nu_u(k)*e_u; + Z_v = Z_v + e_v'*nu_v(l)*C_v{l,i,k,j}*nu_v(k)*e_v; + end + end + + if flipFlag + Z_v = rot90(Z_v,2); + end + + Z = -tuning*dim*( 1/(4*h11_u)*Z_u + 1/(4*h11_v)*Z_v ); + X = Y + Z*e_u'; + closure = closure + E_u{i}*X'*H_gamma*e_u'*E_u{j}'; + penalty = penalty - E_u{i}*X'*H_gamma*e_v_flip'*E_v{j}'; + + end + end + + % ---- Continuity of traction ------ + closure = closure - 1/2*e_kron*H_gamma_kron*tau_u'; + penalty = penalty - 1/2*e_kron*H_gamma_kron*tau_v'; + + % ---- Multiply by inverse of density x quadraure ---- + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + error('Non-conforming interfaces not implemented yet.'); + end + + % Returns the component number that is the tangential/normal component + % at the specified boundary + function comp = getComponent(obj, comp_str, boundary) + assertIsMember(comp_str, {'normal', 'tangential'}); + assertIsMember(boundary, {'w', 'e', 's', 'n'}); + + switch boundary + case {'w', 'e'} + switch comp_str + case 'normal' + comp = 1; + case 'tangential' + comp = 2; + end + case {'s', 'n'} + switch comp_str + case 'normal' + comp = 2; + case 'tangential' + comp = 1; + end + end + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.h11{1}; + case {'s', 'n'} + h11 = obj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + function nu = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case 'w' + nu = [-1,0]; + case 'e' + nu = [1,0]; + case 's' + nu = [0,-1]; + case 'n' + nu = [0,1]; + end + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'}) + + switch op + case {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'} + o = obj.([op, '_', boundary]); + end + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Elastic2dVariableAnisotropicUpwind.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Elastic2dVariableAnisotropicUpwind.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,651 @@ +classdef Elastic2dVariableAnisotropicUpwind < scheme.Scheme + +% Discretizes the elastic wave equation: +% rho u_{i,tt} = dj C_{ijkl} dk u_j +% opSet should be cell array of opSets, one per dimension. This +% is useful if we have periodic BC in one direction. +% Assumes fully compatible operators + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + RHO, RHOi, RHOi_kron % Density + C % Elastic stiffness tensor + + D % Total operator + Dp, Dm % First derivatives + + % Boundary operators in cell format, used for BC + T_w, T_e, T_s, T_n + + % Traction operators + tau_w, tau_e, tau_s, tau_n % Return vector field + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + + % Inner products + H, Hi, Hi_kron, H_1D + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Boundary restriction operators + e_w, e_e, e_s, e_n % Act on vector field, return vector field at boundary + e1_w, e1_e, e1_s, e1_n % Act on vector field, return scalar field at boundary + e2_w, e2_e, e2_s, e2_n % Act on vector field, return scalar field at boundary + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n; % Act on scalar field, return scalar field + + % E{i}^T picks out component i + E + + % Borrowing constants of the form gamma*h, where gamma is a dimensionless constant. + h11 % First entry in norm matrix + + end + + methods + + % The coefficients can either be function handles or grid functions + % optFlag -- if true, extra computations are performed, which may be helpful for optimization. + function obj = Elastic2dVariableAnisotropicUpwind(g, order, rho, C, opSet, optFlag) + default_arg('rho', @(x,y) 0*x+1); + default_arg('opSet',{@sbp.D1Upwind, @sbp.D1Upwind}); + default_arg('optFlag', false); + dim = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x + 1; + end + end + end + end + default_arg('C', C_default); + assert(isa(g, 'grid.Cartesian')) + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + C_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C{i,j,k,l}, 'function_handle') + C{i,j,k,l} = grid.evalOn(g, C{i,j,k,l}); + end + C_mat{i,j,k,l} = spdiag(C{i,j,k,l}); + end + end + end + end + obj.C = C_mat; + + m = g.size(); + m_tot = g.N(); + lim = g.lim; + if isempty(lim) + x = g.x; + lim = cell(length(x),1); + for i = 1:length(x) + lim{i} = {min(x{i}), max(x{i})}; + end + end + + % 1D operators + ops = cell(dim,1); + h = zeros(dim,1); + for i = 1:dim + ops{i} = opSet{i}(m(i), lim{i}, order); + h(i) = ops{i}.h; + end + + % Borrowing constants + for i = 1:dim + obj.h11{i} = ops{i}.H(1,1); + end + + I = cell(dim,1); + Dp = cell(dim,1); + Dm = cell(dim,1); + H = cell(dim,1); + Hi = cell(dim,1); + e_0 = cell(dim,1); + e_m = cell(dim,1); + d1_0 = cell(dim,1); + d1_m = cell(dim,1); + + for i = 1:dim + I{i} = speye(m(i)); + + if isprop(ops{i}, 'Dp') && isprop(ops{i}, 'Dm') + Dp{i} = ops{i}.Dp; + Dm{i} = ops{i}.Dm; + elseif isprop(ops{i}, 'D1') + Dp{i} = ops{i}.D1; + Dm{i} = ops{i}.D1; + else + error('opSet does not have Dp and Dm or D1'); + end + + H{i} = ops{i}.H; + Hi{i} = ops{i}.HI; + e_0{i} = ops{i}.e_l; + e_m{i} = ops{i}.e_r; + d1_0{i} = (ops{i}.e_l' * Dm{i})'; + d1_m{i} = (ops{i}.e_r' * Dm{i})'; + end + + %====== Assemble full operators ======== + I_dim = speye(dim, dim); + RHO = spdiag(rho); + obj.RHO = RHO; + obj.RHOi = inv(RHO); + obj.RHOi_kron = kron(obj.RHOi, I_dim); + + obj.Dp = cell(dim,1); + obj.Dm = cell(dim,1); + + % D1 + obj.Dp{1} = kron(Dp{1},I{2}); + obj.Dp{2} = kron(I{1},Dp{2}); + obj.Dm{1} = kron(Dm{1},I{2}); + obj.Dm{2} = kron(I{1},Dm{2}); + + % Boundary restriction operators + e_l = cell(dim,1); + e_r = cell(dim,1); + e_l{1} = kron(e_0{1}, I{2}); + e_l{2} = kron(I{1}, e_0{2}); + e_r{1} = kron(e_m{1}, I{2}); + e_r{2} = kron(I{1}, e_m{2}); + + e_scalar_w = e_l{1}; + e_scalar_e = e_r{1}; + e_scalar_s = e_l{2}; + e_scalar_n = e_r{2}; + + e_w = kron(e_scalar_w, I_dim); + e_e = kron(e_scalar_e, I_dim); + e_s = kron(e_scalar_s, I_dim); + e_n = kron(e_scalar_n, I_dim); + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + obj.E = E; + + e1_w = (e_scalar_w'*E{1}')'; + e1_e = (e_scalar_e'*E{1}')'; + e1_s = (e_scalar_s'*E{1}')'; + e1_n = (e_scalar_n'*E{1}')'; + + e2_w = (e_scalar_w'*E{2}')'; + e2_e = (e_scalar_e'*E{2}')'; + e2_s = (e_scalar_s'*E{2}')'; + e2_n = (e_scalar_n'*E{2}')'; + + % Quadratures + obj.H = kron(H{1},H{2}); + obj.Hi = inv(obj.H); + obj.H_w = H{2}; + obj.H_e = H{2}; + obj.H_s = H{1}; + obj.H_n = H{1}; + obj.H_1D = {H{1}, H{2}}; + + % Differentiation matrix D (without SAT) + Dp = obj.Dp; + Dm = obj.Dm; + D = sparse(dim*m_tot,dim*m_tot); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + D = D + E{j}*Dp{i}*C_mat{i,j,k,l}*Dm{k}*E{l}'; + end + end + end + end + D = obj.RHOi_kron*D; + obj.D = D; + %=========================================%' + + % Numerical traction operators for BC. + % + % Formula at boundary j: % tau^{j}_i = sum_l T^{j}_{il} u_l + % + T_l = cell(dim,1); + T_r = cell(dim,1); + tau_l = cell(dim,1); + tau_r = cell(dim,1); + + D1 = obj.Dm; + + % Boundary j + for j = 1:dim + T_l{j} = cell(dim,dim); + T_r{j} = cell(dim,dim); + tau_l{j} = cell(dim,1); + tau_r{j} = cell(dim,1); + + [~, n_l] = size(e_l{j}); + [~, n_r] = size(e_r{j}); + + % Traction component i + for i = 1:dim + tau_l{j}{i} = sparse(dim*m_tot, n_l); + tau_r{j}{i} = sparse(dim*m_tot, n_r); + + % Displacement component l + for l = 1:dim + T_l{j}{i,l} = sparse(m_tot, n_l); + T_r{j}{i,l} = sparse(m_tot, n_r); + + % Derivative direction k + for k = 1:dim + T_l{j}{i,l} = T_l{j}{i,l} ... + - (e_l{j}'*C_mat{j,i,k,l}*D1{k})'; + T_r{j}{i,l} = T_r{j}{i,l} ... + + (e_r{j}'*C_mat{j,i,k,l}*D1{k})'; + end + tau_l{j}{i} = tau_l{j}{i} + (T_l{j}{i,l}'*E{l}')'; + tau_r{j}{i} = tau_r{j}{i} + (T_r{j}{i,l}'*E{l}')'; + end + end + end + + % Traction tensors, T_ij + obj.T_w = T_l{1}; + obj.T_e = T_r{1}; + obj.T_s = T_l{2}; + obj.T_n = T_r{2}; + + % Restriction operators + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + obj.e1_w = e1_w; + obj.e1_e = e1_e; + obj.e1_s = e1_s; + obj.e1_n = e1_n; + + obj.e2_w = e2_w; + obj.e2_e = e2_e; + obj.e2_s = e2_s; + obj.e2_n = e2_n; + + obj.e_scalar_w = e_scalar_w; + obj.e_scalar_e = e_scalar_e; + obj.e_scalar_s = e_scalar_s; + obj.e_scalar_n = e_scalar_n; + + % First component of traction + obj.tau1_w = tau_l{1}{1}; + obj.tau1_e = tau_r{1}{1}; + obj.tau1_s = tau_l{2}{1}; + obj.tau1_n = tau_r{2}{1}; + + % Second component of traction + obj.tau2_w = tau_l{1}{2}; + obj.tau2_e = tau_r{1}{2}; + obj.tau2_s = tau_l{2}{2}; + obj.tau2_n = tau_r{2}{2}; + + % Traction vectors + obj.tau_w = (e_w'*e1_w*obj.tau1_w')' + (e_w'*e2_w*obj.tau2_w')'; + obj.tau_e = (e_e'*e1_e*obj.tau1_e')' + (e_e'*e2_e*obj.tau2_e')'; + obj.tau_s = (e_s'*e1_s*obj.tau1_s')' + (e_s'*e2_s*obj.tau2_s')'; + obj.tau_n = (e_n'*e1_n*obj.tau1_n')' + (e_n'*e2_n*obj.tau2_n')'; + + % Kroneckered norms and coefficients + obj.Hi_kron = kron(obj.Hi, I_dim); + + % Misc. + obj.m = m; + obj.h = h; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + comp = bc{1}; + type = bc{2}; + if ischar(comp) + comp = obj.getComponent(comp, boundary); + end + + e = obj.getBoundaryOperatorForScalarField('e', boundary); + tau = obj.getBoundaryOperator(['tau' num2str(comp)], boundary); + T = obj.getBoundaryTractionOperator(boundary); + h11 = obj.getBorrowing(boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); + nu = obj.getNormal(boundary); + + E = obj.E; + Hi = obj.Hi; + RHOi = obj.RHOi; + C = obj.C; + + dim = obj.dim; + m_tot = obj.grid.N(); + + % Preallocate + [~, col] = size(tau); + closure = sparse(dim*m_tot, dim*m_tot); + penalty = sparse(dim*m_tot, col); + + j = comp; + switch type + + % Dirichlet boundary condition + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + + if numel(bc) >= 3 + dComps = bc{3}; + else + dComps = 1:dim; + end + + % Loops over components that Dirichlet penalties end up on + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Nonsymmetric part goes on all components to + % yield traction in discrete energy rate + for i = 1:dim + Y = T{j,i}'; + X = e*Y; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Symmetric part only required on components with displacement BC. + % (Otherwise it's not symmetric.) + for i = dComps + Z = sparse(m_tot, m_tot); + for l = 1:dim + for k = 1:dim + Z = Z + nu(l)*C{l,i,k,j}*nu(k); + end + end + Z = -tuning*dim/h11*Z; + X = Z; + closure = closure + E{i}*RHOi*Hi*X'*e*H_gamma*(e'*E{j}' ); + penalty = penalty - E{i}*RHOi*Hi*X'*e*H_gamma; + end + + % Free boundary condition + case {'F','f','Free','free','traction','Traction','t','T'} + closure = closure - E{j}*RHOi*Hi*e*H_gamma*tau'; + penalty = penalty + E{j}*RHOi*Hi*e*H_gamma; + + % Unknown boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + + u = obj; + v = neighbour_scheme; + + % Operators, u side + e_u = u.getBoundaryOperatorForScalarField('e', boundary); + tau_u = u.getBoundaryOperator('tau', boundary); + h11_u = u.getBorrowing(boundary); + nu_u = u.getNormal(boundary); + + E_u = u.E; + C_u = u.C; + m_tot_u = u.grid.N(); + + % Operators, v side + e_v = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + tau_v = v.getBoundaryOperator('tau', neighbour_boundary); + h11_v = v.getBorrowing(neighbour_boundary); + nu_v = v.getNormal(neighbour_boundary); + + E_v = v.E; + C_v = v.C; + m_tot_v = v.grid.N(); + + % Operators that are only required for own domain + Hi = u.Hi_kron; + RHOi = u.RHOi_kron; + e_kron = u.getBoundaryOperator('e', boundary); + T_u = u.getBoundaryTractionOperator(boundary); + + % Shared operators + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + H_gamma_kron = u.getBoundaryQuadrature(boundary); + dim = u.dim; + + % Preallocate + [~, m_int] = size(H_gamma); + closure = sparse(dim*m_tot_u, dim*m_tot_u); + penalty = sparse(dim*m_tot_u, dim*m_tot_v); + + % ---- Continuity of displacement ------ + + % Y: symmetrizing part of penalty + % Z: symmetric part of penalty + % X = Y + Z. + + % Loop over components to couple across interface + for j = 1:dim + + % Loop over components that penalties end up on + for i = 1:dim + Y = 1/2*T_u{j,i}'; + Z_u = sparse(m_int, m_int); + Z_v = sparse(m_int, m_int); + for l = 1:dim + for k = 1:dim + Z_u = Z_u + e_u'*nu_u(l)*C_u{l,i,k,j}*nu_u(k)*e_u; + Z_v = Z_v + e_v'*nu_v(l)*C_v{l,i,k,j}*nu_v(k)*e_v; + end + end + Z = -tuning*dim*( 1/(4*h11_u)*Z_u + 1/(4*h11_v)*Z_v ); + X = Y + Z*e_u'; + closure = closure + E_u{i}*X'*H_gamma*e_u'*E_u{j}'; + penalty = penalty - E_u{i}*X'*H_gamma*e_v'*E_v{j}'; + end + end + + % ---- Continuity of traction ------ + closure = closure - 1/2*e_kron*H_gamma_kron*tau_u'; + penalty = penalty - 1/2*e_kron*H_gamma_kron*tau_v'; + + % ---- Multiply by inverse of density x quadraure ---- + closure = RHOi*Hi*closure; + penalty = RHOi*Hi*penalty; + + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + error('Non-conforming interfaces not implemented yet.'); + end + + % Returns the component number that is the tangential/normal component + % at the specified boundary + function comp = getComponent(obj, comp_str, boundary) + assertIsMember(comp_str, {'normal', 'tangential'}); + assertIsMember(boundary, {'w', 'e', 's', 'n'}); + + switch boundary + case {'w', 'e'} + switch comp_str + case 'normal' + comp = 1; + case 'tangential' + comp = 2; + end + case {'s', 'n'} + switch comp_str + case 'normal' + comp = 2; + case 'tangential' + comp = 1; + end + end + end + + % Returns h11 for the boundary specified by the string boundary. + % op -- string + function h11 = getBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + h11 = obj.h11{1}; + case {'s', 'n'} + h11 = obj.h11{2}; + end + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + function nu = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case 'w' + nu = [-1,0]; + case 'e' + nu = [1,0]; + case 's' + nu = [0,-1]; + case 'n' + nu = [0,1]; + end + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'}) + + switch op + case {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2'} + o = obj.([op, '_', boundary]); + end + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/Gradient.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/Gradient.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,113 @@ +classdef Gradient < scheme.Scheme + +% Approximates the divergence +% Interface and boundary condition methods are just dummies + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + D + D1 + H + end + + methods + + function obj = Gradient(g, order, opSet) + default_arg('opSet',{@sbp.D2Variable, @sbp.D2Variable}); + + dim = 2; + + m = g.size(); + m_tot = g.N(); + + h = g.scaling(); + lim = g.lim; + if isempty(lim) + x = g.x; + lim = cell(length(x),1); + for i = 1:length(x) + lim{i} = {min(x{i}), max(x{i})}; + end + end + + % 1D operators + ops = cell(dim,1); + for i = 1:dim + ops{i} = opSet{i}(m(i), lim{i}, order); + end + + I = cell(dim,1); + D1 = cell(dim,1); + + for i = 1:dim + I{i} = speye(m(i)); + D1{i} = ops{i}.D1; + end + + %====== Assemble full operators ======== + + % D1 + obj.D1{1} = kron(D1{1},I{2}); + obj.D1{2} = kron(I{1},D1{2}); + + I_dim = speye(dim, dim); + + % E{i}^T picks out component i. + E = cell(dim,1); + I = speye(m_tot,m_tot); + for i = 1:dim + e = sparse(dim,1); + e(i) = 1; + E{i} = kron(I,e); + end + + Grad = sparse(dim*m_tot, m_tot); + for i = 1:dim + Grad = Grad + E{i}*obj.D1{i}; + end + obj.D = Grad; + obj.H = []; + %=========================================%' + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + function [closure, penalty] = boundary_condition(obj, boundary, bc) + error('Not implemented') + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.2 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + [m, n] = size(obj.D); + closure = sparse(m, n); + + [m, n] = size(neighbour_scheme.D); + penalty = sparse(m, n); + end + + function N = size(obj) + N = obj.dim*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/LaplaceCurvilinearNew.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/LaplaceCurvilinearNew.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,605 @@ +classdef LaplaceCurvilinearNew < scheme.Scheme + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + dim % Number of spatial dimensions + + grid + + order % Order of accuracy for the approximation + + a,b % Parameters of the operator + weight % Parameter in front of time derivative (e.g. u_tt in wave equation) here: 1/a. + + + % Inner products and operators for physical coordinates + D % Laplace operator + H, Hi % Inner product + e_w, e_e, e_s, e_n + d_w, d_e, d_s, d_n % Normal derivatives at the boundary + H_w, H_e, H_s, H_n % Boundary inner products + Dx, Dy % Physical derivatives + M % Gradient inner product + + % Metric coefficients + J, Ji + a11, a12, a22 + K + x_u + x_v + y_u + y_v + s_w, s_e, s_s, s_n % Boundary integral scale factors + + % Inner product and operators for logical coordinates + H_u, H_v % Norms in the x and y directions + Hi_u, Hi_v + Hu,Hv % Kroneckerd norms. 1'*Hx*v corresponds to integration in the x dir. + Hiu, Hiv + du_w, dv_w + du_e, dv_e + du_s, dv_s + du_n, dv_n + + % Borrowing constants + theta_M_u, theta_M_v + theta_R_u, theta_R_v + theta_H_u, theta_H_v + + % Temporary, only used for nonconforming interfaces but should be removed. + lambda + end + + methods + % Implements a*div(b*grad(u)) as a SBP scheme + % TODO: Implement proper H, it should be the real physical quadrature, the logic quadrature may be but in a separate variable (H_logic?) + + function obj = LaplaceCurvilinearNew(g, order, a, b, opSet) + default_arg('opSet',@sbp.D2Variable); + default_arg('a', 1); + default_arg('b', @(x,y) 0*x + 1); + + % assert(isa(g, 'grid.Curvilinear')) + if isa(a, 'function_handle') + a = grid.evalOn(g, a); + end + + % If a is scalar + if length(a) == 1 + a = a*ones(g.N(), 1); + end + a = spdiag(a); + + if isa(b, 'function_handle') + b = grid.evalOn(g, b); + end + + % If b is scalar + if length(b) == 1 + % b = b*speye(g.N(), g.N()); + b = b*ones(g.N(), 1); + end + b = spdiag(b); + + dim = 2; + m = g.size(); + m_u = m(1); + m_v = m(2); + m_tot = g.N(); + + % 1D operators + ops_u = opSet(m_u, {0, 1}, order); + ops_v = opSet(m_v, {0, 1}, order); + + h_u = ops_u.h; + h_v = ops_v.h; + + I_u = speye(m_u); + I_v = speye(m_v); + + D1_u = ops_u.D1; + D2_u = ops_u.D2; + H_u = ops_u.H; + Hi_u = ops_u.HI; + e_l_u = ops_u.e_l; + e_r_u = ops_u.e_r; + d1_l_u = ops_u.d1_l; + d1_r_u = ops_u.d1_r; + + D1_v = ops_v.D1; + D2_v = ops_v.D2; + H_v = ops_v.H; + Hi_v = ops_v.HI; + e_l_v = ops_v.e_l; + e_r_v = ops_v.e_r; + d1_l_v = ops_v.d1_l; + d1_r_v = ops_v.d1_r; + + + % Logical operators + Du = kr(D1_u,I_v); + Dv = kr(I_u,D1_v); + obj.Hu = kr(H_u,I_v); + obj.Hv = kr(I_u,H_v); + obj.Hiu = kr(Hi_u,I_v); + obj.Hiv = kr(I_u,Hi_v); + + e_w = kr(e_l_u,I_v); + e_e = kr(e_r_u,I_v); + e_s = kr(I_u,e_l_v); + e_n = kr(I_u,e_r_v); + obj.du_w = kr(d1_l_u,I_v); + obj.dv_w = (e_w'*Dv)'; + obj.du_e = kr(d1_r_u,I_v); + obj.dv_e = (e_e'*Dv)'; + obj.du_s = (e_s'*Du)'; + obj.dv_s = kr(I_u,d1_l_v); + obj.du_n = (e_n'*Du)'; + obj.dv_n = kr(I_u,d1_r_v); + + + % Metric coefficients + coords = g.points(); + x = coords(:,1); + y = coords(:,2); + + x_u = Du*x; + x_v = Dv*x; + y_u = Du*y; + y_v = Dv*y; + + J = x_u.*y_v - x_v.*y_u; + a11 = 1./J .* (x_v.^2 + y_v.^2); + a12 = -1./J .* (x_u.*x_v + y_u.*y_v); + a22 = 1./J .* (x_u.^2 + y_u.^2); + lambda = 1/2 * (a11 + a22 - sqrt((a11-a22).^2 + 4*a12.^2)); + + K = cell(dim, dim); + K{1,1} = spdiag(y_v./J); + K{1,2} = spdiag(-y_u./J); + K{2,1} = spdiag(-x_v./J); + K{2,2} = spdiag(x_u./J); + obj.K = K; + + obj.x_u = x_u; + obj.x_v = x_v; + obj.y_u = y_u; + obj.y_v = y_v; + + % Assemble full operators + L_12 = spdiag(a12); + Duv = Du*b*L_12*Dv; + Dvu = Dv*b*L_12*Du; + + Duu = sparse(m_tot); + Dvv = sparse(m_tot); + ind = grid.funcToMatrix(g, 1:m_tot); + + for i = 1:m_v + b_a11 = b*a11; + D = D2_u(b_a11(ind(:,i))); + p = ind(:,i); + Duu(p,p) = D; + end + + for i = 1:m_u + b_a22 = b*a22; + D = D2_v(b_a22(ind(i,:))); + p = ind(i,:); + Dvv(p,p) = D; + end + + + % Physical operators + obj.J = spdiag(J); + obj.Ji = spdiag(1./J); + + obj.D = obj.Ji*a*(Duu + Duv + Dvu + Dvv); + obj.H = obj.J*kr(H_u,H_v); + obj.Hi = obj.Ji*kr(Hi_u,Hi_v); + + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + %% normal derivatives + I_w = ind(1,:); + I_e = ind(end,:); + I_s = ind(:,1); + I_n = ind(:,end); + + a11_w = spdiag(a11(I_w)); + a12_w = spdiag(a12(I_w)); + a11_e = spdiag(a11(I_e)); + a12_e = spdiag(a12(I_e)); + a22_s = spdiag(a22(I_s)); + a12_s = spdiag(a12(I_s)); + a22_n = spdiag(a22(I_n)); + a12_n = spdiag(a12(I_n)); + + s_w = sqrt((e_w'*x_v).^2 + (e_w'*y_v).^2); + s_e = sqrt((e_e'*x_v).^2 + (e_e'*y_v).^2); + s_s = sqrt((e_s'*x_u).^2 + (e_s'*y_u).^2); + s_n = sqrt((e_n'*x_u).^2 + (e_n'*y_u).^2); + + obj.d_w = -1*(spdiag(1./s_w)*(a11_w*obj.du_w' + a12_w*obj.dv_w'))'; + obj.d_e = (spdiag(1./s_e)*(a11_e*obj.du_e' + a12_e*obj.dv_e'))'; + obj.d_s = -1*(spdiag(1./s_s)*(a22_s*obj.dv_s' + a12_s*obj.du_s'))'; + obj.d_n = (spdiag(1./s_n)*(a22_n*obj.dv_n' + a12_n*obj.du_n'))'; + + obj.Dx = spdiag( y_v./J)*Du + spdiag(-y_u./J)*Dv; + obj.Dy = spdiag(-x_v./J)*Du + spdiag( x_u./J)*Dv; + + %% Boundary inner products + obj.H_w = H_v*spdiag(s_w); + obj.H_e = H_v*spdiag(s_e); + obj.H_s = H_u*spdiag(s_s); + obj.H_n = H_u*spdiag(s_n); + + % Misc. + obj.m = m; + obj.h = [h_u h_v]; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + obj.a = a; + obj.weight = inv(a); + obj.b = b; + obj.a11 = a11; + obj.a12 = a12; + obj.a22 = a22; + obj.s_w = spdiag(s_w); + obj.s_e = spdiag(s_e); + obj.s_s = spdiag(s_s); + obj.s_n = spdiag(s_n); + + obj.theta_M_u = h_u*ops_u.borrowing.M.d1; + obj.theta_M_v = h_v*ops_v.borrowing.M.d1; + + obj.theta_R_u = h_u*ops_u.borrowing.R.delta_D; + obj.theta_R_v = h_v*ops_v.borrowing.R.delta_D; + + obj.theta_H_u = h_u*ops_u.borrowing.H11; + obj.theta_H_v = h_v*ops_v.borrowing.H11; + + % Temporary + obj.lambda = lambda; + end + + + % Closure functions return the opertors applied to the own doamin to close the boundary + % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % type is a string specifying the type of boundary condition if there are several. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + function [closure, penalty] = boundary_condition(obj, boundary, type, parameter) + default_arg('type','neumann'); + default_arg('parameter', []); + + e = obj.getBoundaryOperator('e', boundary); + d = obj.getBoundaryOperator('d', boundary); + H_b = obj.getBoundaryQuadrature(boundary); + s_b = obj.getBoundaryScaling(boundary); + [th_H, ~, th_R] = obj.getBoundaryBorrowing(boundary); + m = obj.getBoundaryNumber(boundary); + + K = obj.K; + J = obj.J; + Hi = obj.Hi; + a = obj.a; + b_b = e'*obj.b*e; + + switch type + % Dirichlet boundary condition + case {'D','d','dirichlet'} + tuning = 1.0; + + sigma = 0*b_b; + for i = 1:obj.dim + sigma = sigma + e'*J*K{i,m}*K{i,m}*e; + end + sigma = sigma/s_b; + tau = tuning*(1/th_R + obj.dim/th_H)*sigma; + + closure = a*Hi*d*b_b*H_b*e' ... + -a*Hi*e*tau*b_b*H_b*e'; + + penalty = -a*Hi*d*b_b*H_b ... + +a*Hi*e*tau*b_b*H_b; + + + % Neumann boundary condition. Note that the penalty is for du/dn and not b*du/dn. + case {'N','n','neumann'} + tau1 = -1; + tau2 = 0; + tau = (tau1*e + tau2*d)*H_b; + + closure = a*Hi*tau*b_b*d'; + penalty = -a*Hi*tau*b_b; + + + % Unknown, boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.2 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.coupling = 'sat'; + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.coupling + case {'cg', 'CG'} + [closure, penalty] = interfaceCG(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'sat', 'SAT'} + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + otherwise + error('Unknown type of coupling: %s ', type.coupling); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + dim = obj.dim; + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + u = obj; + v = neighbour_scheme; + + % Boundary operators, u + e_u = u.getBoundaryOperator('e', boundary); + d_u = u.getBoundaryOperator('d', boundary); + s_b_u = u.getBoundaryScaling(boundary); + [th_H_u, ~, th_R_u] = u.getBoundaryBorrowing(boundary); + m_u = u.getBoundaryNumber(boundary); + + % Coefficients, u + K_u = u.K; + J_u = u.J; + b_b_u = e_u'*u.b*e_u; + + % Boundary operators, v + e_v = v.getBoundaryOperator('e', neighbour_boundary); + d_v = v.getBoundaryOperator('d', neighbour_boundary); + s_b_v = v.getBoundaryScaling(neighbour_boundary); + [th_H_v, ~, th_R_v] = v.getBoundaryBorrowing(neighbour_boundary); + m_v = v.getBoundaryNumber(neighbour_boundary); + + % BUGFIX?!?!? + if (strcmp(boundary,'s') && strcmp(neighbour_boundary,'e')) || (strcmp(boundary,'e') && strcmp(neighbour_boundary,'s')) + e_v = fliplr(e_v); + d_v = fliplr(d_v); + s_b_v = rot90(s_b_v,2); + end + + % Coefficients, v + K_v = v.K; + J_v = v.J; + b_b_v = e_v'*v.b*e_v; + + %--- Penalty strength tau ------------- + sigma_u = 0*b_b_u; + sigma_v = 0*b_b_v; + for i = 1:obj.dim + sigma_u = sigma_u + e_u'*J_u*K_u{i,m_u}*K_u{i,m_u}*e_u; + sigma_v = sigma_v + e_v'*J_v*K_v{i,m_v}*K_v{i,m_v}*e_v; + end + sigma_u = sigma_u/s_b_u; + sigma_v = sigma_v/s_b_v; + + tau_R_u = 1/th_R_u*sigma_u; + tau_R_v = 1/th_R_v*sigma_v; + + tau_H_u = dim*1/th_H_u*sigma_u; + tau_H_v = dim*1/th_H_v*sigma_v; + + tau = 1/4*tuning*(b_b_u*(tau_R_u + tau_H_u) + b_b_v*(tau_R_v + tau_H_v)); + %-------------------------------------- + + % Operators/coefficients that are only required from this side + Hi = u.Hi; + H_b = u.getBoundaryQuadrature(boundary); + a = u.a; + + closure = 1/2*a*Hi*d_u*b_b_u*H_b*e_u' ... + -1/2*a*Hi*e_u*H_b*b_b_u*d_u' ... + -a*Hi*e_u*tau*H_b*e_u'; + + penalty = -1/2*a*Hi*d_u*b_b_u*H_b*e_v' ... + -1/2*a*Hi*e_u*H_b*b_b_v*d_v' ... + +a*Hi*e_u*tau*H_b*e_v'; + end + + function [closure, penalty] = interfaceCG(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + % There is no penalty, only a closure. And the closure is the same as for Neumann BC + e = obj.getBoundaryOperator('e', boundary); + d = obj.getBoundaryOperator('d', boundary); + H_b = obj.getBoundaryQuadrature(boundary); + + e_v = neighbour_scheme.getBoundaryOperator('e', neighbour_boundary); + + Hi = obj.Hi; + a = obj.a; + b_b = e'*obj.b*e; + + tau1 = -1; + tau2 = 0; + tau = (tau1*e + tau2*d)*H_b; + + closure = a*Hi*tau*b_b*d'; + + % Zero penalty of correct dimensions + penalty = 0*e*e_v'; + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + % TODO: Make this work for curvilinear grids + warning('LaplaceCurvilinear: Non-conforming grid interpolation only works for Cartesian grids.'); + warning('LaplaceCurvilinear: Non-conforming interface uses Virtas penalty strength'); + warning('LaplaceCurvilinear: Non-conforming interface assumes that b is constant'); + + % User can request special interpolation operators by specifying type.interpOpSet + default_field(type, 'interpOpSet', @sbp.InterpOpsOP); + interpOpSet = type.interpOpSet; + tuning = type.tuning; + + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + e_u = obj.getBoundaryOperator('e', boundary); + d_u = obj.getBoundaryOperator('d', boundary); + H_b_u = obj.getBoundaryQuadrature(boundary); + I_u = obj.getBoundaryIndices(boundary); + [~, gamm_u] = obj.getBoundaryBorrowing(boundary); + + e_v = neighbour_scheme.getBoundaryOperator('e', neighbour_boundary); + d_v = neighbour_scheme.getBoundaryOperator('d', neighbour_boundary); + H_b_v = neighbour_scheme.getBoundaryQuadrature(neighbour_boundary); + I_v = neighbour_scheme.getBoundaryIndices(neighbour_boundary); + [~, gamm_v] = neighbour_scheme.getBoundaryBorrowing(neighbour_boundary); + + + % Find the number of grid points along the interface + m_u = size(e_u, 2); + m_v = size(e_v, 2); + + Hi = obj.Hi; + a = obj.a; + + u = obj; + v = neighbour_scheme; + + b1_u = gamm_u*u.lambda(I_u)./u.a11(I_u).^2; + b2_u = gamm_u*u.lambda(I_u)./u.a22(I_u).^2; + b1_v = gamm_v*v.lambda(I_v)./v.a11(I_v).^2; + b2_v = gamm_v*v.lambda(I_v)./v.a22(I_v).^2; + + tau_u = -1./(4*b1_u) -1./(4*b2_u); + tau_v = -1./(4*b1_v) -1./(4*b2_v); + + tau_u = tuning * spdiag(tau_u); + tau_v = tuning * spdiag(tau_v); + beta_u = tau_v; + + % Build interpolation operators + intOps = interpOpSet(m_u, m_v, obj.order, neighbour_scheme.order); + Iu2v = intOps.Iu2v; + Iv2u = intOps.Iv2u; + + closure = a*Hi*e_u*tau_u*H_b_u*e_u' + ... + a*Hi*e_u*H_b_u*Iv2u.bad*beta_u*Iu2v.good*e_u' + ... + a*1/2*Hi*d_u*H_b_u*e_u' + ... + -a*1/2*Hi*e_u*H_b_u*d_u'; + + penalty = -a*Hi*e_u*tau_u*H_b_u*Iv2u.good*e_v' + ... + -a*Hi*e_u*H_b_u*Iv2u.bad*beta_u*e_v' + ... + -a*1/2*Hi*d_u*H_b_u*Iv2u.good*e_v' + ... + -a*1/2*Hi*e_u*H_b_u*Iv2u.bad*d_v'; + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + % boundary -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(op, {'e', 'd'}) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + o = obj.([op, '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary points + % + % boundary -- string + function H_b = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + % Returns square boundary quadrature scaling matrix, of dimension + % corresponding to the number of boundary points + % + % boundary -- string + function s_b = getBoundaryScaling(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + s_b = obj.(['s_', boundary]); + end + + % Returns the coordinate number corresponding to the boundary + % + % boundary -- string + function m = getBoundaryNumber(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w', 'e'} + m = 1; + case {'s', 'n'} + m = 2; + end + end + + % Returns the indices of the boundary points in the grid matrix + % boundary -- string + function I = getBoundaryIndices(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + ind = grid.funcToMatrix(obj.grid, 1:prod(obj.m)); + switch boundary + case 'w' + I = ind(1,:); + case 'e' + I = ind(end,:); + case 's' + I = ind(:,1)'; + case 'n' + I = ind(:,end)'; + end + end + + % Returns borrowing constant gamma + % boundary -- string + function [theta_H, theta_M, theta_R] = getBoundaryBorrowing(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w','e'} + theta_H = obj.theta_H_u; + theta_M = obj.theta_M_u; + theta_R = obj.theta_R_u; + case {'s','n'} + theta_H = obj.theta_H_v; + theta_M = obj.theta_M_v; + theta_R = obj.theta_R_v; + end + end + + function N = size(obj) + N = prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/LaplaceCurvilinearNewCorner.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/LaplaceCurvilinearNewCorner.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,591 @@ +classdef LaplaceCurvilinearNewCorner < scheme.Scheme + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + dim % Number of spatial dimensions + + grid + + order % Order of accuracy for the approximation + + a,b % Parameters of the operator + + + % Inner products and operators for physical coordinates + D % Laplace operator + H, Hi % Inner product + e_w, e_e, e_s, e_n + d_w, d_e, d_s, d_n % Normal derivatives at the boundary + H_w, H_e, H_s, H_n % Boundary inner products + Dx, Dy % Physical derivatives + M % Gradient inner product + + % Metric coefficients + J, Ji + a11, a12, a22 + K + x_u + x_v + y_u + y_v + s_w, s_e, s_s, s_n % Boundary integral scale factors + + % Inner product and operators for logical coordinates + H_u, H_v, H_1D % Norms in the x and y directions + Hi_u, Hi_v + Hu,Hv % Kroneckerd norms. 1'*Hx*v corresponds to integration in the x dir. + Hiu, Hiv + du_w, dv_w + du_e, dv_e + du_s, dv_s + du_n, dv_n + + % Borrowing constants + theta_M_u, theta_M_v + theta_R_u, theta_R_v + theta_H_u, theta_H_v + + % Temporary + lambda + gamm_u, gamm_v + + end + + methods + % Implements a*div(b*grad(u)) as a SBP scheme + % TODO: Implement proper H, it should be the real physical quadrature, the logic quadrature may be but in a separate variable (H_logic?) + + function obj = LaplaceCurvilinearNewCorner(g, order, a, b, opSet) + default_arg('opSet',@sbp.D2Variable); + default_arg('a', 1); + default_arg('b', @(x,y) 0*x + 1); + + % assert(isa(g, 'grid.Curvilinear')) + if isa(a, 'function_handle') + a = grid.evalOn(g, a); + end + a = spdiag(a); + + if isa(b, 'function_handle') + b = grid.evalOn(g, b); + end + b = spdiag(b); + + % If b is scalar + if length(b) == 1 + b = b*speye(g.N(), g.N()); + end + + dim = 2; + m = g.size(); + m_u = m(1); + m_v = m(2); + m_tot = g.N(); + + if isa(g, 'grid.Curvilinear') + h = g.scaling(); + h_u = h(1); + h_v = h(2); + else + h_u = 1/(m_u - 1); + h_v = 1/(m_v - 1); + h = [h_u, h_v]; + end + + % 1D operators + ops_u = opSet(m_u, {0, 1}, order); + ops_v = opSet(m_v, {0, 1}, order); + + I_u = speye(m_u); + I_v = speye(m_v); + + D1_u = ops_u.D1; + D2_u = ops_u.D2; + H_u = ops_u.H; + Hi_u = ops_u.HI; + e_l_u = ops_u.e_l; + e_r_u = ops_u.e_r; + d1_l_u = ops_u.d1_l; + d1_r_u = ops_u.d1_r; + + D1_v = ops_v.D1; + D2_v = ops_v.D2; + H_v = ops_v.H; + Hi_v = ops_v.HI; + e_l_v = ops_v.e_l; + e_r_v = ops_v.e_r; + d1_l_v = ops_v.d1_l; + d1_r_v = ops_v.d1_r; + + + % Logical operators + Du = kr(D1_u,I_v); + Dv = kr(I_u,D1_v); + obj.H_u = H_u; + obj.H_v = H_v; + obj.H_1D = {H_u, H_v}; + + obj.Hu = kr(H_u,I_v); + obj.Hv = kr(I_u,H_v); + obj.Hiu = kr(Hi_u,I_v); + obj.Hiv = kr(I_u,Hi_v); + + e_w = kr(e_l_u,I_v); + e_e = kr(e_r_u,I_v); + e_s = kr(I_u,e_l_v); + e_n = kr(I_u,e_r_v); + obj.du_w = kr(d1_l_u,I_v); + obj.dv_w = (e_w'*Dv)'; + obj.du_e = kr(d1_r_u,I_v); + obj.dv_e = (e_e'*Dv)'; + obj.du_s = (e_s'*Du)'; + obj.dv_s = kr(I_u,d1_l_v); + obj.du_n = (e_n'*Du)'; + obj.dv_n = kr(I_u,d1_r_v); + + + % Metric coefficients + coords = g.points(); + x = coords(:,1); + y = coords(:,2); + + x_u = Du*x; + x_v = Dv*x; + y_u = Du*y; + y_v = Dv*y; + + J = x_u.*y_v - x_v.*y_u; + a11 = 1./J .* (x_v.^2 + y_v.^2); + a12 = -1./J .* (x_u.*x_v + y_u.*y_v); + a22 = 1./J .* (x_u.^2 + y_u.^2); + lambda = 1/2 * (a11 + a22 - sqrt((a11-a22).^2 + 4*a12.^2)); + + K = cell(dim, dim); + K{1,1} = spdiag(y_v./J); + K{1,2} = spdiag(-y_u./J); + K{2,1} = spdiag(-x_v./J); + K{2,2} = spdiag(x_u./J); + obj.K = K; + + obj.x_u = x_u; + obj.x_v = x_v; + obj.y_u = y_u; + obj.y_v = y_v; + + % Assemble full operators + L_12 = spdiag(a12); + Duv = Du*b*L_12*Dv; + Dvu = Dv*b*L_12*Du; + + Duu = sparse(m_tot); + Dvv = sparse(m_tot); + ind = grid.funcToMatrix(g, 1:m_tot); + + for i = 1:m_v + b_a11 = b*a11; + D = D2_u(b_a11(ind(:,i))); + p = ind(:,i); + Duu(p,p) = D; + end + + for i = 1:m_u + b_a22 = b*a22; + D = D2_v(b_a22(ind(i,:))); + p = ind(i,:); + Dvv(p,p) = D; + end + + + % Physical operators + obj.J = spdiag(J); + obj.Ji = spdiag(1./J); + + obj.D = obj.Ji*a*(Duu + Duv + Dvu + Dvv); + obj.H = obj.J*kr(H_u,H_v); + obj.Hi = obj.Ji*kr(Hi_u,Hi_v); + + obj.e_w = e_w; + obj.e_e = e_e; + obj.e_s = e_s; + obj.e_n = e_n; + + %% normal derivatives + I_w = ind(1,:); + I_e = ind(end,:); + I_s = ind(:,1); + I_n = ind(:,end); + + a11_w = spdiag(a11(I_w)); + a12_w = spdiag(a12(I_w)); + a11_e = spdiag(a11(I_e)); + a12_e = spdiag(a12(I_e)); + a22_s = spdiag(a22(I_s)); + a12_s = spdiag(a12(I_s)); + a22_n = spdiag(a22(I_n)); + a12_n = spdiag(a12(I_n)); + + s_w = sqrt((e_w'*x_v).^2 + (e_w'*y_v).^2); + s_e = sqrt((e_e'*x_v).^2 + (e_e'*y_v).^2); + s_s = sqrt((e_s'*x_u).^2 + (e_s'*y_u).^2); + s_n = sqrt((e_n'*x_u).^2 + (e_n'*y_u).^2); + + obj.d_w = -1*(spdiag(1./s_w)*(a11_w*obj.du_w' + a12_w*obj.dv_w'))'; + obj.d_e = (spdiag(1./s_e)*(a11_e*obj.du_e' + a12_e*obj.dv_e'))'; + obj.d_s = -1*(spdiag(1./s_s)*(a22_s*obj.dv_s' + a12_s*obj.du_s'))'; + obj.d_n = (spdiag(1./s_n)*(a22_n*obj.dv_n' + a12_n*obj.du_n'))'; + + obj.Dx = spdiag( y_v./J)*Du + spdiag(-y_u./J)*Dv; + obj.Dy = spdiag(-x_v./J)*Du + spdiag( x_u./J)*Dv; + + %% Boundary inner products + obj.H_w = H_v*spdiag(s_w); + obj.H_e = H_v*spdiag(s_e); + obj.H_s = H_u*spdiag(s_s); + obj.H_n = H_u*spdiag(s_n); + + % Misc. + obj.m = m; + obj.h = [h_u h_v]; + obj.order = order; + obj.grid = g; + obj.dim = dim; + + obj.a = a; + obj.b = b; + obj.a11 = a11; + obj.a12 = a12; + obj.a22 = a22; + obj.s_w = spdiag(s_w); + obj.s_e = spdiag(s_e); + obj.s_s = spdiag(s_s); + obj.s_n = spdiag(s_n); + + obj.theta_M_u = h_u*ops_u.borrowing.M.d1; + obj.theta_M_v = h_v*ops_v.borrowing.M.d1; + + obj.theta_R_u = h_u*ops_u.borrowing.R.delta_D; + obj.theta_R_v = h_v*ops_v.borrowing.R.delta_D; + + obj.theta_H_u = h_u*ops_u.borrowing.H11; + obj.theta_H_v = h_v*ops_v.borrowing.H11; + + % Temporary + obj.lambda = lambda; + obj.gamm_u = h_u*ops_u.borrowing.M.d1; + obj.gamm_v = h_v*ops_v.borrowing.M.d1; + end + + + % Closure functions return the opertors applied to the own doamin to close the boundary + % Penalty functions return the opertors to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % type is a string specifying the type of boundary condition if there are several. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + function [closure, penalty] = boundary_condition(obj, boundary, type, parameter) + default_arg('type','neumann'); + default_arg('parameter', []); + + e = obj.getBoundaryOperator('e', boundary); + d = obj.getBoundaryOperator('d', boundary); + H_b = obj.getBoundaryQuadrature(boundary); + s_b = obj.getBoundaryScaling(boundary); + [th_H, ~, th_R] = obj.getBoundaryBorrowing(boundary); + m = obj.getBoundaryNumber(boundary); + + K = obj.K; + J = obj.J; + Hi = obj.Hi; + a = obj.a; + b_b = e'*obj.b*e; + + switch type + % Dirichlet boundary condition + case {'D','d','dirichlet'} + tuning = 1.0; + + sigma = 0*b_b; + for i = 1:obj.dim + sigma = sigma + e'*J*K{i,m}*K{i,m}*e; + end + sigma = sigma/s_b; + % tau = tuning*(1/th_R + obj.dim/th_H)*sigma; + + tau_R = 1/th_R*sigma; + + tau_H = 1/th_H*sigma; + tau_H(1,1) = obj.dim*tau_H(1,1); + tau_H(end,end) = obj.dim*tau_H(end,end); + + tau = tuning*(tau_R + tau_H); + + closure = a*Hi*d*b_b*H_b*e' ... + -a*Hi*e*tau*b_b*H_b*e'; + + penalty = -a*Hi*d*b_b*H_b ... + +a*Hi*e*tau*b_b*H_b; + + + % Neumann boundary condition. Note that the penalty is for du/dn and not b*du/dn. + case {'N','n','neumann'} + tau1 = -1; + tau2 = 0; + tau = (tau1*e + tau2*d)*H_b; + + closure = a*Hi*tau*b_b*d'; + penalty = -a*Hi*tau*b_b; + + + % Unknown boundary condition + otherwise + error('No such boundary condition: type = %s',type); + end + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.2 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + % error('Not implemented') + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + default_struct('type', defaultType); + + switch type.interpolation + case {'none', ''} + [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type); + case {'op','OP'} + [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type); + otherwise + error('Unknown type of interpolation: %s ', type.interpolation); + end + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + dim = obj.dim; + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + u = obj; + v = neighbour_scheme; + + % Boundary operators, u + e_u = u.getBoundaryOperator('e', boundary); + d_u = u.getBoundaryOperator('d', boundary); + gamm_u = u.getBoundaryBorrowing(boundary); + s_b_u = u.getBoundaryScaling(boundary); + [th_H_u, ~, th_R_u] = u.getBoundaryBorrowing(boundary); + m_u = u.getBoundaryNumber(boundary); + + % Coefficients, u + K_u = u.K; + J_u = u.J; + b_b_u = e_u'*u.b*e_u; + + % Boundary operators, v + e_v = v.getBoundaryOperator('e', neighbour_boundary); + d_v = v.getBoundaryOperator('d', neighbour_boundary); + gamm_v = v.getBoundaryBorrowing(neighbour_boundary); + s_b_v = v.getBoundaryScaling(neighbour_boundary); + [th_H_v, ~, th_R_v] = v.getBoundaryBorrowing(neighbour_boundary); + m_v = v.getBoundaryNumber(neighbour_boundary); + + % Coefficients, v + K_v = v.K; + J_v = v.J; + b_b_v = e_v'*v.b*e_v; + + %--- Penalty strength tau ------------- + sigma_u = 0*b_b_u; + sigma_v = 0*b_b_v; + for i = 1:obj.dim + sigma_u = sigma_u + e_u'*J_u*K_u{i,m_u}*K_u{i,m_u}*e_u; + sigma_v = sigma_v + e_v'*J_v*K_v{i,m_v}*K_v{i,m_v}*e_v; + end + sigma_u = sigma_u/s_b_u; + sigma_v = sigma_v/s_b_v; + + tau_R_u = 1/th_R_u*sigma_u; + tau_R_v = 1/th_R_v*sigma_v; + + tau_H_u = 1/th_H_u*sigma_u; + tau_H_u(1,1) = dim*tau_H_u(1,1); + tau_H_u(end,end) = dim*tau_H_u(end,end); + + tau_H_v = 1/th_H_v*sigma_v; + tau_H_v(1,1) = dim*tau_H_v(1,1); + tau_H_v(end,end) = dim*tau_H_v(end,end); + + tau = 1/4*tuning*(b_b_u*(tau_R_u + tau_H_u) + b_b_v*(tau_R_v + tau_H_v)); + %-------------------------------------- + + % Operators/coefficients that are only required from this side + Hi = u.Hi; + H_b = u.getBoundaryQuadrature(boundary); + a = u.a; + + closure = 1/2*a*Hi*d_u*b_b_u*H_b*e_u' ... + -1/2*a*Hi*e_u*H_b*b_b_u*d_u' ... + -a*Hi*e_u*tau*H_b*e_u'; + + penalty = -1/2*a*Hi*d_u*b_b_u*H_b*e_v' ... + -1/2*a*Hi*e_u*H_b*b_b_v*d_v' ... + +a*Hi*e_u*tau*H_b*e_v'; + end + + function [closure, penalty] = interfaceNonConforming(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + % TODO: Make this work for curvilinear grids + warning('LaplaceCurvilinear: Non-conforming grid interpolation only works for Cartesian grids.'); + warning('LaplaceCurvilinear: Non-conforming interface uses Virtas penalty strength'); + warning('LaplaceCurvilinear: Non-conforming interface assumes that b is constant'); + + % User can request special interpolation operators by specifying type.interpOpSet + default_field(type, 'interpOpSet', @sbp.InterpOpsOP); + interpOpSet = type.interpOpSet; + tuning = type.tuning; + + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + e_u = obj.getBoundaryOperator('e', boundary); + d_u = obj.getBoundaryOperator('d', boundary); + H_b_u = obj.getBoundaryQuadrature(boundary); + I_u = obj.getBoundaryIndices(boundary); + gamm_u = obj.getBoundaryBorrowing(boundary); + + e_v = neighbour_scheme.getBoundaryOperator('e', neighbour_boundary); + d_v = neighbour_scheme.getBoundaryOperator('d', neighbour_boundary); + H_b_v = neighbour_scheme.getBoundaryQuadrature(neighbour_boundary); + I_v = neighbour_scheme.getBoundaryIndices(neighbour_boundary); + gamm_v = neighbour_scheme.getBoundaryBorrowing(neighbour_boundary); + + + % Find the number of grid points along the interface + m_u = size(e_u, 2); + m_v = size(e_v, 2); + + Hi = obj.Hi; + a = obj.a; + + u = obj; + v = neighbour_scheme; + + b1_u = gamm_u*u.lambda(I_u)./u.a11(I_u).^2; + b2_u = gamm_u*u.lambda(I_u)./u.a22(I_u).^2; + b1_v = gamm_v*v.lambda(I_v)./v.a11(I_v).^2; + b2_v = gamm_v*v.lambda(I_v)./v.a22(I_v).^2; + + tau_u = -1./(4*b1_u) -1./(4*b2_u); + tau_v = -1./(4*b1_v) -1./(4*b2_v); + + tau_u = tuning * spdiag(tau_u); + tau_v = tuning * spdiag(tau_v); + beta_u = tau_v; + + % Build interpolation operators + intOps = interpOpSet(m_u, m_v, obj.order, neighbour_scheme.order); + Iu2v = intOps.Iu2v; + Iv2u = intOps.Iv2u; + + closure = a*Hi*e_u*tau_u*H_b_u*e_u' + ... + a*Hi*e_u*H_b_u*Iv2u.bad*beta_u*Iu2v.good*e_u' + ... + a*1/2*Hi*d_u*H_b_u*e_u' + ... + -a*1/2*Hi*e_u*H_b_u*d_u'; + + penalty = -a*Hi*e_u*tau_u*H_b_u*Iv2u.good*e_v' + ... + -a*Hi*e_u*H_b_u*Iv2u.bad*beta_u*e_v' + ... + -a*1/2*Hi*d_u*H_b_u*Iv2u.good*e_v' + ... + -a*1/2*Hi*e_u*H_b_u*Iv2u.bad*d_v'; + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + % boundary -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(op, {'e', 'd'}) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + o = obj.([op, '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary points + % + % boundary -- string + function H_b = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + % Returns square boundary quadrature scaling matrix, of dimension + % corresponding to the number of boundary points + % + % boundary -- string + function s_b = getBoundaryScaling(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + s_b = obj.(['s_', boundary]); + end + + % Returns the coordinate number corresponding to the boundary + % + % boundary -- string + function m = getBoundaryNumber(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + switch boundary + case {'w', 'e'} + m = 1; + case {'s', 'n'} + m = 2; + end + end + + % Returns the indices of the boundary points in the grid matrix + % boundary -- string + function I = getBoundaryIndices(obj, boundary) + ind = grid.funcToMatrix(obj.grid, 1:prod(obj.m)); + switch boundary + case 'w' + I = ind(1,:); + case 'e' + I = ind(end,:); + case 's' + I = ind(:,1)'; + case 'n' + I = ind(:,end)'; + otherwise + error('No such boundary: boundary = %s',boundary); + end + end + + % Returns borrowing constant gamma + % boundary -- string + function [theta_H, theta_M, theta_R] = getBoundaryBorrowing(obj, boundary) + switch boundary + case {'w','e'} + theta_H = obj.theta_H_u; + theta_M = obj.theta_M_u; + theta_R = obj.theta_R_u; + case {'s','n'} + theta_H = obj.theta_H_v; + theta_M = obj.theta_M_v; + theta_R = obj.theta_R_v; + otherwise + error('No such boundary: boundary = %s',boundary); + end + end + + function N = size(obj) + N = prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/ViscoElastic2d.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/ViscoElastic2d.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,733 @@ +classdef ViscoElastic2d < scheme.Scheme + +% Discretizes the visco-elastic wave equation in curvilinear coordinates. +% Assumes fully compatible operators. + + properties + m % Number of points in each direction, possibly a vector + h % Grid spacing + + grid + dim + + order % Order of accuracy for the approximation + + % Diagonal matrices for variable coefficients + % J, Ji + RHO % Density + C % Elastic stiffness tensor + ETA % Effective viscosity, used in strain rate eq + + D % Total operator + Delastic % Elastic operator (momentum balance) + Dviscous % Acts on viscous strains in momentum balance + DstrainRate % Acts on u and gamma, returns strain rate gamma_t + + D1, D1Tilde % Physical derivatives + sigma % Cell matrix of physical stress operators + + % Inner products + H + + % Boundary inner products (for scalar field) + H_w, H_e, H_s, H_n + + % Restriction operators + Eu, Egamma % Pick out all components of u/gamma + eU, eGamma % Pick out one specific component + + % Bundary restriction ops + e_scalar_w, e_scalar_e, e_scalar_s, e_scalar_n + + n_w, n_e, n_s, n_n % Physical normals + tangent_w, tangent_e, tangent_s, tangent_n % Physical tangents + + tau1_w, tau1_e, tau1_s, tau1_n % Return scalar field + tau2_w, tau2_e, tau2_s, tau2_n % Return scalar field + tau_n_w, tau_n_e, tau_n_s, tau_n_n % Return scalar field + tau_t_w, tau_t_e, tau_t_s, tau_t_n % Return scalar field + + elasticObj + + end + + methods + + % The coefficients can either be function handles or grid functions + function obj = ViscoElastic2d(g, order, rho, C, eta) + default_arg('rho', @(x,y) 0*x+1); + default_arg('eta', @(x,y) 0*x+1); + dim = 2; + + C_default = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C_default{i,j,k,l} = @(x,y) 0*x ; + end + end + end + end + default_arg('C', C_default); + + assert(isa(g, 'grid.Curvilinear')); + + if isa(rho, 'function_handle') + rho = grid.evalOn(g, rho); + end + + if isa(eta, 'function_handle') + eta = grid.evalOn(g, eta); + end + + C_mat = cell(dim,dim,dim,dim); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + if isa(C{i,j,k,l}, 'function_handle') + C{i,j,k,l} = grid.evalOn(g, C{i,j,k,l}); + end + C_mat{i,j,k,l} = spdiag(C{i,j,k,l}); + end + end + end + end + obj.C = C_mat; + + elasticObj = scheme.Elastic2dCurvilinearAnisotropic(g, order, rho, C); + + % Construct a pair of first derivatives + K = elasticObj.K; + for i = 1:dim + for j = 1:dim + K{i,j} = spdiag(K{i,j}); + end + end + J = elasticObj.J; + Ji = elasticObj.Ji; + D_ref = elasticObj.refObj.D1; + D1 = cell(dim, 1); + D1Tilde = cell(dim, 1); + for i = 1:dim + D1{i} = 0*D_ref{i}; + D1Tilde{i} = 0*D_ref{i}; + for j = 1:dim + D1{i} = D1{i} + K{i,j}*D_ref{j}; + D1Tilde{i} = D1Tilde{i} + Ji*D_ref{j}*J*K{i,j}; + end + end + obj.D1 = D1; + obj.D1Tilde = D1Tilde; + + eU = elasticObj.E; + + % Storage order for gamma: 11-12-21-22. + I = speye(g.N(), g.N()); + eGamma = cell(dim, dim); + e = cell(dim, dim); + e{1,1} = [1;0;0;0]; + e{1,2} = [0;1;0;0]; + e{2,1} = [0;0;1;0]; + e{2,2} = [0;0;0;1]; + for i = 1:dim + for j = 1:dim + eGamma{i,j} = kron(I, e{i,j}); + end + end + + % Store u first, then gamma + mU = dim*g.N(); + mGamma = dim^2*g.N(); + Iu = speye(mU, mU); + Igamma = speye(mGamma, mGamma); + + Eu = cell2mat({Iu, sparse(mU, mGamma)})'; + Egamma = cell2mat({sparse(mGamma, mU), Igamma})'; + + for i = 1:dim + eU{i} = Eu*eU{i}; + end + for i = 1:dim + for j = 1:dim + eGamma{i,j} = Egamma*eGamma{i,j}; + end + end + + obj.eGamma = eGamma; + obj.eU = eU; + obj.Egamma = Egamma; + obj.Eu = Eu; + + % Build stress operator + sigma = cell(dim, dim); + C = obj.C; + for i = 1:dim + for j = 1:dim + sigma{i,j} = spalloc(g.N(), (dim^2 + dim)*g.N(), order^2*g.N()); + for k = 1:dim + for l = 1:dim + sigma{i,j} = sigma{i,j} + C{i,j,k,l}*(D1{k}*eU{l}' - eGamma{k,l}'); + end + end + end + end + + % Elastic operator + Delastic = Eu*elasticObj.D*Eu'; + + % Add viscous strains to momentum balance + RHOi = spdiag(1./rho); + Dviscous = spalloc((dim^2 + dim)*g.N(), (dim^2 + dim)*g.N(), order^2*(dim^2 + dim)*g.N()); + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + Dviscous = Dviscous - eU{j}*RHOi*D1Tilde{i}*C{i,j,k,l}*eGamma{k,l}'; + end + end + end + end + + ETA = spdiag(eta); + DstrainRate = 0*Delastic; + for i = 1:dim + for j = 1:dim + DstrainRate = DstrainRate + eGamma{i,j}*(ETA\sigma{i,j}); + end + end + + obj.D = Delastic + Dviscous + DstrainRate; + obj.Delastic = Delastic; + obj.Dviscous = Dviscous; + obj.DstrainRate = DstrainRate; + obj.sigma = sigma; + + %---- Set remaining object properties ------ + obj.RHO = elasticObj.RHO; + obj.ETA = ETA; + obj.H = elasticObj.H; + + obj.n_w = elasticObj.n_w; + obj.n_e = elasticObj.n_e; + obj.n_s = elasticObj.n_s; + obj.n_n = elasticObj.n_n; + + obj.tangent_w = elasticObj.tangent_w; + obj.tangent_e = elasticObj.tangent_e; + obj.tangent_s = elasticObj.tangent_s; + obj.tangent_n = elasticObj.tangent_n; + + obj.H_w = elasticObj.H_w; + obj.H_e = elasticObj.H_e; + obj.H_s = elasticObj.H_s; + obj.H_n = elasticObj.H_n; + + obj.e_scalar_w = elasticObj.e_scalar_w; + obj.e_scalar_e = elasticObj.e_scalar_e; + obj.e_scalar_s = elasticObj.e_scalar_s; + obj.e_scalar_n = elasticObj.e_scalar_n; + + % -- Create new traction operators including viscous strain contribution -- + tau1 = struct; + tau2 = struct; + tau_n = struct; + tau_t = struct; + boundaries = {'w', 'e', 's', 'n'}; + for bNumber = 1:numel(boundaries) + b = boundaries{bNumber}; + + n = elasticObj.getNormal(b); + t = elasticObj.getTangent(b); + e = elasticObj.getBoundaryOperatorForScalarField('e', b); + tau1.(b) = (elasticObj.getBoundaryOperator('tau1', b)'*Eu')'; + tau2.(b) = (elasticObj.getBoundaryOperator('tau2', b)'*Eu')'; + + % Add viscous contributions + for i = 1:dim + for k = 1:dim + for l = 1:dim + tau1.(b) = tau1.(b) - (n{i}*e'*C{i,1,k,l}*eGamma{k,l}')'; + tau2.(b) = tau2.(b) - (n{i}*e'*C{i,2,k,l}*eGamma{k,l}')'; + end + end + end + + % Compute normal and tangential components + tau_n.(b) = tau1.(b)*n{1} + tau2.(b)*n{2}; + tau_t.(b) = tau1.(b)*t{1} + tau2.(b)*t{2}; + end + + obj.tau1_w = tau1.w; + obj.tau1_e = tau1.e; + obj.tau1_s = tau1.s; + obj.tau1_n = tau1.n; + + obj.tau2_w = tau2.w; + obj.tau2_e = tau2.e; + obj.tau2_s = tau2.s; + obj.tau2_n = tau2.n; + + obj.tau_n_w = tau_n.w; + obj.tau_n_e = tau_n.e; + obj.tau_n_s = tau_n.s; + obj.tau_n_n = tau_n.n; + + obj.tau_t_w = tau_t.w; + obj.tau_t_e = tau_t.e; + obj.tau_t_s = tau_t.s; + obj.tau_t_n = tau_t.n; + %---------------------------------------- + + % Misc. + obj.elasticObj = elasticObj; + obj.m = elasticObj.m; + obj.h = elasticObj.h; + + obj.order = order; + obj.grid = g; + obj.dim = dim; + + end + + + % Closure functions return the operators applied to the own domain to close the boundary + % Penalty functions return the operators to force the solution. In the case of an interface it returns the operator applied to the other doamin. + % boundary is a string specifying the boundary e.g. 'l','r' or 'e','w','n','s'. + % bc is a cell array of component and bc type, e.g. {1, 'd'} for Dirichlet condition + % on the first component. Can also be e.g. + % {'normal', 'd'} or {'tangential', 't'} for conditions on + % tangential/normal component. + % data is a function returning the data that should be applied at the boundary. + % neighbour_scheme is an instance of Scheme that should be interfaced to. + % neighbour_boundary is a string specifying which boundary to interface to. + + % For displacement bc: + % bc = {comp, 'd', dComps}, + % where + % dComps = vector of components with displacement BC. Default: 1:dim. + % In this way, we can specify one BC at a time even though the SATs depend on all BC. + function [closure, penalty] = boundary_condition(obj, boundary, bc, tuning) + default_arg('tuning', 1.0); + assert( iscell(bc), 'The BC type must be a 2x1 or 3x1 cell array' ); + + component = bc{1}; + type = bc{2}; + dim = obj.dim; + + n = obj.getNormal(boundary); + H_gamma = obj.getBoundaryQuadratureForScalarField(boundary); + e = obj.getBoundaryOperatorForScalarField('e', boundary); + + H = obj.H; + RHO = obj.RHO; + ETA = obj.ETA; + C = obj.C; + Eu = obj.Eu; + eU = obj.eU; + eGamma = obj.eGamma; + + % Get elastic closure and penalty + [closure, penalty] = obj.elasticObj.boundary_condition(boundary, bc, tuning); + closure = Eu*closure*Eu'; + penalty = Eu*penalty; + + switch component + case 't' + dir = obj.getTangent(boundary); + tau = obj.getBoundaryOperator('tau_t', boundary); + case 'n' + dir = obj.getNormal(boundary); + tau = obj.getBoundaryOperator('tau_n', boundary); + case 1 + dir = {1, 0}; + tau = obj.getBoundaryOperator('tau1', boundary); + case 2 + dir = {0, 1}; + tau = obj.getBoundaryOperator('tau2', boundary); + end + + switch type + case {'F','f','Free','free','traction','Traction','t','T'} + + % Set elastic closure to zero + closure = 0*closure; + + for m = 1:dim + closure = closure - eU{m}*( (RHO*H)\(e*dir{m}*H_gamma*tau') ); + end + + case {'D','d','dirichlet','Dirichlet','displacement','Displacement'} + + % Add penalty to strain rate eq + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + for m = 1:dim + closure = closure - eGamma{i,j}*( (H*ETA)\(C{i,j,k,l}*e*H_gamma*dir{l}*dir{m}*n{k}*e'*eU{m}') ); + end + penalty = penalty + eGamma{i,j}*( (H*ETA)\(C{i,j,k,l}*e*H_gamma*dir{l}*n{k}) ); + end + end + end + end + + end + + end + + % type Struct that specifies the interface coupling. + % Fields: + % -- tuning: penalty strength, defaults to 1.0 + % -- interpolation: type of interpolation, default 'none' + function [closure, penalty, forcingPenalties] = interface(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + defaultType.tuning = 1.0; + defaultType.interpolation = 'none'; + defaultType.type = 'standard'; + default_struct('type', defaultType); + + forcingPenalties = []; + + switch type.type + case 'standard' + [closure, penalty] = obj.interfaceStandard(boundary,neighbour_scheme,neighbour_boundary,type); + case 'frictionalFault' + [closure, penalty] = obj.interfaceFrictionalFault(boundary,neighbour_scheme,neighbour_boundary,type); + case 'normalTangential' + [closure, penalty, forcingPenalties] = obj.interfaceNormalTangential(boundary,neighbour_scheme,neighbour_boundary,type); + end + + end + + function [closure, penalty] = interfaceStandard(obj,boundary,neighbour_scheme,neighbour_boundary,type) + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + u = obj; + v = neighbour_scheme; + + dim = obj.dim; + + n = u.getNormal(boundary); + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + e = u.getBoundaryOperatorForScalarField('e', boundary); + + ev = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + + H = u.H; + RHO = u.RHO; + ETA = u.ETA; + C = u.C; + Eu = u.Eu; + eU = u.eU; + eGamma = u.eGamma; + + CV = v.C; + Ev = v.Eu; + eV = v.eU; + eGammaV = v.eGamma; + nV = v.getNormal(neighbour_boundary); + + + % Get elastic closure and penalty + [closure, penalty] = obj.elasticObj.interface(boundary, v.elasticObj, neighbour_boundary, type); + closure = Eu*closure*Eu'; + penalty = Eu*penalty*Ev'; + + % Add viscous part of traction coupling + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + closure = closure + 1/2*eU{j}*( (RHO*H)\(C{i,j,k,l}*e*H_gamma*n{i}*e'*eGamma{k,l}') ); + penalty = penalty + 1/2*eU{j}*( (RHO*H)\(e*H_gamma*nV{i}*(ev'*CV{i,j,k,l}*ev)*ev'*eGammaV{k,l}') ); + end + end + end + end + + % Add penalty to strain rate eq + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + closure = closure - 1/2*eGamma{i,j}*( (H*ETA)\(C{i,j,k,l}*e*H_gamma*n{k}*e'*eU{l}') ); + penalty = penalty + 1/2*eGamma{i,j}*( (H*ETA)\(C{i,j,k,l}*e*H_gamma*n{k}*ev'*eV{l}') ); + end + end + end + end + + + end + + function [closure, penalty] = interfaceFrictionalFault(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + u = obj; + v = neighbour_scheme; + + dim = obj.dim; + + n = u.getNormal(boundary); + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + e = u.getBoundaryOperatorForScalarField('e', boundary); + + ev = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + + H = u.H; + RHO = u.RHO; + ETA = u.ETA; + C = u.C; + Eu = u.Eu; + eU = u.eU; + eGamma = u.eGamma; + Egamma = u.Egamma; + + CV = v.C; + Ev = v.Eu; + eV = v.eU; + eGammaV = v.eGamma; + nV = v.getNormal(neighbour_boundary); + + % Reduce stiffness tensors to boundary size + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C{i,j,k,l} = e'*C{i,j,k,l}*e; + CV{i,j,k,l} = ev'*CV{i,j,k,l}*ev; + end + end + end + end + + % Get elastic closure and penalty + [closure, penalty] = obj.elasticObj.interface(boundary, v.elasticObj, neighbour_boundary, type); + closure = Eu*closure*Eu'; + penalty = Eu*penalty*Ev'; + + % ---- Tangential tractions are imposed just like traction BC ------ + % We only need the viscous part + closure_tangential = obj.boundary_condition(boundary, {'t', 't'}); + closure = closure + closure_tangential*Egamma*Egamma'; + + + % ------ Coupling of normal component ----------- + % Add viscous part of traction coupling + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + for m = 1:dim + closure = closure + 1/2*eU{m}*( (RHO*H)\(e*n{m}*H_gamma*n{j}*n{i}*C{i,j,k,l}*e'*eGamma{k,l}') ); + penalty = penalty - 1/2*eU{m}*( (RHO*H)\(e*n{m}*H_gamma*nV{j}*nV{i}*CV{i,j,k,l}*ev'*eGammaV{k,l}') ); + end + end + end + end + end + + % Add penalty to strain rate eq + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + for m = 1:dim + closure = closure - 1/2*eGamma{i,j}*( (H*ETA)\(e*n{l}*n{k}*C{i,j,k,l}*H_gamma*n{m}*e'*eU{m}') ); + penalty = penalty - 1/2*eGamma{i,j}*( (H*ETA)\(e*n{l}*n{k}*C{i,j,k,l}*H_gamma*nV{m}*ev'*eV{m}') ); + end + end + end + end + end + %------------------------------------------------- + + end + + function [closure, penalty, forcingPenalties] = interfaceNormalTangential(obj,boundary,neighbour_scheme,neighbour_boundary,type) + tuning = type.tuning; + + % u denotes the solution in the own domain + % v denotes the solution in the neighbour domain + u = obj; + v = neighbour_scheme; + + dim = obj.dim; + + n = u.getNormal(boundary); + t = u.getTangent(boundary); + H_gamma = u.getBoundaryQuadratureForScalarField(boundary); + e = u.getBoundaryOperatorForScalarField('e', boundary); + + ev = v.getBoundaryOperatorForScalarField('e', neighbour_boundary); + + H = u.H; + RHO = u.RHO; + ETA = u.ETA; + C = u.C; + Eu = u.Eu; + eU = u.eU; + eGamma = u.eGamma; + Egamma = u.Egamma; + + CV = v.C; + Ev = v.Eu; + eV = v.eU; + eGammaV = v.eGamma; + nV = v.getNormal(neighbour_boundary); + tV = v.getTangent(neighbour_boundary); + + % Reduce stiffness tensors to boundary size + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + C{i,j,k,l} = e'*C{i,j,k,l}*e; + CV{i,j,k,l} = ev'*CV{i,j,k,l}*ev; + end + end + end + end + + % Get elastic closure and penalty + [closure, penalty, forcingPenalties] = obj.elasticObj.interface(boundary, v.elasticObj, neighbour_boundary, type); + closure = Eu*closure*Eu'; + penalty = Eu*penalty*Ev'; + + for i = 1:numel(forcingPenalties) + forcingPenalties{i} = Eu*forcingPenalties{i}; + end + forcing_u_n = forcingPenalties{1}; + forcing_u_t = forcingPenalties{2}; + + % ------ Traction coupling, viscous part ----------- + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + for m = 1:dim + % Normal component + closure = closure + 1/2*eU{m}*( (RHO*H)\(e*n{m}*H_gamma*n{j}*n{i}*C{i,j,k,l}*e'*eGamma{k,l}') ); + penalty = penalty - 1/2*eU{m}*( (RHO*H)\(e*n{m}*H_gamma*nV{j}*nV{i}*CV{i,j,k,l}*ev'*eGammaV{k,l}') ); + + % Tangential component + closure = closure + 1/2*eU{m}*( (RHO*H)\(e*t{m}*H_gamma*t{j}*n{i}*C{i,j,k,l}*e'*eGamma{k,l}') ); + penalty = penalty - 1/2*eU{m}*( (RHO*H)\(e*t{m}*H_gamma*tV{j}*nV{i}*CV{i,j,k,l}*ev'*eGammaV{k,l}') ); + end + end + end + end + end + %------------------------------------------------- + + % --- Displacement coupling ---------------------- + % Add penalty to strain rate eq + for i = 1:dim + for j = 1:dim + for k = 1:dim + for l = 1:dim + for m = 1:dim + % Normal component + closure = closure - 1/2*eGamma{i,j}*( (H*ETA)\(e*n{l}*n{k}*C{i,j,k,l}*H_gamma*n{m}*e'*eU{m}') ); + penalty = penalty - 1/2*eGamma{i,j}*( (H*ETA)\(e*n{l}*n{k}*C{i,j,k,l}*H_gamma*nV{m}*ev'*eV{m}') ); + + + % Tangential component + closure = closure - 1/2*eGamma{i,j}*( (H*ETA)\(e*t{l}*n{k}*C{i,j,k,l}*H_gamma*t{m}*e'*eU{m}') ); + penalty = penalty - 1/2*eGamma{i,j}*( (H*ETA)\(e*t{l}*n{k}*C{i,j,k,l}*H_gamma*tV{m}*ev'*eV{m}') ); + end + forcing_u_n = forcing_u_n + 1/2*eGamma{i,j}*( (H*ETA)\(e*n{l}*n{k}*C{i,j,k,l}*H_gamma) ); + forcing_u_t = forcing_u_t + 1/2*eGamma{i,j}*( (H*ETA)\(e*t{l}*n{k}*C{i,j,k,l}*H_gamma) ); + end + end + end + end + %------------------------------------------------- + + forcingPenalties{1} = forcing_u_n; + forcingPenalties{2} = forcing_u_t; + + end + + % Returns the outward unit normal vector for the boundary specified by the string boundary. + % n is a cell of diagonal matrices for each normal component, n{1} = n_1, n{2} = n_2. + function n = getNormal(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + n = obj.(['n_' boundary]); + end + + % Returns the unit tangent vector for the boundary specified by the string boundary. + % t is a cell of diagonal matrices for each normal component, t{1} = t_1, t{2} = t_2. + function t = getTangent(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + t = obj.(['tangent_' boundary]); + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperator(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e', 'e1', 'e2', 'tau', 'tau1', 'tau2', 'en', 'et', 'tau_n', 'tau_t'}) + + o = obj.([op, '_', boundary]); + + end + + % Returns the boundary operator op for the boundary specified by the string boundary. + % op -- string + function o = getBoundaryOperatorForScalarField(obj, op, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + assertIsMember(op, {'e'}) + + switch op + + case 'e' + o = obj.(['e_scalar', '_', boundary]); + end + + end + + % Returns the boundary operator T_ij (cell format) for the boundary specified by the string boundary. + % Formula: tau_i = T_ij u_j + % op -- string + function T = getBoundaryTractionOperator(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + T = obj.(['T', '_', boundary]); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary unknowns + % + % boundary -- string + function H = getBoundaryQuadrature(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H = obj.getBoundaryQuadratureForScalarField(boundary); + I_dim = speye(obj.dim, obj.dim); + H = kron(H, I_dim); + end + + % Returns square boundary quadrature matrix, of dimension + % corresponding to the number of boundary grid points + % + % boundary -- string + function H_b = getBoundaryQuadratureForScalarField(obj, boundary) + assertIsMember(boundary, {'w', 'e', 's', 'n'}) + + H_b = obj.(['H_', boundary]); + end + + function N = size(obj) + N = (obj.dim + obj.dim^2)*prod(obj.m); + end + end +end
diff -r 855871e0b852 -r 60c875c18de3 +scheme/bcSetupStaggered.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+scheme/bcSetupStaggered.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,20 @@ +% Takes a diffOp and a cell array of boundary condition definitions. +% Each bc is a struct with the fields +% * type -- Type of boundary condition +% * boundary -- Boundary identifier +% * data -- A function_handle for a function which provides boundary data.(see below) +% Also takes S_sign which modifies the sign of the penalty function, [-1,1] +% Returns a closure matrix and a forcing function S. +% +% The boundary data function can either be a function of time or a function of time and space coordinates. +% In the case where it only depends on time it should return the data as grid function for the boundary. +% In the case where it also takes space coordinates the number of space coordinates should match the number of dimensions of the problem domain. +% For example in the 2D case: f(t,x,y). +function [closure, S] = bcSetupStaggered(diffOp, bcs, S_sign) + default_arg('S_sign', 1); + assertType(bcs, 'cell'); + assert(S_sign == 1 || S_sign == -1, 'S_sign must be either 1 or -1'); + + [closure, penalties] = scheme.bc.closureSetup(diffOp, bcs); + S = scheme.bc.forcingSetupStaggered(diffOp, penalties, bcs, S_sign); +end
diff -r 855871e0b852 -r 60c875c18de3 +time/+rkparameters/rk4.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+time/+rkparameters/rk4.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,12 @@ +function [a,b,c,s] = rk4() + +% Butcher tableau for classical RK$ +s = 4; +a = sparse(s,s); +a(2,1) = 1/2; +a(3,2) = 1/2; +a(4,3) = 1; +b = 1/6*[1; 2; 2; 1]; +c = [0; 1/2; 1/2; 1]; + +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +time/ExplicitRungeKuttaDiscreteData.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+time/ExplicitRungeKuttaDiscreteData.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,121 @@ +classdef ExplicitRungeKuttaDiscreteData < time.Timestepper + properties + D + S % Function handle for time-dependent data + data % Matrix of data vectors, one column per stage + k + t + v + m + n + order + a, b, c, s % Butcher tableau + K % Stage rates + U % Stage approximations + T % Stage times + end + + + methods + function obj = ExplicitRungeKuttaDiscreteData(D, S, data, k, t0, v0, order) + default_arg('order', 4); + default_arg('S', []); + default_arg('data', []); + + obj.D = D; + obj.S = S; + obj.k = k; + obj.t = t0; + obj.v = v0; + obj.m = length(v0); + obj.n = 0; + obj.order = order; + obj.data = data; + + switch order + case 4 + [obj.a, obj.b, obj.c, obj.s] = time.rkparameters.rk4(); + otherwise + error('That RK method is not available'); + end + + obj.K = zeros(obj.m, obj.s); + obj.U = zeros(obj.m, obj.s); + + end + + function [v,t,U,T,K] = getV(obj) + v = obj.v; + t = obj.t; + U = obj.U; % Stage approximations in previous time step. + T = obj.T; % Stage times in previous time step. + K = obj.K; % Stage rates in previous time step. + end + + function [a,b,c,s] = getTableau(obj) + a = obj.a; + b = obj.b; + c = obj.c; + s = obj.s; + end + + % Returns quadrature weights for stages in one time step + function quadWeights = getTimeStepQuadrature(obj) + [~, b] = obj.getTableau(); + quadWeights = obj.k*b; + end + + function obj = step(obj) + v = obj.v; + a = obj.a; + b = obj.b; + c = obj.c; + s = obj.s; + S = obj.S; + dt = obj.k; + K = obj.K; + U = obj.U; + D = obj.D; + data = obj.data; + + for i = 1:s + U(:,i) = v; + for j = 1:i-1 + U(:,i) = U(:,i) + dt*a(i,j)*K(:,j); + end + + K(:,i) = D*U(:,i); + obj.T(i) = obj.t + c(i)*dt; + + % Data from continuous function and discrete time-points. + if ~isempty(S) + K(:,i) = K(:,i) + S(obj.T(i)); + end + if ~isempty(data) + K(:,i) = K(:,i) + data(:,obj.n*s + i); + end + + end + + obj.v = v + dt*K*b; + obj.t = obj.t + dt; + obj.n = obj.n + 1; + obj.U = U; + obj.K = K; + end + end + + + methods (Static) + function k = getTimeStep(lambda, order) + default_arg('order', 4); + switch order + case 4 + k = time.rk4.get_rk4_time_step(lambda); + otherwise + error('Time-step function not available for this order'); + end + end + end + +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 +time/ExplicitRungeKuttaSecondOrderDiscreteData.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/+time/ExplicitRungeKuttaSecondOrderDiscreteData.m Thu Mar 10 16:54:26 2022 +0100 @@ -0,0 +1,129 @@ +classdef ExplicitRungeKuttaSecondOrderDiscreteData < time.Timestepper + properties + k + t + w + m + D + E + M + C_cont % Continuous part (function handle) of forcing on first order form. + C_discr% Discrete part (matrix) of forcing on first order form. + n + order + tsImplementation % Time stepper object, RK first order form, + % which this wraps around. + end + + + methods + % Solves u_tt = Du + Eu_t + S by + % Rewriting on first order form: + % w_t = M*w + C(t) + % where + % M = [ + % 0, I; + % D, E; + % ] + % and + % C(t) = [ + % 0; + % S(t) + % ] + % D, E, should be matrices (or empty for zero) + % They can also be omitted by setting them equal to the empty matrix. + % S = S_cont + S_discr, where S_cont is a function handle + % S_discr a matrix of data vectors, one column per stage. + function obj = ExplicitRungeKuttaSecondOrderDiscreteData(D, E, S_cont, S_discr, k, t0, v0, v0t, order) + default_arg('order', 4); + default_arg('S_cont', []); + default_arg('S_discr', []); + obj.D = D; + obj.E = E; + obj.m = length(v0); + obj.n = 0; + + default_arg('D', sparse(obj.m, obj.m) ); + default_arg('E', sparse(obj.m, obj.m) ); + + obj.k = k; + obj.t = t0; + obj.w = [v0; v0t]; + + I = speye(obj.m); + O = sparse(obj.m,obj.m); + + obj.M = [ + O, I; + D, E; + ]; + + % Build C_cont + if ~isempty(S_cont) + obj.C_cont = @(t)[ + sparse(obj.m,1); + S_cont(t) + ]; + else + obj.C_cont = []; + end + + % Build C_discr + if ~isempty(S_discr) + [~, nt] = size(S_discr); + obj.C_discr = [sparse(obj.m, nt); + S_discr + ]; + else + obj.C_discr = []; + end + obj.tsImplementation = time.ExplicitRungeKuttaDiscreteData(obj.M, obj.C_cont, obj.C_discr,... + k, obj.t, obj.w, order); + end + + function [v,t,U,T,K] = getV(obj) + [w,t,U,T,K] = obj.tsImplementation.getV(); + + v = w(1:end/2); + U = U(1:end/2, :); % Stage approximations in previous time step. + K = K(1:end/2, :); % Stage rates in previous time step. + % T: Stage times in previous time step. + end + + function [vt,t,U,T,K] = getVt(obj) + [w,t,U,T,K] = obj.tsImplementation.getV(); + + vt = w(end/2+1:end); + U = U(end/2+1:end, :); % Stage approximations in previous time step. + K = K(end/2+1:end, :); % Stage rates in previous time step. + % T: Stage times in previous time step. + end + + function [a,b,c,s] = getTableau(obj) + [a,b,c,s] = obj.tsImplementation.getTableau(); + end + + % Returns quadrature weights for stages in one time step + function quadWeights = getTimeStepQuadrature(obj) + [~, b] = obj.getTableau(); + quadWeights = obj.k*b; + end + + % Use RK for first order form to step + function obj = step(obj) + obj.tsImplementation.step(); + [v, t] = obj.tsImplementation.getV(); + obj.w = v; + obj.t = t; + obj.n = obj.n + 1; + end + end + + methods (Static) + function k = getTimeStep(lambda, order) + default_arg('order', 4); + k = obj.tsImplementation.getTimeStep(lambda, order); + end + end + +end \ No newline at end of file
diff -r 855871e0b852 -r 60c875c18de3 .hgtags --- a/.hgtags Thu Feb 17 18:55:11 2022 +0100 +++ b/.hgtags Thu Mar 10 16:54:26 2022 +0100 @@ -2,3 +2,4 @@ 0776fa4754ff0c1918f6e1278c66f48c62d05736 grids0.1 b723495cdb2f96314d7b3f0aa79723a7dc088c7d v0.2 08f3ffe63f484d02abce8df4df61e826f568193f elastic1.0 +08f3ffe63f484d02abce8df4df61e826f568193f Heimisson2018