annotate +rv/ResidualViscosity.m @ 1012:1e437c9e5132 feature/advectionRV

Create residual viscosity package +rv and generalize the ResidualViscosity class - Generalize residual viscosity, by passing user-defined flux and calculating the time derivative outside of the update. - Create separate RungekuttaRV specifically using interior RV updates - Separate the artifical dissipation operator from the scheme AdvectionRV1D so that the same scheme can be reused for creating the diff op used by the ResidualViscosity class
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Wed, 05 Dec 2018 13:44:10 +0100
parents
children e547794a9407
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1012
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
1 % class describing the viscosity
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
2 classdef ResidualViscosity < handle
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
3 properties
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
4 D % Diff op approximating the gradient of the flux f(u)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
5 waveSpeed % Wave speed at each grid point, e.g f'(u). %TBD: Better naming?
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
6 Cmax % Constant controling magnitude of upwind dissipation
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
7 Cres % Constant controling magnitude residual dissipation
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
8 h % Length scale used for scaling the viscosity.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
9 viscosity % Stores the computed viscosity.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
10
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
11 % Convenice (for verification and plotting) TBD: Decide on if it should be kept.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
12 u_t % Stores the latest approximated time derivative of the solution.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
13 grad_f % Stores the latest approximated gradient of the flux
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
14 residual % Stores the computed residual
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
15 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
16
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
17 methods
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
18 % TODO: - Consider passing residual normalization as a function handle.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
19 % or choosing a type of normalization on construction.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
20 % Could for example be 1, norm((v-mean(v),inf) or normInfNeighborhood(v)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
21 % but working
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
22 % - Decide on how to treat waveSpeed. It would be nice to just pass a constant value without
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
23 % wrapping it in a function.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
24 function obj = ResidualViscosity(D, waveSpeed, Cmax, Cres, h, N)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
25 obj.D = D;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
26 obj.waveSpeed = waveSpeed;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
27 obj.h = h;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
28 obj.Cmax = Cmax;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
29 obj.Cres = Cres;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
30 obj.viscosity = zeros(N,1);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
31 obj.u_t = zeros(N,1);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
32 obj.grad_f = zeros(N,1);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
33 obj.residual = zeros(N,1);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
34 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
35
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
36 function obj = update(obj, v, dvdt)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
37 obj.u_t = dvdt;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
38 obj.grad_f = obj.D(v);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
39 obj.residual = obj.u_t + obj.grad_f;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
40 obj.viscosity = min(obj.Cmax*obj.h*abs(obj.waveSpeed(v)), obj.Cres*obj.h^2*abs(obj.residual)/norm(v-mean(v),inf));
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
41 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
42
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
43 function [residual, u_t, grad_f] = getResidual(obj)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
44 residual = obj.residual;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
45 u_t = obj.u_t;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
46 grad_f = obj.grad_f;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
47 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
48
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
49 function viscosity = getViscosity(obj)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
50 viscosity = obj.viscosity;
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
51 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
52 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
53 % Remove or fix. Should be able to handle values close to zero. Should work in 2d and 3d.
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
54 methods (Static)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
55 function R_norm = normInfNeighborhood(v)
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
56 n = length(v);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
57 R_norm = zeros(n,1);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
58 R_norm(1,1) = norm(v(1:3), inf);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
59 R_norm(n,1) = norm(v(n-3:n), inf);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
60 for i = 2:n-1
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
61 R_norm(i,1) = norm(v(i-1:i+1), inf);
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
62 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
63 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
64 end
1e437c9e5132 Create residual viscosity package +rv and generalize the ResidualViscosity class
Vidar Stiernström <vidar.stiernstrom@it.uu.se>
parents:
diff changeset
65 end