forked from christophersanborn/Radiative3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
636 lines (519 loc) · 21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include <iostream>
#include <cstdlib> /* for exit() */
#include <cmath>
#include <sstream>
#include <string>
#include <stdexcept>
#include "params.hpp"
#include "model.hpp"
#include "dataout.hpp" /* for SuppressAllReports() */
#include "rtcoef.hpp"
#include "cmdline.hpp"
//
#ifndef REVISION_NUM
#define REVISION_NUM "N/A"
#endif
void print_banner(); // Prints startup banner
void print_help(); // Prints help message
void process_option(CmdOpt &,
ModelParams &, // Process a command-line
MissionParams &); // option
//////
// FUNCTION: main()
//
int main(int argc, char *argv[]) {
print_banner();
MissionParams mission;
ModelParams MParams;
dataout.SuppressAllReports(); // Don't give the play-by-play; just
// dump the seismic traces at the
// end.
// ***
// ** Process Command-Line Options:
// *
CmdOpt::OptList opt_list // Get "options" from
= CmdOpt::PackageArgCArgV(argc, argv); // "arguments"
for (Index i = 0; //
i < opt_list.size(); i++) { // Process all the Options
//
try {process_option(opt_list[i], MParams, mission);}
catch (std::exception &e) {
std::cout << "** Error processing command-line option: "
<< opt_list[i].GetOptionText() << "\n** Message: "
<< e.what() << "\n** Exiting...\n";
exit(1);
}
} // Done processing command-line args
// ***
// ** Now DO the things the user asked for:
// *
if (mission.bHelpMsg) { // Print help message and exit
print_help(); //
exit(0);
}
if (true) { // Dump Model Parameters
MParams.Output(); //
}
if (mission.bOutputModParamsOctv) { // Output model params for Octave
std::stringstream outfilename;
outfilename << dataout.GetOutputDirectory();
if (outfilename.str().size() > 0) {
outfilename << "/"; // directory separator, if needed
}
outfilename << mission.FNModParamsOctv;
std::ofstream outfile(outfilename.str().c_str());
MParams.OutputOctaveText(&outfile);
outfile.close();
}
if (mission.bRTCoefTest) { // Reflection/Transmission Test
RTCoef::RunRTCoefTest(100, 10, 8, 4, 8, 4, 2);
}
if (mission.bSourcePatternTest) { // Examine source radiation pattern
std::cout << "@@ __EVENT_SOURCE_TEST__" << std::endl;
S2::S2Set * pTOAA // Degree-3 Tesselsphere for pattern-testing
= new S2::TesselSphere(S2::TESS_ICO, 3);
PhononSource::Set_TOA_Array(pTOAA);
PhononSource S
= ShearDislocation(MParams.EventSourceMT,
ECS.Convert(MParams.EventSourceLoc));
S.output_differential_probabilities(RAY_NA, RAY_P, "c");
S.output_differential_probabilities(RAY_NA, RAY_SH, "-");
S.output_differential_probabilities(RAY_NA, RAY_SV, "y");
// Characters tell GMT circle, dash,
// or vertical bar, respectively
}
if (mission.ModelBuildRequired()) { // (true if any flags that
Text IfErrorMsg; // require a constructed model
try{ // are true)
IfErrorMsg = "while constructing Earth model:";
Model Mod(MParams); // Construct Model
IfErrorMsg = "during model retrospective output:";
if (mission.bDumpGrid) { // Output grid as plaintext
Mod.GetGridRef().DumpGridToAscii();
}
if (true) { // Output all scattering objects
Scatterer::PrintAllScatteringStats();
}
IfErrorMsg = "during simulation execution:";
if (mission.bRunSim) { // RUN SIMULATION
Mod.RunSimulation(); //
}
} catch (std::exception &e) {
std::cout << "**\n** Error " << IfErrorMsg << "\n"
<< "** What: " << e.what() << "\n** Exiting...\n";
exit(1);
}
}
////
}// end main()
//
//////
// FUNCTION: print_banner()
//
// Print a startup banner at program start.
//
void print_banner() {
std::ostream * out = &std::cout;
*out << "**" << std::endl
<< "** Radiative3D - "
<< "A code for radiative transport in 3D Earth models" << std::endl
<< "**" << std::endl
<< "** (c) 2020 Christopher J. Sanborn and the" << std::endl
<< "** Solid Earth Geophysics Team" << std::endl
<< "** at the University of Connecticut, Storrs, CT."
<< std::endl
<< "** https://github.com/christophersanborn/Radiative3D"
<< std::endl
<< "**" << std::endl;
*out << "** BUILD STATS: "
<< "Revision number: " << REVISION_NUM << std::endl
<< "** "
<< "Floating-point representation: " << (8*sizeof(Real))
<< "-bit" << std::endl
<< "**" << std::endl
<< "**" << std::endl;
}
//////
// FUNCTION: print_help()
//
// Print a help message
//
void print_help() {
std::ostream * out = &std::cout;
*out << "\nRadiative3D Manual Page is available at:\n"
<< "https://github.com/christophersanborn/Radiative3D/blob/master/doc/MANUAL.md\n\n";
*out << "Recognized command-line options:\n\n";
CmdOpt::OutputAllRecognizedTokens(out);
*out << std::endl;
}
//////
// FUNCTION: process_option()
//
// Takes a CmdOpt object and sets the relevent model or operational
// parameters accordingly. When called in a loop that iterates over
// a set of CmdOpt objects that have been initialized from argc/argv,
// this has the effect of "processing" all the command-line
// arguments.
//
void process_option(CmdOpt & opt, ModelParams & params,
MissionParams & mission) {
switch (opt.GetID()) {
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_FREQ: // *** Frequency:
// ***
//
params.Frequency = opt.PopValue_Real();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_NUMBER: // *** Number of Phonons:
// ***
//
params.NumPhonons = opt.PopValue_Integer();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_REPORTS: // *** Reports (On or Off):
// ***
//
if (!opt.PeekValue()) { // If token-only,
dataout.SuppressAllReports(false); // then same as ALL_ON
}
else { // Else start from ALL_OFF and
dataout.SuppressAllReports(true); // add in what's requested
while (opt.PeekValue()) {
//
Text keywd = opt.PopValue_Text();
if (keywd == "ALL_ON") {dataout.SuppressAllReports(false);}
else if (keywd == "ALL_OFF") {dataout.SuppressAllReports(true);}
else if (keywd == "GEN") {dataout.SuppressGenerate(false);}
else if (keywd == "SCT") {dataout.SuppressScatter(false);}
else if (keywd == "REF") {dataout.SuppressReflect(false);}
else if (keywd == "COL") {dataout.SuppressCollect(false);}
else if (keywd == "CEL") {dataout.SuppressTransfer(false);}
else if (keywd == "LST") {dataout.SuppressLost(false);}
else if (keywd == "TMO") {dataout.SuppressTimeout(false);}
else if (keywd == "INV") {dataout.SuppressInvalid(false);}
else if (keywd == "SCATTERS") {
dataout.SuppressGenerate(false);
dataout.SuppressScatter(false);
dataout.SuppressReflect(false);
}
else {
throw(Runtime("Valid report keywords are: ALL_ON, ALL_OFF, GEN, "
+ Text("SCT, REF, COL, CEL, LST, TMO, INV, or SCATTERS.")));
}
//
}// end while
} // end if
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_REPORT_FILE: // *** Reports Filename
// ***
dataout.SetReportsFile(opt.PopValue_Text());
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_OUTDIR: // *** Output-file Directory
// ***
dataout.SetOutputDirectory(opt.PopValue_Text());
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_OCSRAW: // *** Output Coords (OCS)
// *** no-transform
ECS.SetOCSMapping(EarthCoords::OUT_NOTRANSFORM);
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_TTLIVE: // *** Phonon Time-to-Live:
// ***
//
params.PhononTTL = opt.PopValue_Real();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_TOA: // *** Take-Off Angle Degree:
// ***
//
params.TOA_Degree = opt.PopValue_Integer();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_COMPSELECT: // *** Grid-Compiled Model Selector:
// ***
//
params.GridSource = ModelParams::GRID_COMPILED;
params.CompiledSelector = opt.PopValue_Integer(0);
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_MODARGS: // *** Grid-Compiled Model Args:
// ***
//
while (opt.PeekValue()) {
params.CompiledArgs.push_back(opt.PopValue_Real());
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_SEISARRAY: // *** Seismometer Array:
// ***
{
//double AZ = opt.PopValue_Real();
//double R0 = opt.PopValue_Real();
//double gap = opt.PopValue_Real();
//double gather = opt.PopValue_Real();
//double n_seis = opt.PopValue_Real();
//double angle = (Geometry::Pi/180)*AZ;
//R3::XYZ uaz (sin(angle), cos(angle), 0);
//R3::OrthoAxes ori (0,0,0);
//R3::XYZ R0V (sin(angle)*R0, cos(angle)*R0, 0);
//for(int j=0; j < n_seis; j++)
// {
// R3::XYZ loc = uaz.ScaledBy(j*gap) + R0V;
// params.AddSeismometerByWavelength(loc,ori,gather);
// }
throw (Runtime(
"Arg: --seisarray: Disabled; use --seis-p2p instead."
));
// Disabled until I figure out best way to handle arg list
// given that it is not known at this point what
// coordinate system was chosen by user.
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_SEIS_P2P: // *** Point-to-Point Seis Array:
{ // ***
Count ValCount = opt.GetValueCount();
bool twogathers = (ValCount==10);
R3::XYZ Origin = opt.PopValue_XYZ();
R3::XYZ Dest = opt.PopValue_XYZ();
Real Offset = opt.PopValue_Real();
Real Gather1 = opt.PopValue_Real();
Real Gather2 = Gather1;
if (twogathers) Gather2 = opt.PopValue_Real();
int nSeis = opt.PopValue_Integer();
const R3::XYZ Span = Origin.VectorTo(Dest);
const R3::XYZ Dir = Span.Unit();
const R3::XYZ Begin = Origin + Dir.ScaledBy(Offset);
const Real Dist = Begin.VectorTo(Dest).Mag();
const Real Gap = (nSeis>1) ? (Dist / (nSeis-1))
: 0.0;
for (int i=0; i<nSeis; i++) {
R3::XYZ Loc = Begin + Dir.ScaledBy(i*Gap);
Real rangefrac = (Origin.VectorTo(Loc).Mag())/Span.Mag();
Real Gather = Gather1 + rangefrac*(Gather2-Gather1);
EarthCoords::Generic ELoc(Loc.x(),Loc.y(),Loc.z()); // Hack...
static bool warning_issued = false; //
if (!warning_issued) { // Only warn once.
std::cout << "Warning: Seis array interpolation ignores coordinate "
<< "system and could produce distorted results.\n";
warning_issued = true;
} //TODO: Seismoter allocation needs an overhaul...
if (twogathers) {
params.AddSeismometerFixedRadius(ELoc,ModelParams::AX_RTZ,Gather);
} else {
// TODO: Remove conditional and retain ONLY fixed-radius call.
// DEPRECATION NOTE: Use of this option (SEIS_P2P) to
// specify by wavelength when only one gather radius is
// given is retained temporarily to not break existing
// scripts, but this usage is deprecated in favor of using
// SEIS_P2PW when units should be in wavelengths.
params.AddSeismometerByWavelength(ELoc,ModelParams::AX_RTZ,Gather);
}
}
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_SEIS_P2PW: // *** Point-to-Point Seis Array:
{ // *** (units are wavelengths)
Count ValCount = opt.GetValueCount();
bool twogathers = (ValCount==10);
R3::XYZ Origin = opt.PopValue_XYZ();
R3::XYZ Dest = opt.PopValue_XYZ();
Real Offset = opt.PopValue_Real();
Real Gather1 = opt.PopValue_Real();
Real Gather2 = Gather1;
if (twogathers) Gather2 = opt.PopValue_Real();
int nSeis = opt.PopValue_Integer();
const R3::XYZ Span = Origin.VectorTo(Dest);
const R3::XYZ Dir = Span.Unit();
const R3::XYZ Begin = Origin + Dir.ScaledBy(Offset);
const Real Dist = Begin.VectorTo(Dest).Mag();
const Real Gap = (nSeis>1) ? (Dist / (nSeis-1))
: 0.0;
for (int i=0; i<nSeis; i++) {
R3::XYZ Loc = Begin + Dir.ScaledBy(i*Gap);
Real rangefrac = (Origin.VectorTo(Loc).Mag())/Span.Mag();
Real Gather = Gather1 + rangefrac*(Gather2-Gather1);
EarthCoords::Generic ELoc(Loc.x(),Loc.y(),Loc.z()); // Hack...
std::cout << "Warning: Seis array interpolation ignores coordinate "
<< "system and could produce distorted results.\n";
//TODO: Seismoter allocation needs an overhaul...
params.AddSeismometerByWavelength(ELoc,ModelParams::AX_RTZ,Gather);
}
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_SEISBINS: // *** Time Bins per Cycle:
// ***
//
params.TimeBinsPerCycle = opt.PopValue_Real();
params.TimeBinSize = 0;
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_SEISBINSIZE: // *** Time bin width in seconds
// ***
//
params.TimeBinsPerCycle = 0;
params.TimeBinSize = opt.PopValue_Real();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_CYLRANGE: // *** Cylinder Range:
// ***
//
params.CylinderRange = opt.PopValue_Real();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_FLATTEN: // *** Earth-flattening transform
// ***
//
ECS.SetEarthFlattening(true);
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_EARTHRAD: // *** Earth Radius
// ***
//
ECS.SetEarthRadius(opt.PopValue_Real());
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_EVENT_LOC: // *** Event Location:
{ // ***
Real loc_x = opt.PopValue_Real();
Real loc_y = opt.PopValue_Real();
Real loc_z = opt.PopValue_Real();
params.EventSourceLoc = EarthCoords::Generic(loc_x,loc_y,loc_z);
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_EVENT_MT: // *** Source Moment-Tensor:
// ***
{
Text SourceType = opt.PopValue_Text();
if (SourceType == "EQ") {
params.EventSourceMT = Tensor::USGS(0,-1,1,0,0,0);
}
else if (SourceType == "EXPL") {
params.EventSourceMT = Tensor::USGS(1,1,1,0,0,0);
}
// OP_EVENT_MT: USGS Option:
else if (SourceType == "USGS") {
try {
double rr = opt.PopValue_Real();
double tt = opt.PopValue_Real();
double pp = opt.PopValue_Real();
double rt = opt.PopValue_Real();
double rp = opt.PopValue_Real();
double tp = opt.PopValue_Real();
params.EventSourceMT = Tensor::USGS(rr,tt,pp,rt,rp,tp);
} catch(...) {
throw(Runtime(
"USGS keyword expects six numeric moment tensor elements."));
}
}
// OP_EVENT_MT: SDR Option:
else if (SourceType == "SDR") {
std::vector<Real> MTargs;
while (opt.PeekValue()) {
MTargs.push_back(opt.PopValue_Real());
}
if(MTargs.size() == 3){ // SDR only
params.EventSourceMT
= Tensor::SDR(MTargs.at(0),MTargs.at(1),MTargs.at(2));
}
else if (MTargs.size() == 4) { // SDR, and Iso
Real iso = MTargs.at(3);
if (iso > 1.0 || iso < -1.0) {
iso = Tensor::SDR::IsoFracFromIsoAngle(iso);
} // Assume iso is an angle not a fraction if outside [-1,1].
params.EventSourceMT
= Tensor::SDR(MTargs.at(0), MTargs.at(1),
MTargs.at(2), iso);
}
else if (MTargs.size() == 5) { // SDR, Iso, and Moment
Real iso = MTargs.at(3);
Real moment = MTargs.at(4);
if (moment==0.0) { // Note: New argument order. This catches
Real temp = iso; // old scripts using old arg order (unless they
iso = moment; // specified a non-zero isofrac, but there
moment = temp; // have not been many of those.)
std::cout //
<< "Warning: SDR Event Specification: "
<< "Swapped iso and moment arguments.\n"
<< "Warning: (Note new argument order: "
<< "--source=SDR,strike,dip,rake,iso,moment)\n";
} // Catch possible confusion over argument order.
if (iso > 1.0 || iso < -1.0) {
iso = Tensor::SDR::IsoFracFromIsoAngle(iso);
} // Assume iso is an angle not a fraction if outside [-1,1].
params.EventSourceMT
= Tensor::SDR(MTargs.at(0), MTargs.at(1),
MTargs.at(2), iso, moment);
}
else {
throw(Runtime(
"SDR keyword expects SDR,strike,dip,rake[,iso[,moment]]."));
}
}
else {
throw(Runtime(SourceType
+ " is not a valid source mechanism keyword."));
}
}
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_OVR_MFP: // *** Override MFPs
{ // ***
Real mfpP = opt.PopValue_Real();
Real mfpS = opt.PopValue_Real();
Scatterer::OverrideMFP(mfpP,mfpS);
} break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPT_NODEFLECT: // *** No Scattering Deflection
//
Scatterer::SetNoDeflect();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_HELP: // *** Print Help Message
// ***
mission.bHelpMsg = true;
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_RUNSIM: // *** Run Simulation
// *** (on by default, this
// *** counteracts an override)
mission.bRunSim = true;
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_DUMPGRID: // *** Plaintext Grid Dump
// ***
mission.bDumpGrid = true;
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_PARAMOUTFN: // *** Output Model Params to Octave File
// ***
mission.bOutputModParamsOctv = true;
mission.FNModParamsOctv = opt.PopValue_Text();
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_RTTEST: // *** R/T Coefficient Test
// ***
mission.bRTCoefTest = true;
mission.bRunSim = false;
break;
////////////////////////////////////////////////////////////////////
case CmdOpt::OPTM_EVENTTEST: // *** Event Source Analytics
// ***
mission.bSourcePatternTest = true;
mission.bRunSim = false;
break;
////////////////////////////////////////////////////////////////////
default: // *** Unrecognized OP_ID:
// ***
//
throw(Runtime("Unrecognized option."));
break;
}
}