-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluent.c
1832 lines (1479 loc) · 52.4 KB
/
fluent.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
/***********************************************************************/
/* Routines to define and read a "simple" Fluent case file. This code */
/* is not fully Fluent case format compliant. It is my adoptation of */
/* the Fluent case format for use in my codes. */
/* The code can write an HDF file with the case-file contents. */
/* */
/* Version 1.9 */
/* Copyright 2011-2020 Ioannis Nompelis <nompelis@nobelware.com> */
/***********************************************************************/
/**************************************************************************
Copyright (c) 2011-2020, Ioannis Nompelis
All rights reserved.
Redistribution and use in source and binary forms, with or without any
modification, are permitted provided that the following conditions are met:
1. Redistribution of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistribution in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
"This product includes software developed by Ioannis Nompelis."
4. Neither the name of Ioannis Nompelis and his partners/affiliates nor the
names of other contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
5. Redistribution or use of source code and binary forms for profit must
have written permission of the copyright holder.
THIS SOFTWARE IS PROVIDED BY IOANNIS NOMPELIS ''AS IS'' AND ANY
EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL IOANNIS NOMPELIS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************/
/* -----------------------------------------------------------------
"What this file is and what it isn't."
- Will display the line number in the file at state changes.
- Will read nodes and faces correctly. It will allocate memory
for the necessary arrays, but it has a bit of my twist to it.
- Will store the number of cells, but it will not identify any
cell-region of the domain in any zone as anything special. And
this means that any zone with an id corresponding to cells in
the case file will be left blank.
- Will store the dimensionality of the problem (physical space)
- Will echo comments to the screen.
- There is significant error trapping but no clean-up on fatal
errors. (This needs to be changed in the future.)
---------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "fluent.h"
#include "fluent_defs.h"
#ifdef _USE_HDF
#include <hdf5.h>
#include "hdf5_struct.h"
#endif
/*
* Fucntion to handle the reading of nodes from the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20130617
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20191216
*/
int inFluent_HandleCells(struct my_fluentcase *cas, char *data,
long *nline, FILE *fp) {
char *FUNC = "inFluent_HandleCells";
int id,nd,itmp;
long istart,iend;
struct my_fluentzone *zp;
int ins;
// note that it is like this:
// (12 (zone-id first-index last-index type XX)) OR
// (12 (0 first-index last-index type))
sscanf(data,"(12 (%x %lx %lx %d %d)",&id,&istart,&iend,&itmp,&nd);
if(id == 0) {
fprintf(stdout," i [%s] Case file provided total number of cells: %ld \n",FUNC,iend);
cas->nel = iend;
ins = 0;
} else {
fprintf(stdout," i [%s] Found cell-zone %d with bounds [%ld:%ld] \n",FUNC,id,istart,iend);
if(cas->nzone == cas->mzone) {
fprintf(stdout," e [%s] Not enough preallocated zones (%d) \n",FUNC,cas->mzone);
return(1);
} else {
zp = (struct my_fluentzone *) &(cas->zones[cas->nzone]);
zp->id = id; // this may have to be moved further down
zp->type = 12; // this may have to be moved further down
zp->nstart = istart;
zp->nend = iend;
zp->iattr = nd;
}
if(cas->nel <= 0) {
zp->imem = 20;
ins = 2;
} else {
zp->imem = 2;
ins = 1;
}
}
// if(ins == 1) {
// for(n=istart-1;n<iend;++n) {
// fgets(line,100,fp);
// *nline = *nline + 1;
//
// sscanf(line,"%lf %lf %lf",&x,&y,&z);
//
// // some data needs to be inserted somewhare
// // this is likely volumetric designator data
// }
//
// } else if(ins == 2) {
//
// for(n=istart-1;n<iend;++n) {
// fgets(line,100,fp);
// *nline = *nline + 1;
//
// sscanf(line,"%lf %lf %lf",&x,&y,&z);
//
// // some data needs to be inserted somewhare
// // this is likely volumetric designator data
// }
// }
if(ins != 0) {
// fgets(line,100,fp);
// *nline = *nline + 1;
// if(strncmp("))",line,2) != 0) {
// fprintf(stdout," e [%s] Could not find cells-terminating line\n",FUNC);
// return(1);
// } else {
// fprintf(stdout," i [%s] Got info on %ld cells \n",FUNC,iend-istart+1);
// zp->nstart = istart;
// zp->nend = iend;
// // fprintf(stdout," This zones's bounds %ld %ld \n",zp->nstart,zp->nend);
// }
fprintf(stdout," i [%s] Got info on %ld cells \n",FUNC,iend-istart+1);
zp->nstart = istart;
zp->nend = iend;
cas->nzone += 1;
}
return(0);
}
/*
* Fucntion to handle the reading of nodes from the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110401
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20160930
*/
int inFluent_HandleNodes(struct my_fluentcase *cas, char *data,
long *nline, FILE *fp) {
int id,it,nd;
long istart,iend;
size_t size;
struct my_fluentzone *zp;
long n;
int ins;
double x,y,z;
char line[200];
// not that it is like this:
// (10 (zone-id first-index last-index type ND)( OR
// (10 (0 first-index last-index type ND))
// where "type" is always zero
sscanf(data,"(10 (%x %lx %lx %x %d)",&id,&istart,&iend,&it,&nd);
if(id == 0) {
fprintf(stdout," i [inFluent_HandleNodes] Case file provided total number of nodes: %ld \n",iend);
size = iend*sizeof(double);
cas->x = (double *) malloc(size);
cas->y = (double *) malloc(size);
cas->z = (double *) malloc(size);
if(cas->x == NULL || cas->y == NULL || cas->z == NULL) {
fprintf(stdout," e [inFluent_HandleNodes] Could not allocate memory for nodes \n");
if(cas->x != NULL) free(cas->x);
if(cas->y != NULL) free(cas->y);
if(cas->z != NULL) free(cas->z);
cas->x = NULL;
cas->y = NULL;
cas->z = NULL;
} else {
fprintf(stdout," i [inFluent_HandleNodes] Allocated %f MBs for nodes \n",
((double) (size*3)) / ((double) (1024*1024)) );
}
cas->nno = iend;
ins = 0;
} else {
fprintf(stdout," i [inFluent_HandleNodes] Found node-zone %d with bounds [%ld:%ld] \n",id,istart,iend);
if(cas->nzone == cas->mzone) {
fprintf(stdout," e [inFluent_HandleNodes] Not enough preallocated zones (%d) \n",cas->mzone);
return(1);
} else {
zp = (struct my_fluentzone *) &(cas->zones[cas->nzone]);
zp->id = id; // this may have to be moved further down
zp->type = 10; // this may have to be moved further down
zp->iattr = it;
}
if(cas->nno <= 0) {
zp->imem = 10;
// Here I am speculating that there could be a possibility where
// nodes are part of a zone but the total number of nodes was not
// given in the case file. If true, I allocate space for the nodes
// as if they are part of this zone, which is what happens below.
// If false, I should just return an error for out-of-order case items
size = (iend - istart + 1)*sizeof(double);
zp->x = (double *) malloc(size);
zp->y = (double *) malloc(size);
zp->z = (double *) malloc(size);
if(zp->x == NULL || zp->y == NULL || zp->z == NULL) {
fprintf(stdout," e [inFluent_HandleNodes] Could not allocate memory for nodes within a zone \n");
if(zp->x != NULL) free(zp->x);
if(zp->y != NULL) free(zp->y);
if(zp->z != NULL) free(zp->z);
zp->x = NULL;
zp->y = NULL;
zp->z = NULL;
return(-1);
} else {
fprintf(stdout," i [inFluent_HandleNodes] Allocated %f MBs for nodes in zone %d \n",
((double) (size*3)) / ((double) (1024*1024)) ,id);
}
ins = 2;
} else {
zp->imem = 1;
fprintf(stdout," i [inFluent_HandleNodes] Inserting nodes in array\n");
zp->x = (double *) &(cas->x[istart-1]);
zp->y = (double *) &(cas->y[istart-1]);
zp->z = (double *) &(cas->z[istart-1]);
ins = 1;
}
}
// no error-trapping during reading operations!
if(ins == 1) {
for(n=istart-1;n<iend;++n) {
fgets(line,100,fp);
*nline = *nline + 1;
sscanf(line,"%lf %lf %lf",&x,&y,&z);
cas->x[n] = x;
cas->y[n] = y;
cas->z[n] = z;
}
} else if(ins == 2) {
for(n=istart-1;n<iend;++n) {
fgets(line,100,fp);
*nline = *nline + 1;
sscanf(line,"%lf %lf %lf",&x,&y,&z);
zp->x[n - istart+1] = x;
zp->y[n - istart+1] = y;
zp->z[n - istart+1] = z;
}
}
if(ins != 0) {
fgets(line,100,fp);
*nline = *nline + 1;
if(strncmp("))",line,2) != 0) {
fprintf(stdout," e [inFluent_HandleNodes] Could not find nodes-terminating line\n");
return(1);
} else {
fprintf(stdout," i [inFluent_HandleNodes] Read %ld nodes \n",iend-istart+1);
zp->nstart = istart;
zp->nend = iend;
// fprintf(stdout," This zones's bounds %ld %ld \n",zp->nstart,zp->nend);
}
cas->nzone += 1;
}
return(0);
}
/*
* Fucntion to handle the reading of faces from the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110401
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20160930
*/
int inFluent_HandleFaces(struct my_fluentcase *cas, char *data,
long *nline, FILE *fp)
{
int id,type,et;
long istart,iend;
size_t size;
struct my_fluentzone *zp;
long n;
int ins;
long in1,in2,in3,in4,ic1,ic2;
char line[200];
// Note that there are two posibilities!
// (13 (0 first-index last-index 0))
// (13 (zone-id first-index last-index type element-type))
sscanf(data,"(13 (%x %lx %lx %x",&id,&istart,&iend,&type);
// for now die when a mixed element type is given
if(id != 0) {
// (13 (zone-id first-index last-index type element-type))
sscanf(data,"(13 (%x %lx %lx %x %d)",&id,&istart,&iend,&type,&et);
if(et == 0) {
fprintf(stdout," e [inFluent_HandleFaces] A face-zone with mixed elements types exists. \n");
return(1);
}
}
if(id == 0) {
fprintf(stdout," i [inFluent_HandleFaces] Case file provided total number of faces: %ld \n",iend);
size = iend*sizeof(long);
cas->ifnp = (long *) malloc(size*4);
cas->ifep = (long *) malloc(size*2);
if(cas->ifnp == NULL || cas->ifep == NULL) {
fprintf(stdout," e [inFluent_HandleFaces] Could not allocate memory for faces \n");
if(cas->ifnp != NULL) free(cas->ifnp);
if(cas->ifep != NULL) free(cas->ifep);
cas->ifnp = NULL;
cas->ifep = NULL;
} else {
fprintf(stdout," i [inFluent_HandleFaces] Allocated %f MBs for faces \n",
((double) (size*6)) / ((double) (1024*1024)) );
cas->ifn = (ifn_t *) &(cas->ifnp[0]);
cas->ife = (ife_t *) &(cas->ifep[0]);
}
cas->nfa = iend;
ins = 0;
} else {
fprintf(stdout," i [inFluent_HandleFaces] Found face-zone %d with bounds [%ld:%ld] \n",id,istart,iend);
if(cas->nzone == cas->mzone) {
fprintf(stdout," e [inFluent_HandleFaces] Not enough preallocated zones (%d) \n",cas->mzone);
return(1);
} else {
zp = (struct my_fluentzone *) &(cas->zones[cas->nzone]);
zp->id = id; // this may have to be moved further down
zp->type = 13; // this may have to be moved further down
zp->iattr = type;
}
if(cas->nfa <= 0) {
zp->imem = 30;
// Here I am speculating that there could be a possibility where
// faces are part of a zone but the total number of faces was not
// given in the case file. If true, I allocate space for the faces
// as if they are part of this zone, which is what happens below.
// If false, I should just return an error for out-of-order case items
size = (iend - istart + 1);
zp->ifnp = (long *) malloc(size*4);
zp->ifep = (long *) malloc(size*2);
if(zp->ifnp == NULL || cas->ifep == NULL) {
fprintf(stdout," e [inFluent_HandleFaces] Could not allocate memory for faces within a zone \n");
if(zp->ifnp != NULL) free(zp->ifnp);
if(zp->ifep != NULL) free(zp->ifep);
zp->ifnp = NULL;
zp->ifep = NULL;
return(-1);
} else {
fprintf(stdout," i [inFluent_HandleFaces] Allocated %f MBs for faces in zone %d \n",
((double) (size*6)) / ((double) (1024*1024)) ,id);
}
ins = 2;
} else {
zp->imem = 3;
fprintf(stdout," i [inFluent_HandleFaces] Inserting faces in array\n");
zp->ifnp = (long *) &(cas->ifnp[4*(istart-1)]);
zp->ifep = (long *) &(cas->ifep[2*(istart-1)]);
ins = 1;
}
zp->ifn = (ifn_t *) zp->ifnp;
zp->ife = (ife_t *) zp->ifep;
}
// no error-trapping during reading operations!
if(ins == 1) {
for(n=istart-1;n<iend;++n) {
fgets(line,100,fp);
*nline = *nline + 1;
in3 = -1;
in4 = -1;
if(et == 4) {
sscanf(line,"%lx %lx %lx %lx %lx %lx",
&in1,&in2,&in3,&in4,&ic1,&ic2);
} else if(et == 3) {
sscanf(line,"%lx %lx %lx %lx %lx",
&in1,&in2,&in3,&ic1,&ic2);
} else if(et == 2) {
sscanf(line,"%lx %lx %lx %lx",
&in1,&in2,&ic1,&ic2);
} else if(et == 0) {
// some decisions need to be made here before reading this line
// sscanf(line,"%d %lx %lx %lx %lx %lx %lx",
// &ie,&in1,&in2,&in3,&in4,&ic1,&ic2);
} else {
fprintf(stdout," e [inFluent_HandleFaces] Unknown element type! \n");
return(1);
}
cas->ifn[n][0] = in1;
cas->ifn[n][1] = in2;
cas->ifn[n][2] = in3;
cas->ifn[n][3] = in4;
cas->ife[n][0] = ic1;
cas->ife[n][1] = ic2;
/*
printf("---> et=%d %lx %lx %lx %lx : %lx %lx \n",
et,
cas->ifn[n][0],
cas->ifn[n][1],
cas->ifn[n][2],
cas->ifn[n][3],
cas->ife[n][0],
cas->ife[n][1]);
*/
}
} else if(ins == 2) {
for(n=istart-1;n<iend;++n) {
fgets(line,100,fp);
*nline = *nline + 1;
in3 = -1;
in4 = -1;
if(et == 4) {
sscanf(line,"%lx %lx %lx %lx %lx %lx",
&in1,&in2,&in3,&in4,&ic1,&ic2);
} else if(et == 3) {
sscanf(line,"%lx %lx %lx %lx %lx",
&in1,&in2,&in3,&ic1,&ic2);
} else if(et == 2) {
sscanf(line,"%lx %lx %lx %lx",
&in1,&in2,&ic1,&ic2);
} else if(et == 0) {
// some decisions need to be made here before reading this line
// sscanf(line,"%d %lx %lx %lx %lx %lx %lx",
// &ie,&in1,&in2,&in3,&in4,&ic1,&ic2);
} else {
fprintf(stdout," e [inFluent_HandleFaces] Unknown element type! \n");
return(1);
}
zp->ifn[n - istart+1][0] = in1;
zp->ifn[n - istart+1][1] = in2;
zp->ifn[n - istart+1][2] = in3;
zp->ifn[n - istart+1][3] = in4;
zp->ife[n - istart+1][0] = ic1;
zp->ife[n - istart+1][1] = ic2;
/*
printf("---> et=%d %lx %lx %lx %lx : %lx %lx \n",
et,
zp->ifn[n][0],
zp->ifn[n][1],
zp->ifn[n][2],
zp->ifn[n][3],
zp->ife[n][0],
zp->ife[n][1]);
*/
}
}
if(ins != 0) {
fgets(line,100,fp);
*nline = *nline + 1;
if(strncmp("))",line,2) != 0) {
fprintf(stdout," e [inFluent_HandleFaces] Could not find faces-terminating line\n");
return(1);
} else {
fprintf(stdout," i [inFluent_HandleFaces] Read %ld faces \n",iend-istart+1);
zp->nstart = istart;
zp->nend = iend;
// fprintf(stdout," This zones's bounds %ld %ld \n",zp->nstart,zp->nend);
}
cas->nzone += 1;
}
return(0);
}
/*
* Fucntion to read the number of dimenstions of the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110330
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20110331
*/
int inFluent_HandleDims(struct my_fluentcase *cas, char *data) {
sscanf(data,"(2 %d)",&(cas->type));
fprintf(stdout," i [inFluent_HandleDims] Case file is for %dD simulations \n",cas->type);
return(0);
}
/*
* Fucntion to determine whether this comment line is followed by a regular one
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20160317
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20130618
*/
int inFluent_ShiftComment(char **data) {
int ic = 0;
int i;
char *str;
//printf("CALLED \n");
str = *data;
// bypass the comment part
i = 0;
while( str[i] != ')' && str[i] != '\0' && str[i] != '\n' ) {
//printf("%c",str[i]);
++i;
}
//printf("%c",str[i]);
//if(str[i] == '\n') printf("\n");
//if(str[i] == '\0') printf("\n");
// test for another section after the comment
while( str[i] != '(' && str[i] != '\0' && str[i] != '\n' ) {
//printf("%c",str[i]);
++i;
}
//printf("%c",str[i]);
//if(str[i] == '\n') printf("\n");
//if(str[i] == '\0') printf("\n");
// make pointer shift if another section exists
if(str[i] == '(') {
ic = 1;
//printf("\n");
*data = (char *) &(str[i]);
}
return(ic);
}
/*
* Fucntion to determine what a valid line in the case file describes
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110330
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20130618
*/
int inFluent_Readline(char **parse) {
char *string;
int len;
int idebug = 0;
char *data = *parse;
string = "(0 ";
len = 3;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A COMMENT LINE \n");
if( inFluent_ShiftComment(parse) != 0) {
return(inFluent_Readline(parse));
} else {
return(0);
}
}
string = "(1 ";
len = 3;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A HEADER LINE \n");
return(1);
}
string = "(2 ";
len = 3;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A DIMENSIONS LINE \n");
return(2);
}
string = "(10 ";
len = 4;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A NODES LINE \n");
return(10);
}
string = "(12 ";
len = 4;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A CELLS LINE \n");
return(12);
}
string = "(13 ";
len = 4;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A FACES LINE \n");
return(13);
}
string = "(45 ";
len = 4;
if(strncmp(data,string,len) == 0) {
if(idebug == 1)
printf("THIS IS A PRESENTLY UNRECOGNIZED LINE \n");
return(45);
}
// if we got this far it is an unrecognized line
return(-1);
}
/*
* Fucntion to sanitize a line read from the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110330
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20110331
*/
int inFluent_Scanline(char *data, char *parse, int size) {
int n,igot,m;
memset(parse,'\0',size);
// first sanitize the line and place it in the destination character array
n = 0;
m = -1;
igot = 0;
while(n<size && data[n] != '\n' && data[n] != '\0') {
if(data[n] == '(') {
igot = 1;
if(m == -1) m = n;
}
if(igot == 1) {
parse[n-m] = data[n];
}
++n;
}
if(igot == 0) {
return(-1); // reached end of line but no endline character
} else {
// Now try to determine what type of line this is.
// The purpose of doing this here is to do error trapping before we hand
// over a "valid" line to the processing routines later on. This way, I
// have only one place to worry about parsing and seg faults.
// I changed my mind. I have a dedicated decision making routine, but I
// will keep this here in case I want to combine the two.
return(0);
}
}
/*
* Fucntion to organize the reading of the case file
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110330
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20160930
*/
int inFluent_ReadCase(struct my_fluentcase *cas, char *filename) {
FILE *fp;
int iterm = 0;
char data[100],parse[100],*str;
int ic,il,size = 100;
int ierr;
long nline = 0;
struct timeval tv1,tv2;
struct timezone tz;
(void) gettimeofday(&tv1,&tz);
fp = fopen(filename,"r");
if(fp == NULL) {
fprintf(stdout," e [inFluent_ReadCase] Could not open file \"%s\" \n",filename);
return(1);
}
fprintf(stdout," r [inFluent_ReadCase] Reading file \"%s\" \n",filename);
while(!feof(fp) && iterm != 1) {
memset(data,'\0',100);
fgets(data,100,fp);
++nline;
ic = inFluent_Scanline(data,parse,size);
// The parser errors may be temporarily disabled
/**/
if(parse[0] == '(' && parse[1] == '\n') {
fprintf(stdout," e [inFluent_ReadCase] The parser has failed. This is a bug or logic flaw!\n");
fclose(fp);
return(1);
}
/**/
if(ic == -1) {
// this is either a blank line or a newline so we do nothing
}
if(ic == 0) {
// this is a valid line
fprintf(stdout," [%ld] %s",nline,data);
str = parse;
il = inFluent_Readline(&str);
if(il == 0) {
// do nothing since this is a comment
} else if(il == 1) {
cas->header = (char *) malloc((strlen(str)+1)*sizeof(char));
if(cas->header != NULL) {
sprintf(cas->header,"%s",str);
} else {
fprintf(stdout," e [inFluent_ReadCase] Could not allocate memory for header (%ld bytes) \n", (long) (strlen(str)+1) );
fclose(fp);
return(-1);
}
} else if(il == 2) {
inFluent_HandleDims(cas,str);
} else if(il == 10) { // nodes section
ierr = inFluent_HandleNodes(cas,str,&nline,fp);
if(ierr != 0) {
fclose(fp);
return(1);
}
} else if(il == 12) { // cells section
ierr = inFluent_HandleCells(cas,str,&nline,fp);
if(ierr != 0) {
fclose(fp);
return(1);
}
} else if(il == 13) { // faces section
ierr = inFluent_HandleFaces(cas,str,&nline,fp);
if(ierr != 0) {
fclose(fp);
return(1);
}
} else if(il == 45) { // unknown (for now) section
fprintf(stdout," i [inFluent_ReadCase] Found the (presently) unknown section \"45\" \n");
} else { // this should be "-1" no other way
fprintf(stdout," e [inFluent_ReadCase] Unrecognized item in case file at line %ld \n",nline);
fclose(fp);
return(1);
}
}
if(ic == 1) {
// this is an invalid line
fprintf(stdout," e [inFluent_ReadCase] Found a problematic line (# %ld) \n",nline);
fprintf(stdout," ---> %s <----",data);
fclose(fp);
return(1);
}
}
fclose(fp);
if(cas->type == 0) cas->type = 3;
(void) gettimeofday(&tv2,&tz);
fprintf(stdout," i [inFluent_ReadCase] Read file in %ld sec \n",
(long) tv2.tv_sec - tv1.tv_sec);
(void) inFluent_CaseInfo(cas);
return(0);
}
int inFluent_ReadCase_(struct my_fluentcase *cas, char *filename) {
return(inFluent_ReadCase(cas,filename));
}
int influent_readcase_(struct my_fluentcase *cas, char *filename) {
return(inFluent_ReadCase(cas,filename));
}
/*
* Fucntion to initialize the fluent case structures
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20110330
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20160928
*/
int inFluent_InitCase(struct my_fluentcase *cas) {
size_t size;
cas->header = NULL;
cas->type = 0;
cas->nno = 0;
cas->nel = 0;
cas->nfa = 0;
cas->mzone = 100;
cas->nzone = 0;
size = cas->mzone * sizeof(struct my_fluentzone);
cas->zones = (struct my_fluentzone *) malloc(size);
if(cas->zones != NULL) {
int n;
for(n=0;n<cas->mzone;++n) {
cas->zones[n].id = -1;
cas->zones[n].type = -1;
// cas->zones[n].nno = 0;
// cas->zones[n].nel = 0;
// cas->zones[n].nfa = 0;
cas->zones[n].nstart = -1;
cas->zones[n].nend = -1;
cas->zones[n].imem = 0;
cas->zones[n].x = NULL;
cas->zones[n].y = NULL;
cas->zones[n].z = NULL;
cas->zones[n].ifn = NULL;
cas->zones[n].ife = NULL;
}
fprintf(stdout," i [inFluent_InitCase] Initialized FLUENT case structure\n");
fprintf(stdout," Preallocated space for %d zones (reallocated automatically if exceeded) \n",cas->mzone);
} else {
cas->mzone = 0;
fprintf(stdout," e [inFluent_InitCase] Could not initialize FLUENT case structure (out of memory?) \n");
return(1);
}
return(0);
}
int inFluent_InitCase_(struct my_fluentcase *cas) {
return(inFluent_InitCase(cas));
}
int influent_initcase_(struct my_fluentcase *cas) {
return(inFluent_InitCase(cas));
}
void inFluent_CaseInfo(struct my_fluentcase *cas) {
int n,id,it,is,ia;
fprintf(stdout," Summary of fluent case file contents\n");
fprintf(stdout," - Total number of nodes: %ld (%.12lx) \n",cas->nno,cas->nno);
fprintf(stdout," - Total number of cells: %ld (%.12lx) \n",cas->nel,cas->nel);
fprintf(stdout," - Total number of faces: %ld (%.12lx) \n",cas->nfa,cas->nfa);
fprintf(stdout," - Total number of zones: %d \n",cas->nzone);
fprintf(stdout," Zone information \n");
fprintf(stdout," Int. num. ID-in-file Type Attrib \n");
for(n=0;n<cas->nzone;++n) {
id = cas->zones[n].id;
it = cas->zones[n].type;
is = cas->zones[n].type; // capped index to avoid array overflow
if(is > 20) is = 0;
ia = cas->zones[n].iattr;
if(ia > 40 || ia < 0) ia = 0;
fprintf(stdout," zone %d id %d type %d (%s) (%s) \n",
n,id,it,fluent_types[is],fluent_attrib[ia]);
}
}
/*
* Fucntion to clear the fluent case structures
* Ioannis Nompelis <nompelis@nobelware.com> Created: 20160915
* Ioannis Nompelis <nompelis@nobelware.com> Last modified: 20160915
*/
int inFluent_ClearCase(struct my_fluentcase *cas)
{
char *FUNC = "inFluent_ClearCase";
if(cas->zones != NULL) {
int n;
for(n=0;n<cas->nzone;++n) {
if(cas->zones[n].imem == 10) {
free(cas->zones[n].x);
free(cas->zones[n].y);
free(cas->zones[n].z);
cas->zones[n].x = NULL;
cas->zones[n].y = NULL;
cas->zones[n].z = NULL;
}
if(cas->zones[n].imem == 30) {
free(cas->zones[n].ifn);
free(cas->zones[n].ife);
cas->zones[n].ifn = NULL;
cas->zones[n].ife = NULL;
cas->zones[n].ifnp = NULL;
cas->zones[n].ifep = NULL;
}
cas->zones[n].imem = 0;
cas->zones[n].id = -1;
cas->zones[n].type = -1;
// cas->zones[n].nno = 0;
// cas->zones[n].nel = 0;
// cas->zones[n].nfa = 0;
cas->zones[n].nstart = -1;
cas->zones[n].nend = -1;
}
if(cas->zones != NULL) free(cas->zones);
}
if(cas->x != NULL) free(cas->x);
cas->x = NULL;
if(cas->y != NULL) free(cas->y);
cas->y = NULL;
if(cas->z != NULL) free(cas->z);
cas->z = NULL;
if(cas->ifn != NULL) free(cas->ifn);
cas->ifn = NULL;
cas->ifnp = NULL;
if(cas->ife != NULL) free(cas->ife);
cas->ife = NULL;
cas->ifep = NULL;
if(cas->header != NULL) free(cas->header);