GBR.cc 27.4 KB
Newer Older
thomas's avatar
thomas committed
1
2
3
4
5
6
/*
 * General library of functions for GBR simulation.
 *  Part 1: "Numerical & computational" functions
 *  Part 2: "Physical" functions
 */

thomas's avatar
thomas committed
7
8
#include <stdio.h>
#include "math.h"
thomas's avatar
thomas committed
9
10
11
12
13
#include "fullMatrix.h"
#include "function.h"
#include "dgGroupOfElements.h"
#include "polynomialBasis.h"
#include "dgMeshJacobian.h"
thomas's avatar
thomas committed
14
15
16
17
18
19
#include "dgConfig.h"

#ifdef HAVE_PROJ
#include <proj_api.h>
static projPJ pj_utm, pj_latlon;
#endif
thomas's avatar
thomas committed
20
21
22
23
24

static double pOx, pOy, pOz, pPhiX, pPhiY, pPhiZ, pThetaX, pThetaY, pThetaZ, R;
static bool init = false;

static void initialize() {
thomas's avatar
thomas committed
25
  // Initialise variables for OLD-type projection
thomas's avatar
thomas committed
26
27
28
29
30
31
32
33
34
35
36
37
  double phi = 146.5892/180*M_PI;
  double theta = -17.147569/180*M_PI;
  R = 6.371e6;
  pOx = cos(theta)*cos(phi)*R;
  pOy = cos(theta)*sin(phi)*R;
  pOz = sin(theta)*R;
  pPhiX = -sin(phi);
  pPhiY = cos(phi);
  pPhiZ = 0;
  pThetaX = -sin(theta)*cos(phi);
  pThetaY = -sin(theta)*sin(phi);
  pThetaZ = cos(theta);
thomas's avatar
thomas committed
38
39
40
41
//   init = true;
  
  #ifdef HAVE_PROJ
  // Initialise variables for PROJ-type projection
42
  if ( !(pj_latlon = pj_init_plus("+proj=latlong +ellps=WGS84")) )  {
thomas's avatar
thomas committed
43
44
45
    printf("Error init latLon\n");
    exit(1); }

thomas's avatar
thomas committed
46
  if ( !(pj_utm = pj_init_plus("+proj=utm +ellps=WGS84 +zone=55 +south")) ) {
thomas's avatar
thomas committed
47
48
49
50
    printf("Error init UTM\n");
    exit(1); }
  #endif
    
thomas's avatar
thomas committed
51
52
53
54
55
56
57
  init = true;
}

