-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParticle.java
2236 lines (2042 loc) · 86.3 KB
/
Particle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
124
125
126
127
128
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
514
515
516
517
518
519
520
521
522
523
524
525
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
559
560
561
562
563
564
565
566
567
568
569
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.scottishseafarms.particle_track;
import java.awt.geom.Path2D;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;
//import org.apache.commons.lang.ArrayUtils;
/**
*
* @author tomdude
*/
public class Particle {
final private int id;
// horizontal position
private double[] xy = new double[2];
final private double[] startLoc = new double[2];
private String startSiteID = "0";
private int startSiteIndex = 0;
private String coordRef;
private final ISO_datestr startDate;
private double startTime = 0;
private int mesh;
private int elem;
private int[] elemRomsU;
private int[] elemRomsV;
private int[] nearestROMSGridPointU;
private int[] nearestROMSGridPointV;
private double[][] nrListROMSU;
private double[][] nrListROMSV;
private double[][] nrList = new double[5][2];
private double[][] cornerList = new double[3][2];
private int whichMesh;
// release time
private double releaseTime = 0;
// vertical position
private double depth = 0;
private int depLayer = 0;
// settlement details
private double age = 0;
private double degreeDays = 0;
private int status = 0;
private double density = 1;
private double mortRate = 0.01; // default hourly rate, based on results of Stein et al. 2005 (copepodid, nauplii rate marginally lower = 0.0078)
private boolean arrived = false;
private boolean viable = false;
private boolean free = false;
private boolean settledThisHour = false;
private boolean boundaryExit = false;
private String species = "none";
private String lastArrival = "0";
// A list to store data on the arrivals made by each particle.
// If rp.endOnArrival=true, this will contain a maximum of one element.
// --- Not presently used ---
private List<Arrival> arrivals;
// create a new particle at a defined location, at the water surface
// public Particle(double xstart, double ystart, double startDepth, String startSiteID, int id, double mortalityRate, String coordRef, String species)
// {
// this.id = id;
// this.xy[0] = xstart;
// this.xy[1] = ystart;
// this.startSiteID = startSiteID;
// this.startLoc[0] = xstart;
// this.startLoc[1] = ystart;
// this.mortRate = mortalityRate;
// this.z = startDepth;
//
// this.arrivals = new ArrayList<>();
//
// this.coordRef = coordRef;
// this.species = species;
// }
public Particle(double xstart, double ystart, double startDepth, String startSiteID, int startIndex, int id, double mortalityRate,
ISO_datestr startDate, double startTime, String coordRef, String species)
{
this.id = id;
this.xy[0] = xstart;
this.xy[1] = ystart;
this.startSiteID = startSiteID;
this.startSiteIndex = startIndex;
this.startDate = new ISO_datestr(startDate.getDateStr());
this.startTime = startTime;
this.startLoc[0] = xstart;
this.startLoc[1] = ystart;
this.mortRate = mortalityRate;
this.depth = startDepth;
this.coordRef = coordRef;
this.species = species;
//System.out.println("creating particle, startDate = "+this.startDate.getDateStr());
//this.arrivals = new ArrayList<Arrival>();
}
/**
* Create a new particle from the line of a location ASCII file.
*
* @param locationFileLine
*/
public Particle(String locationFileLine, String species)
{
String[] values = locationFileLine.split(" ");
// Don't use the 0 th entry on the line (current time in locations)
this.id = Integer.parseInt(values[1]);
this.startDate = new ISO_datestr(values[2]);
this.age = Double.parseDouble(values[3]);
this.startSiteID = values[4];
this.xy[0] = Double.parseDouble(values[5]);
this.xy[1] = Double.parseDouble(values[6]);
this.elem = Integer.parseInt(values[7]);
this.status = Integer.parseInt(values[8]);
this.density = Double.parseDouble(values[9]);
this.mesh = Integer.parseInt(values[10]);
this.depth = Double.parseDouble(values[11]);
this.degreeDays = Double.parseDouble(values[12]);
// Check the details of the particle created
// System.out.printf("%d %s %.1f %s %.5f %.5f %d %d %.4f\n",
// this.getID(),
// this.getStartDate().getDateStr(),
// this.getAge(),
// this.getStartID(),
// this.getLocation()[0],
// this.getLocation()[1],
// this.getElem(),
// this.getStatus(),
// this.getDensity()
// );
this.species = species;
this.coordRef = "OSGB1936";
}
// public void setReleaseScenario(int releaseScenario, double[][] startlocs)
// {
// switch (releaseScenario) {
// // integer to switch release scenario
// // 0 all at time zero
// // 1 tidal release (evenly over first 24 hours)
// // 2 continuous release (1 per hour per site)
// // 3 continuous release (5 per hour per site)
// // 4 continuous release (10 per hour per site)
// // 5 continuous release (20 per hour per site)
// // 10 defined release times
// case 0:
// this.setReleaseTime(0);
// break;
// case 1:
// this.setReleaseTime((this.id / startlocs.length) % 25);
// break;
// case 2:
// this.setReleaseTime(Math.floor(this.id / startlocs.length));
// break;
// case 3:
// this.setReleaseTime(Math.floor(this.id / (5 * startlocs.length)));
// break;
// case 4:
// this.setReleaseTime(Math.floor(this.id / (10 * startlocs.length)));
// break;
// case 5:
// this.setReleaseTime(Math.floor(this.id / (20 * startlocs.length)));
// break;
// case 10:
// this.setReleaseTime(startlocs[this.id][3]);
// break;
// }
// }
@Override
public String toString()
{
return this.getID()+" "+this.xy.toString();
}
// Not presently used
public void reportArrival(int sourceLocation, int arrivalLocation, double arrivalTime, double arrivalDensity)
{
arrivals.add(new Arrival(sourceLocation,arrivalLocation,arrivalTime,arrivalDensity));
//System.out.printf("Arrival (particle %d): %d %d %f %f\n", this.getID(),sourceLocation,arrivalLocation,arrivalTime,arrivalDensity);
}
public List<Arrival> getArrivals()
{
return this.arrivals;
}
public void setReleaseTime(double releaseTime)
{
this.releaseTime = releaseTime;
}
public double getReleaseTime()
{
return this.releaseTime;
}
public int getID()
{
return this.id;
}
public double[] getLocation()
{
return this.xy;
}
public double[] getStartLocation()
{
return this.startLoc;
}
public String getStartID()
{
return this.startSiteID;
}
public int getStartIndex()
{
return this.startSiteIndex;
}
public ISO_datestr getStartDate()
{
return this.startDate;
}
public double getStartTime()
{
return this.startTime;
}
public void setLocation(double x, double y)
{
this.xy[0] = x;
this.xy[1] = y;
}
public void setElem(int elem)
{
this.elem = elem;
}
public int getElem()
{
return this.elem;
}
public void setROMSElemU(int[] elem)
{
this.elemRomsU = elem;
}
public void setROMSElemV(int[] elem)
{
this.elemRomsU = elem;
}
public int[] getROMSElemU()
{
return this.elemRomsU;
}
public int[] getROMSElemV()
{
return this.elemRomsV;
}
public void setROMSnearestPointU(int[] point)
{
this.nearestROMSGridPointU = point;
}
public void setROMSnearestPointV(int[] point)
{
this.nearestROMSGridPointV = point;
}
public int[] getROMSnearestPointU()
{
return this.nearestROMSGridPointU;
}
public int[] getROMSnearestPointV()
{
return this.nearestROMSGridPointV;
}
public void setMesh(int meshID)
{
this.mesh = meshID;
}
public int getMesh()
{
return this.mesh;
}
public String printLocation()
{
// if (this.mesh == 0)
// return "xy "+this.xy[0]+" "+this.xy[1]+" mesh "+this.mesh+" "+" elem "+this.elem;
// else
// return "xy "+this.xy[0]+" "+this.xy[1]+" mesh "+this.mesh+" "+" elem "+this.elemRomsU[0]+" "+this.elemRomsU[1];
return "xy "+this.xy[0]+" "+this.xy[1]+" mesh "+this.mesh+" "+" elem "+this.elem;
}
// public void setNrList(float[][] uvnode)
// {
// this.nrList = nearestCentroidList(this.xy[0], this.xy[1], uvnode);
// }
public double[][] getNrList()
{
return this.nrList;
}
public void setCornerList(int elemPart, int[][] trinodes, double[][] nodexy)
{
this.cornerList[0][0]=nodexy[trinodes[elemPart][0]][0];
this.cornerList[0][1]=nodexy[trinodes[elemPart][0]][1];
this.cornerList[1][0]=nodexy[trinodes[elemPart][1]][0];
this.cornerList[1][1]=nodexy[trinodes[elemPart][1]][1];
this.cornerList[2][0]=nodexy[trinodes[elemPart][2]][0];
this.cornerList[2][1]=nodexy[trinodes[elemPart][2]][1];
}
public double[][] getCornerList()
{
return this.cornerList;
}
public double getDepth()
{
return this.depth;
}
/**
* Set depth of particle
*
* @param depth
*/
public void setZ(double depth)
{
this.depth = depth;
}
/**
* Set depth of particle with check against local depth
* @param z (this is a negative value)
* @param localDepth (this is a positive value)
*/
public void setDepth(double depth, double localDepth)
{
if (depth > localDepth)
{
depth = localDepth;
}
this.depth = depth;
}
public int getDepthLayer()
{
return this.depLayer;
}
public void setDepthLayer(int dep)
{
this.depLayer = dep;
}
// see lower down for setMortRate (based on salinity)
public double getMortRate()
{
return mortRate;
}
public void setDensity()
{
this.density = this.density*(1-this.mortRate);
//System.out.println("density = "+this.density);
}
public double getDensity()
{
return this.density;
}
public void setStatus(int status)
{
// 0 = not free
// 1 = free
// 2 = viable (able to settle)
// 3 = settled
// 66 = boundary exit
// 666 = dead (exceeded lifespan)
this.status=status;
}
public int getStatus()
{
return this.status;
}
/** Set particle depth in the water column based on its defined behaviour and
* the time
* 1 - passive, stay on surface
* 2 - passive, stay on bottom (layer 10)
* 3 - passive, stay in mid layer (layer 5)
* 4 - vertical swimming: surface for hours 19-6, mid layer (5) hours 7-18
* 5 - rapid drop (1->10) at hour 6, then gradually move back up
* 6 - top during flood tides, mid during ebb (local)
* 7 - mid during flood tides, bed during ebb (local)
* 8 - top during flood tides, bed during ebb (local)
* MORE...? "homing" ability
*/
public void setDepthLayer(int behaviour, String tideState)
{
boolean reducedFile = true;
switch (behaviour)
{
case 1: this.depLayer = 0; break;
case 2: this.depLayer = 9; break;
case 3: this.depLayer = 5; break;
case 6:
if (tideState.equalsIgnoreCase("flood"))
{
this.depLayer = 0;
} else
{
this.depLayer = 5;
}
break;
case 7:
if (tideState.equalsIgnoreCase("flood"))
{
this.depLayer = 5;
} else
{
this.depLayer = 9;
}
break;
case 8:
if (tideState.equalsIgnoreCase("flood"))
{
this.depLayer = 0;
} else
{
this.depLayer = 9;
}
break;
}
// enable running file with two depth layers ("top"=0 and "bottom"=1)
if (reducedFile==true)
{
if (this.depLayer>0)
{
this.depLayer=1;
}
}
//return 0;
}
/** put particle in the correct depth layer, based upon
* its z position, and
*
* @param localDepth element bathymetry value
* @param layers
*/
public void setLayerFromDepth(double localDepth, float[] layers)
{
int depNearest = 0;
double dZmin = 1000;
//System.out.println("in setLayerFromDepth");
for (int i = 0; i < layers.length; i++)
{
// NOTE: changed particle depth to be positive
if (Math.abs(this.depth - localDepth*layers[i]) < dZmin)
{
depNearest = i;
dZmin = Math.abs(this.depth - localDepth*layers[i]);
}
}
//System.out.printf("setting depth layer: %d (%f, particle depth = %f)\n",depNearest,localDepth*layers[depNearest],this.z);
this.setDepthLayer(depNearest);
}
public double verticalDiffusion()
{
double vertDiff = 0;
return vertDiff;
}
public void setViable(boolean viable)
{
this.viable = viable;
}
public void setArrived(boolean arrived)
{
this.arrived = arrived;
}
public void setLastArrival(String loc)
{
this.lastArrival = loc;
}
public String getLastArrival()
{
return this.lastArrival;
}
public void setSettledThisHour(boolean settled)
{
this.settledThisHour = settled;
}
public void setFree(boolean free)
{
this.free = free;
}
public void setBoundaryExit(boolean exit)
{
this.boundaryExit = exit;
}
public boolean getViable()
{
return this.viable;
}
public boolean getArrived()
{
return this.arrived;
}
public boolean getSettledThisHour()
{
return this.settledThisHour;
}
public boolean getFree()
{
return this.free;
}
public boolean getBoundaryExit()
{
return this.boundaryExit;
}
public double[] behaveVelocity(int behaviour)
{
double[] uv = new double[2];
return uv;
}
public double[] smagorinskyDiffusionVelocity(int node, int[] neighbours, double u, double v, double[][] uvnode)
{
double[] uv = new double[2];
return uv;
}
/**
* Compute salinity from an average of the corner nodes of the containing element.
* @param tt
* @param salinity
* @param trinodes
* @return
*/
public double salinity(int tt, double[][] salinity, int[][] trinodes)
{
double s = 0;
double dist1 = distanceEuclid(this.xy[0],this.xy[1],cornerList[0][0],cornerList[0][1]);
double dist2 = distanceEuclid(this.xy[0],this.xy[1],cornerList[1][0],cornerList[1][1]);
double dist3 = distanceEuclid(this.xy[0],this.xy[1],cornerList[2][0],cornerList[2][1]);
double weight1 = 1.0/(dist1*dist1);
double weight2 = 1.0/(dist2*dist2);
double weight3 = 1.0/(dist3*dist3);
double weightSum = weight1+weight2+weight3;
s = (1.0/weightSum)*(weight1*salinity[tt][trinodes[0][elem]]+weight2*salinity[tt][trinodes[1][elem]]+weight3*salinity[tt][trinodes[2][elem]]);
return s;
}
/**
* Sets the mortality rate for the particle packet based upon local salinity
* @param salinity
* @return
*/
public void setMortRate(double salinity)
{
// estimated 2nd order polynomial fit to Bricknell et al.'s (2006) data
this.mortRate = 0.0011*salinity*salinity - 0.07*salinity + 1.1439;
//System.out.println("salinity = "+salinity+" mortrate = "+this.mortRate);
}
public void incrementAge(double increment)
{
this.age+=increment;
}
public void incrementDegreeDays(double temp, RunProperties rp)
{
double inc = temp*(rp.dt/rp.stepsPerStep)/86400;
//System.out.println("DD increment: T="+temp+" dt="+rp.dt+" stepsPerStep="+rp.stepsPerStep+" dt/st="+(rp.dt/rp.stepsPerStep)+" inc="+inc);
this.degreeDays += inc;
}
public double getAge()
{
return this.age;
}
public double getDegreeDays()
{
return this.degreeDays;
}
public void verticalMovement(RunProperties rp, double D_hVertDz, double tt, double dt, double localDepth, double localSalinity)
{
double depthNew = this.depth;
//double dielSwimmingSpeed = 0;
double vertSwim_M = 0;
double vertSwim_S = 0;
double sinking_M = 0;
double sinking_S = 0;
if (rp.species.equalsIgnoreCase("sealice"))
{
// Case of naupliar lice - no active surface behaviour
if (this.status==1)
{
System.out.println("Juvenile: no phototaxis, neutrally buoyant");
vertSwim_M = 0;
vertSwim_S = 0;
sinking_M = 0;
sinking_S = 0;
}
// Case of low salinity - stop swimming to surface and just sink
else if (localSalinity < rp.salinityThreshold || tt < 7 || tt > 19)
{
System.out.println("Low salinity OR night time: sinking only (salinity="+localSalinity+", time="+tt);
vertSwim_M = 0;
vertSwim_S = 0;
sinking_M = rp.sinkingRateMean;
sinking_S = rp.sinkingRateStd;
}
// Case for "normal" copepodids - no sinking, just swimming
else
{
System.out.println("Normal water (above salinity threshold) and daytime: phototaxis only");
vertSwim_M = rp.vertSwimSpeedMean;
vertSwim_S = rp.vertSwimSpeedStd;
sinking_M = 0;
sinking_S = 0;
}
}
if (rp.species.equalsIgnoreCase("fishfaeces"))
{
}
if (this.species.equalsIgnoreCase("modiolus"))
{
if (this.status==1)
{
rp.sinkingRateMean=0;
}
else
{
// Use the supplied
//sinkingRateMean=
}
}
// Do some stuff with the sinking and diffusion parameters here
// Simple example here PRESENTLY UNTESTED, and enforces a uniform distribution
depthNew += dt * (rp.sinkingRateMean + rp.sinkingRateStd*ThreadLocalRandom.current().nextDouble(-1.0,1.0)
+ rp.vertSwimSpeedMean + rp.vertSwimSpeedStd*ThreadLocalRandom.current().nextDouble(-1.0,1.0));
// Naive vertical diffusion; use this for fixed diffusion parameter
//depthNew += dt * D_hVert * ThreadLocalRandom.current().nextDouble(-1.0,1.0);
// Variable vertical diffusion, following Visser (1997) and Ross & Sharples (2004)
double r = 1.0/3.0;
double mult= (2*dt/r)*rp.D_hVert*(this.depth + (dt/2)*D_hVertDz);
double rand= ThreadLocalRandom.current().nextGaussian();
if (rand < 0)
{
depthNew -= dt*D_hVertDz + Math.pow(mult * -rand,0.5);
}
else
{
depthNew += dt*D_hVertDz + Math.pow(mult * rand,0.5);
}
if (depthNew < 0)
{
depthNew = 0;
}
if (depthNew > localDepth)
{
depthNew = localDepth;
}
this.depth = depthNew;
}
// --------------------------------------------------------------------------------------------------------------
// Everything to do with velocity calculation below here
// --------------------------------------------------------------------------------------------------------------
/**
* Is the particle still in the present mesh, should it change mesh, and has it exited the overall domain?
*
* @param newLoc
* @param meshes
* @param rp
*/
public void meshSelectOrExit(double[] newLoc, List<Mesh> meshes, RunProperties rp)
{
// i) Is particle in present mesh?
// i.i) If YES and presently in mesh n > 0, is it also within mesh < n?
// If YES, place in that mesh
// If NO, stay in same mesh
// i.ii) If YES and in mesh 0, is it within certain range of boundary?
// i.ii.i) If YES, is it in other mesh > 0?
// If YES, switch to other mesh
// If NO, BOUNDARY EXIT!! (nearest bnode)
// If NO: CARRY ON!!!
// i.iii) If NO, is particle within other mesh > n?
// If YES, switch mesh
// If NO, BOUNDARY EXIT!! (nearest bnode)
// Get current mesh and element
boolean report = false;
if (report) {System.out.println("Particle.meshSelectOrExit");}
int move = 0;
int meshID = this.getMesh();
double[] oldLoc = this.getLocation();
int[] el = new int[2];
if (meshes.get(meshID).getType().equalsIgnoreCase("ROMS"))
{
el = this.getROMSnearestPointU();
if (report) {System.out.println(" in ROMS mesh, el = "+el[0]+" "+el[1]);}
}
else if (meshes.get(meshID).getType().equalsIgnoreCase("ROMS_TRI"))
{
el[0] = this.getElem();
if (report) {System.out.println(" in ROMS_TRI mesh, el = "+el[0]);}
}
else
{
el[0] = this.getElem();
if (report) {System.out.println(" in FVCOM mesh, el = "+el[0]);}
}
// Only move the particle at the end of this method, IFF it is allocated to move
//this.setLocation(newLoc[0],newLoc[1]);
// i)
Mesh m = meshes.get(meshID);
if (m.isInMesh(newLoc,true,false,el))
{
if (report) {System.out.println(" in same mesh as before");}
// i.i) in mesh > 0
if (meshID > 0)
{
if (report) {System.out.println(" but this isn't the lowest ID mesh, checking lower ID mesh");}
m = meshes.get(0);
if (m.isInMesh(newLoc,true,true,null))
{
if (report) {System.out.println(" in lower ID mesh");}
// switch to mesh 0
this.setLocation(newLoc[0],newLoc[1]);
this.placeInMesh(meshes,0,true);
move = 1;
}
else
{
// stay in current mesh
if (report) {System.out.println(" not in lower ID mesh, stay in same mesh");}
//System.out.println("In mesh "+meshID+", which is of type "+meshes.get(meshID).getType());
this.setLocation(newLoc[0],newLoc[1]);
this.placeInMesh(meshes,1,false);
move = 1;
}
}
// i.ii) in mesh 0
else
{
if (report) {System.out.println(" this is the lowest ID mesh, checking boundaries");}
// boundary check
int bnode = -1;
if (rp.checkOpenBoundaries == true){
bnode = ParallelParticleMover.openBoundaryCheck((float)newLoc[0],(float)newLoc[1],m,rp);
}
if (bnode != -1)
{
if (report) {System.out.println(" close to a mesh boundary node");}
// Close to boundary
if (meshes.size() == 2)
{
m = meshes.get(1);
if (m.isInMesh(newLoc,true,true,null))
{
if (report) {System.out.println(" in higher ID mesh, switch to that");}
// switch to other mesh
this.setLocation(newLoc[0],newLoc[1]);
this.placeInMesh(meshes,1,true);
move = 1;
}
}
else
{
if (report) {System.out.println(" not in higher ID mesh, boundary exit");}
// boundary exit
if (report) {System.out.println("Boundary exit: bNode "+bnode);}
this.setBoundaryExit(true);
this.setStatus(66);
move = 0;
}
}
else
{
if (report) {System.out.println(" in same mesh as was before: keep going as was");}
this.setLocation(newLoc[0],newLoc[1]);
this.placeInMesh(meshes,0,false);
move = 1;
// stay in same mesh and keep going!
}
}
}
else
{
if (meshes.size() == 2)
{
if (report) {System.out.println(" not in same mesh as before (first one in list), check other mesh");}
// Not in original mesh, so check the other one
int otherMesh = 1;
if (meshID==1)
{
otherMesh=0;
}
m = meshes.get(otherMesh);
if (m.isInMesh(newLoc,true,true,null))
{
if (report) {System.out.println(" is in other mesh, switch to that");}
// switch to other mesh
this.setLocation(newLoc[0],newLoc[1]);
this.placeInMesh(meshes,otherMesh,true);
move = 1;
}
else
{
// boundary exit
if (report) {System.out.println(" not in other mesh, boundary exit");}
this.setBoundaryExit(true);
this.setStatus(66);
move = 0;
}
}
else
{
if (report) {System.out.println(" not in single available mesh, stay at present location");}
move = 0;
}
}
// if (move == 1)
// {
// this.setLocation(newLoc[0],newLoc[1]);
// }
if (report) {System.out.println("Particle location at end of meshSelectOrExit: "+this.printLocation());}
//this.placeInMesh(m,false);
// if (this.getMesh() != meshID)
// {
if (report) {System.out.println("Particle "+this.getID()+" original mesh: "+meshID+", new mesh: "+this.getMesh());}
// }
// if (this.getMesh() != meshID)
// {
// System.out.println("Particle "+this.getID()+" original mesh: "+meshID+", new mesh: "+this.getMesh());
// }
}
/**
* This method updates the particle mesh neighbourhood information, dealing with a change in mesh ID if required
*
* @param m The mesh that has been shown to contain a particle
* @param switchedMesh Logical, has the particle changed mesh?
*/
public void placeInMesh(List<Mesh> meshes, int id, boolean switchedMesh)
{
//System.out.println("In placeInMesh "+m.getType());
Mesh m = meshes.get(id);
this.setMesh(id);
if (m.getType().equalsIgnoreCase("FVCOM") || m.getType().equalsIgnoreCase("ROMS_TRI"))
{
//System.out.println("mesh verified as FVCOM");
int el = 0;
boolean checkAll = true;
if (switchedMesh == false)
{
el = this.getElem();
checkAll = false;
}
int[] c = findContainingElement(this.getLocation(), el,
m.getNodexy(), m.getTrinodes(), m.getNeighbours(), checkAll);
// if particle is within the mesh, update location normally and save the distance travelled
this.setElem(c[0]);
}
else if (m.getType().equalsIgnoreCase("ROMS"))
{
//System.out.println("mesh verified as ROMS");
int[] searchCentreU = null, searchCentreV = null;
if (switchedMesh == false)
{
searchCentreU = this.getROMSnearestPointU();
searchCentreV = this.getROMSnearestPointV();
}
int[] nearU = nearestROMSGridPoint((float)this.getLocation()[0],(float)this.getLocation()[1],
m.getLonU(), m.getLatU(), searchCentreU);
int[] nearV = nearestROMSGridPoint((float)this.getLocation()[0],(float)this.getLocation()[1],
m.getLonV(), m.getLatV(), searchCentreV);
//System.out.println("found nearest point");
// More to do here to turn the nearest grid point into the containing element
int[] containingROMSElemU = whichROMSElement((float)this.getLocation()[0],(float)this.getLocation()[1],
m.getLonU(), m.getLatU(),
nearU);
int[] containingROMSElemV = whichROMSElement((float)this.getLocation()[0],(float)this.getLocation()[1],
m.getLonV(), m.getLatV(),
nearV);
//System.out.println("found which element");
// Need to save nearest point, in order to read into
this.setROMSnearestPointU(nearU);
this.setROMSnearestPointV(nearV);
this.setROMSElemU(containingROMSElemU);
this.setROMSElemV(containingROMSElemV);
}
}
/**
* Find the nearest mesh element centroid
* @param x
* @param y
* @param uvnode
* @return
*/
public static int nearestCentroid(double x, double y, float[][] uvnode)
{
int nearest = -1;
double dist=10000000;
for (int i = 0; i < uvnode[0].length; i++)
{
double distnew = Math.sqrt((x-uvnode[0][i])*(x-uvnode[0][i])+(y-uvnode[1][i])*(y-uvnode[1][i]));
if (distnew<dist)
{
dist=distnew;
nearest=i;
}
}
//System.out.printf("In Particle.nearestCentroid "+nearest+"\n");
return nearest;
}
/**
* Make a list of nearest mesh element centroids
* @param x
* @param y
* @param uvnode
* @return
*/
public static double[][] nearestCentroidList(double x, double y, float[][] uvnode)
{
double[][] nearestList = new double[5][2];
int nearest = -1;
double dist=10000000;
for (int i = 0; i < uvnode[0].length; i++)
{
double distnew = Math.sqrt((x-uvnode[0][i])*(x-uvnode[0][i])+(y-uvnode[1][i])*(y-uvnode[1][i]));
if (distnew<dist)
{
dist=distnew;
nearest=i;
// Shift everything along one element
nearestList[4][0]=nearestList[3][0];
nearestList[4][1]=nearestList[3][1];
nearestList[3][0]=nearestList[2][0];
nearestList[3][1]=nearestList[2][1];
nearestList[2][0]=nearestList[1][0];
nearestList[2][1]=nearestList[1][1];
nearestList[1][0]=nearestList[0][0];
nearestList[1][1]=nearestList[0][1];
nearestList[0][0]=i;
nearestList[0][1]=dist;
}
}
return nearestList;
}
/**
* Look for the nearest point in a grid of points.
* This method is somewhat naive in that it searches the entire grid. This
* means that it can handle grids which are not oriented on a strict N/S
* lattice, but is inefficient.
*
* @param x
* @param y
* @param xGrid
* @param yGrid
* @return