-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.cpp
4292 lines (3611 loc) · 129 KB
/
fish.cpp
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
#include "fish.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "tga.h"
#include <strings.h>
#include <sys/time.h>
#include <fstream>
#if MPI_ON == 1
#include "mpi.h"
#endif
const double pi = M_PI;
/* This seeds the function srandom() so that it's different every run (\mu a.e.) */
void seedRandom()
{
struct timeval tv;
gettimeofday(&tv,NULL);
srandom(tv.tv_usec);
}
/* This returns a randomVariable with value between -1 and 1 */
double randomVariable()
{
/* the maximum value of random() is (2**31)-1, so we scale down to something in the range from -1 to 1
and then multiply by amplitude to get a random number of the right size */
double rand = (double)random()/((0x40000000)-1)-1; //NB 0x40000000 is hex for 2**30
return rand;
}
//This function describe how the probability of reproduction is assigned for any given day
int probOfReproduction(int type)
{
int dayOfRep;
if (type == 0)
{
dayOfRep = rand()%366;
}
else printf("Not yet implemented!!");
return dayOfRep;
}
int probOfReproduction(int type, double mu, double std)
{
int dayOfRep;
if (type == 1)
{
double u =(double)(random() %100000 + 1)/100000; //for precision
double v =(double)(random() %100000 + 1)/100000; //for precision
double x = sqrt(-2*log(u))*cos(2*pi*v); //or sin(2*pi*v)
double y = x * std + mu;
dayOfRep = y;
}
else printf("Not yet implemented!!");
return dayOfRep;
}
//This is a recursive function to make sure the day stay between 1 and 365
int JulDay(int day) //
{
if (day > 365) {day = JulDay(day - 365);}
return day;
}
/* Instantiates a Fish: */
Fish::Fish()
{
ID = -1; //Should be assigned a positive integer later
removeMe = false;
isGhost = false;
quadrant = 0;
updateWeights(DEFAULT_INTERACTIONWEIGHT, DEFAULT_TEMPERATUREWEIGHT, DEFAULT_SELFWEIGHT);
makeRadii();
zero();
}
void Fish::print() const
{
printf( "p:(%5.3f, %5.3f) d:(%5.3f %5.3f) s:%5.3f : %d\n", x,y, cosPhi, sinPhi, speed, quadrant );
}
/* NB has not been tested */
void Fish::printToFile() const
{
FILE* file;
char positionInfo[20];
positionInfo[0]=0;
sprintf( positionInfo, "%8.6f %8.6f %d\n", x, y, (int)removeMe );
char filename[255];
filename[0] = 0;
sprintf( filename, "positionInfo_fish%d.dat", ID );
file = fopen(filename, "a+b");
if( file==NULL )
{
printf( "file %s failed to open for writing.\n", filename );
}
fwrite(positionInfo, 1, sizeof(positionInfo), file);
fclose(file);
printf("fish.%d is calling printToFile() \n", ID);
}
/* this function finds the nearest gridpoint on the grid which stores the straumur and temperature */
void Fish::findNearestGridPoint(int gridSizeX, int gridSizeY)
{
nearestGridPoint.x = (int)(x + 0.5);
nearestGridPoint.y = (int)(y+0.5);
//printf("(F.x,F.y) = (%f, %f) BUT (nearestGridPoint.x, nearestGridPoint.y) = (%d, %d) \n", (double)x, (double)y, nearestGridPoint.x, nearestGridPoint.y);
if( nearestGridPoint.x < 0 ) nearestGridPoint.x=0;
if( nearestGridPoint.x > gridSizeX-1 ) nearestGridPoint.x=gridSizeX-1;
if( nearestGridPoint.y < 0 ) nearestGridPoint.y=0;
if( nearestGridPoint.y > gridSizeY-1 ) nearestGridPoint.y=gridSizeY-1;
}
/* this function finds the quadrant of the nearest grid point where the fish is located
Doesn't yet work for isTorus=true */
void Fish::findQuadrant()
{
if (y > nearestGridPoint.y)
{
if ( x > nearestGridPoint.x)
quadrant = 0;
else
quadrant = 1;
}
else
{
if ( x < nearestGridPoint.x)
quadrant = 2;
else
quadrant = 3;
}
}
void Fish::updateWeights(double iWeight, double tWeight, double sWeight)
{
interactionWeight = iWeight;
temperatureWeight = tWeight;
selfWeight = sWeight;
}
void Fish::makeRadii()
{
/* For the moment I just want them to swim in random directions*/
radiusOfRepulsion = DEFAULT_RADIUS_OF_REPULSION;
radiusOfOrientation = DEFAULT_RADIUS_OF_ORIENTATION;
radiusOfAttraction = DEFAULT_RADIUS_OF_ATTRACTION;
}
void Fish::zero()
{
x=y=speed=cosPhi=sinPhi=0.0;
nearestGridPoint.x=nearestGridPoint.y=0;
}
/* Randomizes the position of a Fish in terms of global World coordinates */
void Fish::randomPosition(double displacementX, double displacementY, double amplitudeX, double amplitudeY)
{
/* The center of the Ocean that the Fish lives in is(displacementX,displacementY)
The distances from the center to the boundaries of the Ocean are amplitudeX and amplitudeY*/
x = amplitudeX*randomVariable() + displacementX;
y = amplitudeY*randomVariable() + displacementY;
}
void Fish::randomDirection()
{
double angle = pi*(randomVariable()+1); /* Uniform [0,2*pi] */
cosPhi = cos(angle);
sinPhi = sin(angle);
}
void Fish::randomSpeed(double lowerBound, double upperBound)
{ //Gives a uniform distribution in [lowerBound, upperBound]
// speed = amplitude*((double)random()/(2*(0x40000000)-1));
speed = lowerBound + (upperBound-lowerBound)*fabs(randomVariable());
}
void Fish::setPosition(double inx, double iny)
{
x=inx;
y=iny;
}
void Fish::setDirection(double angle)
{
cosPhi = cos(angle*(pi/180));
sinPhi = sin(angle*(pi/180));
}
void Fish::setSpeed(double inSpeed)
{
speed=inSpeed;
}
void Fish::setVelocity(double inSpeed, double inCosPhi, double inSinPhi, double inSelfWeight)
{
speed = inSpeed;
cosPhi = inCosPhi;
sinPhi = inSinPhi;
selfWeight = inSelfWeight;
}
void Fish::resetSpeed(double inSpeed)
{
//if(x>68) //This corresponds to 13.5 West
printf("Fish.%d.speed is resetting! From Feb through July 2008, this only was reset if F.x > 68 \n", ID);
{ speed=inSpeed;
}
}
/* This function resets the direction of a fish to theta with a random perturbation of amplitude noiseAmp */
void Fish::resetDirection(double theta, double noiseAmp)
{
double direction = theta+noiseAmp*randomVariable();
cosPhi = cos(direction);
sinPhi = sin(direction);
}
void Fish::setVelocity(double vx, double vy, double inSelfWeight)
{
speed = sqrt(vx*vx+vy*vy);
if(speed < DECISION_TOL)
cosPhi = sinPhi = 0.0;
else
{
cosPhi = vx/speed;
sinPhi = vy/speed;
}
selfWeight = inSelfWeight;
}
void Fish::copyVelocity()
{
oldCos = cosPhi;
oldSin = sinPhi;
oldSpeed = speed;
}
/* Calculates the distance squared from this Fish to Fish F */
double Fish::distanceToSquared(const Fish& F)
{
return (F.x-x)*(F.x-x) + (F.y-y)*(F.y-y);
}
double Fish::getPrefSpeed()
{
//Fish keeps track of its roe percentage: F.roePercentage
//NB The numbers here are set manually!
double prefSpeed;
if(roePercentage < DEB_ROE_PERCENTAGE_MARK) //About 8% roe
prefSpeed = DEFAULT_SPEED_NO_ROE; // km/day
else if(roePercentage < DEB_ROE_PERCENTAGE_MAX) //Abotu 25%
prefSpeed = DEFAULT_SPEED_NO_ROE + MAX_SPEED_INCREASE*(roePercentage-DEB_ROE_PERCENTAGE_MARK)/(DEB_ROE_PERCENTAGE_MAX - DEB_ROE_PERCENTAGE_MARK); // km/day
else
prefSpeed = DEFAULT_SPEED_NO_ROE + MAX_SPEED_INCREASE; // km/day
prefSpeed /= 12.0; //Done to switch to grid units
return prefSpeed;
}
void Fish::updateVelocity()
{
if( !isGhost and age >= AGESELFMOVE)
{
double averageCos = selfWeight*oldCos;
double averageSin = selfWeight*oldSin;
double averageSpeed = oldSpeed;
double radiusOfAttractionSquared = radiusOfAttraction*radiusOfAttraction;
double neighborCounter = selfWeight;
int orientationCounter = 1;
for(int i=myOcean->binarySearchxCoordinate(x-radiusOfAttraction); i < myOcean->numberOfFish; i++)
{
Fish& neighbor(myOcean->fish[i]);
if(&neighbor==this)
continue;
if (neighbor.age < AGESELFMOVE) //Avoid the juveniles to affect the movement of the adults!!)
break;
if(neighbor.x > x+radiusOfAttraction)
break;
if(neighbor.y > y+radiusOfAttraction || neighbor.y < y - radiusOfAttraction)
continue;
double proximitySquared = distanceToSquared(neighbor);
if (proximitySquared < radiusOfAttractionSquared) // if its in the radius of attraction we'll consider it
{
double proximity = sqrt(proximitySquared); // first take the distance to the neighbor fish
neighborCounter = neighborCounter+1; // count it as a neighbor to be used in averaging later
if(proximity < radiusOfOrientation) // determine if its in the radius of orientation
{
averageSpeed += neighbor.oldSpeed;
orientationCounter++; // only things in the zone of orientation affect the speed, so they get counted separately
if(proximity < radiusOfRepulsion)
{ // if its in the radius of repulsion, we add to the average velocity in the direction pointed away from the neighbor...
if( proximity > DECISION_TOL)
{
averageCos += (x-neighbor.x)/proximity;
averageSin += (y-neighbor.y)/proximity;
}
continue; // ... and that's all we do for this neighbor
}
averageCos += neighbor.oldCos; // if it's in the zone of orientation, but not the zone of repulsion, then we add the velocity of the neighbor to the average velocity...
averageSin += neighbor.oldSin;
continue; // ... and that's that
}
averageCos += (neighbor.x-x)/proximity; // if it's outside the zone of orientation, but inside the zone of attraction, we add to the average velocity in the direction pointed toward from the neighbor
averageSin += (neighbor.y-y)/proximity;
}
}
averageCos /= neighborCounter;
averageSin /= neighborCounter;
double normOfAveDirection = sqrt(averageCos*averageCos + averageSin*averageSin);
if(normOfAveDirection < DECISION_TOL)
{
//printf("uh oh, at least one inf \n");
cosPhi = averageCos;
sinPhi = averageSin;
}
else
{
cosPhi = averageCos/normOfAveDirection; // (cosPhi, sinPhi) should be a unit vector, so we divide by the norm.
sinPhi = averageSin/normOfAveDirection;
}
//Calculate the speed of neighboring particles:
averageSpeed /= orientationCounter; //nb orientation counter is at least 1
//Finally, set the found speed:
#if DEB_ON
double prefSpeed;
prefSpeed = getPrefSpeed();
speed = (1-ALPHA)*averageSpeed + ALPHA*prefSpeed;
#else
//Just average the interactions:
speed = averageSpeed;
#endif
myOcean->interactionCounter += int(neighborCounter - selfWeight);
} // Ending the isGhost check
} // Ending Fish::updateVelocity
/* This function adds noise of the given amplitude to the direction angle of the fish */
void Fish::addNoiseToDirectionAngle(double noiseAmplitude)
{
double R = noiseAmplitude*randomVariable();
double cosR = cos(R);
double sinR = sin(R);
sinPhi = sinPhi*cosR + cosPhi*sinR;
cosPhi = cosPhi*cosR - sinPhi*sinR;
}
void Fish::move(double timestep)
{
x += cosPhi*speed*timestep;
y += sinPhi*speed*timestep;
}
void Fish::emptyLandOfFish()
{
if( myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].temperature > 998)
{
removeMe = true;
}
if(myOcean->myWorld->getTemperature(nearestGridPoint)>998)
removeMe=true;
}
void Fish::initializeFishPositions_Rectangle(double leftBoundary, double rightBoundary, double upperBoundary, double lowerBoundary)
{
if( myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].temperature < 998)
if(nearestGridPoint.x < leftBoundary || nearestGridPoint.x > rightBoundary
|| nearestGridPoint.y < lowerBoundary || nearestGridPoint.y > upperBoundary)
removeMe = true;
}
void Fish::initializeFishPositions(Vector center1, double majAx1, double minAx1, Vector center2, double majAx2, double minAx2,
Vector center3, double majAx3, double minAx3, Vector center4, double majAx4, double minAx4,
Vector center5, double majAx5, double minAx5, Vector center6, double majAx6, double minAx6,
Vector center7, double majAx7, double minAx7, Vector center8, double majAx8, double minAx8)
{
/* removeMe is set false if the fish are in "safe zones" */
if( minAx1*minAx1*(x-center1.x)*(x-center1.x) + majAx1*majAx1*(y-center1.y)*(y-center1.y) < minAx1*minAx1*majAx1*majAx1 ||
minAx2*minAx2*(x-center2.x)*(x-center2.x) + majAx2*majAx2*(y-center2.y)*(y-center2.y) < minAx2*minAx2*majAx2*majAx2 ||
minAx3*minAx3*(x-center3.x)*(x-center3.x) + majAx3*majAx3*(y-center3.y)*(y-center3.y) < minAx3*minAx3*majAx3*majAx3 ||
minAx4*minAx4*(x-center4.x)*(x-center4.x) + majAx4*majAx4*(y-center4.y)*(y-center4.y) < minAx4*minAx4*majAx4*majAx4 ||
minAx5*minAx5*(x-center5.x)*(x-center5.x) + majAx5*majAx5*(y-center5.y)*(y-center5.y) < minAx5*minAx5*majAx5*majAx5 ||
minAx6*minAx6*(x-center6.x)*(x-center6.x) + majAx6*majAx6*(y-center6.y)*(y-center6.y) < minAx6*minAx6*majAx6*majAx6 ||
minAx7*minAx7*(x-center7.x)*(x-center7.x) + majAx7*majAx7*(y-center7.y)*(y-center7.y) < minAx7*minAx7*majAx7*majAx7 ||
minAx8*minAx8*(x-center8.x)*(x-center8.x) + majAx8*majAx8*(y-center8.y)*(y-center8.y) < minAx8*minAx8*majAx8*majAx8 )
removeMe = false;
else
{
removeMe = true;
}
}
int Fish::escapeRegion()
{
return myOcean->escapeRegion(*this);
}
void Fish::mimmicRecord(const FishRecord& R)
{
x = R.x;
y = R.y;
cosPhi = R.cosPhi;
sinPhi = R.sinPhi;
speed = R.speed;
selfWeight = R.selfWeight;
}
void Fish::makeRecord(FishRecord& R) const
{
R.x = x;
R.y = y;
R.cosPhi = cosPhi;
R.sinPhi = sinPhi;
R.speed = speed;
R.selfWeight = selfWeight;
}
void Fish::initializeMaturityLevels(int initialMaturityLevel)
{
maturityLevel = initialMaturityLevel;
}
/* DEB functions: */
/* Initiates the variables for the DEB model */
void Fish::initializeDEB(double l0, double e0, double uR0, double er0, int initialMaturityLevel, double initialDaysSinceMature, double currentTime)
{
l = l0;
e = e0;
uR = uR0;
er = er0;
maturityLevel = initialMaturityLevel;
daysSinceMature = initialDaysSinceMature;
double Wroe = (DEB_U2E/DEB_RHO_ROE) * er;
double Lm3 = DEB_LM*DEB_LM*DEB_LM;
double l3 = l*l*l;
double V = Lm3*l3;
double Weight = V*DEB_DV + (DEB_U2E/DEB_RHO_E)*(l3*e + uR + er) + Wroe;
roePercentage = 100*Wroe/Weight;
if(ID%CHOOSE_EVERY_NTH_FISH_TO_PRINT_DEB == 0)
{
printInitialBiologyToFile();
}
}
/* This function solves the coupled DEB ODEs using 4th order Runge Kutta with time step h.
Functions when h<(endTime-startTime)
It saves a copy of the current length, energy reserves, reproduction buffer and roe energy so they won't be overwritten while needed.
At the end of the function, it updates F.weight, etc */
void Fish::solveDEB(double h, double startTime, double endTime)
{
//printf("ID of fish = %d \n",ID);
double length = l;
double energy = e;
double repBuffer = uR;
double roeEn = er;
double t = startTime;
double k11,k12,k13,k14;
double k21,k22,k23,k24;
double k31,k32,k33,k34;
double k41,k42,k43,k44;
bool flaggy = myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].tempCorrectionFlag;
if (!flaggy)
{ printf("Holy shit, %d %9.6f %9.6f %9.6f\n",ID,x,y,endTime);
printf("Temp in nearest grid point: %9.6f\n",myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].temperature);
exit(0);
}
double nu = myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].tempCorrected_nu;
double kJ = myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].tempCorrected_kJ;
double gamma = myOcean->myWorld->grid[nearestGridPoint.x][nearestGridPoint.y].tempCorrected_gamma;
while (t+h <= endTime)
{
/* Here comes 4th order Runge-Kutta. Summary:
y_prime = f(t,y), y(t_0) = y_0
Then:
k_1 = f(t_n, y_n)
k_2 = f(t_n + h/2, y_n + (h/2)*k1)
k_3 = f(t_n + h/2, y_n + (h/2)*k2)
k_4 = f(t_n + h, y_n + h*k_3)
y_{n+1} = y_n + (h/6)*(k_1 + 2*k_2 + 2*k_3 + k_4)
t+{n+1} = t_n + h
*/
k11 = dl( t, length, energy,nu);
k12 = de( t, length, energy,nu);
k13 = duR(t, length, energy,nu,kJ);
k14 = der(t, repBuffer, roeEn,gamma);
k21 = dl( t+h/2, length+(h/2)*k11, energy+(h/2)*k12,nu);
k22 = de( t+h/2, length+(h/2)*k11, energy+(h/2)*k12,nu);
k23 = duR(t+h/2, length+(h/2)*k11, energy+(h/2)*k12,nu,kJ);
k24 = der(t+h/2, repBuffer+(h/2)*k13, roeEn+(h/2)*k14,gamma);
k31 = dl( t+h/2, length+(h/2)*k21, energy+(h/2)*k22,nu);
k32 = de( t+h/2, length+(h/2)*k21, energy+(h/2)*k22,nu);
k33 = duR(t+h/2, length+(h/2)*k21, energy+(h/2)*k22,nu,kJ);
k34 = der(t+h/2, repBuffer+(h/2)*k23, roeEn+(h/2)*k24,gamma);
k41 = dl( t+h, length+h*k31, energy+h*k32,nu);
k42 = de( t+h, length+h*k31, energy+h*k32,nu);
k43 = duR(t+h, length+h*k31, energy+h*k32,nu,kJ);
k44 = der(t+h, repBuffer+h*k33, roeEn+h*k34,gamma);
length += (h/6)*(k11 + 2*k21 + 2*k31 + k41);
energy += (h/6)*(k12 + 2*k22 + 2*k32 + k42);
repBuffer += (h/6)*(k13 + 2*k23 + 2*k33 + k43);
roeEn += (h/6)*(k14 + 2*k24 + 2*k34 + k44);
t += h;
}
// Now it's ok to overwrite these variables, since the functions which need the old
//info have already been computed
l = length;
e = energy;
uR = repBuffer;
er = roeEn;
if (maturityLevel == IMMATURE)
{// Check whether the roe percentage has exceeded 8% -
double Wroe = (DEB_U2E/DEB_RHO_ROE) * er;
double Lm3 = DEB_LM*DEB_LM*DEB_LM;
double l3 = l*l*l;
double V = Lm3*l3;
double Weight = V*DEB_DV + (DEB_U2E/DEB_RHO_E)*(l3*e + uR + er) + Wroe;
roePercentage = 100*Wroe/Weight;
// NB The roe percentage is 100*Wroe/Weight;
if( roePercentage > DEB_ROE_PERCENTAGE_MARK) // here denoting a mature fish
{
maturityLevel = MATURE;
}
}
else
{// We need to account for a higher water content of the roe
double rho_r;
//double time2max = DEB_TIME2MAX; //days
//double rhoMaxIncrease = DEB_ROE_MAX_WATERINCREASE;// percentage
daysSinceMature += endTime-startTime;
if ( DEB_TIME2MAX <= daysSinceMature)
{//Take to be the maximum
rho_r = DEB_RHO_ROE/( 1 + DEB_ROE_MAX_WATERINCREASE );
}
else
{//We increase by some value:
rho_r = DEB_RHO_ROE/( 1 + DEB_ROE_MAX_WATERINCREASE*(daysSinceMature/DEB_TIME2MAX) );
}
//And now, calculate the roe percentage:
double Wroe = (DEB_U2E/rho_r) * er;
//double Lm3 = DEB_LM*DEB_LM*DEB_LM;
double l3 = l*l*l;
//double V = Lm3*l3;
double V = (DEB_LM*DEB_LM*DEB_LM)*l3;
double Weight = V*DEB_DV + (DEB_U2E/DEB_RHO_E)*(l3*e + uR + er) + Wroe;
roePercentage = 100*Wroe/Weight;
}
// This might slow down the code a bit
if(ID%CHOOSE_EVERY_NTH_FISH_TO_PRINT_DEB == 0 && ((int)(endTime*100))%100 == 0) // writes out every day
{
printBiologyHistoryToFile(endTime);
}
}//void Fish::solveDEB
//NB nu needs to be temperature dependent, should be done when called!
double Fish::dl(double t, double length, double energy, double nu)
{
double l_prime = nu/(3*DEB_LM) * (energy - length)/(energy + DEB_G);
if (l_prime<0)
return 0;
else
return l_prime;
}
//NB nu needs to be temperature dependent, should be done when called!
double Fish::de(double t, double length, double energy, double nu)
{
double f;//=0.5*(cos(t/365.0*2*pi)+1.0)/2.0+0.5;
double daysWithFood = 30.0;
if (t<daysWithFood)
f = 0.5*( 1.0 - t/daysWithFood ); // This is food being eaten or food density or something
else
f=0;
double e_prime = nu/(length*DEB_LM) * (f - energy);
return e_prime;
}
//NB nu and kJ need to be temperature dependent, should be done when called!
/* This includes the temperature of the nearest gridPoint as the temperature */
double Fish::duR(double t, double length, double energy, double nu, double kJ)
{
double uR_prime = nu/DEB_LM * (1-DEB_KAPPA)* energy*length*length * (length + DEB_G) / (energy + DEB_G) - kJ*DEB_UHP;
return uR_prime;
}
//NB nu needs to be temperature dependent, should be done when called!
double Fish::der(double t, double repBuffer, double roeEn, double gamma)
{
double er_prime = gamma * (repBuffer - roeEn) * roeEn;
return er_prime;
}
// Begins the .m file with a variable name, etc.
void Fish::printInitialBiologyToFile()
{
FILE* file;
char biologyData[90];
biologyData[0]=0;
sprintf( biologyData, "%9.6f %9.6f %10.6f %10.6f %10.6f %10.6f %11.6f %d %11.6f\n", l, e, uR, er, roePercentage, speed, 0.0, maturityLevel, daysSinceMature );
char filename[255];
filename[0] = 0;
sprintf( filename, "DEBoutput/biologicalHistory_fish%d.dat", ID );
char initialComment[202]; // length of following string
initialComment[0]=0;
sprintf( initialComment, "%% This file provides biological history for fish with ID given in file name\n%% columns record, from left to right: \n%% l, e, u_R, e_r, roePercentage, speed, time in days, maturityLevel, daysSinceMature\n\n");
file = fopen(filename, "wb");
if( file==NULL )
{
printf( "file %s failed to open for writing.\n", filename );
}
fwrite(initialComment, 1, sizeof(initialComment), file);
fwrite(biologyData, 1, sizeof(biologyData), file);
fclose(file);
//printf("fish.%d is calling printInitialBiologyToFile() \n", ID);
}
// Prints the weight, fat, roe, time into an array of numbers which matlab can read
void Fish::printBiologyHistoryToFile(double time)
{
FILE* file;
char biologyData[90];
biologyData[0]=0;
sprintf( biologyData, "%9.6f %9.6f %10.6f %10.6f %10.6f %10.6f %11.6f %d %11.6f\n", l, e, uR, er, roePercentage, speed, time, maturityLevel, daysSinceMature );
char filename[255];
filename[0] = 0;
sprintf( filename, "%s/Beta_%f/SST_%d_%d/DEBoutput/biologicalHistory_fish%d.dat", MAINDIR, BETA, SSTmin, SSTmax, ID );
file = fopen(filename, "a+b");
if( file==NULL )
{
printf( "file %s failed to open for writing.\n", filename );
}
fwrite(biologyData, 1, sizeof(biologyData), file);
fclose(file);
//printf("fish.%d is calling printBiologyHistoryToFile() \n", ID);
}
// --------------------------------------------------//
/* Here are all the functions that I added to *
* the class Fish */
// --------------------------------------------------//
// This function create a new fish when the prpulation reproduce
// This function allows me to update the reproductive status of each fish!
void Fish::setReproduction(int k, bool Reproduced)
{
if (age >= AGEMATURE) //365/TIMESTEP)
{
isAdult = true;
}
}
/* En of Fish class*/
GridPoint::GridPoint()
{
flagQuadrant = new bool[4];
temperatureGradientAtCenter = new Vector[NUMMATURITYLEVELS];
temperatureGradAtCenterFactor = new double[NUMMATURITYLEVELS];
temperatureGradient = new Vector*[4];
temperatureGradFactor = new double*[4];
for (int i=0; i<4; i++)
{
temperatureGradient[i] = new Vector[NUMMATURITYLEVELS];
temperatureGradFactor[i] = new double[NUMMATURITYLEVELS];
}
zero();}
GridPoint::~GridPoint()
{
delete[] flagQuadrant;
//These guys depend on the maturity levels:
delete[] temperatureGradientAtCenter;
delete[] temperatureGradAtCenterFactor;
//The rest is [quadrants][maturity levels]
for (int j=0; j<4; j++)
{//First, take care of the maturity levels:
delete[] temperatureGradient[j];
delete[] temperatureGradFactor[j];
}
//And then the quadrants:
delete[] temperatureGradient;
delete[] temperatureGradFactor;
}
void GridPoint::zero()
{
temperature = 0.0;
density = 0;
AdultDensity = 0;
FM = 0;
FMpp = 0; //Number of fish that can potentially be catch!
FCatches = 0; //Number of fish caught by the fishery.
AnnualCatches = 0;
boats = totOfBoats/(GRIDSIZEX * GRIDSIZEY); // Number of boats to allocate in the each pixel of the grid.
BoatsXFMpp = 0;
bp = 0;
//Used if DEB is on:
tempCorrectionFlag = false;
temperatureCorrection = 1.0;
tempCorrected_nu = 0.0;
tempCorrected_kJ = 0.0;
tempCorrected_gamma = 0.0;
flagQuadrant[0] = false;
flagQuadrant[1] = false;
flagQuadrant[2] = false;
flagQuadrant[3] = false;
gradientFlag = false;
for (int j=0; j<NUMMATURITYLEVELS; j++)
{
//Note the quadrants:
temperatureGradient[0][j].x = temperatureGradient[0][j].y = 0;
temperatureGradient[1][j].x = temperatureGradient[1][j].y = 0;
temperatureGradient[2][j].x = temperatureGradient[2][j].y = 0;
temperatureGradient[3][j].x = temperatureGradient[3][j].y = 0;
temperatureGradFactor[0][j] = 0;
temperatureGradFactor[1][j] = 0;
temperatureGradFactor[2][j] = 0;
temperatureGradFactor[3][j] = 0;
//Here there are no quadrants:
temperatureGradientAtCenter[j].x = 0;
temperatureGradientAtCenter[j].y = 0;
temperatureGradAtCenterFactor[j] = 0;
}
straumur.x = straumur.y = 0;
}
Ocean::Ocean(const Ocean& O)
{
printf( "SNAP! You called Ocean's copy constructor.\n" );
exit(0);
}
Ocean::Ocean()
{
RunOnThread = 0;
fishRecordList = NULL;
numberOfFish = 0;
int NumberOfFishToRep = 0;
sizeOfFish = 32;
fish = (Fish*)malloc( sizeof(Fish)*sizeOfFish );
}
Ocean::~Ocean()
{
free(fishRecordList);
free(fish);
}
void Ocean::add(const FishRecord& R, bool inIsGhost)
{
Fish F;
F.mimmicRecord(R);
if( inIsGhost )
addAsGhost(F);
else
add(F);
}
void Ocean::add(const Fish& F)
{
reallocate();
fish[numberOfFish] = F;
fish[numberOfFish].myOcean = this;
numberOfFish++;
}
void Ocean::addAsGhost(const Fish& F)
{
reallocate();
fish[numberOfFish] = F;
fish[numberOfFish].isGhost = true;
fish[numberOfFish].myOcean = this;
numberOfFish++;
}
// Here sizeOfFish is the size of the memory block used for storing the fish
// It gets doubled when not big enough and halved when huge
void Ocean::reallocate()
{
if( numberOfFish<16 )
return; //(if it's that small don't even bother.)
if( numberOfFish<sizeOfFish/2 )
{
sizeOfFish/=2;
fish = (Fish*)realloc( fish, sizeof(Fish)*sizeOfFish );
}
else if( numberOfFish >= sizeOfFish )
{
sizeOfFish*=2;
fish = (Fish*)realloc( fish, sizeof(Fish)*sizeOfFish );
}
}
void Ocean::print() const
{
for(int i=0; i<numberOfFish; i++)
{
Fish& F(fish[i]);
printf( "%3.d ", i );
F.print();
}
printf( "\n\n" );
}
void Ocean::printGhostFish() const
{
for(int i=0; i<numberOfFish; i++)
{
Fish& F(fish[i]);
if( F.isGhost )
{
printf( "%3.d ", i );
F.print();
}
}
printf( "\n\n" );
}
void Ocean::clear()
{
numberOfFish = 0;
sizeOfFish = 32;
free(fish);
fish = (Fish*)malloc(sizeof(Fish)*sizeOfFish); //heh
}
void Ocean::setSize(int inNumberOfFish)
{
clear();
Fish F;
for (int i=0; i < inNumberOfFish; i++)
add(F); /* every call to add copies F, so this will actually add numberOfFish brand new fish to the ocean
each of which is identical to F*/
}
void Ocean::setDirection(double angle)
{
for(int i=0; i<numberOfFish; i++)
{
fish[i].setDirection(angle);
}
}
Ocean::Ocean(int inNumberOfFish)
{
RunOnThread = 0;
fishRecordList = NULL;
sizeOfFish = 32;
fish = (Fish*)malloc(sizeof(Fish)*sizeOfFish);
setSize(inNumberOfFish);
}
void Ocean::zero()
{
for(int i=0; i<numberOfFish; i++)
fish[i].zero();
}
int compare(const void * a, const void * b)
{
double t = ((Fish*)a) -> x - ((Fish*)b) -> x;
if(t<0)
return -1;
else if(t==0)
return 0;
else
return 1;
}
void Ocean::sortByx()
{
qsort(fish, numberOfFish, sizeof(Fish), compare);
}
int Ocean::binarySearchxCoordinate(double M)
{
int lower = 0;
int upper = numberOfFish-1;
while(upper-lower>1)
{
int middle = (upper+lower)/2;
if( fish[middle].x <= M)
lower = middle;
else
upper = middle;
}
if(fish[lower].x < M)
return upper;
else
return lower;
}
void Ocean::initializeFish(double displacementX, double displacementY, double amplitudeX, double amplitudeY, double speedLowerBound, double speedUpperBound, double inSelfWeight)
{
for(int i=0; i<numberOfFish; i++)
{
fish[i].randomSpeed(speedLowerBound, speedUpperBound);
fish[i].selfWeight = inSelfWeight;
}
}
/* Assigns ID numbers to each fish in its fish array */
void Ocean::initializeFishID(int currentFishID)
{
for(int i=0; i<numberOfFish; i++)
{
fish[i].ID = currentFishID+i;