extern "C" {

//Numerical & computational functions:

thomas's avatar
thomas committed
58
59
60
#ifdef HAVE_PROJ
//Use the following function to get angleFM for UTM vector functions
void getAngleProjLonLat(dataCacheMap *, fullMatrix<double> &angle, fullMatrix<double> &xyz) {
thomas's avatar
thomas committed
61
  if (! init) initialize();
thomas's avatar
thomas committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  for (size_t i = 0; i < xyz.size1(); i++) {
    double x[2], y[2];
    x[0] = xyz.get(i, 0) - 1000.0;
    x[1] = xyz.get(i, 0) + 1000.0;
    y[0] = xyz.get(i, 1);
    y[1] = xyz.get(i, 1);
//     printf("x[0]_utm: %.12f, x[1]_utm: %.12f\n", x[0], x[1]);
    
    //Convert points to lat/lon:
    int er=0;
    if ( (er = pj_transform( pj_utm, pj_latlon, 2, 1, x, y, NULL )) != 0) {
      printf ("Transform failed: %s\n", pj_strerrno (er));
      exit(1);
    }
//     printf("x[0]_lon: %.12f, x[1]_lon: %.12f\n", x[0], x[1]);
    double dlon = x[1] - x[0];
    double dlat = y[1] - y[0];
//     printf("dlon: %.12f, dlat: %.12f\n", dlon, dlat);
    angle.set(i, 0, atan2(dlat, dlon));
thomas's avatar
thomas committed
81
82
  }
}
thomas's avatar
thomas committed
83
84
  
void latLonDegreesToUTM(dataCacheMap *, fullMatrix<double> &UTM, fullMatrix<double> &latLonDegrees) {
thomas's avatar
thomas committed
85
  if (! init) initialize();
thomas's avatar
thomas committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  double x, y;
  double pi = 4*atan(1.0);
  for (size_t i = 0; i < UTM.size1(); i++) {
    y = latLonDegrees.get(i,0) * pi / 180.0;
    x = latLonDegrees.get(i,1) * pi / 180.0;

//     printf("Using latLonDegreesToUTM (LIB.cc); lat/lon (degrees): %.2f\t%.2f\n", x*180.0/pi, y*180.0/pi);
    int er=0;
    if ( (er = pj_transform( pj_latlon, pj_utm, 1, 1, &x, &y, NULL )) != 0) {
      printf ("Transform failed: %s\n", pj_strerrno (er));
      exit(1);
    }
    
    UTM.set(i,0, x);
    UTM.set(i,1, y);
    UTM.set(i,2, 0.0);
  }
}
  
void UTMtoLatLonDegrees(dataCacheMap *, fullMatrix<double> &latLonDegrees, fullMatrix<double> &UTM) {
  if (! init) initialize();
  double x, y;
  double pi = 4*atan(1.0);
  for (size_t i = 0; i < latLonDegrees.size1(); i++) {
    x = UTM.get(i,0);
    y = UTM.get(i,1);

//     printf("Using UTMtoLatLonDegrees (LIB.cc); UTM: %.2f\t%.2f\n", x, y);
    
    int er=0;
    if ( (er = pj_transform( pj_utm, pj_latlon, 1, 1, &x, &y, NULL )) != 0) {
      printf ("Transform failed: %s\n", pj_strerrno (er));
      exit(1);
    }
//     printf("lat/lon: %.2f, %.2f\n", y* 180.0 / pi, x* 180.0 / pi);
    latLonDegrees.set(i,0, y * 180.0 / pi);
    latLonDegrees.set(i,1, x * 180.0 / pi);
    latLonDegrees.set(i,2, 0.0);
thomas's avatar
thomas committed
124
125
126
  }
}

thomas's avatar
thomas committed
127
void UTMtoLonLatDegrees(dataCacheMap *, fullMatrix<double> &lonLatDegrees, fullMatrix<double> &UTM) {
thomas's avatar
thomas committed
128
  if (! init) initialize();
thomas's avatar
thomas committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  double x, y;
  double pi = 4*atan(1.0);
  for (size_t i = 0; i < lonLatDegrees.size1(); i++) {
    x = UTM.get(i,0);
    y = UTM.get(i,1);

//     printf("Using UTMtoLatLonDegrees (LIB.cc); UTM: %.2f\t%.2f\n", x, y);
    
    int er=0;
    if ( (er = pj_transform( pj_utm, pj_latlon, 1, 1, &x, &y, NULL )) != 0) {
      printf ("Transform failed: %s\n", pj_strerrno (er));
      exit(1);
    }
//     printf("Using UTMtoLatLonDegrees (LIB.cc); lon/lat: %.2f\t%.2f\n", x*180.0/pi, y*180.0/pi);
    lonLatDegrees.set(i,0, x * 180.0 / pi);
    lonLatDegrees.set(i,1, y * 180.0 / pi);
    lonLatDegrees.set(i,2, 0.0);
  }
}
  
// Functions to convert vectors:
//Convert vector in lon/lat space to UTM space. u and v give the lon and lat components of vector to be projected.
void lonLatVector_To_UTMVector(dataCacheMap *, fullMatrix<double> &uv, fullMatrix<double> &angle_FM, fullMatrix<double> &u, fullMatrix<double> &v){
  for (size_t i = 0; i < uv.size1(); i++) {
//     printf("Converting vectors from lon/lat to UTM space.\n");
    double dlon = u.get(i,0);
    double dlat = v.get(i,0);
//     printf("lon/lat: u: %.4f, v: %.4f\n", dlon, dlat);
    double angle = angle_FM.get(i,0);
    double dx = dlon*cos(angle) + dlat*sin(angle);
    double dy = dlat*cos(angle) + dlon*sin(angle);
//     printf("utm: u: %.4f, v: %.4f\n", dx, dy);
    uv.set(i, 0, dx);
    uv.set(i, 1, dy);
    uv.set(i, 2, 0.0);
thomas's avatar
thomas committed
164
165
166
  }
}

thomas's avatar
thomas committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//This function is redundant as you can just use the above function and reverse the u and v to get the same thing.
void latLonVector_To_UTMVector(dataCacheMap *, fullMatrix<double> &uv, fullMatrix<double> &angle_FM, fullMatrix<double> &u, fullMatrix<double> &v){
  for (size_t i = 0; i < uv.size1(); i++) {
//     printf("Converting vectors from lon/lat to UTM space.\n");
    double dlat = u.get(i,0);
    double dlon = v.get(i,0);
//     printf("lon/lat: u: %.4f, v: %.4f\n", dlon, dlat);
    double angle = angle_FM.get(i,0);
    double dx = dlon*cos(angle) + dlat*sin(angle);
    double dy = dlat*cos(angle) + dlon*sin(angle);
//     printf("utm: u: %.4f, v: %.4f; \t angle: %.6f\n", dx, dy, angle*180.0/M_PI);
    uv.set(i, 0, dx);
    uv.set(i, 1, dy);
    uv.set(i, 2, 0.0);
  }
}
#endif


thomas's avatar
thomas committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
void lonLat (dataCacheMap *, fullMatrix<double> &lonlat, fullMatrix<double> &xyz) {
  if (! init) initialize();
  for (size_t i = 0; i< lonlat.size1(); i++) {
    double x = pPhiX * xyz(i,0) + pThetaX * xyz(i,1) + pOx;
    double y = pPhiY * xyz(i,0) + pThetaY * xyz(i,1) + pOy;
    double z = pPhiZ * xyz(i,0) + pThetaZ * xyz(i,1) + pOz;
    double r = sqrt(x*x+y*y+z*z);
    lonlat.set(i, 0, atan2(y,x));
    lonlat.set(i, 1, asin(z/r));
    lonlat.set(i, 2, 0);
  }
}

void latLon (dataCacheMap *, fullMatrix<double> &lonlat, fullMatrix<double> &xyz) {
  if (! init) initialize();
  for (size_t i = 0; i< lonlat.size1(); i++) {
    double x = pPhiX * xyz(i,0) + pThetaX * xyz(i,1) + pOx;
    double y = pPhiY * xyz(i,0) + pThetaY * xyz(i,1) + pOy;
    double z = pPhiZ * xyz(i,0) + pThetaZ * xyz(i,1) + pOz;
    double r = sqrt(x*x+y*y+z*z);
    lonlat.set(i, 0, asin(z/r));
    lonlat.set(i, 1, atan2(y,x));
    lonlat.set(i, 2, 0);
  }
}

void lonLatDegrees (dataCacheMap *, fullMatrix<double> &lonlat, fullMatrix<double> &xyz) {
	if (! init) initialize();
	for (size_t i = 0; i< lonlat.size1(); i++) {
		double x = pPhiX * xyz(i,0) + pThetaX * xyz(i,1) + pOx;
		double y = pPhiY * xyz(i,0) + pThetaY * xyz(i,1) + pOy;
		double z = pPhiZ * xyz(i,0) + pThetaZ * xyz(i,1) + pOz;
		double r = sqrt(x*x+y*y+z*z);
		lonlat.set(i, 0, atan2(y,x)*180/M_PI);
		lonlat.set(i, 1, asin(z/r)*180/M_PI);
		lonlat.set(i, 2, 0);
	}
}

void latLonDegrees (dataCacheMap *, fullMatrix<double> &lonlat, fullMatrix<double> &xyz) {
	if (! init) initialize();
	for (size_t i = 0; i< lonlat.size1(); i++) {
		double x = pPhiX * xyz(i,0) + pThetaX * xyz(i,1) + pOx;
		double y = pPhiY * xyz(i,0) + pThetaY * xyz(i,1) + pOy;
		double z = pPhiZ * xyz(i,0) + pThetaZ * xyz(i,1) + pOz;
		double r = sqrt(x*x+y*y+z*z);
		lonlat.set(i, 1, atan2(y,x)*180/M_PI);
		lonlat.set(i, 0, asin(z/r)*180/M_PI);
		lonlat.set(i, 2, 0);
	}
}

void lonLatVector(dataCacheMap *, fullMatrix<double> &uv, fullMatrix<double> &lonlat, fullMatrix<double> &u, fullMatrix<double> &v){
	for (size_t i = 0; i< lonlat.size1(); i++) {
		double lon=lonlat(i,0);
		double lat=lonlat(i,1);
		double uLon=u(i,0);
		double uLat=v(i,0);
    double lon0=146.5892*M_PI/180;
    double lat0=-17.147569*M_PI/180;
    double u1= -uLat*sin(lat)*cos(lon)-uLon*sin(lon);
    double u2= -uLat*sin(lat)*sin(lon)+uLon*cos(lon);
    double u3= uLat*cos(lat);
    double alpha1= -sin(lon0);
    double alpha2= cos(lon0);
    double alpha3=0;
    double beta1=-sin(lat0)*cos(lon0);
    double beta2=-sin(lat0)*sin(lon0);
    double beta3=cos(lat0);
		uv.set(i, 0, u1*alpha1+u2*alpha2+u3*alpha3);
		uv.set(i, 1, u1*beta1+u2*beta2+u3*beta3);
		uv.set(i, 2,  0);
	}
}

void latLonVector(dataCacheMap *, fullMatrix<double> &uv, fullMatrix<double> &latlon, fullMatrix<double> &u, fullMatrix<double> &v){
	for (size_t i = 0; i< latlon.size1(); i++) {
		double lat=latlon(i,0);
		double lon=latlon(i,1);
		double uLat=u(i,0);
		double uLon=v(i,0);
    double lon0=146.5892*M_PI/180;
    double lat0=-17.147569*M_PI/180;
    double u1= -uLat*sin(lat)*cos(lon)-uLon*sin(lon);
    double u2= -uLat*sin(lat)*sin(lon)+uLon*cos(lon);
    double u3= uLat*cos(lat);
    double alpha1= -sin(lon0);
    double alpha2= cos(lon0);
    double alpha3=0;
    double beta1=-sin(lat0)*cos(lon0);
    double beta2=-sin(lat0)*sin(lon0);
    double beta3=cos(lat0);
		uv.set(i, 0, u1*alpha1+u2*alpha2+u3*alpha3);
		uv.set(i, 1, u1*beta1+u2*beta2+u3*beta3);
		uv.set(i, 2,  0);
	}
}

void transport2velocity(dataCacheMap *,fullMatrix<double> &uv, fullMatrix<double> &UV, fullMatrix<double> &bath, fullMatrix<double> &solution){
  for (size_t i = 0; i< solution.size1(); i++) {
    uv.set(i,0,UV(i,0)/(bath(i,0)+solution(i,0))); 
    uv.set(i,1,UV(i,1)/(bath(i,0)+solution(i,0))); 
    uv.set(i,2,UV(i,2)/(bath(i,0)+solution(i,0))); 
  }
}

void computeEl(dataCacheMap *,fullMatrix<double> &val,fullMatrix<double> &solution) {
  for (size_t i = 0; i< val.size1(); i++) {
    val.set(i, 0, solution(i,0)); 
  }
}

void computeVel(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &solution){
  for (size_t i = 0; i< val.size1(); i++) {
    val.set(i, 0, solution(i,1)); 
    val.set(i, 1, solution(i,2)); 
  }
}

void computeErrorEl(dataCacheMap *,fullMatrix<double> &err,fullMatrix<double> &solution1, fullMatrix<double> &solution2) {
  for (size_t i = 0; i< err.size1(); i++) {
    err.set(i, 0, solution1(i,0)-solution2(i, 0)); 
  }
}

void computeErrorVel(dataCacheMap *, fullMatrix<double> &err, fullMatrix<double> &solution1, fullMatrix<double> &solution2){
  for (size_t i = 0; i< err.size1(); i++) {
    err.set(i, 0, solution1(i,1)-solution2(i, 1)); 
    err.set(i, 1, solution1(i,2)-solution2(i, 2)); 
  }
}

thomas's avatar
thomas committed
318
void merge_3DFunctions(dataCacheMap *,fullMatrix<double> &sol, fullMatrix<double> &funkOne, fullMatrix<double> &funkTwo){
thomas's avatar
thomas committed
319
320
321
322
323
324
325
  for (size_t i = 0; i< sol.size1(); i++) {
    sol.set(i, 0, funkOne(i,0) + funkTwo(i,0)); 
    sol.set(i, 1, funkOne(i,1) + funkTwo(i,1));
    sol.set(i, 2, funkOne(i,2) + funkTwo(i,2));
  }
}

thomas's avatar
thomas committed
326
327
void merge(dataCacheMap *,fullMatrix<double> &sol,fullMatrix<double> &eta, fullMatrix<double> &uv){
  for (size_t i = 0; i< sol.size1(); i++) {
thomas's avatar
thomas committed
328
329
330
    sol.set(i,0, eta(i,0)); 
    sol.set(i,1, uv(i,0));
    sol.set(i,2, uv(i,1));
thomas's avatar
thomas committed
331
332
333
  }
}

thomas's avatar
thomas committed
334
void merge2(dataCacheMap *,fullMatrix<double> &sol, fullMatrix<double> &u1, fullMatrix<double> &u2){
thomas's avatar
thomas committed
335
336
337
338
339
340
  for (size_t i = 0; i < sol.size1(); i++) {
    sol.set(i, 0, u1(i,0)); 
    sol.set(i, 1, u2(i,0)); 
  }
}

thomas's avatar
thomas committed
341
void merge3(dataCacheMap *,fullMatrix<double> &sol, fullMatrix<double> &eta, fullMatrix<double> &uv, fullMatrix<double> &currentUV){
thomas's avatar
thomas committed
342
  for (size_t i = 0; i< sol.size1(); i++) {
thomas's avatar
thomas committed
343
344
345
    sol.set(i,0, eta(i,0)); 
    sol.set(i,1, uv(i,0) + currentUV(i,1));
    sol.set(i,2, uv(i,1) + currentUV(i,2)); 
thomas's avatar
thomas committed
346
347
348
  }
}

thomas's avatar
thomas committed
349
350
void merge3D_with_UV(dataCacheMap *,fullMatrix<double> &sol, fullMatrix<double> &UVElev1, fullMatrix<double> &UV2){
  for (size_t i = 0; i< sol.size1(); i++) {
thomas's avatar
thomas committed
351
352
353
    sol.set(i,0, UVElev1(i,0)); 
    sol.set(i,1, UVElev1(i,1) + UV2(i,0));
    sol.set(i,2, UVElev1(i,2) + UV2(i,1)); 
thomas's avatar
thomas committed
354
355
356
  }
}

thomas's avatar
thomas committed
357
//Function to merge shallow (hard) and deep (soft) DCs
thomas's avatar
thomas committed
358
void overwrite2(dataCacheMap *,fullMatrix<double> &sol, fullMatrix<double> &bath, fullMatrix<double> &shallowDC, fullMatrix<double> &deepDC, fullMatrix<double> &nbOfShallowReefs){
thomas's avatar
thomas committed
359
  for (size_t i = 0; i < sol.size1(); i++) {
thomas's avatar
thomas committed
360
    if (shallowDC(i,0) > 0.1 && (bath(i,0) < 20.0)) sol.set(i, 0, shallowDC(i,0));
thomas's avatar
thomas committed
361
    else {
thomas's avatar
thomas committed
362
      if ((deepDC(i,0) > 0.1) && (bath(i,0) > 20.0) && (bath(i,0) < 60.0)) sol.set(i, 0, deepDC(i,0)+nbOfShallowReefs(0,0));
thomas's avatar
thomas committed
363
364
365
366
367
      else sol.set(i, 0, 0.0);
    }
  }
}

thomas's avatar
thomas committed
368
369
370
371
372
373
374
375
376
void cutOffMin(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &ourFunk, fullMatrix<double> &minCutOff){
  double ourValue=0.1;
  for (size_t i = 0; i< sol.size1(); i++) {
    if (ourFunk.get(i,0) < minCutOff(0,0)) ourValue = minCutOff(0,0);
    else ourValue = ourFunk.get(i,0);
    sol.set(i, 0, ourValue);
  }
}

thomas's avatar
thomas committed
377
378
//Physical functions:

379
380
381
382
383
384
385
386
387
/** Calculate water depth */
void getWaterDepth(dataCacheMap *, fullMatrix<double> &fct, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< fct.size1(); i++) {
    double eta = sol.get(i, 0);
    double depth = bath.get(i, 0);
    fct.set(i, 0, eta+depth);
  }
}

