util_minos.f90 102 KB
Newer Older
capdevil's avatar
capdevil committed
1
2
3
4
5
6
7
!--------------------------------------------------------------------------
module minos
!--------------------------------------------------------------------------
private
public :: grav,startl,fsbm,sfbm,derms,dermf,trknt,tprop,spsm,fpsm  &
         ,sdepth,eifout,fprpmn,sprpmn,detqn,model,clean_minos_memory    &
         ,extension,write_fctpR,write_fctpL,check_eigenfreq2            &
capdevil's avatar
   
capdevil committed
8
         ,check_eigenfreq,zknt,get_fctpL,get_fctpR
capdevil's avatar
capdevil committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
logical :: first_in_startl=.true.,reallocation=.false.,allocation=.true.
integer :: nmx_backup=0,ncm_backup=0
!
contains
!###################################################################
!
! les routines suivantes sont celles de minos sans ces putains de goto
! (le prochain que je vois coder avec des goto, je le bute)
! yann Capdeville 28/10/98
!
!###################################################################
! 

!-------------------------------------------------------------------------
capdevil's avatar
   
capdevil committed
23
  subroutine model(mod,rn_,vn2_,rhobar_)
capdevil's avatar
capdevil committed
24
25
26
27
28
29
30
31
32
33
34
35
36
!lecture du model d'entre, et calcul des constante, vitesses, des 
!numeros de couches etc..
!-------------------------------------------------------------------------
    use bits
    use rindex
    use param_modele
    use global_prameter
    use earth_modele
    use yannos_flag
!      
    implicit none
!
    type(modele), intent(in) :: mod
capdevil's avatar
   
capdevil committed
37
    doubleprecision, intent(out) ::  rn_,vn2_,rhobar_
capdevil's avatar
capdevil committed
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
!
    character(len=80) :: title
    doubleprecision, dimension(:), allocatable :: vpv,vph,vsv,vsh,eta,wrk
!
    doubleprecision :: bigg=6.6723d-11,tau=1.d3,rhobar=5515.d0    
    doubleprecision :: rx,r1,r2,dr,rt,val,gn,vn2,rat
    integer :: ifdeck,ibidon,i,nreg,j,jj,knt,nn,ind,nlay,err
!
! model par couches:
!
       n        =mod%nbcou
       ncm=n
!       ncm=3000
       allocate(vsv(ncm),stat=err)
       allocate(eta(ncm),stat=err) 
       allocate(wrk(10*ncm),stat=err) 
       allocate(vsh(ncm),stat=err)
       allocate(vpv(ncm),stat=err)
       allocate(vph(ncm),stat=err)
       if (err/=0) stop ' model2 : pb a l''alloc'
!
!allocation des tableaux:
       if (allocation) call allocate_all()
!
       ifanis   =mod%ifanis
       tref     =mod%tref
       ifdeck   =mod%ifdeck
       nic      =mod%nic
       noc      =mod%noc
       r(1:n)     =mod%r(1:n)
       rho(1:n)   =mod%rho(1:n)
       vpv(1:n)   =mod%vpv(1:n)
       vsv(1:n)   =mod%vsv(1:n)
       qkappa(1:n)=mod%qkappa(1:n)
       qshear(1:n)=mod%qshear(1:n)
       vph(1:n)   =mod%vph(1:n)
       vsh(1:n)   =mod%vsh(1:n) 
       eta(1:n)   =mod%eta(1:n)
!
       ray(1:n)=r(1:n)
!
!------------------
    if(ifanis==0) then
!isotrope
       do  i=1,n
          vph(i)=vpv(i)
          vsh(i)=vsv(i)
          eta(i)=1.d0
       enddo
    endif
!
!***  normalise and spline ***
!
    rn=r(n)
    gn=pi*bigg*rhobar*rn
    vn2=gn*rn
    vn=dsqrt(vn2)
    wn=vn/rn
capdevil's avatar
   
capdevil committed
96
97
98
99
100
!
    rhobar_=rhobar
    vn2_=vn2
    rn_=rn
!    
capdevil's avatar
capdevil committed
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
    do i=1,n
       r(i)=r(i)/rn
       if(i.gt.1) then
          if (dabs(r(i)-r(i-1)).lt.1.d-7) r(i)=r(i-1)
       endif
       if(qshear(i).gt.0.d0) qshear(i)=1.d0/qshear(i)
       if(qkappa(i).gt.0.d0) qkappa(i)=1.d0/qkappa(i)
       rho(i)=rho(i)/rhobar
       acon(i)=rho(i)*vph(i)*vph(i)/vn2
       ccon(i)=rho(i)*vpv(i)*vpv(i)/vn2
       lcon(i)=rho(i)*vsv(i)*vsv(i)/vn2
       ncon(i)=rho(i)*vsh(i)*vsh(i)/vn2
       fcon(i)=eta(i)*(acon(i)-2.d0*lcon(i))
       fmu(i)=(acon(i)+ccon(i)-2.d0*fcon(i)+5.d0*ncon(i)+6.d0*lcon(i))/15.d0
       flam(i)=(4.d0*(acon(i)+fcon(i)-ncon(i))+ccon(i))/9.d0-2.d0*fmu(i)/3.d0
       rat=4.d0*fmu(i)/(3.d0*(flam(i)+2.d0*fmu(i)))
       xlam(i)=((1.d0-rat)*qkappa(i)-.5d0*rat*qshear(i))/(1.d0-1.5d0*rat)
       xa2(i)=(1.d0-rat)*qkappa(i)+rat*qshear(i)
    enddo
    call drspln(1,n,r,rho,qro,wrk)
