Commit fa2630c0dc54d6ec9e202015ca7a22194bf2db0f

Authored by Jean-Luc Shaw
1 parent cea4e2a9
Exists in master

Added a current extrapolation routine currextrap.m which uses the genetic optimi…

…sation routine genopt.m . Added a wrapper function for gradient.m that calculates horizontal current shear given a vector, matrix of 3d array because I am just that lazy.
Showing 3 changed files with 134 additions and 5 deletions   Show diff stats
math/genopt.m
1   -function MOM = genopt(Q,X,dom,NP,NI,pc,pm)
2   -% Synthax: MOM = genopt(@Q,@X,dom,NP,NI,pc,pm)
  1 +function [A,HS] = genopt(Q,C,dom,NP,NI,pc,pm)
  2 +% Synthax: [A,HS] = genopt(@Q,@X,dom,NP,NI,pc,pm)
3 3 %
4 4 % Optimisation routine following a genetic algorithm. Returns the best parameter
5 5 % set which solves the problem expressed by the anonymous function 'Q', when
... ... @@ -33,12 +33,11 @@ for kk = 1:NI
33 33 PP = PP(RK,:) ;
34 34  
35 35 %TEST
36   - if X(HS(1)) ; break; end
  36 + if HS(1) < C ; break; end
37 37  
38 38 %SELECTION
39   - DICE = randi([2 4]) ;
40 39 MOM = PP(1, :) ;
41   - DAD = PP(DICE,:) ;
  40 + DAD = PP(randi([2 4]),:) ;
42 41  
43 42 %REPRODUCTION
44 43 PP(1,:) = MOM ;
... ... @@ -62,5 +61,9 @@ for kk = 1:NI
62 61 end
63 62 end
64 63 end
  64 +disp(['Last iteration: ' num2str(kk)]) ;
65 65  
  66 +% OUTPUT
  67 +A = MOM ;
  68 +HS = HS(1) ;