thomas's avatar
thomas committed
388
389
390
391
392
393
394
395
396
397
/** Constant wind */
void wind (dataCacheMap *, fullMatrix<double> &sol) {
  for (size_t i = 0; i< sol.size1(); i++) {
    sol.set(i,0,0.000005);
    sol.set(i,1,0.000005);
  }
}

/** Coriolis force */
void coriolisLonLatDegrees (dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &lonlat) {
thomas's avatar
thomas committed
398
  for (size_t i = 0; i< sol.size1(); i++) {
thomas's avatar
thomas committed
399
    sol.set(i,0,2*7.292e-5*sin(lonlat(i,1)/180.0*M_PI));
thomas's avatar
thomas committed
400
401
402
  }
}

thomas's avatar
thomas committed
403
void coriolisConst (dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &xyz) {
thomas's avatar
thomas committed
404
405
  double theta = -17.147569/180*M_PI;
  for (size_t i = 0; i< sol.size1(); i++) {
thomas's avatar
thomas committed
406
//     sol.set(i,0,2*7.292e-5*xyz(i,2)/6.371e6);
thomas's avatar
thomas committed
407
408
409
410
    sol.set(i,0,2*7.292e-5*sin(theta));
  }
}

thomas's avatar
thomas committed
411
/** Different bottom drag paramaterisations */
thomas's avatar
thomas committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
void bottomDrag_old(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath){
  for(int i=0;  i<val.size1(); i++){
    double H = sol(i, 0)+bath(i, 0);
    double reef = 1.0;
    if (H < 10.0) reef = 10.0;
    val.set(i, 0, 9.81*reef*0.0235*0.0235/(pow(H, 1.333333333333)));
  }
}

