-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.c
1159 lines (974 loc) · 29.1 KB
/
tasks.c
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
/***************************************************************************
*
* File : tasks.c
* Student Id : <584 768>
* Name : <Beau Colley-Allerton>
*
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>
#include "tasks.h"
#include <assert.h>
//Constants
#define BUFFER 50
//Debug Switches
#define DEBUG_TASK1 0
#define DEBUG_TASK2 0
#define DEBUG_TASK3 0
#define DEBUG_TASK4 0
#define TASK2_CONSOLE_RESULTS 0
#define BST_SUCCESS 1
#define BST_FAILURE 0
void maxveldiff(const char* flow_file)
{
//Search Data keeping track of uMin, uMax, vMin, vMax
//O(n)
//Open Data File
FILE *data_in;
data_in = fopen(flow_file, "r");
if(data_in == NULL){
printf("Failed to Open File\n");
return;
}
//Store Column Headings
char headings[BUFFER];
//Check heading format
fscanf(data_in, "%s",headings);
if(strcmp(headings,"x,y,u,v")!=0){
printf("Incorrect data format - expecting <x,y,u,v>\n");
printf("Data is in %s\n",headings);
return;
}
//Initialise Temp Variables
float xTemp, yTemp, uTemp, vTemp;
//Use first point to initialise min and max variables
fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp);
float uMin = uTemp;
float uMax = uTemp;
float vMin = vTemp;
float vMax = vTemp;
struct point_data *uMin_point = setData(xTemp,yTemp, uTemp,vTemp);
struct point_data *uMax_point = setData(xTemp,yTemp, uTemp,vTemp);
struct point_data *vMin_point = setData(xTemp,yTemp, uTemp,vTemp);
struct point_data *vMax_point = setData(xTemp,yTemp, uTemp,vTemp);
//Process Data
while (fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp) > 0){
if(DEBUG_TASK1){
printf("%f,%f,%f,%f\n", xTemp,yTemp,uTemp,vTemp);
}
if(uTemp>uMax){
uMax = uTemp;
free(uMax_point);
uMax_point = setData(xTemp,yTemp, uTemp,vTemp);
}
if(uTemp<uMin){
uMin = uTemp;
free(uMin_point);
uMin_point = setData(xTemp,yTemp, uTemp,vTemp);
}
if(vTemp>vMax){
vMax = vTemp;
free(vMax_point);
vMax_point = setData(xTemp,yTemp,uTemp,vTemp);
}
if(vTemp<vMin){
vMin = vTemp;
free(vMin_point);
vMin_point = setData(xTemp,yTemp,uTemp,vTemp);
}
}
//Close Data File
fclose(data_in);
//Print Results to Console
if(DEBUG_TASK1){
printf("%s\n",headings);
printData(uMin_point);
printData(uMax_point);
printData(vMin_point);
printData(vMax_point);
}
//Print Results to CSV
FILE *data_out;
data_out = fopen("task1.csv","w+");
fprintf(data_out,"%s\n",headings);
fprintData(uMin_point,data_out);
fprintData(uMax_point,data_out);
fprintData(vMin_point,data_out);
fprintData(vMax_point,data_out);
fclose(data_out);
free(uMax_point);
free(uMin_point);
free(vMax_point);
free(vMin_point);
return;
}
void coarsegrid(const char* flow_file, int resolution)
{
//Grid Range
float CG_XMIN = 10;
float CG_XMAX = 70;
float CG_YMIN = -20;
float CG_YMAX = 20;
//Calculate dX and dY
float dx = (CG_XMAX-CG_XMIN)/(resolution-1);
float dy = (CG_YMAX-CG_YMIN)/(resolution-1);
if(DEBUG_TASK2){
printf("dx = %f dy= %f\n",dx,dy);
}
//Define 2D array of linked lists to store all values located in each cell
list_t** grid[resolution];
//Allocate Memory
for(int i=0; i<resolution; i++){
grid[i] = (list_t**)malloc(resolution * sizeof(list_t*));
for(int j=0; j<resolution; j++){
//Create New List
grid[i][j] = list_new();
//Add first node to list to be used to store a running sum of values
//as they are added. Total sum will later be converted to average
list_push_front(grid[i][j],setDataS(0,0,0,0,0));
if(DEBUG_TASK2){
printf("1 grid[%i][%i] -",i,j);
printList(grid[i][j],1);
}
}
}
if(DEBUG_TASK2){
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
printf("2 grid[%i][%i] -",i,j);
printList(grid[i][j],1);
}
}
}
//Open Data File
FILE *data_in;
data_in = fopen(flow_file, "r");
//Check File is open
if(data_in == NULL){
printf("Failed to Open File\n");
return;
}
//Store Column Headings
char headings[BUFFER];
//Check heading format
fscanf(data_in, "%s",headings);
if(strcmp(headings,"x,y,u,v")!=0){
printf("Incorrect data format - expecting <x,y,u,v>\n");
printf("Data is in %s\n",headings);
return;
}
//Initialise Temp Variables
float xTemp, yTemp, uTemp, vTemp;
float xShift, yShift;
//Process Data
while (fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp) > 0){
if(DEBUG_TASK2){
printf("Processing Point (x,y,u,v) = (%f,%f,%f,%f)\n",
xTemp,yTemp,uTemp,vTemp);
}
//Check if point is within range
if(xTemp>=CG_XMIN && xTemp<=CG_XMAX && yTemp>=CG_YMIN && yTemp<=CG_YMAX){
//Shift by XMIN and YMIN so all values are positive to allow grid
//location to be calculated
xShift = xTemp - CG_XMIN;
yShift = yTemp - CG_YMIN;
//Calculate Grid location
int xCell = xShift/dx;
int yCell = yShift/dy;
if(DEBUG_TASK2){
printf("Shifted (x,y)=(%f,%f) -> Grid[%i,%i]\n",
xShift,yShift,xCell,yCell);
}
//Add values to running total
runningSum(grid[xCell][yCell],xTemp,yTemp,uTemp,vTemp);
//Add to back of Linked List
list_push_back(grid[xCell][yCell],setData(xTemp,yTemp,uTemp,vTemp));
}else{
if(DEBUG_TASK2){
printf("Point out of range\n");
}
}
}
//Close File
fclose(data_in);
//Check List Sizes
if(DEBUG_TASK2){
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
printf("grid[%i,%i]->Num_elements = %i\n",i,j,grid[i][j]->num_elements);
printf("Sum (x,y,u,v) = (%f,%f,%f,%f)\n",
grid[i][j]->head->data->x,grid[i][j]->head->data->y,
grid[i][j]->head->data->u,grid[i][j]->head->data->v);
}
}
}
//Convert head of each list from running sum to average and calculate S value
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
calcAverage(grid[i][j]);
calcSValue(grid[i][j]);
}
}
if(TASK2_CONSOLE_RESULTS){
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
printf("grid[%i,%i] - (x_av,y_av,u_av,v_av,s) = (%f,%f,%f,%f,%f)\n",i,j,
grid[i][j]->head->data->x,grid[i][j]->head->data->y,
grid[i][j]->head->data->u,grid[i][j]->head->data->v,
grid[i][j]->head->data->s);
}
}
}
//Transfer cell averages to array
//Define Array Variables
int numCells = pow(resolution,2);
int index = 0;
struct point_data sPoints[numCells];
//Transfer Data into Array
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
sPoints[index] = *grid[i][j]->head->data;
index += 1;
}
}
//Free Memory
for(int i=0; i<resolution; i++){
for(int j=0; j<resolution; j++){
linked_list_free(grid[i][j]);
}
free(grid[i]);
}
if(TASK2_CONSOLE_RESULTS){
printf("S-Points Before Sorting\n");
for(int i=0; i<numCells; i++){
printDataS(&sPoints[i]);
}
}
//Sort Array with S Descending Comparison Function
qsort(sPoints,numCells,sizeof(struct point_data),qSortCMP_sDecending);
if(TASK2_CONSOLE_RESULTS){
printf("S-Points After Sorting\n");
for(int i=0; i<numCells; i++){
printDataS(&sPoints[i]);
}
}
//Print Results to CSV
FILE *data_out;
data_out = fopen("task2.csv","w+");
fprintf(data_out,"%s,s\n",headings);
for(int i=0; i<numCells; i++){
fprintDataS(&sPoints[i],data_out);
}
//Close File
fclose(data_out);
}
void searching(const char* flow_file)
{
//Open Data File
FILE *data_in;
data_in = fopen(flow_file, "r");
if(data_in == NULL){
printf("Failed to Open File\n");
return;
}
//Store Column Headings
char headings[BUFFER];
//Check heading format
fscanf(data_in, "%s",headings);
if(strcmp(headings,"x,y,u,v")!=0){
printf("Incorrect data format - expecting <x,y,u,v>\n");
printf("Data is in %s\n",headings);
return;
}
//Create List of y=0 Points
/* Here I decided to read data into a linked list first so I could then
* allocate a static array with the list->numElements as the size */
list_t* midPointsList = list_new();
//Temp variables
float xTemp, yTemp, uTemp, vTemp;
//Process Points
while(fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp) > 0){
if(DEBUG_TASK3){
printf("Processing Point (x,y,u,v) = (%f,%f,%f,%f)\n",
xTemp,yTemp,uTemp,vTemp);
}
//Add to linked list
if(yTemp == 0){
list_push_back(midPointsList,setData(xTemp,yTemp,uTemp,vTemp));
}else{
if(DEBUG_TASK3){
printf("Point out of Range\n");
}
}
}
//Close Data File
fclose(data_in);
//Create Array y=0 Points
point_data_Array midPointsArray;
midPointsArray.array = malloc(midPointsList->num_elements*sizeof(point_data));
midPointsArray.size = 0;
//Populate Array
for(int i=0; midPointsList->num_elements>0; i++){
midPointsArray.array[i] = list_pop_front(midPointsList);
midPointsArray.size++;
}
if(DEBUG_TASK3){
printf("Data Before Sorting\n");
for(int i=0; i<midPointsArray.size; i++){
printData(midPointsArray.array[i]);
}
}
//Sort Array with Quick Sort uAscending compare function
qsort(midPointsArray.array,midPointsArray.size,
sizeof(struct point_data),qSortcmp_uAscending);
if(DEBUG_TASK3){
printf("Data After Sorting\n");
for(int i=0; i<midPointsArray.size; i++){
printData(midPointsArray.array[i]);
}
}
//Insert into Sorted Linked List
for(int i=0; i<midPointsArray.size; i++){
list_push_back(midPointsList,midPointsArray.array[i]);
}
//Insert into BST
bst_t* bst = bst_new(no_free, bstFloatCmp);
perfect_insert(bst, midPointsArray.array, 0, midPointsArray.size - 1);
assert(bst->num_elements == midPointsArray.size);
if(DEBUG_TASK3){
printf("BST - num_elements = %d\n", bst->num_elements);
print_BST(bst->root);
}
//Get Max U Velocity
float uMax = midPointsArray.array[midPointsArray.size-1]->u;
//Target u Velocity
float uTarget = 0.4*uMax;
if(DEBUG_TASK3){
printf("uMax = %f, uTarget = %f\n",uMax,uTarget);
}
FILE *data_out;
data_out = fopen("task3.csv","w+");
struct timeval start;
struct timeval stop;
//Linear Search of Array
gettimeofday(&start, NULL);
float closestValue = arrayLinearSearch(midPointsArray.array,uTarget,data_out);
fprintf(data_out,"%f\n",closestValue);
gettimeofday(&stop, NULL);
double elapsed_ms = (stop.tv_sec - start.tv_sec) * 1000.0;
elapsed_ms += (stop.tv_usec - start.tv_usec) / 1000.0;
printf("TASK 3 Array Linear Search: %.2f milliseconds\n", elapsed_ms);
//Binary Search of Array
gettimeofday(&start, NULL);
closestValue = arrayBinarySearch(midPointsArray.array,midPointsArray.size,uTarget,data_out);
fprintf(data_out,"%f\n",closestValue);
gettimeofday(&stop, NULL);
elapsed_ms = (stop.tv_sec - start.tv_sec) * 1000.0;
elapsed_ms += (stop.tv_usec - start.tv_usec) / 1000.0;
printf("TASK 3 Array Binary Search: %.2f milliseconds\n", elapsed_ms);
//Linear Search of Linked List
gettimeofday(&start, NULL);
closestValue = linkedListLinearSearch(midPointsList->head,uTarget,data_out);
fprintf(data_out,"%f\n",closestValue);
gettimeofday(&stop, NULL);
elapsed_ms = (stop.tv_sec - start.tv_sec) * 1000.0;
elapsed_ms += (stop.tv_usec - start.tv_usec) / 1000.0;
printf("TASK 3 Linked List Linear Search: %.2f milliseconds\n", elapsed_ms);
//Linear Search of Linked List
gettimeofday(&start, NULL);
bstSearch(bst->root,uTarget,data_out);
gettimeofday(&stop, NULL);
elapsed_ms = (stop.tv_sec - start.tv_sec) * 1000.0;
elapsed_ms += (stop.tv_usec - start.tv_usec) / 1000.0;
printf("TASK 3 BST Search: %.2f milliseconds\n", elapsed_ms);
fclose(data_out);
}
void vortcalc(const char* flow_file)
{
//Open Data File
FILE *data_in;
data_in = fopen(flow_file, "r");
if(data_in == NULL){
printf("Failed to Open File\n");
return;
}
//Store Column Headings
char headings[BUFFER];
//Check heading format
fscanf(data_in, "%s",headings);
if(strcmp(headings,"x,y,u,v")!=0){
printf("Incorrect data format - expecting <x,y,u,v>\n");
printf("Data is in %s\n",headings);
return;
}
//Define holding variables
float xTempPrev, xTemp;
float yTempPrev, yTemp;
float uTempPrev, uTemp;
float vTempPrev, vTemp;
//Define index and range variables
int xRange = 1;
int yRange = 1;
int endOfX = 0;
int endOfY = 0;
//Initialise Variables with first element
fscanf(data_in, "%f,%f,%f,%f", &xTempPrev,&yTempPrev,&uTempPrev,&vTempPrev);
//Scan data to calculate necessary dimensions of x and y
while(fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp) > 0){
if(xTempPrev>xTemp){
endOfX = 1;
}
if(xTempPrev != xTemp && endOfX == 0){
xRange++;
}
if(yTempPrev != yTemp && endOfY == 0){
yRange++;
}
if(yTempPrev > yTemp){
endOfY = 1;
}
xTempPrev = xTemp;
yTempPrev = yTemp;
uTempPrev = uTemp;
vTempPrev = vTemp;
}
if(DEBUG_TASK4){
printf("Array Dimensions = [%i,%i]\n",xRange,yRange);
}
//Define 2D array to hold points
point_data*** points = (point_data***)malloc(xRange*sizeof(point_data**));
for(int i = 0; i < xRange; i++){
points[i] = (point_data**)malloc(yRange*sizeof(point_data*));
for(int j = 0 ; j < yRange ; j++){
points[i][j] = (point_data*)malloc(sizeof(point_data));
}
}
//Rewind stream to start to rescan file for contents
rewind(data_in);
int xIndex = 0;
int yIndex = 0;
//Skip Headings
fscanf(data_in, "%s",headings);
//Process Data
while(fscanf(data_in, "%f,%f,%f,%f", &xTemp,&yTemp,&uTemp,&vTemp) > 0){
if(xIndex > xRange-1){
xIndex = 0;
yIndex++;
}
if(yIndex > yRange-1){
yIndex = 0;
}
points[xIndex][yIndex]=setData(xTemp,yTemp,uTemp,vTemp);
xIndex++;
}
if(DEBUG_TASK4){
for(int i = 0; i < xRange; i++){
for(int j = 0; j < yRange; j++){
printData(points[i][j]);
}
}
}
//Define 2D array for omega values
float omega[xRange][yRange];
//Calculate Omega Values
for(int i = 0 ; i < xRange ; i++){
for(int j = 0 ; j < yRange ; j++){
if(i == xRange-1 && j == yRange-1){
//Corner Case
omega[i][j] = cornerCase(points,i,j);
}else if(j == yRange-1){
//Y Edge Case
omega[i][j] = yEdgeCase(points,i,j);
}else if(i == xRange-1){
//X Edge Case
omega[i][j] = xEdgeCase(points,i,j);
}else{
//Normal Case
omega[i][j] = normalCase(points,i,j);
}
}
}
if(DEBUG_TASK4){
printf("\nOmega Vales\n");
for(int i = 0 ; i < xRange ; i++){
for(int j = 0 ; j < yRange ; j++){
printf("%f\n",omega[i][j]);
}
}
}
//Copy values into 1D array to be sorted
int numValues = xRange*yRange;
int index = 0;
float omegaArray[numValues];
for(int i = 0 ; i < xRange ; i++){
for(int j = 0 ; j < yRange ; j++){
omegaArray[index] = omega[i][j];
index++;
}
}
// Free Memory
for(int i = 0; i < xRange; i++){
for(int j = 0 ; j < yRange ; j++){
free(points[i][j]);
}
}
free(points);
//Quick Sort array with qSortCMP Descending Floats
qsort(omegaArray,numValues,sizeof(float),qSortCMP_Descending_F);
if(DEBUG_TASK4){
printf("\nSorted Vales\n");
for(int i = 0 ; i<numValues ; i++){
printf("%f\n",omegaArray[i]);
}
}
//Open output File
FILE *data_out;
data_out = fopen("task4.csv","w+");
//Print Results
fprintf(data_out,"omega\n");
for(int i=0; i<numValues; i++){
fprintf(data_out,"%f\n",omegaArray[i]);
}
//Close File
fclose(data_out);
}
//##########################################################################
//########################### FUNCTIONS ####################################
//##########################################################################
//Vorticity Functions
float cornerCase(point_data*** points, int i, int j){
float part1 = (points[i][j]->v - points[i-1][j]->v)/
(points[i][j]->x-points[i-1][j]->x);
float part2 = (points[i][j]->u-points[i][j-1]->u)/
(points[i][j]->y - points[i][j-1]->y);
return (part1 - part2);
}
float yEdgeCase(point_data*** points, int i, int j){
float part1 = (points[i+1][j]->v - points[i][j]->v)/
(points[i+1][j]->x-points[i][j]->x);
float part2 = (points[i][j]->u-points[i][j-1]->u)/
(points[i][j]->y - points[i][j-1]->y);
return (part1 - part2);
}
float xEdgeCase(point_data*** points, int i, int j){
float part1 = (points[i][j]->v - points[i-1][j]->v)/
(points[i][j]->x-points[i-1][j]->x);
float part2 = (points[i][j+1]->u-points[i][j]->u)/
(points[i][j+1]->y - points[i][j]->y);
return (part1 - part2);
}
float normalCase(point_data*** points, int i, int j){
float part1 = (points[i+1][j]->v - points[i][j]->v)/
(points[i+1][j]->x-points[i][j]->x);
float part2 = (points[i][j+1]->u-points[i][j]->u)/
(points[i][j+1]->y - points[i][j]->y);
return (part1 - part2);
}
//Function to set data in point_data structure
struct point_data* setData(float x, float y, float u, float v){
struct point_data *point = (point_data*)malloc(sizeof(struct point_data));
point->x=x;
point->y=y;
point->u=u;
point->v=v;
//This function is only used when the S value is not used,
//Thus it is set to 0, if S value is needed us setDataS
point->s=0;
return point;
}
//Function to set data in point_data structure with s value
struct point_data* setDataS(float x, float y, float u, float v, float s){
struct point_data *point = malloc(sizeof(struct point_data));
point->x=x;
point->y=y;
point->u=u;
point->v=v;
point->s=s;
return point;
}
//DEBUG Function to print Data Point
void printData(struct point_data *point){
printf("%f,%f,%f,%f\n",point->x,point->y,point->u,point->v);
}
//DEBUG Function to print Data Point with S value
void printDataS(struct point_data *point){
printf("%f,%f,%f,%f,%f\n",point->x,point->y,point->u,point->v,point->s);
}
//Function to print data point to file
void fprintData(struct point_data *point,FILE *file){
fprintf(file,"%f,%f,%f,%f\n",point->x,point->y,point->u,point->v);
}
//Function to print data point to file with S value
void fprintDataS(struct point_data *point,FILE *file){
fprintf(file,"%f,%f,%f,%f,%f\n",
point->x,point->y,point->u,point->v,point->s);
}
//Function to print BST node to file
void fprintNode(struct node node,FILE *file){
fprintf(file,"%f,%f,%f,%f,%f\n",
node.data->x,node.data->y,node.data->u,node.data->v,node.data->s);
}
//DEBUG - Print node to console
void printNode(struct node node){
printf("%f,%f,%f,%f,%f\n",
node.data->x,node.data->y,node.data->u,node.data->v,node.data->s);
}
//Create new Linked List
list_t* list_new()
{
list_t* list;
list = (list_t*)malloc(sizeof(list_t));
assert(list != NULL);
list->head = NULL;
list->tail = NULL;
list->num_elements = 0;
return list;
}
//Add node to front of Linked List
void list_push_front(list_t* list, struct point_data* data)
{
assert(list != NULL);
node_t* new = (node_t*)malloc(sizeof(node_t));
assert(new != NULL);
new->data = data;
new->next = list->head;
list->head = new;
if (list->tail == NULL)
list->tail = new;
list->num_elements++;
}
//Add node to back of Linked List
void list_push_back(list_t* list, struct point_data* data)
{
assert(list != NULL);
node_t* new = (node_t*)malloc(sizeof(node_t));
assert(new != NULL);
new->data = data;
new->next = NULL;
if (list->tail)
list->tail->next = new;
list->tail = new;
if (list->head == NULL)
list->head = new;
list->num_elements++;
}
//Delete front element and return pointer to data
struct point_data* list_pop_front(list_t* list)
{
assert(list != NULL);
assert(list->num_elements > 0);
node_t* old;
assert(list->head != NULL);
old = list->head;
list->head = list->head->next;
struct point_data* d = old->data;
free(old);
list->num_elements--;
if (list->num_elements == 0) {
list->head = NULL;
list->tail = NULL;
}
return d;
}
//Free all memory associated with linked list
void linked_list_free(list_t* list){
while (list->head!=NULL){
free(list_pop_front(list));
}
free(list);
}
//Add values to running total stored at head node
void runningSum(list_t* list, float x, float y,float u,float v){
assert(list != NULL && "running sum = NULL");
list->head->data->x += x;
list->head->data->y += y;
list->head->data->u += u;
list->head->data->v += v;
}
//Convert Running total to averages by dividing by num_elements - 1
//The -1 is to account for the extra node to store running total
void calcAverage(list_t* list){
if(list->num_elements>1){
//Cell has at least 1 element
list->head->data->x = list->head->data->x/(list->num_elements-1);
list->head->data->y = list->head->data->y/(list->num_elements-1);
list->head->data->u = list->head->data->u/(list->num_elements-1);
list->head->data->v = list->head->data->v/(list->num_elements-1);
}else{
//No Data Points in Cell, set averages to 0
list->head->data->x = 0;
list->head->data->y = 0;
list->head->data->u = 0;
list->head->data->v = 0;
}
}
//Calculate S value
void calcSValue(list_t* list){
assert(list != NULL);
if(list->num_elements>1){
//Cell has at least 1 element
float x_av_2 = pow(list->head->data->x,2);
float y_av_2 = pow(list->head->data->y,2);
float u_av_2 = pow(list->head->data->u,2);
float v_av_2 = pow(list->head->data->v,2);
list->head->data->s = 100*((sqrt(u_av_2+v_av_2)/sqrt(x_av_2+y_av_2)));
}else{
//No data points in cell, set S = 0
list->head->data->s = 0;
}
}
//DEBUG - Print Contents of Linked List
void printList(list_t* list,int recursive){
assert(list != NULL);
printf("num_elements->%i\n",list->num_elements);
node_t* tempNode = (node_t*)malloc(sizeof(node_t));
tempNode = list->head;
printf("%f,%f,%f,%f,%f\n",tempNode->data->x,tempNode->data->y,
tempNode->data->u,tempNode->data->v,tempNode->data->s);
if(recursive){
while(tempNode->next != NULL){
printf("%f,%f,%f,%f\n",tempNode->data->x,tempNode->data->y,
tempNode->data->u,tempNode->data->v);
tempNode = tempNode->next;
}
}
}
//Create new BST structure
//Skeleton obtained from www.geeksforgeek.com
bst_t* bst_new(void (*delfunc)(void*), int (*cmpfunc)(struct point_data*, struct point_data*))
{
bst_t* bst;
bst = (bst_t*)malloc(sizeof(bst_t));
assert(bst != NULL);
bst->root = NULL;
bst->num_elements = 0;
bst->del = delfunc;
bst->cmp = cmpfunc;
return bst;
}
//Free memory from subtree
void bst_free_subtree(bst_t* bst, BSTnode_t* n)
{
assert(bst != NULL);
if (n) {
bst_free_subtree(bst, n->left);
bst_free_subtree(bst, n->right);
bst->del(n->data);
free(n);
bst->num_elements--;
}
}
//Free all memory associated with a bst
void bst_free(bst_t* bst)
{
assert(bst != NULL);
bst_free_subtree(bst, bst->root);
free(bst);
}
//insert a new element into the bst
int bst_insert(bst_t* bst, void* d)
{
assert(bst != NULL);
assert(d != NULL);
BSTnode_t* parent = NULL;
BSTnode_t* tmp = bst->root;
while (tmp) {
parent = tmp;
if (bst->cmp(tmp->data, d) > 0) { // element is smaller
tmp = tmp->left;
}
else if (bst->cmp(tmp->data, d) < 0) { // element is bigger
tmp = tmp->right;
}
else {
printf("Error bstFloatCmp returned: %i\n",bst->cmp(tmp->data, d));
return BST_FAILURE;
}
}
//Insert as child of element
BSTnode_t* new_node = (BSTnode_t*)malloc(sizeof(BSTnode_t));
assert(new_node != NULL);
new_node->data = d;
new_node->left = NULL;
new_node->right = NULL;
if (parent != NULL) {
if (bst->cmp(parent->data, d) > 0) { // element is smaller
assert(parent->left == NULL);
parent->left = new_node;
}
else {
assert(parent->right == NULL);
parent->right = new_node;
}
}
else {
assert(bst->root == NULL);
bst->root = new_node;
}
bst->num_elements++;
return BST_SUCCESS;
}
//Comparison Function for BST
int bstFloatCmp(struct point_data* a, struct point_data* b)
{
if(a->u > b->u)
return 1;
else
return -1;
}
//BST Perfect Insert
void perfect_insert(bst_t* bst, struct point_data** array, int low, int high)
{
if (low <= high) {
// Choose root from array and insert
// Recursively do the same on left and right (1)
int mid = low + (high - low) / 2;
struct point_data** ptr = array + mid;
bst_insert(bst, ptr);
perfect_insert(bst, array, low, mid - 1);
perfect_insert(bst, array, mid + 1, high);
}
}
//BST delete function
void no_free(void* v)
{
}