66 69 end
... ...
physics/currextrap.m 0 → 100644
... ... @@ -0,0 +1,95 @@
  1 +function [ue,F] = currextrap(u,N2,p,z,varargin)
  2 +% Synthax: [ue,F] = currextrap(u,N2,p,z,varargin)
  3 +%
  4 +% Uses the dynmodes routine and genetic optimisation to fit the first
  5 +% 4 vertical modes on current profile 'u', where 'N2' is the buoyancy
  6 +% frequency, 'p' is the pressure, and 'z' is the depth (positive) of
  7 +% the associated current profile.
  8 +%
  9 +% Output 'ue' is the current profile fit provided and 'F' is a 5
  10 +% element vector representing the weights of the 4 first modes plus
  11 +% the speed offset. This is the solution set of the genetic optimising
  12 +% routine.
  13 +%
  14 +% Optional arguments should be supplied as parameter/value pairs.
  15 +% Supported options are:
  16 +%
  17 +% - section,[string] Give higher importance to fitting the
  18 +% surface 'top', bottom 'bottom', or the
  19 +% entire velocity profile 'all'. In
  20 +% effect this applies a weighing function
  21 +% which decreases as zc/z, increases as
  22 +% z/zc, where zc is a constant,
  23 +% or is flat with respect to depth
  24 +% to the compute the scoring function.
  25 +%
  26 +% - zc,[float] Depth where the weighing function becomes
  27 +% unity. Defaults to 1.
  28 +%
  29 +% - itermax,[int] Set the maximum number of iterations
  30 +% of the genetic algorithm. Defaults to
  31 +% 500.
  32 +%
  33 +% - popsize,[int] Set the population size for the genetic
  34 +% algorithm. Defaults to 20.
  35 +%
  36 +% - nsol,[int] Number of solution sets computed and
  37 +% from the genetic algorithm. Only the
  38 +% highest scoring solution is returned
  39 +% Defaults to 1.
  40 +% Dependecies:
  41 +% - dynmodes.m : https://github.com/sea-mat/dynmodes
  42 +% - genopt.m : https://github.com/jeanlucshaw/matlab_stuff/blob/master/math/genopt.m
  43 +
  44 +
  45 +%OPTIONS
  46 +
  47 +%default
  48 +zc = 1 ;
  49 +NP = 20 ; % Genetic algo population size
  50 +NI = 500 ; % Genetic algo max iterations
  51 +NS = 1 ; % Genetic algo number of solutions averaged
  52 +
  53 +%user
  54 +for ii = 1:2:nargin-5
  55 + switch varargin{ii}
  56 + case 'section'
  57 + switch varargin{ii+1}
  58 + case 'top'
  59 + W = zc/z;
  60 + case 'bottom'
  61 + W = z/zc;
  62 + case 'all'
  63 + W = ones(size(z));
  64 + end
  65 + case 'itermax'
  66 + NI = varargin{ii+1} ;
  67 + case 'popsize'
  68 + NP = varargin{ii+1} ;
  69 + case 'nsol'
  70 + NS = varargin{ii+1} ;
  71 + case 'zc'
  72 + zc = varargin{ii+1} ;
  73 + end
  74 +end
  75 +
  76 +I = find(isfinite(N2)) ;
  77 +[wmodes,pmodes,ce]=dynmodes(N2(I)',p(I)',1) ;
  78 +
  79 +%REGRID
  80 +pm = interp1(1:0.5:1+0.5*numel(I),pmodes,z,'linear','extrap') ;
  81 +
  82 +%FIT
  83 +I = isfinite(u) ;
  84 +Q = @(a) sum( W(I).*sqrt( (a(1)*pm(I,1)+a(2)*pm(I,2)+a(3)*pm(I,3)+a(4)*pm(I,4)+a(5) - u(I)).^2 ) ) ;
  85 +A = nan([NS,5]) ;
  86 +HS = nan([NS,1]) ;
  87 +for ii = 1:NS
  88 + [A(ii,:),HS(ii)] = genopt(Q,0.01,[-0.2 0.2;-0.2 0.2;-0.2 0.2;-0.2 0.2;-2 2],NP,NI,0.8,0.2) ;
  89 +end
  90 +
  91 +%OUTPUT
  92 +[~,I] = min(HS) ;
  93 +F = A(I,:) ;
  94 +ue = F(1)*pm(:,1)+F(2)*pm(:,2)+F(3)*pm(:,3)+F(4)*pm(:,4)+F(5) ;
  95 +end
... ...
physics/uvz2S.m 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +function S = uvz2S(u,v,z)
  2 +% Synthax: S = uvz2S(u,v,z)
  3 +%
  4 +% Wrapper for the gradient function which takes as input
  5 +% horizontal velocities 'u' and 'v' as well as their
  6 +% corresponding depths 'z'. Note that 'u','v', and 'z'
  7 +% are all assumed to be of same size.
  8 +%
  9 +% Returns the horizontal shear:
  10 +%
  11 +% S = sqrt( (du/dz)^2 + (dv/dz)^2 )
  12 +%
  13 +D = ndims(u) ;
  14 +M = nan([1 D]) ;
  15 +M = size(u) ;
  16 +I = find( M > 1 ) ;
  17 +switch numel(I) ;
  18 + case 1
  19 + [dudz] = gradient(u,z) ;
  20 + [dvdz] = gradient(v,z) ;
  21 + case 2
  22 + [~,dudz] = gradient(u,1,z) ;
  23 + [~,dvdz] = gradient(v,1,z) ;
  24 + case 3
  25 + [~,dudz,~] = gradient(u,1,z,1) ;
  26 + [~,dvdz,~] = gradient(v,1,z,1) ;
  27 +end
  28 +
  29 +S = sqrt( dudz.^2 + dvdz.^2 ) ;
  30 +
  31 +end
... ...