void bottomDrag(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath, fullMatrix<double> &reefs){
  for(int i=0;  i<val.size1(); i++){
    double reef = 1.0;
    double H = sol(i, 0)+bath(i, 0);
    //Verify if there is a reef or not. 
    //  Reef --> multiply normal bottom drag by factor 10x
    int reefYesOrNo = int(reefs(i, 0));
//    printf("i is %d. reefYesOrNo: %d, reefs(i,0): %f\n",i, reefYesOrNo, reefs(i,0));
    if (reefYesOrNo > 0) reef = 10.0;
    val.set(i, 0, 9.81*reef*0.0235*0.0235/(pow(H, 1.333333333333)));
  }
}

thomas's avatar
thomas committed
434
435
436
437
438
439
440
void bottomDrag_const_n008(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath){
  for(int i=0;  i<val.size1(); i++){
    double H = sol(i, 0)+bath(i, 0);
    val.set(i, 0, 9.81*0.008*0.008/(pow(H, 1.333333333333)));
  }
}

thomas's avatar
thomas committed
441
442
void bottomDrag_const_n016(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath){
  for(int i=0;  i<val.size1(); i++){
thomas's avatar
thomas committed
443
    double depthFactor=1.0;
thomas's avatar
thomas committed
444
    double H = sol(i, 0)+bath(i, 0);
thomas's avatar
thomas committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//     if H > 200 depthFactor = 10.0;
//     if H > 500 depthFactor = 100.0;
    val.set(i, 0, 9.81*depthFactor*0.016*0.016/(pow(H, 1.333333333333)));
  }
}

