forked from geodynamics/hc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hc_init.c
1474 lines (1303 loc) · 47.6 KB
/
hc_init.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
#include "hc.h"
/*
general routines dealing with the hager & Connell implementation
$Id: hc_init.c,v 1.14 2006/03/21 08:07:18 becker Exp becker $
*/
/*
initialize the basic operational modes
*/
void hc_init_parameters(struct hc_parameters *p)
{
/*
operational modes and parameters
*/
p->compressible = FALSE; /* compressibility following Panasyuk
& Steinberger */
/* surface mechanical boundary condition */
p->free_slip = TRUE; /* free slip? */
p->no_slip = FALSE; /* no slip boundary condition? */
p->platebc = FALSE; /* plate velocities? */
p->compute_geoid = 1; /* compute the geoid? 1: surface 2: all layers */
p->compute_geoid_correlations = FALSE; /* compute the geoid
correlation with
refernece only
(only works for
surface) */
p->dens_anom_scale = HC_D_LOG_V_D_LOG_D ; /* default density anomaly scaling to
go from PREM percent traveltime
anomalies to density anomalies */
p->scale_dens_anom_with_prem = TRUE; /* scale the input file
relative density anomalies
with the absolute rho value
of PREM at that layer. if
set to FALSE, will use the
average rho value */
p->read_short_dens_sh = FALSE; /* read the density anomaly file in
the short format of Becker &
Boschi (2002)? */
p->read_short_pvel_sh = FALSE; /* read the plate velocities in short format */
p->verbose = 0; /* debugging output? (0,1,2,3,4...) */
p->sol_binary_out = TRUE; /* binary or ASCII output of SH expansion */
p->solution_mode = HC_VEL; /* default: velocity output */
p->print_density_field = TRUE; /* print the scaled density field (useful for debugging) */
p->solver_mode = HC_SOLVER_MODE_DEFAULT ;
p->print_pt_sol = FALSE;
p->print_spatial = FALSE; /* by default, only print the spectral solution */
/* for four layer approaches */
p->rlayer[0] = HC_ND_RADIUS(660);
p->rlayer[1] = HC_ND_RADIUS(410);
p->rlayer[2] = HC_ND_RADIUS(100);
/* default viscosities for the four layers, in units of reference viscosity*/
p->elayer[0] = 50.; p->elayer[1] = 1.; p->elayer[2] = 0.1; p->elayer[3] = 50.;
p->visc_init_mode = HC_INIT_E_FROM_FILE; /* by default, read viscosity from file */
p->solver_kludge_l = INT_MAX; /* default: no solver tricks */
/*
depth dependent scaling of density files?
*/
p->dd_dens_scale = HC_DD_CONSTANT; /* can be HC_DD_CONSTANT, HC_DD_READ_FROM_FILE, HC_DD_POLYNOMIAL */
p->ndf = 0;
p->rdf = p->sdf = NULL;
/* plate velocities */
p->pvel_mode = HC_INIT_P_FROM_FILE; /* default is single plate velocity */
p->pvel_time = -1;
/*
filenames
*/
strncpy(p->visc_filename,HC_VISC_FILE,HC_CHAR_LENGTH);
strncpy(p->dens_filename,HC_DENS_SH_FILE,HC_CHAR_LENGTH);
strncpy(p->prem_model_filename,PREM_MODEL_FILE,HC_CHAR_LENGTH);
}
/*
first, call this routine with a blank **hc
*/
void hc_struc_init(struct hcs **hc)
{
/* this will take care of all flags and such */
*hc = (struct hcs *)calloc(1,sizeof(struct hcs ));
if(!(*hc))
HC_MEMERROR("hc_struc_init: hc");
/* just to be sure */
(*hc)->initialized = (*hc)->const_init = (*hc)->visc_init =
(*hc)->dens_init = (*hc)->pvel_init = (*hc)->orig_danom_saved = FALSE;
hc_init_polsol_struct(&((*hc)->psp));
/*
assign NULL pointers to allow reallocating
*/
(*hc)->r = (*hc)->visc = (*hc)->rvisc =
(*hc)->dfact = (*hc)->rden = (*hc)->dvisc = NULL;
(*hc)->rpb = (*hc)->fpb= NULL;
(*hc)->dens_anom = NULL; /* expansions */
(*hc)->plm = NULL;
(*hc)->prem_init = FALSE;
}
void hc_init_polsol_struct(struct hc_ps *psp)
{
psp->ncalled = 0;
/* scaling factors will only be computed once */
psp->rho_scale = 1.0;
psp->rho_init = FALSE;
psp->prop_params_init = FALSE; /* parameters for propagator computation */
psp->abg_init = FALSE; /* alpha, beta factors */
psp->prop_mats_init = FALSE; /* will be true only if save_prop_mats is */
psp->tor_init = psp->pol_init = FALSE;
psp->solver_kludge_l = INT_MAX; /* every l > psp->solver_kludge_l will
have modified core boundary
conditions */
}
/*
initialize all variables, after initializing the parameters
INPUT: sh_type: type of expansion storage/spherical haronics scheme to use
INPUT: hc_parameters: holds all the settings
OUTPUT: hc structure, gets modified
*/
void hc_init_main(struct hcs *hc,int sh_type,
struct hc_parameters *p)
{
int dummy=0;
HC_PREC dd_dummy[4]={1,1,1,1};
/* mechanical boundary condition */
if(p->free_slip){
if(p->no_slip || p->platebc)
HC_ERROR("hc_init_main","free slip and no slip does not make sense");
hc->free_slip = TRUE;
}
/*
set the default expansion type, input expansions will be
converted
*/
hc->sh_type = sh_type;
/*
start by reading in physical constants and PREM model
*/
hc_init_constants(hc,p->dens_anom_scale,p->prem_model_filename,p->verbose);
/*
initialize viscosity structure from file
*/
hc_assign_viscosity(hc,p->visc_init_mode,p->elayer,p);
/*
initialize possible depth dependent scaling of density model
*/
hc_assign_dd_scaling(HC_INIT_DD_FROM_FILE,dd_dummy,p,hc->r_cmb);
if(p->verbose)
switch(p->dd_dens_scale){
case HC_DD_CONSTANT:
fprintf(stderr,"hc_init_main: using constant dln\\rho/dln input density scaling of %g\n",
(double)hc->dens_scale);
break;
case HC_DD_READ_FROM_FILE:
fprintf(stderr,"hc_init_main: reading density scaling from file\n");
break;
case HC_DD_POLYNOMIAL:
fprintf(stderr,"hc_init_main: using polynomial density scaling (NOT IMPLEMENTED YET)\n");
exit(-1);
break;
default:
fprintf(stderr,"hc_init_main: error, dd mode %i undefined\n",
p->dd_dens_scale);
exit(-1);
}
if(p->no_slip && (!p->platebc)){
/*
no slip (zero velocity) surface boundary conditions
*/
if(p->free_slip)
HC_ERROR("hc_init","no slip and free_slip doesn't make sense");
if(p->verbose)
fprintf(stderr,"hc_init: initializing for no slip\n");
/*
read in the densities first to determine L from the density expansion
*/
hc_assign_density(hc,p->compressible,HC_INIT_D_FROM_FILE,
p->dens_filename,-1,FALSE,FALSE,p->scale_dens_anom_with_prem,
p->verbose,p->read_short_dens_sh,
p->dd_dens_scale,p->ndf,p->rdf,p->sdf,
(p->solver_mode == HC_SOLVER_MODE_VISC_SCAN)?(TRUE):(FALSE));
/*
assign all zeroes up to the lmax of the density expansion
*/
hc_assign_plate_velocities(hc,p->pvel_mode,p->pvel_filename,TRUE,hc->dens_anom[0].lmax,
FALSE,p->read_short_pvel_sh,p->verbose);
}else if(p->platebc){
/*
surface velocities
*/
if(p->free_slip)
HC_ERROR("hc_init","plate boundary condition and free_slip doesn't make sense");
if(p->verbose)
fprintf(stderr,"hc_init: initializing for surface velocities\n");
/*
read in velocities, which will determine the solution lmax
*/
hc_assign_plate_velocities(hc,HC_INIT_P_FROM_FILE,p->pvel_filename,FALSE,dummy,FALSE,
p->read_short_pvel_sh,p->verbose);
/* then read in the density anomalies */
hc_assign_density(hc,p->compressible,HC_INIT_D_FROM_FILE,p->dens_filename,hc->pvel.p[0].lmax,
FALSE,FALSE,p->scale_dens_anom_with_prem,
p->verbose,p->read_short_dens_sh, p->dd_dens_scale,p->ndf,p->rdf,p->sdf,
(p->solver_mode == HC_SOLVER_MODE_VISC_SCAN)?(TRUE):(FALSE));
}else if(p->free_slip){
/*
free slip
*/
if(p->no_slip)
HC_ERROR("hc_init","no slip and free slip does not make sense");
if(p->verbose)
fprintf(stderr,"hc_init: initializing for free-slip\n");
/* read in the density fields */
hc_assign_density(hc,p->compressible,HC_INIT_D_FROM_FILE,p->dens_filename,-1,FALSE,FALSE,
p->scale_dens_anom_with_prem,
p->verbose,p->read_short_dens_sh, p->dd_dens_scale,p->ndf,p->rdf,p->sdf,
(p->solver_mode == HC_SOLVER_MODE_VISC_SCAN)?(TRUE):(FALSE));
}else{
HC_ERROR("hc_init","boundary condition logic error");
}
/* solver tricks */
hc->psp.solver_kludge_l = p->solver_kludge_l;
if(hc->psp.solver_kludge_l != INT_MAX)
if(p->verbose)
fprintf(stderr,"hc_init_main: WARNING: applying solver CMB kludge for l > %i\n",hc->psp.solver_kludge_l);
/*
phase boundaries, if any
*/
hc_init_phase_boundaries(hc,0,p->verbose);
/* */
hc->save_solution = TRUE; /* (don')t save the propagator
matrices in hc_polsol and the
poloidal/toroidal solutions
*/
hc->initialized = TRUE;
}
/*
some of those numbers might be a bit funny, but leave them like
this for now for backward compatibility.
*/
void hc_init_constants(struct hcs *hc, HC_PREC dens_anom_scale,
char *prem_filename,hc_boolean verbose)
{
int ec;
if(hc->const_init)
HC_ERROR("hc_init_constants","why would you call this routine twice?")
if(!hc->prem_init){
/* PREM constants */
if((ec=prem_read_model(prem_filename,hc->prem,verbose))){
fprintf(stderr,"hc_init_constants: error: could not init PREM, error code: %i\n",
ec);
exit(-1);
}
hc->prem_init = TRUE;
}
/*
density scale
*/
hc->dens_scale = dens_anom_scale;
/*
constants
*/
hc->timesc = HC_TIMESCALE_YR; /* timescale [yr], like 1e6 yrs */
hc->visnor = HC_VISNOR; /* normalizing viscosity [Pas]*/
hc->gacc = HC_GACC; /* gravitational acceleration [cm/s2]*/
hc->g = HC_CAPITAL_G; /* gravitational constant [Nm2/kg2]*/
/*
radius of Earth in [m]
*/
hc->re = hc->prem->r0;
if(fabs(hc->re - (HC_RE_KM * 1e3)) > 1e-7){
fprintf(stderr,"%.7e %.7e\n",(double)(HC_RE_KM * 1e3),(double)hc->re);
HC_ERROR("hc_init_constants","Earth radius mismatch");
}
hc->secyr = HC_SECYR; /* seconds/year */
/*
those are in g/cm^3
*/
hc->avg_den_mantle = HC_AVG_DEN_MANTLE;
hc->avg_den_core = HC_AVG_DEN_CORE;
/*
take the CMB radius from the Earth model
*/
hc->r_cmb = hc->prem->r[1];
if(fabs(hc->r_cmb - 0.55) > 0.02)
HC_ERROR("hc_init_constants","Earth model CMB radius appears off");
/*
derived quantities
*/
/*
velocity scale if input is in [cm/yr], works out to be ~0.11
*/
hc->vel_scale = hc->re*PIOVERONEEIGHTY/hc->timesc/HC_VEL_IO_SCALE;
/*
stress scaling, will later be divided by non-dim radius, to go
to MPa
*/
hc->stress_scale = (PIOVERONEEIGHTY * hc->visnor / hc->secyr)/
(hc->timesc * HC_TIMESCALE_YR);
hc->const_init = TRUE;
}
/*
handle command line parameters
visc_filename[] needs to be [HC_CHAR_LENGTH]
*/
void hc_handle_command_line(int argc, char **argv,int start_from_i,
struct hc_parameters *p)
{
int i;
hc_boolean used_parameter;
for(i=start_from_i;i < argc;i++){
used_parameter = FALSE; /* */
if((p->solver_mode == HC_SOLVER_MODE_VISC_SCAN && argc < 2) || /* need
one
or
two
arguments
for
some
of
the
other
modes */
(p->solver_mode == HC_SOLVER_MODE_DYNTOPO_INVERT && argc < 3) ||
(strcmp(argv[i],"-help")==0) ||
(strcmp(argv[i],"--help")==0) ||
(strcmp(argv[i],"-h")==0) ||
(strcmp(argv[i],"-?")==0)){// help
/*
help page
*/
fprintf(stderr,"%s - perform Hager & O'Connell flow computation\n\n",
argv[0]);
fprintf(stderr,"This code can compute velocities, tractions, and geoid for simple density distributions\n");
fprintf(stderr,"and plate velocities using the semi-analytical approach of Hager & O'Connell (1981).\n");
fprintf(stderr,"This particular implementation illustrates one possible way to combine the HC solver routines.\n");
fprintf(stderr,"Based on code by Brad Hager, Richard O'Connell, and Bernhard Steinberger.\n");
fprintf(stderr,"This version by Thorsten Becker and Craig O'Neill\n\n");
switch(p->solver_mode){
case HC_SOLVER_MODE_VISC_SCAN:
fprintf(stderr,"usage example:\n\n");
fprintf(stderr,"bin/hc_visc_scan geoid.ab\n\n");
fprintf(stderr,"Scan through viscosity values and compute correlation with the geoid in geoid.ab\n\n");
break;
case HC_SOLVER_MODE_DYNTOPO_INVERT:
fprintf(stderr,"usage example:\n\n");
fprintf(stderr,"bin/hc_invert_dtopo geoid.ab dtopo.ab\n\n");
fprintf(stderr,"Invert for density anomalies based on geoid in geoid.ab and res topo wrt to air in dtopo.ab\n\n");
break;
default:
fprintf(stderr,"usage example:\n\n");
fprintf(stderr,"bin/hc -vvv\n\n");
fprintf(stderr,"Compute mantle flow solution using the default input files:\n");
fprintf(stderr," viscosity profile visc.dat\n");
fprintf(stderr," density profile dens.sh.dat\n");
fprintf(stderr," earth model prem/prem.dat\n");
fprintf(stderr,"and provide lots of output. Default setting is quiet operation.\n\n");
break;
}
fprintf(stderr,"See README.TXT in the installation directory for example for how to plot output, and\n");
fprintf(stderr,"http://geosys.usc.edu/projects/seatree/ for a graphical user interface.\n");
fprintf(stderr,"http://geodynamics.usc.edu/~becker/ugesce.html for a VirtualBox install.\n\n");
fprintf(stderr,"density anomaly options:\n");
fprintf(stderr,"-dens\tname\tuse name as a SH density anomaly model (%s)\n",
p->dens_filename);
fprintf(stderr,"\t\tAll density anomalies are in units of %g%% of PREM, all SH coefficients\n\t\tin Dahlen & Tromp convention.\n",
HC_DENSITY_SCALING*100);
fprintf(stderr,"-dshs\t\tuse the short, Becker & Boschi (2002) format for the SH density model (%s)\n",
hc_name_boolean(p->read_short_dens_sh));
fprintf(stderr,"-ds\t\tdensity scaling factor (%g)\n",
(double)p->dens_anom_scale);
fprintf(stderr,"-dnp\t\tdo not scale density anomalies with PREM but rather mean density (%s)\n",
hc_name_boolean(!p->scale_dens_anom_with_prem));
fprintf(stderr,"-dsf\tfile\tread depth dependent density scaling from file\n");
fprintf(stderr,"\t\t(overrides -ds, %s), use pdens.py to edit\n\n",
hc_name_boolean((p->dd_dens_scale == HC_DD_READ_FROM_FILE)));
//fprintf(stderr,"-dsp\t\tuse polynomial density scaling (overrides -ds, clashes with -dsf, %s)\n\n",
//hc_name_boolean((p->dd_dens_scale == HC_DD_POLYNOMIAL)));
fprintf(stderr,"Earth model options:\n");
fprintf(stderr,"-prem\tname\tset Earth model to name (%s)\n",
p->prem_model_filename);
fprintf(stderr,"-vf\tname\tviscosity structure filename (%s), use pvisc.py to edit\n",
p->visc_filename);
fprintf(stderr,"\t\tThis file is in non_dim_radius viscosity[Pas] format\n");
if(p->solver_mode == HC_SOLVER_MODE_VISC_SCAN)
fprintf(stderr,"\t\tWARNING: Will loop through a four layer viscosity scan\n\n");
fprintf(stderr,"boundary condition options:\n");
fprintf(stderr,"-fs\t\tperform free slip computation (%s)\n",hc_name_boolean(p->free_slip));
fprintf(stderr,"-ns\t\tperform no slip computation (%s)\n",hc_name_boolean(p->no_slip));
fprintf(stderr,"-pvel\tname\tset prescribed surface velocities from file name (%s)\n",
hc_name_boolean(p->platebc));
fprintf(stderr,"\t\tThe file (e.g. %s) is based on a DT expansion of cm/yr velocity fields.\n",HC_PVEL_FILE);
fprintf(stderr,"-vshs\t\tuse the short format (only lmax in header) for the plate velocities (%s)\n",
hc_name_boolean(p->read_short_pvel_sh));
fprintf(stderr,"-vdir\t\tvelocities are given in files name/vel.1.ab to vel.%i.ab for different times,\n\t\t-%g to -1 Ma before present, where name is from -pvel\n",
HC_PVEL_TSTEPS,(double)HC_PVEL_TSTEPS);
fprintf(stderr,"-vtime\ttime\tuse this particular time step of the plate velocities (%g)\n\n",
(double)p->pvel_time);
fprintf(stderr,"solution procedure and I/O options:\n");
fprintf(stderr,"-cbckl\tval\twill modify CMB boundary condition for all l > val with solver kludge (%i)\n",
p->solver_kludge_l);
if(p->solver_mode == HC_SOLVER_MODE_DEFAULT){
/* these only apply to the regular mode */
fprintf(stderr,"-ng\t\tdo not compute and print the geoid (%i)\n",
p->compute_geoid);
fprintf(stderr,"-ag\t\tcompute geoid at all layer depths, as opposed to the surface only\n");
fprintf(stderr,"-rg\tname\tcompute correlation of surface geoid with that in file \"name\",\n");
fprintf(stderr,"\t\tthis will not print out the geoid file, but only correlations (%s)\n",
hc_name_boolean(p->compute_geoid_correlations));
fprintf(stderr,"-pptsol\t\tprint pol[6] and tor[2] solution vectors (%s)\n",
hc_name_boolean(p->print_pt_sol));
fprintf(stderr,"-px\t\tprint the spatial solution to file (%s)\n",
hc_name_boolean(p->print_spatial));
fprintf(stderr,"-rtrac\t\tcompute srr,srt,srp tractions [MPa] instead of velocities [cm/yr] (default: vel)\n");
fprintf(stderr,"-htrac\t\tcompute stt,stp,spp tractions [MPa] instead of velocities [cm/yr] (default: vel)\n");
}
fprintf(stderr,"-v\t-vv\t-vvv: verbosity levels (%i)\n",
(int)(p->verbose));
fprintf(stderr,"\n\n");
exit(-1);
}else if(strcmp(argv[i],"-ds")==0){ /* density anomaly scaling factor */
hc_advance_argument(&i,argc,argv);
sscanf(argv[i],HC_FLT_FORMAT,&p->dens_anom_scale);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-cbckl")==0){ /* solver kludge */
hc_advance_argument(&i,argc,argv);
sscanf(argv[i],"%i",&p->solver_kludge_l);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vtime")==0){ /* */
hc_advance_argument(&i,argc,argv);
sscanf(argv[i],HC_FLT_FORMAT,&p->pvel_time);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-fs")==0){ /* free slip flag */
p->free_slip = TRUE;p->no_slip = FALSE;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-ns")==0){ /* no slip flag */
p->no_slip = TRUE;p->free_slip = FALSE;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-dshs")==0){ /* use short format for densities ? */
hc_toggle_boolean(&p->read_short_dens_sh);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vshs")==0){ /* use short format for velocities? */
hc_toggle_boolean(&p->read_short_pvel_sh);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-prem")==0){ /* PREM filename */
hc_advance_argument(&i,argc,argv);
strncpy(p->prem_model_filename,argv[i],HC_CHAR_LENGTH);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-dnp")==0){
hc_toggle_boolean(&p->scale_dens_anom_with_prem);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-pvel")==0){ /* velocity filename, this will switch off free slip */
hc_advance_argument(&i,argc,argv);
strncpy(p->pvel_filename,argv[i],HC_CHAR_LENGTH);
p->platebc = TRUE;p->no_slip = TRUE;p->free_slip = FALSE;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-dens")==0){ /* density filename */
hc_advance_argument(&i,argc,argv);
strncpy(p->dens_filename,argv[i],HC_CHAR_LENGTH);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-dsf")==0){ /* density scaling filename */
p->dd_dens_scale = HC_DD_READ_FROM_FILE;
hc_advance_argument(&i,argc,argv);
strncpy(p->dens_scaling_filename,argv[i],HC_CHAR_LENGTH);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-dsp")==0){
p->dd_dens_scale = HC_DD_POLYNOMIAL;
hc_advance_argument(&i,argc,argv);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vf")==0){ /* viscosity filename */
hc_advance_argument(&i,argc,argv);
strncpy(p->visc_filename,argv[i],HC_CHAR_LENGTH);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vdir")==0){ /* velocities in dir */
p->pvel_mode = HC_INIT_P_FROM_DIRECTORY;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-v")==0){ /* verbosities */
p->verbose = 1;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vv")==0){ /* verbosities */
p->verbose = 2;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-vvv")==0){
p->verbose = 3;
used_parameter = TRUE;
}
if(p->solver_mode == HC_SOLVER_MODE_DEFAULT){
/*
those subsequent options only apply for default solver
mode
*/
if(strcmp(argv[i],"-px")==0){ /* print spatial solution? */
hc_toggle_boolean(&p->print_spatial);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-pptsol")==0){ /* print
poloidal/toroidal
solution
parameters */
hc_toggle_boolean(&p->print_pt_sol);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-ng")==0){ /* do not compute geoid */
p->compute_geoid = 0;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-ag")==0){ /* compute geoid at all layers */
p->compute_geoid = 2;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-rg")==0){ /* compute geoid correlations */
p->compute_geoid = TRUE;
hc_toggle_boolean(&p->compute_geoid_correlations);
hc_advance_argument(&i,argc,argv); /* filename */
strncpy(p->ref_geoid_file,argv[i],HC_CHAR_LENGTH);
hc_read_scalar_shexp(p->ref_geoid_file,&(p->ref_geoid),"reference_geoid",p);
used_parameter = TRUE;
}else if(strcmp(argv[i],"-rtrac")==0){ /* compute radial
tractions */
p->solution_mode = HC_RTRACTIONS;
used_parameter = TRUE;
}else if(strcmp(argv[i],"-htrac")==0){ /* compute horizontal
tractions */
p->solution_mode = HC_HTRACTIONS;
used_parameter = TRUE;
}
} /* end default operation mode branch */
if(!used_parameter){
fprintf(stderr,"%s: can not use parameter %s, use -h for help page\n",
argv[0],argv[i]);
exit(-1);
}
}
}
/*
assign viscosity structure
mode == 0
read in a viscosity structure with layers of constant viscosity in
format
r visc
where r is non-dim radius and visc non-dim viscosity, r has to be
ascending
*/
void hc_assign_viscosity(struct hcs *hc,int mode,
HC_PREC elayer[4],struct hc_parameters *p)
{
FILE *in;
int i;
char fstring[100];
HC_PREC mean,mweight,rold,mws;
switch(mode){
case HC_INIT_E_FOUR_LAYERS:
/* initialize a four layer viscosity structure, viscosity values
for 2871-660, 660-410, 410-100,100-0 should be given in units
of visnor [1e21] as elayer[4]
*/
hc_vecrealloc(&hc->rvisc,4,"hc_assign_viscosity");
hc_vecrealloc(&hc->visc,4,"hc_assign_viscosity");
/* number of layers */
hc->nvis = 4;
/* radii */
hc->rvisc[0] = hc->r_cmb;
hc->rvisc[1] = p->rlayer[0];
hc->rvisc[2] = p->rlayer[1];
hc->rvisc[3] = p->rlayer[2];
for(i=0;i < hc->nvis;i++){
hc->visc[i] = elayer[i];
//fprintf(stderr,"%11g %11g\n",hc->rvisc[i],hc->visc[i]);
}
if(p->verbose)
fprintf(stderr,"hc_assign_viscosity: assigned four layer viscosity: %.2e %.2e %.2e %.2e\n",
(double)hc->visc[0],(double)hc->visc[1],
(double)hc->visc[2],(double)hc->visc[3]);
break;
case HC_INIT_E_FROM_FILE:
/*
init from file part
*/
if(hc->visc_init)
HC_ERROR("hc_assign_viscosity","viscosity already read from file, really read again?");
/*
read viscosity structure from file
format:
r[non-dim] visc[non-dim]
from bottom to top
*/
in = ggrd_open(p->visc_filename,"r","hc_assign_viscosity");
hc_vecrealloc(&hc->rvisc,1,"hc_assign_viscosity");
hc_vecrealloc(&hc->visc,1,"hc_assign_viscosity");
hc->nvis = 0;mean = 0.0;mws = 0.0;
/* read sscanf string */
hc_get_flt_frmt_string(fstring,2,FALSE);
rold = hc->r_cmb;
/* start read loop */
while(fscanf(in,HC_TWO_FLT_FORMAT,
(hc->rvisc+hc->nvis),(hc->visc+hc->nvis))==2){
if(hc->visc[hc->nvis] < 1e15)
fprintf(stderr,"hc_assign_viscosity: WARNING: expecting viscosities in Pas, read %g at layer %i\n",
(double)hc->visc[hc->nvis],hc->nvis);
/* normalize viscosity here */
hc->visc[hc->nvis] /= hc->visnor;
if(hc->nvis == 0)
if( hc->rvisc[hc->nvis] < hc->r_cmb-0.01){
fprintf(stderr,"hc_assign_viscosity: error: first radius %g is below CMB, %g\n",
(double)hc->rvisc[hc->nvis], (double)hc->r_cmb);
exit(-1);
}
if(p->verbose){
/* weighted mean, should use volume, really, but this is just
for information */
mweight = ( hc->rvisc[hc->nvis] - rold);
mws += mweight;
rold = hc->rvisc[hc->nvis];
mean += log(hc->visc[hc->nvis]) * mweight;
}
if(hc->nvis){
if(hc->rvisc[hc->nvis] < hc->rvisc[hc->nvis-1]){
fprintf(stderr,"hc_assign_viscosity: error: radius has to be ascing, entry %i (%g) smaller than last (%g)\n",
hc->nvis+1,(double)hc->rvisc[hc->nvis],
(double)hc->rvisc[hc->nvis-1]);
exit(-1);
}
}
hc->nvis++;
hc_vecrealloc(&hc->rvisc,hc->nvis+1,"hc_assign_viscosity");
hc_vecrealloc(&hc->visc,hc->nvis+1,"hc_assign_viscosity");
}
fclose(in);
if(hc->rvisc[hc->nvis-1] > 1.0){
fprintf(stderr,"hc_assign_viscosity: error: first last %g is above surface, 1.0\n",
(double)hc->rvisc[hc->nvis-1]);
exit(-1);
}
if(p->verbose){
/* last entry */
mweight = ( 1.0 - hc->rvisc[hc->nvis-1]);
mws += mweight;
rold = hc->rvisc[hc->nvis-1];
mean += log(hc->visc[hc->nvis-1]) * mweight;
mean = exp(mean/mws);
fprintf(stderr,"hc_assign_viscosity: read %i layered viscosity[Pas] from %s\n",
hc->nvis,p->visc_filename);
fprintf(stderr,"hc_assign_viscosity: rough estimate of mean viscosity: %g x %g = %g Pas\n",
(double)mean, (double)hc->visnor, (double)(mean*hc->visnor));
}
break;
default:
HC_ERROR("hc_assign_viscosity","mode undefined");
break;
}
hc->visc_init = TRUE;
}
/*
assign/initialize the density anomalies and density factors
if mode==0: expects spherical harmonics of density anomalies [%] with
respect to the 1-D reference model (PREM) given in SH
format on decreasing depth levels in [km]
spherical harmonics are real, fully normalized as in
Dahlen & Tromp p. 859
this routine assigns the inho density radii, and the total (nrad=inho)+2
radii
furthermore, the dfact factors are assigned as well
set density_in_binary to TRUE, if expansion given in binary
nominal_lmax: -1: the max order of the density expansion will either
determine the lmax of the solution (free-slip, or vel_bc_zero) or
will have to be the same as the plate expansion lmax (!free_slip && !vel_bc_zero)
else: will zero out all entries > nominal_lmax
*/
void hc_assign_density(struct hcs *hc,
hc_boolean compressible,int mode, /* compressible computation? assignment mode? */
char *filename,int nominal_lmax, /* input density file name */
hc_boolean layer_structure_changed,
hc_boolean density_in_binary,
hc_boolean scale_dens_anom_with_prem,
hc_boolean verbose,
hc_boolean use_short_format,
hc_boolean dd_dens_scale, /* depth dependent or polynomial scaling ? */
int ndf,HC_PREC *rdf,HC_PREC *sdf, /* depth dependent scaling factors */
hc_boolean save_orig_danom) /* save the original density anomalies for later rescaling */
{
FILE *in;
int type,lmax,shps,ilayer,nset,ivec,i,j;
HC_PREC *dtop,*dbot,zlabel,local_scale,dens_scale[1];
double rho0;
hc_boolean reported = FALSE,read_on;
HC_PREC dtmp[3];
hc->compressible = compressible;
hc->inho = 0;
if(hc->dens_init) /* clear old expansions, if
already initialized */
sh_free_expansion(hc->dens_anom,hc->inho);
/* get PREM model, if not initialized */
if(!hc->prem_init)
HC_ERROR("hc_assign_density","assign 1-D reference model (PREM) first");
switch(mode){
case HC_RESCALE_D:
/* resuse old density model, apply new scaling */
if(!hc->orig_danom_saved)
HC_ERROR("hc_assign_density","trying to rescale original density anomaly model, but it was not saved");
for(i=0;i<hc->inho;i++){
sh_aexp_equals_bexp_coeff((hc->dens_anom+i),
(hc->dens_anom_orig+i));
}
break;
case HC_INIT_D_FROM_FILE:
if(hc->dens_init)
HC_ERROR("hc_assign_density","really read dens anomalies again from file?");
/*
read in density anomalies in spherical harmonics format for
different layers from file.
this assumes that we are reading in anomalies in percent
*/
in = ggrd_open(filename,"r","hc_assign_density");
if(verbose)
fprintf(stderr,"hc_assign_density: reading density anomalies in [%g%%] from %s\n",
100*HC_DENSITY_SCALING,filename);
hc->inho = 0; /* counter for density layers */
/* get one density expansion */
hc_get_blank_expansions(&hc->dens_anom,1,0,
"hc_assign_density");
/*
read all layers as spherical harmonics assuming real Dahlen &
Tromp (physical) normalization, short format
*/
if(use_short_format){
if(verbose)
fprintf(stderr,"hc_assign_density: using short format for density SH\n");
fscanf(in,"%i",&nset);
ilayer = -1;
}else{
if(verbose)
fprintf(stderr,"hc_assign_density: using default SH format for density\n");
}
read_on = TRUE;
while(read_on){
if(use_short_format){
/* short format I/O */
i = fscanf(in,HC_FLT_FORMAT,dtmp);zlabel = (HC_PREC)dtmp[0];
i += fscanf(in,"%i",&lmax);
read_on = (i == 2)?(TRUE):(FALSE);
ivec = 0;shps = 1;type = HC_DEFAULT_INTERNAL_FORMAT;
ilayer++;
}else{
read_on = sh_read_parameters_from_stream(&type,&lmax,&shps,&ilayer, &nset,
&zlabel,&ivec,in,FALSE,density_in_binary,
verbose);
}
if(read_on){
if((verbose)&&(!reported)){
if(nominal_lmax > lmax)
fprintf(stderr,"hc_assign_density: density lmax: %3i filling up to nominal lmax: %3i with zeroes\n",
lmax,nominal_lmax);
if(nominal_lmax != -1){
fprintf(stderr,"hc_assign_density: density lmax: %3i limiting to lmax: %3i\n",
lmax,nominal_lmax);
}else{
fprintf(stderr,"hc_assign_density: density lmax: %3i determines solution lmax\n",
lmax);
}
reported = TRUE;
if(verbose >= 2)
fprintf(stderr,"hc_assign_density: non_dim radius %% factor PREM \\rho/mean_rho layer # depth[km] rho[kg/m^3]\n");
}
/*
do tests
*/
if((shps != 1)||(ivec))
HC_ERROR("hc_assign_density","vector field read in but only scalar expansion expected");
/* test and assign depth levels */
hc->rden=(HC_PREC *)
realloc(hc->rden,(1+hc->inho)*sizeof(HC_PREC));
if(!hc->rden)
HC_MEMERROR("hc_assign_density: rden");
/*
assign depth, this assumes that we are reading in depths [km]
*/
hc->rden[hc->inho] = HC_ND_RADIUS((HC_PREC)zlabel);
if(scale_dens_anom_with_prem){
/*
get reference density at this level
*/
prem_get_rho(&rho0,(double)(hc->rden[hc->inho]),
hc->prem);
rho0 /= 1000.0;
if(rho0 < 3)
fprintf(stderr,"\nhc_assign_density: WARNING: using small (%g) density from PREM for layer at depth %g\n\n",
(double)rho0*1000,(double)HC_Z_DEPTH(hc->rden[hc->inho]));
}else{
/* mean value */
rho0 = (double)hc->avg_den_mantle;
}
/*
density anomaly
*/
/* scaling factor without depth dependence */
dens_scale[0] = HC_DENSITY_SCALING * (HC_PREC)rho0;
if(verbose >= 2){
fprintf(stderr,"hc_assign_density: r: %11g anom scales: %11g x %11g = %11g\t%5i out of %i, z: %11g %6.1f\n",
(double)hc->rden[hc->inho],
HC_DENSITY_SCALING,rho0/ (double)hc->avg_den_mantle,(double)dens_scale[0],hc->inho+1,nset,(double)zlabel,rho0*1000);
}
if(hc->inho){
/*
check by comparison with previous expansion
*/
if(nominal_lmax == -1)
if(lmax != hc->dens_anom[0].lmax)
HC_ERROR("hc_assign_density","lmax changed in file");
if(hc->rden[hc->inho] <= hc->rden[hc->inho-1]){
fprintf(stderr,"hc_assign_density: %i %g %g\n",hc->inho,
(double)hc->rden[hc->inho],
(double)hc->rden[hc->inho-1]);
HC_ERROR("hc_assign_density","depth should decrease, radius increase (give z[km])");
}
}
/*
make room for new expansion
*/
hc_get_blank_expansions(&hc->dens_anom,hc->inho+1,hc->inho,
"hc_assign_density");
/*
initialize expansion on irregular grid
*/
sh_init_expansion((hc->dens_anom+hc->inho),
(nominal_lmax > lmax) ? (nominal_lmax):(lmax),
hc->sh_type,0,verbose,FALSE);
/*
read parameters and scale (put possible depth dependence of
scaling here)
will assume input is in physical convention
*/
sh_read_coefficients_from_stream((hc->dens_anom+hc->inho),1,lmax,in,density_in_binary,
dens_scale,verbose);
hc->inho++;
} /* end actualy read on */
} /* end while */
/*
assign actual top layer density
*/
hc->rho_top_kg = rho0 * 1000;
if(hc->inho != nset)
HC_ERROR("hc_assign_density","file mode: mismatch in number of layers");
fclose(in);
break;
default:
HC_ERROR("hc_assign_density","mode undefined");
break;
}
if(save_orig_danom && (!hc->dens_init)){
/*
make a copy of the original density anomaly before applying
depth dependent scaling, only done once per run
*/
hc_get_blank_expansions(&hc->dens_anom_orig,hc->inho+1,hc->inho,
"hc_assign_density");
for(i=0;i<hc->inho;i++){
sh_init_expansion((hc->dens_anom_orig+i),hc->dens_anom[0].lmax,
hc->sh_type,0,FALSE,FALSE);
sh_aexp_equals_bexp_coeff((hc->dens_anom_orig+i),(hc->dens_anom+i));
}
hc->orig_danom_saved=TRUE;
}
/*
scale with possibly depth dependent factor
*/
for(i=0;i < hc->inho;i++){
/*
depth dependent factor?
*/
local_scale = hc_find_dens_scale(hc->rden[i],hc->dens_scale,dd_dens_scale,rdf,sdf,ndf);
sh_scale_expansion((hc->dens_anom+i),local_scale);
if(verbose >= 2){
fprintf(stderr,"hc_assign_density: r: %11g additional %s d\\rho/dinput: %11g \tlayer %5i out of %i\n",
(double)hc->rden[i],
(dd_dens_scale == HC_DD_READ_FROM_FILE)?("depth-dependent"):((dd_dens_scale==HC_DD_CONSTANT)?("constant"):("polynomial")),(double)local_scale,i,hc->inho);
}
}
if((!hc->dens_init)||(layer_structure_changed)){
/*
assign the other radii, nrad + 2
*/
hc->nrad = hc->inho;
hc->nradp2 = hc->nrad + 2;
hc_vecrealloc(&hc->r,hc->nradp2,"hc_assign_density");
/*
viscosity at density layers for horizontal stress
computations */
hc_vecrealloc(&hc->dvisc,hc->nradp2,"hc_assign_density");
hc->r[0] = hc->r_cmb; /* CMB */
if(hc->rden[0] <= hc->r[0]){
fprintf(stderr,"hc_assign_density: rden[0]: %g r[0]: %g\n",(double)hc->rden[0],(double) hc->r[0]);
HC_ERROR("hc_assign_density","first density layer has to be above internal CMB limit");
}
for(i=0;i<hc->nrad;i++) /* density layers */
hc->r[i+1] = hc->rden[i];
if(hc->rden[hc->nrad-1] >= 1.0)
HC_ERROR("hc_assign_density","uppermost density layer has to be below surface");
hc->r[hc->nrad+1] = 1.0; /* surface */
/*