-
Notifications
You must be signed in to change notification settings - Fork 3
/
block_matrix.c
1670 lines (1527 loc) · 44.8 KB
/
block_matrix.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
/*
assemble matrices for the block inversion routine
all matrices are stored FORTRAN style
$Id: block_matrix.c,v 1.16 2004/10/05 01:09:46 becker Exp $
*/
#include "interact.h"
#include "blockinvert.h"
/*
assemble the matrices which change with the fault coupling
factors: F, GF, E, K2, and K
some input: G, D, I
the funny memory handling was added to track down a memory leak problem
*/
void assemble_block_fltdep_matrices(COMP_PRECISION **f,
COMP_PRECISION **gf,
COMP_PRECISION **e,
COMP_PRECISION **k2mat,
COMP_PRECISION *g,
COMP_PRECISION *d,
COMP_PRECISION *imat,
int m,int n,int nflt,int nrb,
int nrgp,int nrsp,int nsnf,
COMP_PRECISION *xtry,
struct bflt *fault,
struct bck *block,
my_boolean invert_for_ld,
my_boolean invert_for_cfac)
{
int pdim,tsize;
static int gf_size=0;
/*
assemble F which relates the block motion parameters
to differences in fault location velocities,
ie. global slip. dimensions: (dim*nflt, nbase*nrb)
F uses the globally-projected fault coordinates
we are passing the locking factor vector, it is a [nflt] and has
potentially different coupling factors for each fault, normally
should be all unity. if we are inverting for both cfacs and lds,
the vector also holds the locking depths, hence we have to shift
this vector gets only reference if invert_for_cfac is set
*/
assemble_block_f(f,fault,nflt,nrb,block,
(xtry+n+((invert_for_ld)?(nflt):(0))),
invert_for_cfac);
#ifdef BLOCK_SPHERICAL
pdim = 3;
#else
pdim = BLOCK_DIM;
#endif
//
//
// calculate GF = G(nslip*nflt,dim*nflt) *
// F(block_dim*nflt,nbase*nrb)
//
// dimensions: (nslip*nflt, nbase*nblock)
//
/* allocate space for GF */
tsize = nsnf *n;
if(gf_size != tsize){
my_vecrealloc(gf,tsize,"assemble_block_fltdep_matrices: gf");
gf_size = tsize;
}
calc_AB_ftn(g,nsnf,BLOCK_DIM*nflt,*f,n,*gf);
//
// calculate E=D(pdim*nrgp,nslip*nflt).GF(nslip*nflt, nbase*nrb)
// dimensions: E(pdim*nrgp, n = nbase*nrb)
//
/* allocate space for E */
tsize = pdim * nrgp *n;
my_vecrealloc(e,tsize,"assemble_block_fltdep_matrices: e");
calc_AB_ftn(d,pdim * nrgp, nsnf, *gf, n, *e);
if(nrsp){
//
// assemble K2(nrsp*6,nbase*nrb) = I G F
//
/* allocate space for K2 */
tsize = nrsp * 6 * n;
my_vecrealloc(k2mat,tsize,"assemble_block_fltdep_matrices: k2");
calc_AB_ftn(imat,6*nrsp, nsnf, *gf, n, *k2mat);
}
}
/*
assemble A matrix that links block motion parameters, b, to
velocities at the nrp velocity observational points
b is NBASE * nrb (nrb: nr of blocks)
A is DIM * nrp x NBASE * nrb
DIM is either BLOCK_DIM or 3 (for spherical)
gpx[nrp*BLOCK_DIM] are the projected
locations of the observational points
bcode[nrp*BLOCK_DIM] the block code of each point
block[nrb].fixed the no-rigid motion flag
assemble A matrix in FORTRAN style (for solvers)
A is only dependent on the block geometry, not the faults
if single_block is >= 0, will assume that all points belong
to single_block.
*/
void assemble_block_a(COMP_PRECISION **a,COMP_PRECISION *gpx,
int *bcode, int nrp,int nrb,struct bck *block,
int single_block,struct bmd *mod)
{
int i,j,k,icol,n,m,offset,*loc_bcode,inode,pdim,irow;
/*
normally, we will use the fixed flag for blocks whose rotation
is set to zero. however, if we are only looking for one block's
rotation, we will set it to FALSE (else all zero entries in A)
*/
my_boolean use_fixed_block_flag = TRUE;
#ifdef SUPER_DEBUG
static my_boolean pinit = FALSE;
#endif
if((BLOCK_NBASE != 3)||(BLOCK_DIM != 2)){
fprintf(stderr,"assemble_block_a: dim: %i or nbase: %i not implemented\n",
BLOCK_DIM,BLOCK_NBASE);
exit(-1);
}
/*
number of rows of A
*/
#ifdef BLOCK_SPHERICAL
pdim = 3; /* x,y,z cartesian components */
#else
pdim = BLOCK_DIM; /* only x and y in projected frame */
#endif
m = pdim * nrp;
/*
generate a copy of the boundary code vector, but make it only nrp
long. (bcode is nrp * BLOCK_DIM)
*/
my_ivecalloc(&loc_bcode,nrp,"assmble_block_a: loc_bcode");
if(single_block >= 0){
if(block[single_block].fixed){
fprintf(stderr,"assmble_block_a: WARNING: assuming all points belong to block %i\n",
single_block+1);
fprintf(stderr,"assmble_block_a: WARNING: disregarding block fixedness for all blocks (nbase: %i)\n",
BLOCK_NBASE);
use_fixed_block_flag = FALSE;
}
for(i=0;i < nrp;i++) /* all points are in single_block */
loc_bcode[i] = single_block;
}else{
/* original */
for(i=0;i < nrp;i++)
loc_bcode[i] = bcode[i*BLOCK_DIM];
}
/*
number of columns of A
*/
n = BLOCK_NBASE * nrb;
/*
re-allocate A array
*/
my_vecrealloc(a,n*m,"assemble_block_a: a");
if((BLOCK_DIM != 2)||(BLOCK_NBASE != 3)){
fprintf(stderr,"assemble_block_a: not prepared for dim != 2 or block_base != 3\n");
exit(-1);
}
#ifdef BLOCK_SPHERICAL
if(!mod->pbase_init) /* init basis vectors */
init_gps_pbase(mod);
#endif
for(k=0;k < nrb;k++){ /* block loop */
for(j=0;j < BLOCK_NBASE;j++){
icol = k * BLOCK_NBASE + j; /* column counter
0 ... n-1 */
offset = icol * m;
for(i=inode=irow=0;i < m;i++){/*
row counter, loops through
all data points and pdim dimensions
*/
if(loc_bcode[inode] == k){
/*
point i is in block k
*/
/*
rigid body translation and rotation part
*/
if((!block[loc_bcode[inode]].fixed)||
(!use_fixed_block_flag)){ /* this block is not fixed with
respect to rigid body
motions and we are using the
fixed block flags
*/
#ifdef BLOCK_SPHERICAL
/*
spherical setup:
| 0 r_z -r_y |
A' w = | -r_z 0 r_x | w = v
| r_y -r_x 0 |
where r_x, r_y, r_z are the cartesian coordinates and the
solution vector will be omega_x, omega_y, omega_z
NOTE THAT THIS ASSIGNMENT WORKS COLUMN BY COLUMN!
*/
if(j == 0){ /* first sub-col */
if(irow == 0) /* 0 */
*(*a+offset+i)= 0.0;
else if(irow == 1)
*(*a+offset+i)= -mod->gcx[inode*3+INT_Z]; /* -r_z */
else
*(*a+offset+i)= mod->gcx[inode*3+INT_Y]; /* r_y */
}else if(j == 1){ /* second sub-col */
if(irow == 0)
*(*a+offset+i)= mod->gcx[inode*3+INT_Z]; /* r_z */
else if(irow == 1)
*(*a+offset+i)= 0.0; /* 0 */
else
*(*a+offset+i)= -mod->gcx[inode*3+INT_X]; /* -r_x */
}else{ /* third sub-col */
if(irow == 0)
*(*a+offset+i)= -mod->gcx[inode*3+INT_Y]; /* -r_y */
else if(irow == 1)
*(*a+offset+i)= mod->gcx[inode*3+INT_X]; /* r_x */
else
*(*a+offset+i)= 0.0; /* 0 */
}
#else
/*
cartesian setup:
| -x_y 1 0 |
| x_x 0 1 |
where x_x, x_y are the mercator projected coordinates
and the solution vector will be omega, v_x, v_y
*/
if(j == 0){ /* first sub-col: omega */
if(irow==0)
*(*a+offset+i)= -gpx[inode*BLOCK_DIM+INT_Y]; /* - x_data_y */
else
*(*a+offset+i)= gpx[inode*BLOCK_DIM+INT_X]; /* x_data_x */
}else if(j == 1){ /* second sub-col: vx_0 */
if(irow==0)
*(*a+offset+i)=1.0;
else
*(*a+offset+i)=0.0;
}else{ /* third sub-col: vy_0 */
if(irow==0)
*(*a+offset+i)=0.0;
else
*(*a+offset+i)=1.0;
}
#endif
}else{
/*
block is fixed with respect to rigid body motions, SVD
should find the minimum norm solution that corresponds
to vx_0,vy_0,omega = 0 for this point
*/
*(*a+offset+i)=0.0;
}
}else{
/*
point is not in block
*/
*(*a+offset+i)=0.0;
}
irow++;/* row counter */
if(irow == pdim){ /* increment node counter */
irow=0;
inode++;
}
}
}
}
free(loc_bcode);
#ifdef SUPER_DEBUG
if(!pinit){
print_matrix_ftrn_file(*a,m,n,"a.dat",FALSE);
pinit=TRUE;
}
#endif
}
/*
assemble the D matrix that links fault-local slip,
\hat{vec{u}}, and global displacement rates
(velocities), \vec{v}
C is the coseismic displacement part as in
v = R - C
D has dimensions DIM * nrp times nr_of_slip_dir * nrf
C = \vec{v} = D \hat\vec{u}
DIM is either BLOCK_DIM or 3 (for spherical)
D will be assembled in FORTRAN convention
*/
void assemble_block_d(COMP_PRECISION **d,struct bflt *fault,
int nflt,int nrp,int nslip,
struct bmd *mod)
{
int i,j,k,m,n,irow,icol,offset,idir,tsize;
static int d_size=0;
#ifdef SUPER_DEBUG
static my_boolean pinit=FALSE;
#endif
#ifdef BLOCK_SPHERICAL
COMP_PRECISION xp[3];
m = 3 * nrp;
if(!mod->cvel_init) /* initialize cartesian GPS velocities */
init_cart_gps_velsig(mod,FALSE);
#else
int l;
m = BLOCK_DIM * nrp;
#endif
n = nslip * nflt; /* nslip is nr of types of slip */
/* allocate space for D */
tsize = n * m;
if(d_size != tsize){
my_vecrealloc(d,tsize,"assemble_block_d: d");
d_size = tsize;
}
#ifdef BLOCK_SPHERICAL
xp[INT_R] = 0.0;
#endif
for(i=0;i < nflt;i++){ /* i: fault loop */
if((!fault[i].vertical)&&(nslip < 2)){
fprintf(stderr,"assemble_block_d: fault %i has dip %g but nslip: %i\n",
i+1,fault[i].dip,nslip);
exit(-1);
}
for(j=0;j < nslip;j++){
/* j: fault slip direction loop */
/* fault slip directions have been sorted:
strike, dip, normal in read_bflt
if j=0: strike direction
j=1: normal or dip direction, depending on the fault
being vertical or not
*/
icol = nslip * i + j;
offset = icol * m;
idir = block_slip_direction(j,(fault+i));
for(k=0;k < nrp;k++){ /* k: loop through observational points */
#ifdef BLOCK_SPHERICAL
// convert polar, rotated displacements to cartesian
xp[INT_THETA]= -(fault+i)->v[k].vc[idir][INT_Y];
xp[INT_PHI] = (fault+i)->v[k].vc[idir][INT_X];
irow = k * 3;
pv2cv(xp,(*d + offset + irow),(mod->pbase + k*9));
#else
for(l=0;l < BLOCK_DIM;l++){ /* l: global directions */
irow = k * BLOCK_DIM + l;
*(*d + offset + irow) = (fault+i)->v[k].vc[idir][l];
}
#endif
}
}
}
#ifdef SUPER_DEBUG
if(!pinit){
print_matrix_ftrn_file(*d,m,n,"d.dat",FALSE);
pinit=TRUE;
}
#endif
}
/*
assemble the I matrix that links fault-local slip, \hat{vec{u}},
and global stresses
I has dimensions 6 * nrp x nr_of_slip_dir * nrf
I will be assembled in FORTRAN convention
*/
void assemble_block_i(COMP_PRECISION **imat,struct bflt *fault,
int nflt,int nrp,int nslip)
{
int i,j,k,l,m,n,irow,icol,offset,idir,tsize;
static int i_size=0;
#ifdef DEBUG
static my_boolean pinit=FALSE;
#endif
m = 6 * nrp;
n = nslip * nflt; /* nslip is nr of types of slip */
/*
realloc space for I
*/
tsize = n * m;
if(i_size != tsize){
my_vecrealloc(imat,tsize,"assemble_block_i: I");
i_size = tsize;
}
for(i=0;i < nflt;i++){ /* i: fault loop */
if((!fault[i].vertical)&&(nslip < 2)){
fprintf(stderr,"assemble_block_i: fault %i has dip %g but nslip: %i\n",
i+1,fault[i].dip,nslip);
exit(-1);
}
for(j=0;j < nslip;j++){ /* j: fault slip direction loop */
icol = nslip * i + j;
offset = icol * m;
idir = block_slip_direction(j,(fault+i));
for(k=irow=0;k < nrp;k++,irow += 6){/* k: loop through observational points */
for(l=0;l < 6;l++) /* l: stress components */
*(*imat + offset + irow + l) =
(fault+i)->s[k].sc[idir][l];
}
}
}
#ifdef DEBUG
if(!pinit){
print_matrix_ftrn_file(*imat,m,n,"i.dat",FALSE);
pinit=TRUE;
}
#endif
}
/*
assemble the G matrix that links global slip, \vec{u}, in a
cartesian lon/lat system to fault-local strike, normal (and dip)
components,\hat{\vec{u}}
G has dimensions
nslip * nrf times BLOCK_DIM * nrf
\hat{u} = G . u
G will be assembled in FORTRAN convention
G depends on the fault geometry orientation, but not the
locking depth
*/
void assemble_block_g(COMP_PRECISION **g,struct bflt *fault,
int nflt, int nslip)
{
int i,j,k,l,m,n,idir,irow,icol,offset,tsize;
static int g_size=0;
#ifdef SUPER_DEBUG
static my_boolean pinit=FALSE;
#endif
m = nslip * nflt; /* nslip is nr of types of slip */
n = BLOCK_DIM * nflt;
if(BLOCK_DIM != 2){
fprintf(stderr,"assemble_block_g should be adpted for DIM != 2\n");
exit(-1);
}
/*
realloc space for G
*/
tsize = n * m;
if(g_size != tsize){
my_vecrealloc(g,tsize,"assemble_block_g: g");
g_size = tsize;
}
for(i=0;i < nflt;i++){ /* i: fault loop */
for(j=0;j < BLOCK_DIM;j++){ /* j: global direction loop */
icol = BLOCK_DIM * i + j;
offset = icol * m;
for(k=0;k < nflt;k++) /* k: second (row) fault loop */
for(l=0;l < nslip;l++){/* l: slip directions */
irow = k * nslip + l;
if(i == k){ /*
this matrix corresponds to t
the unity vectors in
strike and normal/dip
direction and is zero else
*/
idir = block_slip_direction(l,(fault+i));
*(*g + offset + irow) = (fault+i)->evec[idir*3+j];
}else{
*(*g + offset + irow) = 0.0;
}
}
}
}
#ifdef SUPER_DEBUG
if(!pinit){
print_matrix_ftrn_file(*g,m,n,"g.dat",FALSE);
pinit++;
}
#endif
}
/*
assemble F matrix that links block motion parameters, b, to
global velocity differences (v_left - v_right) at the
fault midpoints, interpreted as slip, in a global, lon/lat
cartesian system
b is NBASE * nrb (nrb: nr of blocks)
F is DIM * nflt x NBASE * nrb
assemble F matrix in FORTRAN style (for solvers)
block[nrb].fixed is TRUE if the block is fixed with respect to
rigid body motions
cfac[nflt] is a coupling factor vector (different from the
constant factor we have for each fault during input), that should
normally be initialized to all unity
cfac only gets referenced if invert_for_cfac is set
for a spherical calculation, we determine F from
B . S
where S links the rotation vectors to cartesian velocities
and B rotates those into the lon/lat system
*/
void assemble_block_f(COMP_PRECISION **f,struct bflt *fault,
int nflt,int nrb, struct bck *block,
COMP_PRECISION *cfac,
my_boolean invert_for_cfac)
{
int i,j,k,l,icol,irow,n,m,offset,pdim,mf,tsize;
COMP_PRECISION fac,loc_cfac;
#ifdef BLOCK_SPHERICAL
COMP_PRECISION *b, *s;
#endif
#ifdef SUPER_DEBUG
static my_boolean written=FALSE;
#endif
#ifdef DEBUG
static my_boolean warned=FALSE;
if(invert_for_cfac)
if(!warned){
for(i=0;i<nflt;i++){
if(fabs(cfac[i]-1.0) > EPS_COMP_PREC){
fprintf(stderr,"assemble_block_f: WARNING: at least one coupling factor != 1.0\n");
warned = TRUE;
break;
}
}
}
#endif
if((BLOCK_NBASE != 3)||(BLOCK_DIM!=2)){
fprintf(stderr,"assemble_block_f: dim: %i or nbase: %i not implemented\n",
BLOCK_DIM,BLOCK_NBASE);
exit(-1);
}
/*
nr of columns
*/
n = BLOCK_NBASE * nrb;
#ifdef BLOCK_SPHERICAL
pdim = 3;
/*
nr of rows of S
*/
m = pdim * nflt;
/* nr of rows of F */
mf = BLOCK_DIM * nflt;
/*
allocate space for S
*/
my_vecalloc(&s,n*m,"assemble_block_f: s");
#else
pdim = BLOCK_DIM;
m = BLOCK_DIM * nflt;
mf = m;
tsize = mf * n;
/* allocate space for F */
my_vecrealloc(f,tsize,"assemble_block_f: f");
#endif
for(k=0;k < nrb;k++){ /* loop through blocks */
for(j=0;j < BLOCK_NBASE;j++){ /* loop through base functions */
icol = k * BLOCK_NBASE + j; /* column counter */
offset = icol * m;
for(i=0;i < nflt;i++){ /* loop through faults */
loc_cfac = (invert_for_cfac)?(cfac[i]):(1.0);
if(block[k].fixed) /* block is fixed */
fac = 0.0;
else{ /* block has rigid body motion */
if(fault[i].block[0] == k) /* left border of fault
in block*/
fac = loc_cfac; /* normally unity */
else if(fault[i].block[1] == k) /* right border */
fac = -loc_cfac; /* normally -unity */
else
fac = 0.0; /* zero else */
}
for(l=0;l < pdim;l++){ /* loop through global directions */
irow = pdim * i + l;
if(fac != 0.0){
#ifdef BLOCK_SPHERICAL
/*
assemble S
| 0 r_z -r_y |
| -r_z 0 r_x | w = v
| r_y -r_x 0 |
NOTE THAT THIS ASSIGMENT WORKS COLUMN BY COLUMN
*/
if(j == 0){ /* first sub-col */
if(l == 0)
*(s+offset+irow)= 0.0; /* 0 */
else if(l == 1)
*(s+offset+irow)= -fault[i].xc[INT_Z] * fac; /* -r_z */
else
*(s+offset+irow)= fault[i].xc[INT_Y] * fac; /* r_y */
}else if(j == 1){ /* second sub-col */
if(l == 0)
*(s+offset+irow)= fault[i].xc[INT_Z] * fac; /* r_z */
else if(l == 1)
*(s+offset+irow)= 0.0; /* 0 */
else
*(s+offset+irow)= -fault[i].xc[INT_X]* fac; /* -r_x */
}else{ /* third sub-col */
if(l == 0)
*(s+offset+irow)= -fault[i].xc[INT_Y] * fac; /* -r_y */
else if(l == 1)
*(s+offset+irow)= fault[i].xc[INT_X] * fac; /* r_x */
else
*(s+offset+irow)= 0.0; /* 0 */
}
#else
/* rotation and translation part */
if(j == 0) /* first sub-col: omega */
if(l==0) /* those are, like in assemble_block_a
the projected coordinates of the
fault midpoint (with respect to
the genral, not local projection)
*/
*(*f+offset+irow)= -fault[i].px[INT_Y] * fac;
else
*(*f+offset+irow)= fault[i].px[INT_X] * fac;
else if(j == 1) /* second sub-col: vx_0 */
if(l==0)
*(*f+offset+irow) = fac; /* 1 or -1 */
else
*(*f+offset+irow) = 0.0;
else /* third sub-col: vy_0 */
if(l==0)
*(*f+offset+irow) = 0.0;
else
*(*f+offset+irow) = fac; /* 1 or -1 */
#endif
}else{
#ifdef BLOCK_SPHERICAL
*(s+offset+irow) = 0.0;
#else
*(*f+offset+irow) = 0.0;
#endif
}
}
}
}
}
#ifdef BLOCK_SPHERICAL
/*
assemble B which is mf rows by m columns and links the cartesian
system relative velocities to polar (lon/lat) displacements
*/
tsize = mf * m;
/* allocate space for B */
my_vecalloc(&b,tsize,"assemble_block_f: b");
/* have to initialize with zeros */
for(i=0;i < tsize;i++)
b[i] = 0.0;
/* PS: don't use calloc to allow memory debugging */
for(k=0;k < nflt;k++){ /* column flt loop */
for(j=0;j < pdim;j++){ /* column dimension loop */
icol = k * pdim + j;
offset = icol * mf;
/* assign only one row, i=k, the rest of B is zeroes */
for(l=0;l < BLOCK_DIM;l++){ /* row dimension loop */
irow = BLOCK_DIM * k + l;
if(l==0)// lon or phi direction
*(b+offset+irow) = fault[k].pbase[INT_PHI*3 +j];
else// lat or -theta direction
*(b+offset+irow) = -fault[k].pbase[INT_THETA*3+j];
}
}
}
#ifdef SUPER_DEBUG
if(!written){
print_matrix_ftrn_file(s,m,n,"s.dat",FALSE);
print_matrix_ftrn_file(b,mf,m,"b.dat",FALSE);
/* `written' gets set to true later */
}
#endif /* end debug if */
/*
obtain F from B.S
*/
/* allocate space for F */
tsize = n * mf;
my_vecrealloc(f,tsize,"assemble_block_f: f");
calc_AB_ftn(b,mf,m,s,n,*f);
#ifdef MEM_ALLOC_DEBUG
fprintf(stderr,"assemble_block_f: freeing b and s\n");
#endif /* end mem alloc if */
free(b);free(s);
#endif /* end block_spherical part if */
#ifdef SUPER_DEBUG
if(!written){
print_matrix_ftrn_file(*f,mf,n,"f.dat",FALSE);
written=TRUE;
}
#endif
}
/*
assemble the complete K(m=nrgp+nrsp+nfdamp+nxdamp,
n=nbase*nbl) matrix
-----n-----
( A - E ) E = D.G.F and K2=I.G.F (m1 = nrgp*2/3 rows)
( - K2 ) (m2 = nrsp*6 rows)
( g G.F ) if damping of slip is activated (m3 = nfdamp rows)
( aI ) if damping of x sol. is activated (m4 = nxdamp(0/n), I being the
unity matrix)
m1 = nrgp * BLOCK_DIM or nrgp * 3 (for spherical);
m2 = nrsp * 6;
m3 = nsnf or nflt rows. if nsnf, full G.F matrix, if nflt only
normal motion parts
m4 = 0 or n depending on nxdamp
on input, can be m1, m1+m2, or m1+m2+m3
*/
void assemble_block_k(COMP_PRECISION **kmat, int m,int m1,
int m2,int n, int nrgp, int nrsp, int nflt,
COMP_PRECISION *a, COMP_PRECISION *e,
COMP_PRECISION *k2mat,my_boolean rigid,
my_boolean damp_nslip,COMP_PRECISION *gf,
struct bflt *fault, int nsnf,int nslip,
int nfdamp, int nxdamp,
COMP_PRECISION xdamp)
{
int i,j,k,nm,o1,offset,offset2,m1m2,icount,kcount,tsize;
static int k_size=0;
nm = n * m;
m1m2 = m1 + m2;
if(m1m2 + nfdamp + nxdamp != m){
fprintf(stderr,"assemble_block_k: logic error: m: %i m1: %i m2: %i nfdamp: %i nxdamp: %i\n",
m,m1,m2,nfdamp,nxdamp);
exit(-1);
}
tsize = nm;
if(k_size != tsize){
// reallocate K matrix
my_vecrealloc(kmat,tsize,"assemble_block_k: kmat");
k_size = tsize;
}
//
for(i=0;i < tsize;i++) /* initialize K with zeroes */
*(*kmat+i) = 0.0;
//
// check the damping assignment numbers
//
if(damp_nslip){ /* damping part */
if(rigid){
fprintf(stderr,"assemble_block_k: logic error: both normal damping and rigid flag set\n");
exit(-1);
}
if(nsnf <= 0){
fprintf(stderr,"assemble_block_k: error with m1: %i m2: %i m: %i nsnf: %i\n",
m1,m2,m,nsnf);
exit(-1);
}
/* prepare j-loop in case of damping:
if nfdamp is nflt and nslip=2, use every other (normal)
component.
*/
switch(nslip){
case 1:
if(nfdamp != nflt){
fprintf(stderr,"assemble_block_k: error: nslip: %i nflt: %i nfdamp: %i\n",
nslip,nflt,nfdamp);
exit(-1);
}
break;
case 2:
if((nfdamp != nflt)&&(nfdamp != nsnf)){
fprintf(stderr,"assemble_block_k: error: nslip: %i nflt: %i nfdamp: %i\n",
nslip,nflt,nfdamp);
exit(-1);
}
break;
default:
fprintf(stderr,"assemble_block_k: error: nslip: %i\n",
nslip);
exit(-1);
break;
}
}else{
/*
no slip damping
*/
if(nfdamp != 0){
fprintf(stderr,"assemble_block_k: logic error: no damping set, but nfdamp %i\n",
nfdamp);
exit(-1);
}
}
#ifdef BLOCK_SPHERICAL
if(m1 != 3 * nrgp){
fprintf(stderr,"assemble_block_k: error: m1: %i nrgp: %i pdim: %i (spherical)\n",
m1,nrgp,3);
exit(-1);
}
#else
if(m1 != BLOCK_DIM * nrgp){
fprintf(stderr,"assemble_block_k: error: m1: %i nrgp: %i pdim: %i\n",
m1,nrgp,BLOCK_DIM);
exit(-1);
}
#endif
if((!rigid)&&(nflt)){
for(j=o1=0;j < n;j++,o1+=m){ /* j-loop through solution
parameters, columns of K */
//
// actually subtract interseismic deformation part
/* A - E part */
//
offset2 = o1; /* start off at first row */
/* this is the increment */
offset = j * m1; /* A and E are nrgp*pdim by n */
for(i=0;i < m1;i++){ /* i loop through GPS data rows */
*(*kmat + offset2 + i) = a[offset + i] - e[offset + i];
}
offset2 += m1; /* shift offset to end of GPS row */
/*
if there are stress observations, add the stress part, K2
*/
if(m2){
offset = j * m2; /* K2 is nrsp * 6 by n */
for(i=0;i < m2;i++){ /* -K2 = - I.G.F lower part,
loop through stress rows
*/
*(*kmat + offset2 + i) = -k2mat[offset + i];
}
/* shift to end of stress rows */
offset2 += m2;
}
if(damp_nslip){
/* damping of slip motion part */
offset = j * nsnf; /* nfdamp may be nflt or
nflt*nslip=nsnf however, G.F is
nsnf by n */
for(i=icount=kcount=0;i < nflt;i++){ /* fault loop */
for(k=0;k < nslip;k++){ /* slip direction loop */
/* only those fault slip motions with damping
have entries other than zero */
if(fault[i].use_damp[k]){
*(*kmat+ offset2 + icount) =
fault[i].sdamp[k] * gf[offset + kcount];
icount++; /* increases only with damped
directions */
}
kcount++; /* increases with all possibble
slip directions 0...nfns-1*/
}
}
if((icount != nfdamp)||(kcount != nsnf)){
fprintf(stderr,"assemble_block_k: assembly error: icount: %i m3: %i kcount: %i nsnf: %i\n",
icount,nfdamp,kcount,nsnf);
exit(-1);
}
/* shift offset to end of slip damping */
offset2 += nfdamp;
} /* end slip damping loop */
if(nxdamp){
/*
solution vector damping toward rigid solution,
only the diagonal elements are unity * damping factor,
else leave at zero
*/
*(*kmat + offset2 + j) = xdamp ;
}
} /* end j-loop through the columns of K */
/* end non-rigid computation */
}else{
/*
only rigid computation
*/
if(nfdamp || m2){
fprintf(stderr,"assemble_block_k: logic error: rigid, but m2: %i and nfdamp: %i\n",
m2,nfdamp);
exit(-1);
}
for(j=o1=0;j < n;j++,o1 += m){
/* the upper part of K = A */
offset2 = o1;
offset = j * m1;
for(i=0;i < m1;i++)
*(*kmat + offset2 + i) = a[offset + i];
offset2 += m1; /* shift to end of GPS */
/*
damping of solution vector toward rigid solution,
diagonal elements are 1 * xdamp
*/
if(nxdamp){
*(*kmat + offset2 + j) = xdamp;
}
}
fprintf(stderr,"assemble_block_k: WARNING: rigid calculation: no interseismic deformation\n");
}
}
/*
given a fault structure with initialized interaction coefficients,
and a fault slip vector slip[nslip*nflt] obtained by multiplying
G.F.x (calc_Ax_ftn(gf,nsnf,n,xsol,fslip)) calculate the stress
matrix in short (6) vector storage format at point ipnt
the effect of the stress should be taken negative like
the velocity field which is block motion - effect of locking
*/
void assemble_stress_matrix(COMP_PRECISION *sloc,int ipnt,
struct bflt *fault,
COMP_PRECISION *fslip,int nflt,
int nslip)
{
int i,j,k,offset,idir;
for(i=0;i<6;i++)
sloc[i] = 0.0; /* only upper right half */
for(j=0;j < nflt;j++) /* loop through faults */
for(i = 0;i < nslip;i++){ /* add all slip modes */
offset = j * nslip + i; /* stress contributions */
idir = block_slip_direction(i,(fault+j));
for(k=0;k < 6;k++)
sloc[k] -= (fault+j)->s[ipnt].sc[idir][k] *
fslip[offset];
}
}
/*
calculate the fit vector component jrow where jrow runs from
0 .. m, the number of velocity components (DIM * # GPS obs.)
if jrow == -1, will produce the whole solution (fit) vector
if assemble_k is false, the routine simply performs the
multiplication of row jrow of K with xsol[n] to return
the y[jrow] prediction, assuming that kmat has been assembled
already
if assemble_k is true, then K will be reassembled. for this to
work, a, d, and g will have to be precomputed and allocated,
kmat only has to be allocated
further input is nrb, the block[] array, nsnf