void bottomDrag_reefs_n016(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath, fullMatrix<double> &reefs){
  for(int i=0;  i<val.size1(); i++){
    double reef = 1.0;
    double H = sol(i, 0)+bath(i, 0);
    //Verify if there is a reef or not.
    //  Reef --> multiply normal bottom drag by factor 10x
    int reefYesOrNo = int(reefs(i, 0));
    if (reefYesOrNo > 0 && bath(i,0) < 25.0) reef = 10.0;
    val.set(i, 0, 9.81*reef*0.016*0.016/(pow(H, 1.333333333333)));
thomas's avatar
thomas committed
460
461
462
  }
}

thomas's avatar
thomas committed
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
void bottomDrag_reefs_n008(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath, fullMatrix<double> &reefs){
  for(int i=0;  i<val.size1(); i++){
    double reef = 1.0;
    double H = sol(i, 0)+bath(i, 0);
    //Verify if there is a reef or not.
    //  Reef --> multiply normal bottom drag by factor 10x
    int reefYesOrNo = int(reefs(i, 0));
    if (reefYesOrNo > 0) reef = 10.0;
    val.set(i, 0, 9.81*reef*0.008*0.008/(pow(H, 1.333333333333)));
  }
}

void bottomDrag_const_Smallest(dataCacheMap *, fullMatrix<double> &val, fullMatrix<double> &sol, fullMatrix<double> &bath){
  for(int i=0;  i<val.size1(); i++){
    double H = sol(i, 0)+bath(i, 0);
    val.set(i, 0, 9.81*0.0074*0.0074/(pow(H, 1.333333333333)));
  }
}