!     
!*** compute g *****
!    
    call grav(g,rho,qro,r,n)
!
    if (cancel_gravity) g(1:n)=0.0d0
!
    call drspln(1,n,r,g,qg,wrk)
    call drspln(1,n,r,fcon,fspl,wrk)
    call drspln(1,n,r,lcon,lspl,wrk)
    if(ifanis/=0) then
!anisotrope:
       call drspln(1,n,r,acon,aspl,wrk)
       call drspln(1,n,r,ccon,cspl,wrk)
       call drspln(1,n,r,ncon,nspl,wrk)
    endif
    nsl=n
    do while (vsv(nsl)<=0.d0) 
       nsl=nsl-1 
    enddo
    if (nsl==1.and.(vsv(nsl)<=0.d0))  nsl=0
    if (nsl==nic) nsl=n !ca veut dire qu'il n'y a qu'une couche d''eau
    nicp1=nic+1
    nocp1=noc+1
    nslp1=nsl+1
    tref=0.5d0*tref/pi
!
    deallocate(vpv,vph,vsv,vsh,eta,wrk)
!
!-------------------------------------------------------------------------
  end subroutine model
!-------------------------------------------------------------------------
!------------------------------------------------------------------
subroutine check_eigenfreq(l_in,wp,flag)
!------------------------------------------------------------------
  use bits, only: l,fl,fl1,fl2,fl3,sfl3,knsw,jcom,eps
  use shanks, only:maxo,stepf
  implicit none
!
  integer, intent(in) :: l_in
  doubleprecision, intent(in) :: wp
  logical, intent(out) :: flag
!
  integer, parameter :: sens=0,inss=5
  integer :: ibid
  doubleprecision :: w1,w2,w3,w4,det1,det2,det3,det4,rap
!
  l=l_in  
!
  fl=l
  fl1=fl+1.d0
  fl2=fl+fl1
  fl3=fl*fl1 
  sfl3=dsqrt(fl3) 
capdevil's avatar
   
capdevil committed
175
  knsw=0 !kount swich off
capdevil's avatar
capdevil committed
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
  if (l>4) then
     maxo=inss
     stepf=1.d0
  else
     maxo=inss
     stepf=0.25d0
  endif
  w1=wp*(1d0-1.d0   *dsqrt(eps*0.01))
  w2=wp*(1d0+1.d0   *dsqrt(eps*0.01))
  w3=wp*(1d0-2.d0 *dsqrt(eps*0.01))
  w4=wp*(1d0+2.d0 *dsqrt(eps*0.01))
  
  call detqn(w1,ibid,det1,sens)
  call detqn(w2,ibid,det2,sens)
  call detqn(w3,ibid,det3,sens)
  call detqn(w4,ibid,det4,sens)
  rap=abs((det2-det1)*(w4-w3)/(det4-det3)/(w2-w1))
!telle que sont choisies les wi le rapport precedant doit valoir 1
!si la frequence propre en est une et 2 si c''est seuelement
! un changement de signe foireux du determinant
  if ( (abs(rap-1.d0)>0.1d0)) then
     flag=.true.
     print*,'rap false=',rap,l
  else
     flag=.false.
     print*,'rap true=',rap,l
  endif
  
!------------------------------------------------------------------
end subroutine check_eigenfreq
!------------------------------------------------------------------

!------------------------------------------------------------------
subroutine check_eigenfreq2(l_in,wp,flag,wdiff)
!------------------------------------------------------------------
  use bits, only: l,fl,fl1,fl2,fl3,sfl3,knsw,jcom,eps
  use shanks, only:maxo,stepf
  implicit none
!
  integer, intent(in) :: l_in
  doubleprecision, intent(in) :: wp
  logical, intent(out) :: flag
  doubleprecision, intent(in), optional :: wdiff
!
  integer, parameter :: sens=0,inss=5
  integer :: ibid
  doubleprecision :: w1,w2,check1,check2,bid,epsloc
!
  l=l_in  
!
  fl=l
  fl1=fl+1.d0
  fl2=fl+fl1
  fl3=fl*fl1 
  sfl3=dsqrt(fl3) 
capdevil's avatar
   
capdevil committed
231
  knsw=0 !kount swich off
capdevil's avatar
capdevil committed
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
  if (l>4) then
     maxo=inss
     stepf=1.d0
  else
     maxo=inss
     stepf=0.25d0
  endif
  if (present(wdiff)) then
     epsloc=min(max(dsqrt(eps*0.01d0),abs(wdiff)*10.d0),1.d-4)
  else
     epsloc=dsqrt(eps*0.01d0)
  endif
  w1=wp*(1d0-epsloc)
  w2=wp*(1d0+epsloc)
  call detqn(w1,ibid,bid,sens,check=check1)
  call detqn(w2,ibid,bid,sens,check=check2)
  if (dsign(1.d0,check1)*dsign(1.d0,check2) > 0.0d0 ) then
!c'est un mode de la graine ou de Stonley:
     flag=.true.
!     print*,'check false=',dsign(1.d0,check1)*dsign(1.d0,check2),l
  else
!c'est un mode normal:
     flag=.false.
