-
Notifications
You must be signed in to change notification settings - Fork 66
/
parseInputFile.C
7859 lines (7226 loc) · 240 KB
/
parseInputFile.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
// SW4 LICENSE
// # ----------------------------------------------------------------------
// # SW4 - Seismic Waves, 4th order
// # ----------------------------------------------------------------------
// # Copyright (c) 2013, Lawrence Livermore National Security, LLC.
// # Produced at the Lawrence Livermore National Laboratory.
// #
// # Written by:
// # N. Anders Petersson (petersson1@llnl.gov)
// # Bjorn Sjogreen (sjogreen2@llnl.gov)
// #
// # LLNL-CODE-643337
// #
// # All rights reserved.
// #
// # This file is part of SW4, Version: 1.0
// #
// # Please also read LICENCE.txt, which contains "Our Notice and GNU General Public License"
// #
// # This program is free software; you can redistribute it and/or modify
// # it under the terms of the GNU General Public License (as published by
// # the Free Software Foundation) version 2, dated June 1991.
// #
// # This program is distributed in the hope that it will be useful, but
// # WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
// # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
// # conditions of the GNU General Public License for more details.
// #
// # You should have received a copy of the GNU General Public License
// # along with this program; if not, write to the Free Software
// # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
#include "mpi.h"
#include "EW.h"
#include "version.h"
#include "Require.h"
#include "nearlyEqual.h"
#include "boundaryConditionTypes.h"
#include "AnisotropicMaterialBlock.h"
#include "MaterialBlock.h"
#include "MaterialPfile.h"
#include "MaterialIfile.h"
#include "MaterialVolimagefile.h"
#include "MaterialRfile.h"
#include "EtreeFile.h"
#include "TimeSeries.h"
#include "Filter.h"
#include "Image3D.h"
#include "sacutils.h"
#ifdef ENABLE_OPT
#include "MaterialInvtest.h"
#endif
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <time.h>
using namespace std;
#define SQR(x) ((x)*(x))
//int gcd( int a, int b )
//{
// // Euclidean algorithm
// while( b != 0 )
// {
// int t = b;
// b = a % b;
// a = t;
// }
// return a;
//}
void EW::revvector( int npts, double* v )
{
for( int i=0 ; i < npts/2; i++ )
{
double sl=v[i];
v[i]=v[npts-1-i];
v[npts-1-i]=sl;
}
}
int computeEndGridPoint( double maxval, double dh )
{
// We round up one, so that the end point
// specified by the user is always included
// in the domain. i.e. if z was specified
// as 15.0, and dh was computed to be 3.33,
// the number of grid points would be 15.0/3.33 + 1
// or 5, giving us a new max z = 16.64.
int pts = 0;
double x = 0.0;
while (x < maxval && !dbc::nearlyEqual(x, maxval) )
{
x += dh;
pts++;
}
// 1 based indexing
pts++;
return pts;
}
//-----------------------------------------------------------------------
//bool endswith(string end, string& mystr)
//{
// int lenEnd = end.length();
// int lenStr = mystr.length();
// if (lenEnd > lenStr) return false;
// cout << "mystr: " << mystr << " end: " << end << " " << mystr.substr(lenStr-lenEnd, lenEnd) << endl;
// if (mystr.substr(lenStr-lenEnd, lenEnd) == end)
// return true;
// else
// return false;
//}
//-----------------------------------------------------------------------
bool EW::startswith(const char begin[], char *line)
{
int lenb = strlen(begin);
// We ignore any preceeding whitespace
while (strncmp(line, " ", 1) == 0 || strncmp(line, "\t", 1) == 0)
line++;
if (strncmp(begin, line, lenb) == 0)
return true;
else
return false;
}
//-----------------------------------------------------------------------
void EW::deprecatedOption(const string& command,
const string& oldone,
const string& newone)
{
if (m_myRank == 0)
cout << "DeprecationWarning: "
<< command << " option " << oldone << " is no longer supported. Use "
<< newone << " instead." << endl;
}
//void unchecked(const char* cmd)
//{
// cout << "*** Not yet checking command: " << cmd << endl;
//}
//void checked(const char* cmd)
//{
// cout << "*** " << cmd << " command checked! " << endl;
//}
//-----------------------------------
//
// Note that parseInputFile() calls a lot of member functions of the EW class that
// should not be called after the initialization of the EW object is completed.
// Make all these functions private!
//
bool EW::parseInputFile( vector<Source*> & a_GlobalUniqueSources,
vector<TimeSeries*> & a_GlobalTimeSeries )
{
char buffer[2048];
ifstream inputFile;
int blockCount=0;
int ablockCount=0;
MPI_Barrier(MPI_COMM_WORLD);
double time_start = MPI_Wtime();
inputFile.open(mName.c_str());
if (!inputFile.is_open())
{
if (m_myRank == 0)
cerr << endl << "ERROR OPENING INPUT FILE: " << mName << endl << endl;
return false;
}
bool foundGrid = false;
// tmp (the fileio command has not yet been parsed, so we don't know mVerbose
// cout << "********Reading the input file, proc=" << m_myRank << endl;
// First process Geodyn input for restrictions of allowable grid sizes.
// while (!inputFile.eof())
// {
// inputFile.getline(buffer, 2048);
// if( startswith("geodynbc",buffer ) )
// geodynFindFile(buffer);
// }
// inputFile.clear();
// inputFile.seekg(0, ios::beg);
// process the testrayleigh command to enable a periodic domain in the (x,y)-directions
// these commands can enter data directly the object (this->)
while (!inputFile.eof())
{
inputFile.getline(buffer, 2048);
if (startswith("testrayleigh", buffer) )
{
m_doubly_periodic = true;
}
else if( startswith("testenergy",buffer) )
{
m_doubly_periodic = checkTestEnergyPeriodic(buffer);
}
else if (startswith("refinement",buffer) )
{
// mesh refinements require 3 ghost points, must know
// before processing grid command.
m_mesh_refinements = true;
}
else if( startswith("supergrid",buffer) )
{
// If supergrid damping is 6th order, 3 ghost points are needed, must know
// before processing grid command.
processSupergrid(buffer);
}
}
inputFile.clear();
inputFile.seekg(0, ios::beg); // reset file pointer to the beginning of the input file
//---------------------------------------------------------------
// Then process the grid, fileio, and topography commands so
// we know how big the solution arrays need to be.
//
// Also, if we are using attenuation, enable it on now so it
// can be read in with the other material properties
//---------------------------------------------------------------
// these commands can enter data directly into the object (this->)
while (!inputFile.eof())
{
inputFile.getline(buffer, 2048);
if( startswith("grid", buffer) )
{
foundGrid = true;
processGrid(buffer);
}
// read fileio here to enable verbose warnings in other commands
else if (startswith("fileio", buffer))
{
processFileIO(buffer);
}
else if (startswith("refinement", buffer))
{
processRefinement(buffer);
}
else if (startswith("topography", buffer))
{
processTopography(buffer);
}
else if (startswith("attenuation", buffer))
{
processAttenuation(buffer);
}
else if (startswith("anisotropy", buffer))
{
m_anisotropic = true;
}
else if (startswith("time", buffer))
{
processTime(buffer); // process time command to set reference UTC before reading stations.
}
else if (startswith("prefilter", buffer))
{
// before reading any rupture command, we need to know
// if they need to be prefiltered
processPrefilter(buffer);
}
}
// make sure there was a grid command
if (!foundGrid)
{
if (m_myRank == 0)
{
cerr << "Error: No grid found in input file: " << mName << endl;
return false; // unsuccessful
}
}
if( m_anisotropic && m_use_attenuation )
{
if (m_myRank == 0)
{
cerr << "Error: Attenuation not implemented with anisotropy " << endl;
return false; // unsuccessful
}
}
// if( m_mesh_refinements && (m_anisotropic || (m_use_attenuation && m_number_mechanisms>0) ) )
if( m_mesh_refinements && m_anisotropic )
{
if (m_myRank == 0)
{
// cerr << "Error: Grid refinements not implemented with attenuation or anisotropy " << endl;
cerr << "Error: Grid refinements not implemented with anisotropy " << endl;
return false; // unsuccessful
}
}
// sort and correct vector 'm_refinementBoundaries'. Initialize if not already available
cleanUpRefinementLevels();
inputFile.clear();
inputFile.seekg(0, ios::beg); // reset file pointer to the beginning of the input file
// At this point we only allocate solution arrays for the Cartesian grids
// Need to read the topography information before we can decide on sizes for the
// curvilinear grid.
allocateCartesianSolverArrays(m_global_zmax);
// setup 2D communicators on the finest grid so that we can smooth the topography
setup2D_MPICommunications();
// deal with topography
if (m_topography_exists)
{
// 1. read topography from efile
if (m_topoInputStyle == EW::Efile)
{
extractTopographyFromEfile(m_topoFileName, m_topoExtFileName, m_QueryType,
m_EFileResolution);
}
else if (m_topoInputStyle == EW::GridFile)
{
extractTopographyFromGridFile(m_topoFileName);
}
else if (m_topoInputStyle == EW::CartesianGrid)
{
extractTopographyFromCartesianFile(m_topoFileName);
}
else if (m_topoInputStyle == EW::TopoImage)
{
extractTopographyFromImageFile(m_topoFileName);
}
else if (m_topoInputStyle == EW::GaussianHill) // assumed to populate all grid points
{
buildGaussianHillTopography(m_GaussianAmp, m_GaussianLx, m_GaussianLy, m_GaussianXc, m_GaussianYc);
}
else if( m_topoInputStyle == EW::Rfile )
extractTopographyFromRfile( m_topoFileName );
// preprocess the mTopo array
if (m_topoInputStyle != EW::GaussianHill) // no smoothing or extrapolation for a gaussian hill
{
// 1. fill in any undefined ghost point values by extrapolation
extrapolateTopo(mTopo);
// 2. check that all values are defined...
checkTopo(mTopo);
// 3. smooth the topo
smoothTopography(m_maxIter);
}
// // 3. Figure out the number of grid points in the vertical direction and allocate solution arrays on the curvilinear grid
allocateCurvilinearArrays(); // need to assign m_global_nz[g] = klast - m_ghost_points; + allocate mUacc
}
else
{
if (m_myRank == 0)
cout << endl <<
"*** No topography command found in input file. Using z=0 as free surface boundary ***" << endl << endl;
}
// setup communicators for 3D solutions on all grids
setupMPICommunications();
// make the grid, allocate arrays for the curvilinear grid
if (m_topography_exists)
{
generate_grid();
setup_metric();
}
// output grid size info
if (m_myRank == 0)
{
int nx, ny, nz;
double nTot=0.;
printf("\nGlobal grid sizes (without ghost points)\n");
// 1234 12345679 12345679 12345679 12345679
printf("Grid h Nx Ny Nz Points\n");
for (int g = 0; g < mNumberOfGrids; g++)
{
nx = m_global_nx[g];
ny = m_global_ny[g];
nz = m_kEnd[g] - m_ghost_points;
nTot += ((long long int)nx)*ny*nz;
printf("%4i %9g %9i %9i %9i %12lld\n", g, mGridSize[g], nx, ny, nz, ((long long int)nx)*ny*nz);
}
printf("Total number of grid points (without ghost points): %g\n\n", nTot);
}
//----------------------------------------------------------
// Now onto the rest of the input file...
//----------------------------------------------------------
while (!inputFile.eof())
{
inputFile.getline(buffer, 2048);
if (strlen(buffer) > 0) // empty lines produce this
{
if (startswith("#", buffer) ||
startswith("grid", buffer) ||
startswith("refinement", buffer) ||
startswith("topography", buffer) ||
startswith("attenuation", buffer) ||
startswith("anisotropy", buffer) ||
startswith("fileio", buffer) ||
startswith("supergrid", buffer) ||
startswith("prefilter", buffer) ||
startswith("time", buffer) ||
startswith("\n", buffer) || startswith("\r", buffer) )
// || startswith("\r", buffer) || startswith("\0", buffer))
{
// Ignore commented lines, newlines,
// grid, fileio, and topography, since we have already processed those commands.
}
else if (startswith("gmt", buffer))
processGMT(buffer);
// else if (startswith("time", buffer))
// processTime(buffer);
else if (startswith("globalmaterial", buffer))
processGlobalMaterial(buffer);
else if (!m_inverse_problem && (startswith("rec", buffer) || startswith("sac", buffer)) ) // was called "sac" in WPP
processReceiver(buffer, a_GlobalTimeSeries);
else if (m_inverse_problem && startswith("obs", buffer)) //
processObservation(buffer, a_GlobalTimeSeries);
else if (m_inverse_problem && startswith("scalefactors", buffer)) //
processScaleFactors(buffer);
else if (m_inverse_problem && startswith("cg", buffer)) //
processCG(buffer);
// else if (startswith("energy", buffer))
// processEnergy(buffer);
else if (startswith("twilight", buffer))
processTwilight(buffer);
else if (startswith("testpointsource", buffer))
processTestPointSource(buffer);
else if (startswith("testlamb", buffer))
processTestLamb(buffer);
else if (startswith("testrayleigh", buffer))
processTestRayleigh(buffer);
else if (startswith("testenergy", buffer))
processTestEnergy(buffer);
else if (startswith("source", buffer))
processSource(buffer, a_GlobalUniqueSources);
else if (startswith("rupture", buffer))
processRupture(buffer, a_GlobalUniqueSources);
else if (startswith("block", buffer))
processMaterialBlock(buffer, blockCount);
else if (startswith("ablock", buffer) && m_anisotropic )
processAnisotropicMaterialBlock(buffer, ablockCount);
else if (startswith("pfile", buffer))
processMaterialPfile( buffer );
else if (startswith("rfile", buffer))
processMaterialRfile( buffer );
else if (startswith("vimaterial", buffer))
processMaterialVimaterial( buffer );
else if (startswith("invtestmaterial", buffer))
{
#ifdef ENABLE_OPT
processMaterialInvtest(buffer);
#else
if (m_myRank==0)
cout << "Error: SW4 was not built with source/material optimization support" << endl;
return false;
#endif
}
else if (startswith("efile", buffer))
{
#ifndef ENABLE_ETREE
if (m_myRank==0)
cout << "Error: SW4 was not built with Etree (efile) support" << endl;
return false;
#endif
processMaterialEtree(buffer);
}
else if (startswith("ifile", buffer))
processMaterialIfile(buffer);
else if (startswith("material", buffer))
processMaterial(buffer);
else if (startswith("image", buffer))
processImage(buffer);
else if (startswith("volimage", buffer))
processImage3D(buffer);
else if (startswith("boundary_conditions", buffer))
processBoundaryConditions(buffer);
// else if (startswith("supergrid", buffer))
// processSupergrid(buffer);
// else if (startswith("prefilter", buffer))
// processPrefilter(buffer);
else if( startswith("developer", buffer ) )
processDeveloper(buffer);
// else if( startswith("geodynbc", buffer ) )
// processGeodynbc(buffer);
else if( startswith("randomize", buffer ) )
processRandomize(buffer);
else if (!inputFile.eof() && m_myRank == 0)
{
// Maybe just reached eof, don't want to echo
// the ignoring command line for nothing
cout << "*** Ignoring command: '" << buffer << "'" << endl;
}
} // end if strlen(buffer) > 0
} // end while !inputFile.eof()
if (m_myRank == 0)
cout << endl;
inputFile.close();
// tmp:
// if (m_myRank == 0)
// {
// cout << "INFO: m_mesh_refinements=" << m_mesh_refinements << " m_use_attenuation=" << m_use_attenuation << " mOrder=" << mOrder << endl;
// }
if (mVerbose >=3 && proc_zero())
cout << "********Done reading the input file*********" << endl;
// wait until all processes have read the input file
MPI_Barrier(MPI_COMM_WORLD);
print_execution_time( time_start, MPI_Wtime(), "reading input file" );
// ---------------------------------------------
// cross command line checks
// ---------------------------------------------
if( mTopoImageFound && !m_topography_exists)
{
if (m_myRank == 0)
cerr << "Error: The input file is requesting a topo image but there is no topography command" << endl;
return false;
}
// if we made it this far, the object should be ready for time stepping
return true;
}
void EW::processGrid(char* buffer)
{
double x = 0.0;
double y = 0.0;
double z = 0.0;
int nx=0, ny=0, nz=0;
double h = 0.0;
//-----------------------------------------------------------------
// default geographical coordinates will be the
// nevada test site (see: en.wikipedia.org/wiki/Nevada_Test_Site
//-----------------------------------------------------------------
double lat, lon;
bool latSet = false, lonSet = false, lon_p_set=false, lat_p_set=false, datum_set=false;
bool ellps_set=false, proj_set=false;
bool use_geoprojection=false;
stringstream proj0;
// hard code units to be in meters
proj0 << "+units=m";
// default azimuth
mGeoAz=0;
char* token = strtok(buffer, " \t");
REQUIRE2(strcmp("grid", token) == 0, "ERROR: not a grid...: " << token);
token = strtok(NULL, " \t");
string err = "Grid Error: ";
stringstream gridSetupErrStream;
gridSetupErrStream << endl
<< "----------------------------------------" << endl
<< " Only five ways to setup grid: " << endl
<< " 1. provide h and nx, ny, nz " << endl
<< " 2. provide h and x, y, z " << endl
<< " 3. provide x,y,z and nx " << endl
<< " 4. provide x,y,z and ny " << endl
<< " 5. provide x,y,z and nz " << endl
<< "----------------------------------------" << endl
<< endl;
string gridSetupErr = gridSetupErrStream.str();
if (m_myRank == 0)
cout << endl << "* Processing the grid command..." << endl;
// Assume presence of mesh refinements has already been checked.
// Assume supergrid command has already been processed.
if( m_mesh_refinements || m_sg_damping_order == 6 )
{
m_ghost_points = 3;
m_ppadding = 3;
}
// if (m_myRank == 0)
// cout << endl << "* number of ghost points = " << m_ghost_points << endl;
while (token != NULL)
{
// while there are tokens in the string still
if (startswith("#", token) || startswith(" ", buffer))
// Ignore commented lines and lines with just a space.
break;
if (startswith("ny=", token))
{
token += 3; // skip ny=
CHECK_INPUT(atoi(token) > 0,
err << "ny is not a positive integer: " << token);
ny = atoi(token);
}
else if (startswith("nx=", token))
{
token += 3; // skip nx=
CHECK_INPUT(atoi(token) > 0,
err << "nx is not a positive integer: " << token);
nx = atoi(token);
}
else if (startswith("nz=", token))
{
token += 3; // skip nz=
CHECK_INPUT(atoi(token) >= 0,
err << "nz is not a positive integer: " << token);
nz = atoi(token);
}
else if (startswith("x=", token))
{
token += 2; // skip x=
CHECK_INPUT(atof(token) > 0.0, err << "x is not a positive float: " << token);
x = atof(token);
}
else if (startswith("y=", token))
{
token += 2; // skip y=
CHECK_INPUT(atof(token) >= 0.0, err << "y is negative: " << token);
y = atof(token);
}
else if (startswith("z=", token))
{
token += 2; // skip z=
CHECK_INPUT(atof(token) > 0.0, err << "z is not a positive float: " << token);
z = atof(token);
}
else if (startswith("h=", token))
{
token += 2; // skip h=
CHECK_INPUT(atof(token) > 0.0,
err << "h is not a positive float: " << token);
h = atof(token);
}
else if (startswith("az=", token))
{
token += 3; // skip az=
mGeoAz = atof(token);
CHECK_INPUT(mGeoAz >= 0.0,
err << "az must be greater than or equal to zero degrees, not: " << mGeoAz);
CHECK_INPUT(mGeoAz <= 360.0,
err << "az must be less than or equal to 360 degrees, not " << mGeoAz);
}
else if (startswith("lat=", token))
{
token += 4;
lat = atof(token);
CHECK_INPUT(lat >= -90.0,
err << "lat must be greater than or equal to -90 degrees, not "
<< lat);
CHECK_INPUT(lat <= 90.0,
err << "lat must be less than or equal to 90 degrees, not "
<< lat);
latSet = true;
}
else if (startswith("lon=", token))
{
token += 4;
lon = atof(token);
CHECK_INPUT(lon >= -180.0,
err << "lon must be greater or equal to -180 degrees, not "
<< lon);
CHECK_INPUT(lon <= 180.0,
err << "lon must be less than or equal to 180 degrees, not "
<< lon);
lonSet = true;
}
// 1234567890
else if (startswith("mlon=", token))
{
token += 5;
mMetersPerLongitude = atof(token);
CHECK_INPUT(mMetersPerLongitude > 0.0,
err << "mMetersPerLongitude must be greater than 0, not "
<< mMetersPerLongitude);
mConstMetersPerLongitude = true;
}
// 1234567890
else if (startswith("mlat=", token))
{
token += 5;
mMetersPerDegree = atof(token);
CHECK_INPUT(mMetersPerDegree > 0.0,
err << "mMetersPerDegree must be greater than 0, not "
<< mMetersPerDegree);
}
// 1234567890123456
else if (startswith("extrapolate=", token))
{
token += 12;
int extrapolate = atoi(token);
CHECK_INPUT(extrapolate >= 0 && extrapolate <= 5,
err << "extrapolate must be an integer between 0 and 5, not "
<< extrapolate);
mMaterialExtrapolate = extrapolate;
}
// else if( startswith("ghostpts=",token))
// {
// token += 9;
// int ghost = atoi(token);
// CHECK_INPUT( ghost == 2 || ghost == 3, err << "Number of ghost points must be 2 or 3, not " << ghost );
// if( m_mesh_refinements && ghost == 2 )
// CHECK_INPUT( false, err << "Number of ghost points must be 3 when using mesh refinement ");
// m_ghost_points = ghost;
// m_ppadding = ghost;
// }
// 123456789
else if( startswith("proj=",token))
{
token +=5;
// accumulate new style string
proj0 << " +proj=" << token;
use_geoprojection = true;
proj_set=true;
}
// 123456789
else if( startswith("ellps=",token))
{
token +=6;
// accumulate new style string
proj0 << " +ellps=" << token;
use_geoprojection = true;
ellps_set=true;
}
// 123456789
else if( startswith("datum=",token))
{
token +=6;
proj0 << " +datum=" << token;
datum_set=true;
use_geoprojection = true;
}
// 123456789
else if( startswith("lon_p=",token))
{
token +=6;
proj0 << " +lon_0=" << atof(token);
use_geoprojection = true;
lon_p_set=true;
}
// 123456789
else if( startswith("lat_p=",token))
{
token +=6;
proj0 << " +lat_0=" << atof(token);
use_geoprojection = true;
lat_p_set=true;
}
// 123456789
else if( startswith("scale=",token))
{
token +=6;
proj0 << " +scale=" << atof(token);
use_geoprojection = true;
}
else
{
badOption("grid", token);
}
token = strtok(NULL, " \t");
}
//--------------------------------------------------------------------
// There are only three ways to specify a grid.
//--------------------------------------------------------------------
if (h != 0.0)
{
if (nx > 0 || nz > 0 || ny > 0)
{
//----------------------------------------------------------------
// 1. nx, [ny], nz and h
//----------------------------------------------------------------
CHECK_INPUT(nx && nz, gridSetupErr);
CHECK_INPUT(x == 0.0 && y == 0.0 && z == 0.0, gridSetupErr);
}
else
{
//--------------------------------------------------------------
// 2. x, [y], z and h
//--------------------------------------------------------------
CHECK_INPUT(x > 0.0 && z > 0.0, gridSetupErr);
CHECK_INPUT(nx == 0 && ny == 0 && nz == 0, gridSetupErr);
}
}
else
{
//--------------------------------------------------------------------
// 3. x, [y], z and nx|ny|nz
//--------------------------------------------------------------------
CHECK_INPUT(x > 0.0 && z > 0.0, gridSetupErr);
CHECK_INPUT((nx > 0) + (ny > 0) + (nz > 0) == 1, gridSetupErr);
}
int nxprime, nyprime, nzprime;
// -------------------------------------------------------------
// Make sure all the bounds are consistent.
//
// In order to divide up the space properly, we must take the
// coordinate dimension, say x, and divide by the number of grid
// points requested minus one. This is because we'd like to
// include the end points in the spatial data arrays. For example,
// if x = 1000. and nx = 10, you'd think h would be 100. However,
// if we'd like the bounds to go from -500 to 500, we actually
// need x divided up into 9 cells so that both x = -500 and x = 500
// will be included in the data array. In this case, h would be
// 111.11, giving:
//
// x[1] = -500, x[2] = -388 x[3] = -277 x[4] = -166 x[5] = -55
// x[6] = 55 x[7] = 166 x[8] = 277 x[9] = 388 x[10] = 500.
// -------------------------------------------------------------
// check syntax for lat & lon
if (!(latSet && lonSet) && (latSet || lonSet))
{
stringstream msg;
if (m_myRank == 0)
{
msg << " \n* Improper grid location specification, must specify both lat and lon variables " << endl
<< " * Missing... ";
if (!latSet)
msg << " lat=value ";
if (!lonSet)
msg << " lon=value ";
}
CHECK_INPUT(0, msg.str());
}
if (latSet && lonSet)
{
mLatOrigin = lat;
mLonOrigin = lon;
}
else
{
// Default is NTS
mLatOrigin = 37.0;
mLonOrigin = 118.0;
}
// default arguments for proj4 projection
if (use_geoprojection)
{
if (!proj_set)
{
// Default projection: Universal Transverse Mercator (UTM)
proj0 << " +proj=utm";
}
if (!ellps_set && !datum_set)
{
// default ellipse
proj0 << " +ellps=WGS84";
}
// if lon_p not given, use lon
if (!lon_p_set)
{
proj0 << " +lon_0=" << mLonOrigin;
}
// if lat_p not given, use lat
if (!lat_p_set)
{
proj0 << " +lat_0=" << mLatOrigin;
}
}
double cubelen, zcubelen;
// if( m_geodynbc_found ) {
// // Set WPP grid spacing based on Geodyn cube data
// double origin[3]={0,0,0}, ibclat, ibclon, ibcaz;
// bool found_latlon;
// int adjust;
// geodynbcGetSizes( m_geodynbc_filename, origin, cubelen, zcubelen, found_latlon, ibclat,
// ibclon, ibcaz, adjust );
// // Use approximate h
// if( h == 0.0 )
// {
// if( nx > 0 )
// h = x/(nx-1);
// else if( nz > 0 )
// h = z/(nz-1);
// else
// h = y/(ny-1);
// }
// // rounding of cube position to two decimals (prec=100), three (prec=1000) etc..
// double prec = 100;
// if( found_latlon )
// {
// CHECK_INPUT( fabs(ibcaz - mGeoAz) < 1e-5, "Error: Az in Geodyn file, "
// << ibcaz << " is different from Az in WPP, " << mGeoAz );
// // lat-lon corner of cube given
// if( adjust == 1 || origin[2] == 0 )
// {
// // h based on cube length only, adjust z-position of cube
// int nc = static_cast<int>(round(cubelen)/h);
// h = cubelen/nc;
// origin[2] -= h*( origin[2]/h-round(origin[2]/h) );
// }
// else
// {
// // h based on cube length and z-position of cube
// int a = static_cast<int>(round(origin[2]*prec));
// int b = static_cast<int>(round((origin[2]+zcubelen)*prec));
// //
// int d = gcd(a,b);
// int n1 = a/d;
// int k = static_cast<int>(round(origin[2]/(n1*h)));
// h = origin[2]/(k*n1);
// }
// // Geographic origin adjustment:
// double gridLat = mLatOrigin;
// double gridLon = mLonOrigin;
// double metersPerDegree = mMetersPerDegree;
// double deg2rad = M_PI/180;
// double phi = mGeoAz*deg2rad;
// double x = metersPerDegree*( cos(phi)*(ibclat-gridLat) + cos(ibclat*deg2rad)*(ibclon-gridLon)*sin(phi));
// double y = metersPerDegree*(-sin(phi)*(ibclat-gridLat) + cos(ibclat*deg2rad)*(ibclon-gridLon)*cos(phi));
// x -= h*(x/h-round(x/h));
// y -= h*(y/h-round(y/h));
// gridLat = ibclat - (x*cos(phi) - y*sin(phi))/metersPerDegree;
// gridLon = ibclon - (x*sin(phi) + y*cos(phi))/(metersPerDegree*cos(ibclat*deg2rad));
// mLatOrigin = gridLat;
// mLonOrigin = gridLon;
// origin[0] = x;
// origin[1] = y;
// }
// else
// {
// // lat-lon corner of cube not given, interpret origin realtive (0,0,0)
// if( m_geodynbc_center )
// {
// // Center cube in the middle of the domain (in x,y), discarding input origin.
// double xlen = x;
// double ylen = y;
// if( xlen == 0 )
// xlen = h*(nx-1);
// if( ylen == 0 )
// ylen = h*(ny-1);
// origin[0] = 0.5*(xlen-cubelen);
// origin[1] = 0.5*(ylen-cubelen);
// }
// if( adjust == 1 )
// {
// // h based on cube length only, adjust cube position
// int nc = static_cast<int>(round(cubelen/h));
// h = cubelen/nc;
// origin[0] -= h*( origin[0]/h-round(origin[0]/h) );
// origin[1] -= h*( origin[1]/h-round(origin[1]/h) );
// origin[2] -= h*( origin[2]/h-round(origin[2]/h) );
// }
// else
// {
// // h based on cube length and cube position, might be very restrictive