/** Smagorinsky term */
thomas's avatar
thomas committed
483
void smagorinsky(dataCacheMap *map, fullMatrix<double> &val, fullMatrix<double> &solGradient){
thomas's avatar
thomas committed
484
485
486
487
488
  const dgGroupOfElements *g = map->getGroupOfElements();
  int elNum = map->getElementId();
  const dgMeshJacobian &jaco = map->getJacobians();
  const fullVector<double> &elRadi = jaco.elementInnerRadius(g->elementVectorId());
  double radi = elRadi(elNum);
thomas's avatar
thomas committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  for(int i=0;  i<val.size1(); i++){
    double dudx = solGradient(i, 3);
    double dudy = solGradient(i, 4);
    double dvdx = solGradient(i, 6);
    double dvdy = solGradient(i, 7);
    val(i, 0)= pow_int(0.1*radi, 2)*sqrt(2*dudx*dudx+2*dvdy*dvdy+pow_int(dudy+dvdx, 2));
  }
}

void current (dataCacheMap *, fullMatrix<double> &current, fullMatrix<double> &solution) {
  for (size_t i = 0; i< current.size1(); i++) {
    current.set(i, 0, solution(i,1));
    current.set(i, 1, solution(i,2));
    current.set(i, 2, 0);
  }
}

