Skip to content

Commit 0e5a73d

Browse files
authoredJan 24, 2023
Add files via upload
1 parent f759527 commit 0e5a73d

4 files changed

+344
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
3+
%%% This script fit Surrogate Models and Generate results (River Surface water lavels, SWL) from the simulated boundary conditions
4+
5+
%% 1. read the HEC-RAS simulated data
6+
%% 2. read the simulated boundary conditions
7+
%% 3. Fit the models and predict the Surface Water Level
8+
9+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10+
11+
%% Load HEC-RAS simulated data (SWL, Q, and WL) %% This data will be used for model fitting
12+
13+
%%Load MDA selected dischrage and sea water level and HEC-RAS simulated river water level
14+
load('C:\Potomac_conditions_all_ft.mat');
15+
Inp = [wl_surrogate,Q_surrogate];
16+
17+
%%% HEC-RAS simulations
18+
load('C:\Data\PotomacRiver\Potomac_sim.mat');
19+
20+
21+
cd('C:\Data\PotomacRiver\synthetic_simulations\Simulation data');
22+
23+
FF=dir('*.txt');
24+
25+
%%
26+
for r=1:length(FF);
27+
AA=importdata(strcat(FF(r).folder,'\',FF(r).name));
28+
A=AA.data;
29+
30+
WLs = A(:,6);
31+
Diss = A(:,5);
32+
33+
Inp2 = [WLs,Diss];
34+
35+
%%
36+
for i = 1:length(STAT)
37+
disp(strcat('Analysis performing for Simultion' ,num2str(r),' XXXXXXXXX', ' Transect No:', num2str(i)));
38+
WL = WS_ELEV(i,:);
39+
Res = WL';
40+
41+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42+
% MLRM
43+
mdl = fitlm(Inp,Res);
44+
[m] = predict(mdl,Inp2);
45+
preds_tmp(:,i) = m;
46+
%%
47+
%%%%%%%%%% Radial Basis Function %%%%%
48+
ZI = rbfinterp([Inp2(:,1)'; Inp2(:,2)'], rbfcreate([Inp(:,1)'; Inp(:,2)'], Res','RBFFunction', 'linear', 'RBFConstant', 0.01,'RBFSmooth', 10));
49+
% reshape
50+
ZI = reshape(ZI, size(Inp2(:,1)));
51+
preds_RBF_tmp(:,i) = ZI;
52+
53+
%%%%%%%%%%%%%RADIAN BASIS FUNCT %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
54+
55+
%%
56+
%%%%%%%%%%%%%%%%% Scatter Interpolant %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57+
F = scatteredInterpolant(Inp(:,1),Inp(:,2),Res);
58+
F.Method = 'natural';% methods
59+
WSE_I = F(Inp2(:,1),Inp2(:,2));
60+
preds_SI_tmp(:,i) = WSE_I;
61+
%%%%%%%%%%%%%%%%% Scatter Interpolant %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62+
63+
end;
64+
65+
preds_MLR(:,:,r)=preds_tmp;
66+
preds_RBF(:,:,r)=preds_RBF_tmp;
67+
preds_SI(:,:,r)=preds_SI_tmp;
68+
69+
Q_simu(:,r)=Inp2(:,2);
70+
SWL_simu(:,r)=Inp2(:,1);
71+
72+
end;
73+
74+
%%%Save the data
75+
cd('C:\Surroate Model_Results');
76+
77+
save('surrogate_model_results_potomacRiver.mat','preds_MLR','preds_RBF','preds_SI','Q_simu','SWL_simu','-v7.3,'-nocompression');
78+
79+
80+
%%%%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%%%%%%%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
81+
%%XXXXXXXXXXXXX END END XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
82+
%%%%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX%%%%%%%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+
97+

‎rbfcheck.m

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxdiff = rbfcheck(options)
2+
3+
tic;
4+
5+
nodes = options.('x');
6+
y = options.('y');
7+
8+
s = rbfinterp(nodes, options);
9+
10+
fprintf('RBF Check\n');
11+
fprintf('max|y - yi| = %e \n', max(abs(s-y)) );
12+
13+
if (strcmp(options.('Stats'),'on'))
14+
fprintf('%d points were checked in %e sec\n', length(y), toc);
15+
end;
16+
fprintf('\n');

‎rbfcreate.m

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
function options = rbfcreate(x, y, varargin)
2+
%RBFCREATE Creates an RBF interpolation
3+
% OPTIONS = RBFSET(X, Y, 'NAME1',VALUE1,'NAME2',VALUE2,...) creates an
4+
% radial base function interpolation
5+
%
6+
% RBFCREATE with no input arguments displays all property names and their
7+
% possible values.
8+
%
9+
%RBFCREATE PROPERTIES
10+
%
11+
12+
%
13+
% Alex Chirokov, alex.chirokov@gmail.com
14+
% 16 Feb 2006
15+
tic;
16+
% Print out possible values of properties.
17+
if (nargin == 0) & (nargout == 0)
18+
fprintf(' x: [ dim by n matrix of coordinates for the nodes ]\n');
19+
fprintf(' y: [ 1 by n vector of values at nodes ]\n');
20+
fprintf(' RBFFunction: [ gaussian | thinplate | cubic | multiquadrics | {linear} ]\n');
21+
fprintf(' RBFConstant: [ positive scalar ]\n');
22+
fprintf(' RBFSmooth: [ positive scalar {0} ]\n');
23+
fprintf(' Stats: [ on | {off} ]\n');
24+
fprintf('\n');
25+
return;
26+
end
27+
Names = [
28+
'RBFFunction '
29+
'RBFConstant '
30+
'RBFSmooth '
31+
'Stats '
32+
];
33+
[m,n] = size(Names);
34+
names = lower(Names);
35+
36+
options = [];
37+
for j = 1:m
38+
options.(deblank(Names(j,:))) = [];
39+
end
40+
41+
%**************************************************************************
42+
%Check input arrays
43+
%**************************************************************************
44+
[nXDim nXCount]=size(x);
45+
[nYDim nYCount]=size(y);
46+
47+
if (nXCount~=nYCount)
48+
error(sprintf('x and y should have the same number of rows'));
49+
end;
50+
51+
if (nYDim~=1)
52+
error(sprintf('y should be n by 1 vector'));
53+
end;
54+
55+
options.('x') = x;
56+
options.('y') = y;
57+
%**************************************************************************
58+
%Default values
59+
%**************************************************************************
60+
options.('RBFFunction') = 'linear';
61+
options.('RBFConstant') = (prod(max(x')-min(x'))/nXCount)^(1/nXDim); %approx. average distance between the nodes
62+
options.('RBFSmooth') = 0;
63+
options.('Stats') = 'off';
64+
65+
%**************************************************************************
66+
% Argument parsing code: similar to ODESET.m
67+
%**************************************************************************
68+
69+
i = 1;
70+
% A finite state machine to parse name-value pairs.
71+
if rem(nargin-2,2) ~= 0
72+
error('Arguments must occur in name-value pairs.');
73+
end
74+
expectval = 0; % start expecting a name, not a value
75+
while i <= nargin-2
76+
arg = varargin{i};
77+
78+
if ~expectval
79+
if ~isstr(arg)
80+
error(sprintf('Expected argument %d to be a string property name.', i));
81+
end
82+
83+
lowArg = lower(arg);
84+
j = strmatch(lowArg,names);
85+
if isempty(j) % if no matches
86+
error(sprintf('Unrecognized property name ''%s''.', arg));
87+
elseif length(j) > 1 % if more than one match
88+
% Check for any exact matches (in case any names are subsets of others)
89+
k = strmatch(lowArg,names,'exact');
90+
if length(k) == 1
91+
j = k;
92+
else
93+
msg = sprintf('Ambiguous property name ''%s'' ', arg);
94+
msg = [msg '(' deblank(Names(j(1),:))];
95+
for k = j(2:length(j))'
96+
msg = [msg ', ' deblank(Names(k,:))];
97+
end
98+
msg = sprintf('%s).', msg);
99+
error(msg);
100+
end
101+
end
102+
expectval = 1; % we expect a value next
103+
104+
else
105+
options.(deblank(Names(j,:))) = arg;
106+
expectval = 0;
107+
end
108+
i = i + 1;
109+
end
110+
111+
if expectval
112+
error(sprintf('Expected value for property ''%s''.', arg));
113+
end
114+
115+
116+
%**************************************************************************
117+
% Creating RBF Interpolatin
118+
%**************************************************************************
119+
120+
switch lower(options.('RBFFunction'))
121+
case 'linear'
122+
options.('rbfphi') = @rbfphi_linear;
123+
case 'cubic'
124+
options.('rbfphi') = @rbfphi_cubic;
125+
case 'multiquadric'
126+
options.('rbfphi') = @rbfphi_multiquadrics;
127+
case 'thinplate'
128+
options.('rbfphi') = @rbfphi_thinplate;
129+
case 'gaussian'
130+
options.('rbfphi') = @rbfphi_gaussian;
131+
otherwise
132+
options.('rbfphi') = @rbfphi_linear;
133+
end
134+
135+
phi = options.('rbfphi');
136+
137+
A=rbfAssemble(x, phi, options.('RBFConstant'), options.('RBFSmooth'));
138+
139+
b=[y'; zeros(nXDim+1, 1)];
140+
141+
%inverse
142+
rbfcoeff=A\b;
143+
144+
%SVD
145+
% [U,S,V] = svd(A);
146+
%
147+
% for i=1:1:nXCount+1
148+
% if (S(i,i)>0) S(i,i)=1/S(i,i); end;
149+
% end;
150+
% rbfcoeff = V*S'*U*b;
151+
152+
153+
options.('rbfcoeff') = rbfcoeff;
154+
155+
156+
if (strcmp(options.('Stats'),'on'))
157+
fprintf('%d point RBF interpolation was created in %e sec\n', length(y), toc);
158+
fprintf('\n');
159+
end;
160+
161+
function [A]=rbfAssemble(x, phi, const, smooth)
162+
[dim n]=size(x);
163+
A=zeros(n,n);
164+
for i=1:n
165+
for j=1:i
166+
r=norm(x(:,i)-x(:,j));
167+
temp=feval(phi,r, const);
168+
A(i,j)=temp;
169+
A(j,i)=temp;
170+
end
171+
A(i,i) = A(i,i) - smooth;
172+
end
173+
% Polynomial part
174+
P=[ones(n,1) x'];
175+
A = [ A P
176+
P' zeros(dim+1,dim+1)];
177+
178+
%**************************************************************************
179+
% Radial Base Functions
180+
%**************************************************************************
181+
function u=rbfphi_linear(r, const)
182+
u=r;
183+
184+
function u=rbfphi_cubic(r, const)
185+
u=r.*r.*r;
186+
187+
function u=rbfphi_gaussian(r, const)
188+
u=exp(-0.5*r.*r/(const*const));
189+
190+
function u=rbfphi_multiquadrics(r, const)
191+
u=sqrt(1+r.*r/(const*const));
192+
193+
function u=rbfphi_thinplate(r, const)
194+
u=r.*r.*log(r+1);

‎rbfinterp.m

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function [f] = rbfinterp(x, options)
2+
tic;
3+
phi = options.('rbfphi');
4+
rbfconst = options.('RBFConstant');
5+
nodes = options.('x');
6+
rbfcoeff = (options.('rbfcoeff'))';
7+
8+
9+
[dim n] = size(nodes);
10+
[dimPoints nPoints] = size(x);
11+
12+
if (dim~=dimPoints)
13+
error(sprintf('x should have the same number of rows as an array used to create RBF interpolation'));
14+
end;
15+
16+
f = zeros(1, nPoints);
17+
r = zeros(1, n);
18+
19+
for i=1:1:nPoints
20+
s=0;
21+
r = (x(:,i)*ones(1,n)) - nodes;
22+
r = sqrt(sum(r.*r, 1));
23+
% for j=1:n
24+
% r(j) = norm(x(:,i) - nodes(:,j));
25+
% end
26+
27+
s = rbfcoeff(n+1) + sum(rbfcoeff(1:n).*feval(phi, r, rbfconst));
28+
29+
for k=1:dim
30+
s=s+rbfcoeff(k+n+1)*x(k,i); % linear part
31+
end
32+
f(i) = s;
33+
end;
34+
35+
if (strcmp(options.('Stats'),'on'))
36+
fprintf('Interpolation at %d points was computed in %e sec\n', length(f), toc);
37+
end;

0 commit comments

Comments
 (0)
Please sign in to comment.