-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavtSuntansFileFormat.C
2310 lines (1857 loc) · 68.1 KB
/
avtSuntansFileFormat.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
/*****************************************************************************
*
* Copyright (c) 2000 - 2010, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-442911
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS 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 LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS 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.
*
*****************************************************************************/
// ************************************************************************* //
// avtSuntansFileFormat.C //
// ************************************************************************* //
#include <avtSuntansFileFormat.h>
#include <string>
#include <algorithm>
#include <iosfwd>
#include <map>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkUnsignedCharArray.h>
#include <vtkUnstructuredGrid.h>
#include <vtkCellType.h>
#include <vtkCellData.h>
#include <vtkPoints.h>
#include <avtDatabaseMetaData.h>
#include <avtGhostData.h>
#include <DBOptionsAttributes.h>
#include <Expression.h>
#include <InvalidVariableException.h>
#include <StringHelpers.h>
// In Visit 2.0, ReadAndProcessDirectory has moved:
// #include <Utility.h>
#include <FileFunctions.h>
#include <DebugStream.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#if defined(_WIN32)
#include <win32-regex.h>
#else
#include <regex.h>
#endif
using std::string;
using std::vector;
#define EMPTY 999999 /* should match suntans.h */
#define MESH_3D "fullmesh"
#define MESH_2D "surface"
#define MESH_2D_PROF "surf_profile"
#define MESH_FACES "faces"
#define MESH_DEPTH "depth"
#define RAW "raw/"
// utility class for handling grid data
SunGrid::SunGrid(avtSuntansFileFormat *p,int d) {
parent = p;
domain = d;
wet_cell_timestate = -1;
surf_mesh = NULL;
full_mesh = NULL;
face_mesh = NULL;
bathymetry = NULL;
read_edgedata();
read_celldata();
}
// ****************************************************************************
// Method: avtSuntansFileFormat constructor
//
// Programmer: rusty -- generated by xml2avt
// Creation: Mon Oct 27 12:23:29 PDT 2008
//
// ****************************************************************************
avtSuntansFileFormat::avtSuntansFileFormat(const char *filename)
: avtMTMDFileFormat(filename)
{
points = NULL;
// INITIALIZE DATA MEMBERS
ifstream ifile(filename);
debug1 << "SUNTANS: Opening " << filename << endl;
if (ifile.fail()) {
debug1 << "Unable to open file " << filename << endl;
} else {
ReadSuntansDat(ifile);
ReadSteps();
ReadDomainCount();
FindCrashFile();
}
dzmin = 0.001;
}
void avtSuntansFileFormat::ReadSteps(void) {
// more info...
string step_filename = GetFilePath("progressfile");
ifstream step_fp(step_filename.c_str());
available_timesteps = 0;
if ( !step_fp ) {
debug1 << "Couldn't open step file - probably a failed run" << endl;
return;
}
regex_t cre;
regmatch_t pm[10];
// example output:
// On 5000 of 5000, t=49990.00 (100% Complete, 501 output)
if ( regcomp(&cre, "^On ([0-9]+) of ([0-9]+), t=([0-9\\.]+) \\([0-9\\.]+% Complete, ([0-9]+) output\\)$",
REG_EXTENDED)) {
debug1 << "Couldn't compile pattern for reading step.dat" << endl;
available_timesteps = 0;
return;
}
string linebuff;
getline(step_fp,linebuff);
if ( regexec(&cre, linebuff.c_str(), 10, pm, 0) == 0 ) {
if ( pm[4].rm_so >= 0 ) {
available_timesteps = atoi( linebuff.c_str() + pm[4].rm_so );
} else {
debug1 << "Matched, but didn't get substring match!\n";
}
} else {
debug1 << "Unable to read line from step file " << step_filename << endl;
// fall back to reading the store output
string store_filename = GetFilePath("storefile",0);
ifstream store_fp(store_filename.c_str());
if ( store_fp ) {
// RCH: I'm sure there is a visit-compatible way of defining 32 bit integers, but
// for the moment I think most 64-bit platforms still have int as 32 bits.
int last_step=0;
store_fp.read((char*)&last_step,sizeof(last_step));
available_timesteps = 1 + (int) ( last_step / atoi(settings["ntout"].c_str()) );
debug1 << "Got available timesteps " << available_timesteps << " from store.dat.0" << endl;
}
}
// Read the starting timestep from start.dat
string start_filename = GetFilePath("startfile",0);
ifstream start_fp(start_filename.c_str());
if ( !start_fp ) {
starting_timestep = 0;
} else {
start_fp.read((char*)&starting_timestep,sizeof(starting_timestep));
debug1 << "Got starting timestep " << starting_timestep << " from start.dat.0" << endl;
}
// And get the date of time zero:
if ( settings.find("start_day") != settings.end() ) {
t0_days = atof( settings["start_day"].c_str());
} else {
t0_days = 0.0;
}
}
void avtSuntansFileFormat::FindCrashFile(void) {
has_crash_file = 0;
// Is there a crash file (like store.dat.*, but named crash.dat.*)
string crash_path = GetFilePath("CrashFile") + string(".0");
struct stat statbuf;
has_crash_file = stat(crash_path.c_str(),&statbuf) == 0;
debug1 << "Checked for crash file: " << crash_path << " and found " << has_crash_file << endl;
}
void avtSuntansFileFormat::ReadDomainCount(void) {
string cells_filename = GetFilePath("cells");
struct stat statbuf;
char *buff = new char[cells_filename.size()+10];
for(int i=0;i<SUNTANS_MAX_PROCESSORS;i++) {
sprintf(buff,"%s.%d",cells_filename.c_str(),i);
if ( stat(buff,&statbuf) != 0 ) {
domain_count = i;
break;
}
}
domain_cache.resize(domain_count,NULL);
delete[] buff;
}
SunGrid *avtSuntansFileFormat::subdomain(int domain) {
if( domain_cache[domain] == NULL ) {
debug1 << "Allocating new SunGrid instance" << endl;
domain_cache[domain] = new SunGrid(this,domain);
}
return domain_cache[domain];
}
// given the type of file, e.g. SalinityFile, return
// a path that can be used to open that file
std::string avtSuntansFileFormat::GetFilePath(std::string config_name) {
string my_path = StringHelpers::Dirname(GetFilename());
// special handling of CrashFile, since it doesn't get configured via
// suntans.dat
if ( config_name.compare("CrashFile")==0 )
return my_path + "/crash.dat";
else {
std::transform(config_name.begin(), config_name.end(), config_name.begin(), ::tolower);
return my_path + '/' + settings[config_name];
}
}
std::string avtSuntansFileFormat::GetFilePath(std::string config_name,int proc)
{
std::ostringstream extended;
std::string base = GetFilePath(config_name);
extended << base << "." << proc;
return std::string(extended.str());
}
// Read global settings from the suntans file...
void avtSuntansFileFormat::ReadSuntansDat(ifstream &ifile)
{
string linebuff;
regex_t cre;
regmatch_t pm[10];
if ( regcomp(&cre, "^[[:blank:]]*([a-zA-Z_0-9]+)[[:blank:]]+([-a-zA-Z_0-9\\./]+).*$",
REG_EXTENDED)) {
debug1 << "Couldn't compile pattern for reading suntans file" << endl;
return;
}
while( ifile.good() ) {
getline(ifile,linebuff);
if ( regexec(&cre, linebuff.c_str(), 10, pm, 0) == 0 ) {
string keyval[2];
keyval[0] = linebuff.substr(pm[1].rm_so,pm[1].rm_eo - pm[1].rm_so);
keyval[1] = linebuff.substr(pm[2].rm_so,pm[2].rm_eo - pm[2].rm_so);
//settings.push_back(keyval);
std::transform(keyval[0].begin(), keyval[0].end(), keyval[0].begin(), ::tolower);
debug1 << " Read setting: " << keyval[0] << " => " << keyval[1] << endl;
settings[keyval[0]] = keyval[1];
}
}
// some useful settings to pull out ahead of time:
nkmax = atoi(settings["nkmax"].c_str());
debug1 << "nkmax is " << nkmax << endl;
// read the vertical cell spacing. note that this is the summed up, bottom
// of cell elevations (i.e. negative)
string vert_fname = GetFilePath("vertspace");
debug1 << "vertspace file path: "<<vert_fname << endl;
ifstream vert_fp(vert_fname.c_str());
float cell_bottom=0.0,cell_height;
cell_divisions.push_back(cell_bottom);
for(int i=0;i<nkmax;i++) {
vert_fp >> cell_height;
cell_bottom -= cell_height;
cell_divisions.push_back(cell_bottom);
debug1 << "Read cell bottom elevation: " << cell_bottom << endl;
}
}
// ****************************************************************************
// Method: avtEMSTDFileFormat::GetNTimesteps
//
// Purpose:
// Tells the rest of the code how many timesteps there are in this file.
//
// Programmer: rusty -- generated by xml2avt
// Creation: Mon Oct 27 12:23:29 PDT 2008
//
// ****************************************************************************
int
avtSuntansFileFormat::GetNTimesteps(void)
{
if ( available_timesteps > 0 )
return available_timesteps;
else
return 1; // just make up data...
}
// ****************************************************************************
// Method: avtSuntansFileFormat::FreeUpResources
//
// Purpose:
// When VisIt is done focusing on a particular timestep, it asks that
// timestep to free up any resources (memory, file descriptors) that
// it has associated with it. This method is the mechanism for doing
// that.
//
// Programmer: rusty -- generated by xml2avt
// Creation: Mon Oct 27 12:23:29 PDT 2008
//
// ****************************************************************************
void
avtSuntansFileFormat::FreeUpResources(void)
{
}
// ****************************************************************************
// Method: avtSuntansFileFormat::PopulateDatabaseMetaData
//
// Purpose:
// This database meta-data object is like a table of contents for the
// file. By populating it, you are telling the rest of VisIt what
// information it can request from you.
//
// Programmer: rusty -- generated by xml2avt
// Creation: Mon Oct 27 12:23:29 PDT 2008
//
// ****************************************************************************
void
avtSuntansFileFormat::PopulateDatabaseMetaData(avtDatabaseMetaData *md, int timeState)
{
//
// 2D GRID
//
avtMeshMetaData *mmd_2d = new avtMeshMetaData;
mmd_2d->name = MESH_2D;
mmd_2d->spatialDimension = 2;
mmd_2d->topologicalDimension = 2;
mmd_2d->meshType = AVT_UNSTRUCTURED_MESH;
mmd_2d->numBlocks = domain_count;
md->Add(mmd_2d);
avtMeshMetaData *mmd_2dprof = new avtMeshMetaData;
mmd_2dprof->name = MESH_2D_PROF;
mmd_2dprof->spatialDimension = 2;
mmd_2dprof->topologicalDimension = 0;
mmd_2dprof->meshType = AVT_POINT_MESH;
mmd_2dprof->numBlocks = 1;
md->Add(mmd_2dprof);
avtScalarMetaData *smd;
if ( available_timesteps > 0 ) {
// freesurface (might need to actually check to make sure fs was output)
smd = new avtScalarMetaData;
smd->name = "freesurface";
smd->meshName = mmd_2d->name; // "surface";
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
// Profile outputs of the freesurface:
smd = new avtScalarMetaData;
smd->name = "prof_freesurface";
smd->meshName = mmd_2dprof->name;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
}
smd = new avtScalarMetaData;
smd->name = "Nk";
smd->meshName = mmd_2d->name; // "surface";
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
// freesurface at last restore file
smd = new avtScalarMetaData;
smd->name = "store/freesurface";
smd->meshName = mmd_2d->name; //"surface";
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
// bathymetry
smd = new avtScalarMetaData;
smd->name = "bathymetry";
smd->meshName = mmd_2d->name; //"surface";
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
avtVectorMetaData *vmd;
if ( available_timesteps > 0 ) {
// might need to check that velocity was actually output
vmd = new avtVectorMetaData;
vmd->name = "velocity_surface";
vmd->meshName = mmd_2d->name; // "surface";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3; // seems like 2 here didn't really work
vmd->units = "m/s";
md->Add(vmd);
}
// Point grid for depth data, if present
//
struct stat statbuf;
string depth_file = GetFilePath("depth");
if ( stat(depth_file.c_str(),&statbuf) == 0 ) {
avtMeshMetaData *mmd_depth = new avtMeshMetaData;
mmd_depth->name = MESH_DEPTH;
mmd_depth->spatialDimension = 2;
mmd_depth->topologicalDimension = 0;
mmd_depth->meshType = AVT_POINT_MESH;
mmd_depth->numBlocks = 1; // depth data is not decomposed into subdomains
md->Add(mmd_depth);
smd = new avtScalarMetaData;
smd->name = "point_depth";
smd->meshName = MESH_DEPTH;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
}
// values defined throughout the depth cells
// velocity, salinity, temperature
avtMeshMetaData *mmd_3d = new avtMeshMetaData;
mmd_3d->name = MESH_3D;
mmd_3d->spatialDimension = 3;
mmd_3d->topologicalDimension = 3;
mmd_3d->meshType = AVT_UNSTRUCTURED_MESH;
mmd_3d->numBlocks = domain_count;
md->Add(mmd_3d);
if ( available_timesteps > 0 ) {
// might need to check that velocity was actually output
vmd = new avtVectorMetaData;
vmd->name = "velocity_3d";
vmd->meshName = mmd_3d->name; // "fullmesh";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3;
vmd->units = "m/s";
md->Add(vmd);
smd = new avtScalarMetaData;
smd->name = "salinity";
smd->meshName = mmd_3d->name; // fullmesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "psu?";
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "temperature";
smd->meshName = mmd_3d->name; // fullmesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "nu_t_vert";
smd->meshName = mmd_3d->name; // fullmesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "temperature_surface";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "salinity_surface";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "nu_t_vert_surface";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
// experimental bed fields:
smd = new avtScalarMetaData;
smd->name = "temperature_bed";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "salinity_bed";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
smd = new avtScalarMetaData;
smd->name = "nu_t_vert_bed";
smd->meshName = mmd_2d->name; // surface mesh
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
}
// restore files
// should check first to see that a store file actually exists, but
// I don't feel like doing that right now.
/* 3D face data: */
avtMeshMetaData *mmd_faces = new avtMeshMetaData;
mmd_faces->name = MESH_FACES;
mmd_faces->spatialDimension = 3;
mmd_faces->topologicalDimension = 1;
mmd_faces->meshType = AVT_UNSTRUCTURED_MESH;
mmd_faces->numBlocks = domain_count;
md->Add(mmd_faces);
// This one just puts a unit velocity on all faces
vmd = new avtVectorMetaData;
vmd->name = "face_normals";
vmd->meshName = mmd_faces->name; // "faces";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = false;
vmd->varDim = 3;
md->Add(vmd);
if ( 1 ) { // store files
// might need to check that a storefile actually exists
smd = new avtScalarMetaData;
smd->name ="store/norm_velocity";
smd->meshName = mmd_faces->name;
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "m/s";
md->Add(smd);
vmd = new avtVectorMetaData;
vmd->name = "store/velocity";
vmd->meshName = mmd_faces->name; // "faces";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3;
vmd->units = "m/s";
md->Add(vmd);
// A bit of a lie - the w velocity vectors really reside
// between z-levels, but I don't want to make a whole new
// mesh.
vmd = new avtVectorMetaData;
vmd->name = "store/w_velocity";
vmd->meshName = mmd_3d->name; // "fullmesh";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3;
vmd->units = "m/s";
md->Add(vmd);
smd = new avtScalarMetaData;
smd->name ="store/salinity";
smd->meshName = mmd_3d->name;
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "psu";
md->Add(smd);
}
if ( has_crash_file ) {
smd = new avtScalarMetaData;
smd->name = "crash/freesurface";
smd->meshName = mmd_2d->name; //"surface";
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "meter";
md->Add(smd);
smd = new avtScalarMetaData;
smd->name ="crash/norm_velocity";
smd->meshName = mmd_faces->name;
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "m/s";
md->Add(smd);
vmd = new avtVectorMetaData;
vmd->name = "crash/velocity";
vmd->meshName = mmd_faces->name; // "faces";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3;
vmd->units = "m/s";
md->Add(vmd);
vmd = new avtVectorMetaData;
vmd->name = "crash/w_velocity";
vmd->meshName = mmd_3d->name; // "fullmesh";
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = true;
vmd->varDim = 3;
vmd->units = "m/s";
md->Add(vmd);
smd = new avtScalarMetaData;
smd->name ="crash/salinity";
smd->meshName = mmd_3d->name;
smd->centering = AVT_ZONECENT;
smd->hasUnits = true;
smd->units = "psu";
md->Add(smd);
}
// Dynamically add raw data files
AddDynamicFiles(md);
// Expressions :
Expression new_expr;
new_expr.SetName("wc_height");
new_expr.SetDefinition("freesurface - bathymetry");
new_expr.SetType(Expression::ScalarMeshVar);
md->AddExpression(&new_expr);
}
// Some machinery for reading directories the visit way...
void save_file_name(void *data, const std::string &filename, bool a, bool b, long c) {
std::vector<std::string> *files;
files = (std::vector<std::string>*)data;
files->push_back(filename);
}
void avtSuntansFileFormat::AddDynamicFiles(avtDatabaseMetaData *md)
{
string datadir = StringHelpers::Dirname(GetFilename());
std::vector<std::string> filenames;
regex_t cre;
regmatch_t pm[10];
const char *buff;
std::string label;
/* Raw files have names like: u_blah-time_step-edge-z_level-normal.raw.0
conceptually, the filename is split on dashes, with the first element
giving a name of the variable, and the rest describing the layout of the file.
here only a subset of possible layouts are supported.
face: scalar data centered on 3D faces
facenormal: saved as scalar data, but implied as a vector normal to the faces
-uv means 2D vector data is being saved as opposed to scalar.
*/
if ( regcomp(&cre, "([^-]+)(-time_step)?(-cell)?(-edge)?(-z_level)?(-normal)?(-uv)?.raw.0$", REG_EXTENDED)) {
debug1 << "Couldn't compile pattern for finding raw data files" << endl;
return;
}
ReadAndProcessDirectory( datadir,
save_file_name,
&filenames,
false );
LocalMetadata lmd;
for (int i=0; i < filenames.size(); i++ ){
string basename = StringHelpers::Basename(filenames[i].c_str());
debug1 << "Considering file " << basename << endl;
buff = basename.c_str();
if ( regexec(&cre, buff, 10, pm, 0) == 0 ) {
debug1 << "This file looks promising: " << filenames[i] << endl;
lmd.name = string( buff+pm[1].rm_so, pm[1].rm_eo - pm[1].rm_so);
debug1 << "Label: " << lmd.name << endl;
lmd.has_time = (pm[2].rm_so >= 0 );
lmd.has_cell = (pm[3].rm_so >= 0 );
lmd.has_edge = (pm[4].rm_so >= 0 );
lmd.has_z_level = (pm[5].rm_so >= 0);
// for simplicity, go ahead the vector field regardless
lmd.is_normal = lmd.has_edge; // (pm[6].rm_so >= 0);
if( pm[7].rm_so >= 0)
lmd.n_components = 2; // 2-D velocity field
else
lmd.n_components = 1; // scalar
// strip trailing .0:
lmd.filename = filenames[i];
lmd.filename.erase( lmd.filename.end()-2, lmd.filename.end());
lmd.Add(md);
// save it for easy retrieval during GetVar
raw_vars[lmd.visit_label] = lmd;
if ( lmd.is_normal )
raw_vars[lmd.visit_normal_label] = lmd;
debug1 << "And retrieved: " << raw_vars[lmd.visit_label].filename << endl;
}
}
}
void LocalMetadata::Add(avtDatabaseMetaData *md)
{
if ( has_cell ) {
if ( n_components == 1 ) {
avtScalarMetaData *smd = new avtScalarMetaData;
if ( has_z_level ) {
// MESH_3D
smd->name = RAW "3D/" + name;
smd->meshName = MESH_3D;
} else {
smd->name = RAW "2D/" + name;
smd->meshName = MESH_2D;
}
visit_label = smd->name;
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
} else {
// cell-centered vector data
avtVectorMetaData *vmd = new avtVectorMetaData;
if ( has_z_level ) {
// MESH_3D
vmd->name = RAW "3D/" + name;
vmd->meshName = MESH_3D;
} else {
vmd->name = RAW "2D/" + name;
vmd->meshName = MESH_2D;
}
visit_label = vmd->name;
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = false;
vmd->varDim = 3;
md->Add(vmd);
}
}
if ( has_edge ) {
debug1 << "Got a face raw file" << endl;
if ( has_z_level ) {
avtScalarMetaData *smd = new avtScalarMetaData;
smd->name = RAW "3D/" + name;
visit_label = smd->name;
smd->meshName = MESH_FACES;
smd->centering = AVT_ZONECENT;
smd->hasUnits = false;
md->Add(smd);
if ( is_normal ) {
// make a vector version, whether we think it's a vector or not.
avtVectorMetaData *vmd = new avtVectorMetaData;
vmd->name = RAW "3D/vector_" + name;
visit_normal_label = vmd->name;
vmd->meshName = MESH_FACES;
vmd->centering = AVT_ZONECENT;
vmd->hasUnits = false;
vmd->varDim = 3;
md->Add(vmd);
}
} else {
debug1 << "raw files: need to add support for 2D edge centered data" << endl;
}
}
}
void avtSuntansFileFormat::GetTimes(std::vector<double> ×)
{
// in the suntans.dat file this is seconds.
// here, use days to make it a bit friendlier
int ntout = atoi( settings["ntout"].c_str() );
double dt = atof( settings["dt"].c_str() ) / (24.0*3600.0);
if ( available_timesteps == 0 ) {
// put in a fake 0 timestep
times.push_back(t0_days + starting_timestep*dt);
} else {
for(int i=0;i<available_timesteps;i++) {
times.push_back(t0_days + starting_timestep*dt + ntout*dt*i);
}
}
}
void avtSuntansFileFormat::GetCycles(std::vector<int> &cycles)
{
int ntout = atoi( settings["ntout"].c_str() );
if ( available_timesteps == 0 ) {
cycles.push_back(starting_timestep);
} else {
for(int i=0;i<available_timesteps;i++) {
cycles.push_back(starting_timestep + i*ntout);
}
}
}
// ****************************************************************************
// Method: avtSuntansFileFormat::GetMesh
//
// Purpose:
// Gets the mesh associated with this file. The mesh is returned as a
// derived type of vtkDataSet (ie vtkRectilinearGrid, vtkStructuredGrid,
// vtkUnstructuredGrid, etc).
//
// Arguments:
// timestate The index of the timestate. If GetNTimesteps returned
// 'N' time steps, this is guaranteed to be between 0 and N-1.
// domain The index of the domain. If there are NDomains, this
// value is guaranteed to be between 0 and NDomains-1,
// regardless of block origin.
// meshname The name of the mesh of interest. This can be ignored if
// there is only one mesh.
//
// Programmer: rusty -- generated by xml2avt
// Creation: Mon Oct 27 12:23:29 PDT 2008
//
// ****************************************************************************
vtkDataSet *
avtSuntansFileFormat::GetMesh(int timestate, int domain, const char *meshname)
{
if ( strcmp(meshname,MESH_2D) == 0 )
return subdomain(domain)->ReadSurfaceMesh();
else if ( strcmp(meshname,MESH_DEPTH) == 0 )
return ReadPointDepthMesh();
else if ( strcmp(meshname,MESH_3D) == 0 )
return subdomain(domain)->ReadFull3DMesh();
else if ( strcmp(meshname,MESH_2D_PROF) == 0)
return Read2DProfileMesh();
else if ( strcmp(meshname,MESH_FACES) == 0 )
return subdomain(domain)->ReadFaceMesh();
else
return 0;
}
void
SunGrid::initialize_wet_cells(int timestate)
{
int num_set=0;
// already there -
if ( timestate == wet_cell_timestate )
return;
vtkDataArray *fs = GetVarFreesurface(timestate);
wet_cell_index.resize( Nc, 0 );
// iterate over the surface cell ids:
for ( int surf_cell_id=0;
surf_cell_id < Nc;
surf_cell_id++) {
double my_fs = fs->GetTuple1(surf_cell_id);
// is this the first wet layer for this surface cell?
// indexes into cell_divisions is 1 greater than the usual k in the
// suntans code because cell_divisions includes cell_divisions[0]=0.0
int k;
for ( k=0 ; k < cells[surf_cell_id].Nk-1 ; k++) {
// 2*dzmin should reflect DZMIN_SURFACE in phys.c
if ( my_fs >= parent->cell_divisions[k+1] + 2*parent->dzmin )
break;
}
wet_cell_index[surf_cell_id] = k;
}
fs->Delete();
wet_cell_timestate = timestate;
}
vtkDataSet* avtSuntansFileFormat::Read2DProfileMesh() {
double dt,x,y;
int dummy;
string profdata_fname = GetFilePath("ProfileDataFile");
ifstream fp(profdata_fname.c_str(), ios::in | ios::binary);
if ( !fp ) {
debug1 << "failed to open " << profdata_fname << endl;
return NULL;
}
debug1 << "Opened " << profdata_fname << " for profdata" << endl;
debug1 << "Sizeof prof_total_points: " << sizeof(prof_total_points) << endl;
if ( !fp.read((char*)&prof_total_points,4) ) {
debug1 << "Issues reading prof_total_points" << endl;
}
fp.read((char*)&prof_numInterpPoints,4);
fp.read((char*)&prof_NkmaxProfs,4);
fp.read((char*)&dummy,4); /* nsteps */
fp.read((char*)&prof_ntoutProfs,4);
fp.read((char*)&dt,8);
/* skips: dzz all_indices dataxy.dat */
fp.seekg(28+8*prof_NkmaxProfs+4*prof_total_points + 2*8*prof_total_points );
/* finally we can actually read the points */
vtkPoints *points = vtkPoints::New();
points->SetNumberOfPoints( prof_total_points * prof_numInterpPoints);
float *pts = (float *) points->GetVoidPointer(0);
/* the XY locations of the actual points */
for( int i=0;i<prof_total_points * prof_numInterpPoints;i++) {
fp.read((char*)&x,sizeof(x));
pts[3*i] = (float)x;
}
for( int i=0;i<prof_total_points * prof_numInterpPoints;i++) {
fp.read((char*)&y,sizeof(y));
pts[3*i+1] = (float)y;
pts[3*i+2] = 0.0; /* everybody is at 0 elevation */
}
fp.close();
/* and then make a grid out of it */
vtkUnstructuredGrid *ugrid = vtkUnstructuredGrid::New();
ugrid->SetPoints(points);
points->Delete(); /* ??? */
for(vtkIdType i=0;i<prof_total_points * prof_numInterpPoints;i++) {
ugrid->InsertNextCell(VTK_VERTEX,1,&i);
}
return ugrid;