void current2 (dataCacheMap *, fullMatrix<double> &current, fullMatrix<double> &solution) {
  for (size_t i = 0; i< current.size1(); i++) {
    current.set(i, 0, solution(i,0));
    current.set(i, 1, solution(i,1));
    current.set(i, 2, 0.0);
  }
}

thomas's avatar
thomas committed
514
/** Wind stress paramaterisations */
thomas's avatar
thomas committed
515
516
517
518
519
520
521
522
523
524
525
void windStress (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

thomas's avatar
thomas committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
void windStressSmithBanke (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

void windStress_OTT (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=1.25*0.001*63.0*normWind*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=1.25*0.001*63.0*normWind*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

void windStressSmithBankeTimesTen (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=1.25*0.01*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=1.25*0.01*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

thomas's avatar
thomas committed
559
560
561
562
563
564
565
566
567
568
569
void windStressSmithBankeTimesOnePointFive (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=1.5*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=1.5*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

thomas's avatar
thomas committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
void windStressSmithBankeTimesTwo (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=2.0*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=2.0*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

void windStressSmithBankeTimesFour (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=4.0*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=4.0*1.25*0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

void windStressGarratt (dataCacheMap *,fullMatrix<double> &windstress,fullMatrix<double> &winduv, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double rho=1025;
    double sol1=0.001*(0.75*normWind+0.067*normWind*normWind)*winduv(i, 0)/(rho*(sol(i, 0)+bath(i, 0)));
    double sol2=0.001*(0.75*normWind+0.067*normWind*normWind)*winduv(i, 1)/(rho*(sol(i, 0)+bath(i, 0)));
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

void windStressOnly (dataCacheMap *, fullMatrix<double> &windstress, fullMatrix<double> &winduv) {
  for (size_t i = 0; i< windstress.size1(); i++) {
    double normWind=sqrt(winduv(i, 0)*winduv(i, 0)+winduv(i, 1)*winduv(i, 1));
    double sol1=0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 0);
    double sol2=0.001*(0.630*normWind+0.066*normWind*normWind)*winduv(i, 1);
    windstress.set(i, 0, sol1);
    windstress.set(i, 1, sol2);
  }
}

thomas's avatar
thomas committed
613
614
615
616
617
618
619
620
621
/** Function to return time-dependent sine curve (elev only). Arg1: current time, arg2: Ref Time */
void sineFunction (dataCacheMap *, fullMatrix<double> &fct, fullMatrix<double> &timeFunction, fullMatrix<double> &initTime) {
  for (size_t i = 0; i< fct.size1(); i++) {
    fct.set(i, 0, 0.3*sin((timeFunction(0,0)-initTime(0,0))*2*M_PI/3600.0/12.0));
    fct.set(i, 1, 0);
    fct.set(i, 2, 0);
  }
}

thomas's avatar
thomas committed
622
623
624
625
626
627
628
629
630
631
/** Boundary current functions */

/** Currents as a function of wind */
void functionMultiply(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &wind, fullMatrix<double> &multFactor) {
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, wind(i,0) * multFactor(i,0) );
    sol.set(i, 1, wind(i,1) * multFactor(i,1) );
  }
}

thomas's avatar
thomas committed
632
633
634
635
636
637
638
639
640
void functionCurrentAsFWind(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &wind, fullMatrix<double> &multFactor, fullMatrix<double> &depthFactor) {
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, wind(i,0) * multFactor(i,0) * depthFactor(i,0) );
    sol.set(i, 1, wind(i,1) * multFactor(i,1) * depthFactor(i,0) );
  }
}

void functionCurrentAsFWind_calcDepthFactor(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &bath) {
  for (size_t i=0; i<sol.size1(); i++) {
thomas's avatar
thomas committed
641
    double factor = 100.0/bath(i,0);
thomas's avatar
thomas committed
642
643
644
645
646
    if (factor > 2.0) factor = 2.0;
    sol.set(i, 0, factor);
  }
}

thomas's avatar
thomas committed
647
648
649
650
651
652
653
void functionAdd(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &wind, fullMatrix<double> &addTerm) {
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, wind(i,0) + addTerm(i,0) );
    sol.set(i, 1, wind(i,1) + addTerm(i,1) );
  }
}

thomas's avatar
thomas committed
654
/** Following two functions used to calculate the constant reisdual current imposed at boundaries */
thomas's avatar
thomas committed
655
656
/** return bathymetry * (unitVector (dot) normals)
    so size of return value will be greater when vectors and normals coincide, and when depth is larger*/
thomas's avatar
thomas committed
657
658
659
660
661
662
663
664
void bathVec(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &bathymetry, fullMatrix<double> &normals, fullMatrix<double> &boundaryData) {
  double vx, vy;
  vx = boundaryData(0,1);
  vy = boundaryData(0,2);
  for (size_t i=0; i<bathymetry.size1(); i++) {
    sol.set(i, 0, bathymetry(i,0) * (vx*normals(i,0) + vy*normals(i,1)) );
  }
}
thomas's avatar
thomas committed
665
666
/** 1. compute the flux per point (by doing Flux / integral along the boundary of the above function)
    2. set the flux as (flux per point (dot) unitVector) ; so the incoming/outgoing flux points along the direction of the unitVector*/
thomas's avatar
thomas committed
667
668
669
670
671
672
673
674
675
676
677
678
679
void SEC_UV(dataCacheMap *, fullMatrix<double> &UVout, fullMatrix<double> &bathInterfaceIntegrals, fullMatrix<double> &normals, fullMatrix<double> &boundaryData) {
  double F, c, vx, vy;
  F = boundaryData(0,0);
  vx = boundaryData(0,1);
  vy = boundaryData(0,2);
  c = F / bathInterfaceIntegrals(0,0);
  for (size_t i=0; i<UVout.size1(); i++) {
  	UVout.set(i, 0, 0);	//Eta
    UVout.set(i, 1, c * vx);
    UVout.set(i, 2, c * vy);
	}
}

thomas's avatar
thomas committed
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/** Function which returns: currents * (vector DOT boundary normals) 
 i.e. returns the current velocity only along boundaries perpendicular to "vector" (a unit vector) */
void currentsAlongVecDotNormals(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &normal, fullMatrix<double> &vector, fullMatrix<double> &currents) {
  double vx, vy;
  vx = vector(0,0);
  vy = vector(0,1);
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, currents(i,0) * (vx*normal(i,0) + vy*normal(i,1)) );
    sol.set(i, 1, currents(i,1) * (vx*normal(i,0) + vy*normal(i,1)) );
  }
}