!     print*,'check true =',dsign(1.d0,check1)*dsign(1.d0,check2),l
  endif
  
!------------------------------------------------------------------
end subroutine check_eigenfreq2
!------------------------------------------------------------------


!----------------------------------------------------------------------
subroutine write_fctpR(l_in,f_in,iper,iinfo,ieigen,ilost)
!----------------------------------------------------------------------
  use bits, only: l,jcom,qinv,cg,vn,fl,fl1,fl2,fl3,sfl3,knsw,nord,wray,wn,wgrav,cond_limite
  use shanks, only:maxo,stepf
  use def_gparam
  use eifx,only: ar
  use yannos_flag
  implicit none
  integer, intent(in) :: l_in,iper,iinfo,ieigen,ilost
  real(DP), intent(in) :: f_in
!
  real(DP) :: wp,gcom,qmod,bid,tcom,WMHZ,wdiff
  integer :: sens,ibid
  logical :: flag
!initialisation de ar du module eifx:
  ar(:,:)=0.0d0
!
  wp=f_in*2._DP*PI
  l=l_in
  if (l==0) then
     jcom=1
  else
     jcom=3
  endif
!
  fl=l
  fl1=fl+1.d0
  fl2=fl+fl1
  fl3=fl*fl1 
  sfl3=dsqrt(fl3)   
  knsw=0 !kount swich off
  maxo=8 ! precision rks ?
  sens=1  
  stepf=1.d0
!
  call detqn(wp,ibid,bid,sens)
  gcom=vn*cg/1000.d0
  qmod=0.d0
  if(qinv.gt.0.d0) qmod=1.d0/qinv
  TCOM=2.D0*PI/wp
  WMHZ=1000.D0/TCOM
  wdiff=(wp-wray*wn)/wp
  call check_eigenfreq2(l,wp ,flag,wdiff)       