/** Function which returns: currents DOT vector
 i.e. returns the current velocity along an axis defined by "vector" (a unit vector) */
void currentsDotVector(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &vector, fullMatrix<double> &currents) {
  double vx, vy;
  vx = vector(0,0);
  vy = vector(0,1);
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, currents(i,0) * vx );
    sol.set(i, 1, currents(i,1) * vy);
  }
}
thomas's avatar
thomas committed
703

thomas's avatar
thomas committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/** Return Residual current 3 function (vector of size 0.10 spanning range of Open Sea Central) */
void getRes3(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &lonLatDegrees) {
  for (size_t i=0; i<sol.size1(); i++) {
    if (lonLatDegrees(i,1) > -15.1)  {
      sol.set(i, 0, -0.10);
      sol.set(i, 1, 0.0); 
      continue; }
    if (lonLatDegrees(i,1) < -17.6)  {
      sol.set(i, 0, 0.0);
      sol.set(i, 1, -0.10); 
      continue; }
    sol.set(i, 0, -0.10*sqrt(1+(lonLatDegrees(i,1)+15.1)/(-15.1+17.6)) );
    sol.set(i, 1, -0.10*sqrt(-1.0*(lonLatDegrees(i,1)+15.1)/(-15.1+17.6)) );
  }
}

/** Return elevation as a function of perpendicular wind */
void elevAsFWind(dataCacheMap *, fullMatrix<double> &sol, fullMatrix<double> &normals, fullMatrix<double> windUV) {
  for (size_t i=0; i<sol.size1(); i++) {
    sol.set(i, 0, 0.1*normals(i,0)*windUV(i,0) );
    sol.set(i, 1, 0.1*normals(i,1)*windUV(i,1) );
  }
}


thomas's avatar
thomas committed
729
}