!  WRITE(iper,200) NORD,'S',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF,flag
!on est obliger de rappeler detqn (pour faire autrement, il faudrait
!backuper ar et inorm
  if (abs(wdiff)<seuil_ray.or..not.flag) then
     call detqn(wp,ibid,bid,sens)
     WRITE(iper,200) NORD,'S',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF,flag
     call modout(wp,qmod,gcom,wdiff,flag,ieigen,iinfo)
  else
capdevil's avatar
   
capdevil committed
315
     if (cond_limite==1.and. use_remedy) then
capdevil's avatar
capdevil committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
        print*,'force_remedy',wdiff,flag
        call detqn(wp,ibid,bid,sens,force_remedy=.true.)
     endif
     wdiff=(wp-wray*wn)/wp
     if (abs(wdiff)<seuil_ray.or..not.flag) then
        WRITE(iper,200) NORD,'S',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF,flag
        call modout(wp,qmod,gcom,wdiff,flag,ieigen,iinfo)        
     else
        if (keep_bad_modes) then
!on met des zero
!           ar(:,:)=0.0d0
           call modout(wp,qmod,gcom,wdiff,flag,ieigen,iinfo)
           WRITE(iper,200) NORD,'S',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF,flag
        endif
        if (.not.keep_bad_modes) print*,'Mode de la graine ou de Stoneley ellimine: ',NORD,'S',l,wdiff
     endif
  endif
200 FORMAT(I5,A2,I5,6G16.7,l7)
!----------------------------------------------------------------------
end subroutine write_fctpR
!----------------------------------------------------------------------

capdevil's avatar
   
capdevil committed
338
339
340
341
342
343
344
345
346
347
348
349
!----------------------------------------------------------------------
subroutine get_fctpR(l_in,f_in,ibufout,bufoutinfo,bufout)
!----------------------------------------------------------------------
  use bits, only: l,jcom,qinv,cg,vn,fl,fl1,fl2,fl3,sfl3,knsw,nord,wray,wn,wgrav,cond_limite
  use shanks, only:maxo,stepf
  use def_gparam
  use eifx,only: ar
  use yannos_flag
  implicit none
  integer, intent(in) :: l_in
  real(DP), intent(in) :: f_in
  doubleprecision, dimension(:), intent(out) :: bufoutinfo
capdevil's avatar
   
capdevil committed
350
351
!  real, dimension(:), intent(out) :: bufout
  doubleprecision, dimension(:), intent(out) :: bufout
capdevil's avatar
   
capdevil committed
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
  integer, dimension(:), intent(out) :: ibufout
!
  real(DP) :: wp,gcom,qmod,bid,tcom,WMHZ,wdiff
  integer :: sens,ibid
  logical :: flag
!initialisation de ar du module eifx:
  ar(:,:)=0.0d0
!
  wp=f_in*2._DP*PI
  l=l_in
  if (l==0) then
     jcom=1
  else
     jcom=3
  endif
!
  fl=l
  fl1=fl+1.d0
  fl2=fl+fl1
  fl3=fl*fl1 
  sfl3=dsqrt(fl3)   
  knsw=0 !kount swich off
  maxo=8 ! precision rks ?
  sens=1  
  stepf=1.d0
!
  call detqn(wp,ibid,bid,sens)
  gcom=vn*cg/1000.d0
  qmod=0.d0
  if(qinv.gt.0.d0) qmod=1.d0/qinv
  TCOM=2.D0*PI/wp
  WMHZ=1000.D0/TCOM
  wdiff=(wp-wray*wn)/wp
  call check_eigenfreq2(l,wp ,flag,wdiff)       
!  WRITE(iper,200) NORD,'S',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF,flag
!on est obliger de rappeler detqn (pour faire autrement, il faudrait
!backuper ar et inorm
  if (abs(wdiff)<seuil_ray.or..not.flag) then
     call detqn(wp,ibid,bid,sens)
     call modout_buf(wp,qmod,gcom,wdiff,flag,ibufout,bufoutinfo,bufout)
  else
capdevil's avatar
   
capdevil committed
393
     if (cond_limite==1.and. use_remedy) then
capdevil's avatar
   
capdevil committed
394
395
396
397
398
399
400
401
402
403
404
        print*,'force_remedy',wdiff,flag
        call detqn(wp,ibid,bid,sens,force_remedy=.true.)
     endif
     wdiff=(wp-wray*wn)/wp
     call modout_buf(wp,qmod,gcom,wdiff,flag,ibufout,bufoutinfo,bufout)
  endif
200 FORMAT(I5,A2,I5,6G16.7,l7)
!----------------------------------------------------------------------
end subroutine get_fctpR
!----------------------------------------------------------------------

capdevil's avatar
capdevil committed
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
!----------------------------------------------------------------------
subroutine write_fctpL(l_in,f_in,iper,iinfo,ieigen)
!----------------------------------------------------------------------
  use bits, only: l,jcom,qinv,cg,vn,fl,fl1,fl2,fl3,sfl3,knsw,nord,wray,wn
  use shanks, only:maxo,stepf
  use def_gparam
  use yannos_flag
  implicit none
  integer, intent(in) :: l_in,iper,iinfo,ieigen
  real(DP), intent(in) :: f_in
!
  real(DP) :: wp,gcom,qmod,bid,tcom,WMHZ,wdiff
  integer :: sens,ibid
!
  wp=f_in*2._DP*PI
  l=l_in
  if (l/=0) then
     jcom=2
!
     fl=l
     fl1=fl+1.d0
     fl2=fl+fl1
     fl3=fl*fl1 
     sfl3=dsqrt(fl3)   
     knsw=0 !kount swich off
     maxo=8 ! precision rks ?
     sens=1  
     stepf=1.0d0
     call detqn(wp,ibid,bid,sens)
     gcom=vn*cg/1000.d0
     qmod=0.d0
     if(qinv.gt.0.d0) qmod=1.d0/qinv
     TCOM=2.D0*PI/wp
     WMHZ=1000.D0/TCOM
     wdiff=(wp-wray*wn)/wp
     if (abs(wdiff)<seuil_ray) then
        WRITE(iper,200) NORD,'T',L,wp,WMHZ,TCOM,GCOM,QMOD,WDIFF
        call modout(wp,qmod,gcom,wdiff,.false.,ieigen,iinfo)
     else
        print*,'La precision  mauvaise, je le vire',wdiff
     endif
  endif
200 FORMAT(I5,A2,I5,6G16.7)
!----------------------------------------------------------------------
end subroutine write_fctpL
!----------------------------------------------------------------------
capdevil's avatar
   
capdevil committed
451
452
453
454
455
456
457
458
459
460
461
462

!----------------------------------------------------------------------
subroutine get_fctpL(l_in,f_in,ibufout,bufoutinfo,bufout)
!----------------------------------------------------------------------
  use bits, only: l,jcom,qinv,cg,vn,fl,fl1,fl2,fl3,sfl3,knsw,nord,wray,wn
  use shanks, only:maxo,stepf
  use def_gparam
  use yannos_flag
  implicit none
  integer, intent(in) :: l_in
  real(DP), intent(in) :: f_in
  doubleprecision, dimension(:), intent(out) :: bufoutinfo
capdevil's avatar
   
capdevil committed
463
464
!  real, dimension(:), intent(out) :: bufout
  doubleprecision, dimension(:), intent(out) :: bufout
capdevil's avatar
   
capdevil committed
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
  integer, dimension(:), intent(out) :: ibufout
!
  real(DP) :: wp,gcom,qmod,bid,tcom,WMHZ,wdiff
  integer :: sens,ibid
!
  wp=f_in*2._DP*PI
  l=l_in
  if (l/=0) then
     jcom=2
!
     fl=l
     fl1=fl+1.d0
     fl2=fl+fl1
     fl3=fl*fl1 
     sfl3=dsqrt(fl3)   
     knsw=0 !kount swich off
     maxo=8 ! precision rks ?
     sens=1  
     stepf=1.0d0
     call detqn(wp,ibid,bid,sens)
     gcom=vn*cg/1000.d0
     qmod=0.d0
     if(qinv.gt.0.d0) qmod=1.d0/qinv
     TCOM=2.D0*PI/wp
     WMHZ=1000.D0/TCOM
     wdiff=(wp-wray*wn)/wp
     call modout_buf(wp,qmod,gcom,wdiff,.false.,ibufout,bufoutinfo,bufout)
  endif
200 FORMAT(I5,A2,I5,6G16.7)
!----------------------------------------------------------------------
end subroutine get_fctpL
!----------------------------------------------------------------------
capdevil's avatar
capdevil committed
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
!----------------------------------------------------------------------
  subroutine detqn(wdim,knt,det,ifeif,Ann,continu,check,force_remedy)
!input:
! wdim: frequence en Hz a laquelle on integer les mineurs
! ifeif: =0 on integer de bas en haut seulement pour recuperer
!           le determinant (et le mode kount)
!        /=0 on integer aussi de haut en bas pour sortir la solution
! output:
! knt= nombre de branches entre 0 et wdim (clculer si demande, cf common)
! det= determinant de la traction la surface normalise
!
! input par common:
! knsw =1 On compte le nombre de branches
!      /=0 on ne compte pas
!
!**** supevises the integration of the equations,it returns the value
!**** of the secular determinant as det and the count of zero crossings.
!----------------------------------------------------------------------
    use bits
    use eifx, a=>ar
    use rindex
    use param_modele
    use detqn_buffer
    use yannos_flag
capdevil's avatar
   
capdevil committed
521
    use correction
capdevil's avatar
capdevil committed
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
    implicit none
!
    doubleprecision, intent(in) :: wdim
    integer, intent(in)  :: ifeif
    integer, intent(out) :: knt
    doubleprecision, intent(out) :: det
    doubleprecision, dimension(5),optional, intent(out) :: Ann
    integer, optional :: continu
    logical, optional ::    force_remedy
    doubleprecision, optional, intent(out) :: check
!
    doubleprecision, dimension(14) :: ass
    doubleprecision, dimension( 4) :: zi
    integer :: nvefm,nvesm,is,irem,nbakf,nbaks,nto,n2,nb    &
              ,ls1,i,ls,iexp,jexp,j,im
    doubleprecision :: r10,dnorm,asi1,asi2,ff,cc,rnrm,q,m3  &
                      ,detp,y4a2,ll,scale,rhobar=5515.d0,y2a2    
! on calcul les pas d'integration (plus la freq est elevee, plus le precision l''est)
!    call steps(eps/(1.d4*(wdim/2.d0/pi)**2))
    call steps(epsint/(1.d4*(wdim/2.d0/pi)))
!si on passe la premiere fois dans detqn, on alloue vf:
    call allocate_vf()
    vf(:)=0.0d0
    a(:,:)=0.0d0
    ass(:)=0.0d0
!
    iback=0 ! on va de bas en haut pour l'integration
    w=wdim/wn !on normalise la frequence d'entree
    if (abs(wdim)<1.d-30) then
       print*,'wdim=',wdim,w,wn
       print*,'l=',l
       stop 'detqn: la frequence d''entree &
                                  &ne doit pas etre nulle !!!'
    endif
    wsq=w*w
    iexp=0
    kount=0
    kg=0 !kg=0 on ne tient pas compte de la perturbation gravite    
    fct=0.d0
    if(tref.gt.0.d0.and.use_tref) &
             fct=2.d0*dlog(tref*wdim)/pi !utilisation de periode
                                                   !de ref
!    print*,'dans detqn: l=',l,' wdim=',w,kg
!
!
!--------------------------------------------------------------
    select case(jcom)
!--------------------------------------------------------------
    case(3)
! modes spheroiaux
capdevil's avatar
   
capdevil committed
572
       if (layer_corr) call init_lwS(l,w)       
capdevil's avatar
capdevil committed
573
574
575
576
577
578
579
580
581
582
!--------------------------------------------------------------
       if(wdim.le.wgrav) kg=1 !on tient compte de la gravite
       nvefm=2+kg*3 !nombre de mineurs dans le fluide (2, sans gravite
                    ! ou 5 avec gravite)
       nvesm=5+kg*9 !nombre de mineurs dans le solide (5, sans gravite
                    ! ou 14 avec gravite)
!on determine a quelle profondeur commencer l'integration
! sdepth = start depth
       if (use_startlevel) then
          call sdepth(wdim,ls)
capdevil's avatar
   
capdevil committed
583
!print*,ls,nocp1
capdevil's avatar
capdevil committed
584
585
          if (ls>nocp1) then
!             print*,'ls >nocp1!'
capdevil's avatar
   
capdevil committed
586
!             ls=(nocp1+nicp1)/2
capdevil's avatar
capdevil committed
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
          endif
       else
          ls=2
       endif
!
       if (ls <= 2) then
          r10=4.5d-4*(fl+.5d0)/wdim
          if(r10 < r(2)) then
             r(1)=r10
             if (.not.cancel_gravity) then
                g(1)=rho(1)*r(1)*1.333333333333333d0
             else
                g(1)=0.
             endif
             ls=1
          endif
       endif
capdevil's avatar
   
capdevil committed
604
605


capdevil's avatar
capdevil committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
! nocp1 : numero de la couche outer core +1
! nicp1 : numero de la couche inner core +1
!++++++++++++++++++++++++++++++++++++++++++
       if(ls <= nocp1 .and.noc/=0) then
!++++++++++++++++++++++++++++++++++++++++++
!------------------------------------------
          if(ls <= nicp1 .and.nic/=0) then
!------------------------------------------
!on est en dessous la graine

!calcule des solution de depart avec des fonctions de bessel spheriques
! dans une region solide:
             call spsm(ls,nvesm,ass)
!propage les nineurs jusqu'au le noyau liquide (nic)
capdevil's avatar
   
capdevil committed
620
             call sprpmn(ls,nic,ass,vf,nvesm,iexp,.false.)
capdevil's avatar
capdevil committed
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
             r(1)=0.d0
             g(1)=0.d0
!convertie les mineurs d'une region solide vers une region liquides
             call sfbm(ass,kg,iback)
!------------------------------------------
          endif
!------------------------------------------
          is=max0(ls,nicp1)
!si le point de depart (sortie de sdepth)  est dans le liquide
!on calcule  les determinant de depart avec des fct de bessel
!dans une region liquide
          if(is.eq.ls) call fpsm(ls,nvefm,ass)
!propage les nineurs jusqu'au manteau (noc)
          call fprpmn(is,noc,ass,vf,nvefm,iexp)
          if (noc==n) then
!on est deja a la surface
             if (present(continu).and.present(Ann)) then
                Ann(:)=0.0d0
                Ann(1)=a(2,n)/a(1,n)*rhobar*vn**2/rn
             endif
             select case (cond_limite)
             case(1)
!surface libre
                det=a(2,nsl)/dsqrt(a(1,nsl)*a(1,nsl)+a(2,nsl)*a(2,nsl))
!bord rigide
             case(2)
                det=a(1,nsl)/dsqrt(a(1,nsl)*a(1,nsl)+a(2,nsl)*a(2,nsl))
             end select
             if (present(check)) then
                check=a(1,nsl)*a(2,nsl)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
             endif
          else
!convertie les mineurs d'une region fluide vers une region solide
             call fsbm(ass,kg,iback)
          endif
capdevil's avatar
   
capdevil committed
656
657
658



capdevil's avatar
capdevil committed
659
660
661
662
663
664
665
666
667
668
669
670
671
!++++++++++++++++++++++++++++++++++++++++++
       endif
!++++++++++++++++++++++++++++++++++++++++++
!++++++++++++++++++++++++++++++++++++++++++
       if (noc/=n) then
!++++++++++++++++++++++++++++++++++++++++++
          is=max0(ls,nocp1)
!si le point de depart (sortie de sdepth)  est dans manteau
!on calcule  les determinants de depart avec des fct de bessel
!dans une region solide:
          if(is.eq.ls) call spsm(ls,nvesm,ass)
!propage les nineurs jusqu'a  la surface solide (base de l'ocean
! si il existe) :
capdevil's avatar
   
capdevil committed
672
          call sprpmn(is,nsl,ass,vf,nvesm,iexp,layer_corr)
capdevil's avatar
capdevil committed
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
! si nsl=n ca veut dire qu'il n'y a pas d'ocean:
          if(nsl==n) then
             dnorm=a(1,nsl)*a(1,nsl)
             do  i=2,nvesm
                dnorm=dnorm+a(i,nsl)*a(i,nsl)
             enddo
!
             select case (cond_limite)
             case(1)
!surface libre
                det=a(5,nsl)/dsqrt(dnorm)
!bord rigide
             case(2)
                det=a(1,nsl)/dsqrt(dnorm) 
             end select
             if (present(check)) then
                check=a(1,nsl)*a(5,nsl)/dsqrt(dnorm) 
             endif
!
!sortie pour l'operateur de couplage
             if (present(Ann)) then             
                if (present(continu)) then
!A00:
                   Ann(1)= a(4,nsl)
!A10:
                   Ann(2)=-a(2,nsl)/sqrt(2.d0)
!A01:
                   Ann(3)=-a(2,nsl)/sqrt(2.d0)
!A11:
                   Ann(4)=-a(3,nsl)/2.d0
!
                   Ann(:)=-Ann/a(1,nsl)*rhobar*vn**2/rn
                else
!A00:
                   Ann(1)= a(4,nsl)
!A10:
                   Ann(2)=-a(2,nsl)/sqrt(2.d0)
!A01:
                   Ann(3)=-a(2,nsl)/sqrt(2.d0)
!A11:
                   Ann(4)=-a(3,nsl)/2.d0
!
                   m3=a(3,nsl)     
                endif
             endif
          else
! si nsl/=n ca veut dire qu'il y a un ocean:
! dans se cas on convertie les mineurs de solide vers liquide
             call sfbm(ass,kg,iback)
! on propage dans l'ocean:
             call fprpmn(nslp1,n,ass,vf,nvefm,iexp)
!
             select case (cond_limite)
             case(1)
!surface libre
                if(kg.eq.0) det=a(2,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
                if(kg.ne.0) det=a(5,n)/dsqrt(a(1,n)**2+a(2,n)**2+a(3,n)**2+ &
                     a(4,n)**2+a(5,n)**2)
!bord rigide
             case(2)
                det=a(1,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
             end select
             if (present(check)) then
                if (kg.eq.0) then
                   check=a(1,n)*a(2,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
                else
                   check=a(1,n)*a(5,n)/dsqrt(a(1,n)*a(1,n)+a(5,n)*a(5,n))
                endif
             endif
          endif
!+++++++++++++++++++++++++++++++++++++++++++++
       endif
!+++++++++++++++++++++++++++++++++++++++++++++
! si la couche de depart est au dessus de la CMB:
       if(ls.gt.noc.and.noc/=0) det=-det
! si mode count:
       if(knsw==1) then
             if(ls.gt.noc.and.noc/=0) kount=kount-2
             if (nic==0.and.noc==0) kount=kount-1
             irem=mod(kount,2)
             if (noc/=0) then
                if(irem.eq.0.and.det.lt.0.d0) kount=kount+1
                if(irem.ne.0.and.det.gt.0.d0) kount=kount+1
             endif
capdevil's avatar
   
capdevil committed
757
             if (search_tsunami_mode) kount=kount+1
capdevil's avatar
capdevil committed
758
759
760
761
762
763
764
             knt=kount
       endif
!-----------------------------------
       if(ifeif/=0) then
! on a demander le calule des fonctions propres (et non uniquement
! du determinant)
!-----------------------------------
capdevil's avatar
   
capdevil committed
765
!          if (layer_corr) call correct_minors(a,ls,n)
capdevil's avatar
capdevil committed
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
          iback=1 ! on va de haut en bas pour l'integration
          jexp=0
          nbakf=1+kg*3
          nbaks=4+kg*10
          do  i=1,nbaks
             ass(i)=0.d0
          enddo
!+++++++++++++++++++++++++++++++++++++++++++++++++
          if(n==nsl.and.noc/=n) then 
! pas d'ocean, on fixe les determinant a la surface
!+++++++++++++++++++++++++++++++++++++++++++++++++
             if(kg/=0)  then
!gravite
                asi1=a(3,n)*a(3,n)+a(12,n)*a(12,n)
                asi2=a(4,n)*a(4,n)+a(11,n)*a(11,n)
                if(asi2.le.asi1) ass(1)=dsign(1.d0,a(3,n))
                if(asi2.gt.asi1) ass(2)=dsign(1.d0,a(2,n))
             else
!pas de gravite
                asi1=a(3,n)*a(3,n)
                asi2=a(4,n)*a(4,n)
                select case (cond_limite)
                case(1)
!pour surface libre
                   if(asi2.le.asi1) ass(1)=dsign(1.d0,a(3,n))
                   if(asi2.gt.asi1) ass(2)=dsign(1.d0,a(2,n))
                case(2)
!pour bord rigide:
                   if(asi2.le.asi1) ass(3)=dsign(1.d0,a(3,n))
                   if(asi2.gt.asi1) ass(4)=dsign(1.d0,a(2,n))
                end select
             endif
capdevil's avatar
   
capdevil committed
798
             if (layer_corr) call comp_y2y4(ass(1),ass(2),ass(3),ass(4))
capdevil's avatar
capdevil committed
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
!+++++++++++++++++++++++++++++++++++++++++++++++++
          else
!depart dans un liquide
!+++++++++++++++++++++++++++++++++++++++++++++++++
! ocean:
             if(kg/=0)  then
!gravite:
                asi1=a(3,n)*a(3,n)
                asi2=a(4,n)*a(4,n)
                if(asi2.le.asi1) ass(1)=dsign(1.d0,a(3,n))
                if(asi2.gt.asi1) ass(2)=dsign(1.d0,a(2,n))
                if (n/=nsl) then
!propage dans l'ocean
                   call fprpmn(n,nslp1,ass,vf,nbakf,jexp)
!conversion liquide solide:
                   call fsbm(ass,kg,iback)
                endif
             else
! pas de gravite
                select case(cond_limite)
                !pour bord libre:
                case(1)
                   ass(1)=dsign(1.d0,a(1,n))
                case(2)
                   !pour bord rigide:
!                   ass(2)=dsign(1.d0,a(2,n))
                   ass(1)=dsign(1.d0,a(2,n))
                end select
                if (n/=nsl) then
!propage dans l'ocean
                   call fprpmn(n,nslp1,ass,vf,nbakf,jexp)
!conversion liquide solide:
                   call fsbm(ass,kg,iback)
                endif
             endif
!+++++++++++++++++++++++++++++++++++++++++++++++++
          endif
!+++++++++++++++++++++++++++++++++++++++++++++++++
! nto est le numero de couche du manteau ou un numero 
! plus haut si ca ne vaut pas la peine d'aller plus bas (sortie de sdepth)
!+++++++++++++++++++++++++++++++++++++++++++++++++
          nto=max0(ls,nocp1)
          if (noc/=n) then
!on part dans le solide
!+++++++++++++++++++++++++++++++++++++++++++++++++
! on porpage dans le solide
capdevil's avatar
   
capdevil committed
845
             call sprpmn(nsl,nto,ass,vf,nbaks,jexp,.false.)
capdevil's avatar
capdevil committed
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
             if (noc/=0.and.nto/=ls) then
! si on doit aller dans le liquide: conversion:
                call sfbm(ass,kg,iback)
             endif
!+++++++++++++++++++++++++++++++++++++++++++++++++
          endif
          if (noc/=0.and.nto/=ls) then
             nto=max0(ls,nicp1)
!on est dans le liquide
!+++++++++++++++++++++++++++++++++++++++++++++++++             
! on propage de liquide
             call fprpmn(noc,nto,ass,vf,nbakf,jexp)
             if(nto/=ls.and.nic/=0) then
! si on doit aller dans la graine on convertie liquide solide
                call fsbm(ass,kg,iback)
                nto=max0(ls,2)
! on propage dans le solide jusqu'au centre ou ls:
capdevil's avatar
   
capdevil committed
863
                call sprpmn(nic,nto,ass,vf,nbaks,jexp,.false.)
capdevil's avatar
capdevil committed
864
865
             endif
          endif
capdevil's avatar
   
capdevil committed
866
          if (use_remedy.and.(abs(det) > seuil_remedy.or. present(force_remedy) )) &
capdevil's avatar
capdevil committed
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
               call remedy(ls)
! normalisation etc .... du mode :
          call eifout(ls)
!sortie pour l'operateur de couplage: divistion par la
! derivee du determinant
          if (present(Ann)) then
             if (noc/=n) then
                scale=1.0d0/dsqrt(rhobar*rn**3)
                ll=lcon(nsl)*(1.d0+qshear(nsl)*fct)
                y4a2=(w*ll*(a(4,nsl)-a(3,nsl)+a(1,nsl))    &
                              *(scale*rhobar*vn**2/rn))**2
                detp=-2.d0*w*wn*m3/y4a2/rn**2
                Ann(:)=Ann(:)/detp
             else
!interface liquide:
                scale=1.0d0/sqrt(rhobar*rn**3)
                cc=ccon(nsl)*(1.d0+xlam(nsl)*fct)
                ff=fcon(nsl)*(1.d0+xa2(nsl)*fct)
                y2a2=(w*(cc*a(2,nsl)+ff*(2.d0*a(1,nsl)-sfl3*a(3,nsl))) &
                                   *(scale*rhobar*vn**2/rn))**2 
!A00:
                Ann(1)= y2a2/2.d0/w/wn*rn**2
                Ann(2:4)=0.d0
             endif
          endif
       endif
!---------------------------
    case (1)
!*** radial modes ***
!---------------------------
       if(wdim.le.wgrav) kg=1 !on tient compte de la gravite
       if (l/=0) stop 'Les modes radiaux n''existe que pour l=0!'
       ls=2
       call rps(ls,ass)
       call rprop(ls,n,ass)
       select case(cond_limite)
       case(1)
!bord libre:
          det=a(2,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
       case(2)
!bord rigide:
          det=a(1,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
       end select
       if (present(check)) then
          check=a(1,n)*a(2,n)/dsqrt(a(1,n)*a(1,n)+a(2,n)*a(2,n))
       endif
       knt=kount-1
capdevil's avatar
   
capdevil committed
914

capdevil's avatar
capdevil committed
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
!
       if (present(continu).and.present(Ann)) then
          Ann(:)=0.0d0
          Ann(1)=a(2,n)/a(1,n)*rhobar*vn**2/rn
       endif
!------------
       if(ifeif.eq.0) return
!------------
       a(1,1)=0.d0
       a(2,1)=0.d0
       do  i=ls,n
          ff=fcon(i)*(1.d0+xlam(i)*fct)
          cc=ccon(i)*(1.d0+xa2(i)*fct)
          a(2,i)=(a(2,i)-2.d0*ff*a(1,i)/r(i))/cc
       enddo
       zi(1)=0.d0
       zi(2)=0.d0
       zi(3)=0.d0
       do i=ls,n
          im=i-1
          if (im/=0) then
             if(r(i).ne.r(im)) call gauslv(r(im),r(i),im,zi,3)
          endif
       enddo
       rnrm=1.d0/(w*dsqrt(zi(1)))
       cg=0.d0
       qinv=zi(2)/(wsq*zi(1))
       wray=dsqrt(zi(3)/zi(1))
       do  i=2,n
          do  j=1,2
             a(j,i)=a(j,i)*rnrm
          enddo
       enddo
!sortie pour l'operateur de couplage
       if (present(Ann)) then
          scale=1.0d0/sqrt(rhobar*rn**3)
          cc=ccon(n)*(1.d0+xlam(n)*fct)
          ff=fcon(n)*(1.d0+xa2(n)*fct)
          y2a2=(w*(cc*a(2,n)+2.d0*ff*a(1,n))*(scale*rhobar*vn**2/rn))**2
!A00:
          Ann(1)= y2a2/2.d0/w/wn*rn**2
          Ann(2:4)=0.d0
       endif
!---------------------------
    case (2,4)
!*** toroidal modes *** pour l/=0 et quand il n'y a pas que de l'eau
!---------------------------
capdevil's avatar
   
capdevil committed
962
       if (layer_corr) call init_lwT(l,w)
capdevil's avatar
capdevil committed
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
       if (l/=0.and.noc/=n) then
          nb=nocp1
          n2=nsl
          ass(1)=1.d0
          ass(2)=0.d0
          if(jcom==4) then
             nb=2
             a(1,1)=0.d0
             a(2,1)=0.d0
             n2=nic
          endif
!
          q=0.d0
          ls=nb
          call startl(ls,n2,fmu,ls,q)
          ls=max(ls,nocp1)
          ls=max(ls,2)
!
          if(ls.ne.nocp1) call tps(ls,ass)
          call tprop(ls,n2,ass)
!
          select case(cond_limite)
          case(1)
!bord libre:
             det=a(2,n2)/dsqrt(a(1,n2)*a(1,n2)+a(2,n2)*a(2,n2))
!test
!             det=(a(2,n2)+1.771918364456304d-002*(fl3-2.)*a(1,n2)/200.)/dsqrt(a(1,n2)*a(1,n2)+a(2,n2)*a(2,n2))
          case(2)
!bord rigide:
             det=a(1,n2)/dsqrt(a(1,n2)*a(1,n2)+a(2,n2)*a(2,n2))
          end select
          if (present(check)) then
             check=a(1,n2)*a(2,n2)/dsqrt(a(1,n2)*a(1,n2)+a(2,n2)*a(2,n2))
          endif
!
          if (present(continu).and.present(Ann)) then
             Ann(:)=0.0d0
             Ann(1)=a(2,n)/a(1,n)/2.0d0*rhobar*vn**2/rn