initial upload

This commit is contained in:
2025-12-17 10:53:43 +08:00
commit f3f1778f77
308 changed files with 129940 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# Inversion test
This is a test setup for inversion calculation.
1. Run all cells of `make_test_model.ipynb` for creating
- source, receiver file
- true model
- initial model
2. then run TOMOATT forward with `input_params_pre.yml` for calculating the true arrival times at the stations
-> this will output src_rec_result.dat file which includes the arrival time at each station
3. run TOMOATT in inversion mode with `input_params.yml`.

View File

@@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# check the change of objective values\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"fpath='objective_function.txt'\n",
"\n",
"objs=[]\n",
"\n",
"with open(fpath) as f:\n",
" lines = f.readlines()\n",
"\n",
" for l in lines:\n",
" if(l.startswith('i_inv')):\n",
" continue\n",
" objs.append(float(l.strip().split(',')[1]))\n",
"\n",
"\n",
"plt.plot(objs)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]"
},
"vscode": {
"interpreter": {
"hash": "cdd74c396a461f81d13d16403c726c38089d3c42a4b6e59262cdb9511d16556b"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
mpif90 ega5_sphe_3d_inversion_Tong_3Dinv.f90 -o a5.out
mpirun -n 4 a5.out

View File

@@ -0,0 +1,2 @@
1
12.902894 37.503373 16.794572

View File

@@ -0,0 +1,2 @@
1
0.0 36.472809 29.812050

View File

@@ -0,0 +1,737 @@
include "eikon_solver_mpi.f90"
! example: 3-D 3rd order anisotropic eikonal equation in cartesian coordinate (Point source)
! parameter setting:
! domain: R * Theta * Phi = [6275, 6375] * [49^\circ, 51^\circ] * [129^\circ, 131^\circ]
! analytic solution: T = |x-x_s|/c0
! isotropic eik equ: Tx^2 + Ty^2 + Tz^2 = s^2,
! boundary condition: T(x0,y0,z0) = 0 (point source)
! test function: 'FSM_WENO3_PS_sphe_3d' in "eikon_solver.f90"
!
program eikonal_2d
use mpi
! ######################### 参数声明 parameters statement #########################
implicit none
! mesh grid
integer,parameter :: nr=55,nt=55,np=55
double precision,parameter :: pi=3.14159265358979323846264338327950288
double precision,parameter :: rr1=6070,rr2=6400
double precision,parameter :: tt1=(30.0-1.5)/180*pi,tt2=(50.0+1.5)/180*pi
double precision,parameter :: pp1=(15.0-1.5)/180*pi,pp2=(40.0+1.5)/180*pi
double precision :: rr(nr),tt(nt),pp(np),dr,dt,dp
! source (STATIONS) & receiver (EARTHQUAKES)
integer :: nsou
double precision,allocatable :: rsou(:),tsou(:),psou(:) ! source (station)
integer :: nrec
double precision,allocatable :: rrec(:),trec(:),prec(:) ! receiver (earthquake)
! model parameter
double precision,parameter :: gamma = 0.0
double precision :: xi(nr,nt,np),eta(nr,nt,np),zeta(nr,nt,np) ! syn model (ani)
double precision :: xiT(nr,nt,np),etaT(nr,nt,np),zetaT(nr,nt,np) ! true model (ani)
double precision :: sigma1,sigma2,psi
double precision,parameter :: slow_p=0.04,ani_p=0.03
! Eikonal solver
double precision :: aT(nr,nt,np),bT(nr,nt,np),cT(nr,nt,np),fT(nr,nt,np),funT(nr,nt,np) ! true model 真实模型
double precision :: a(nr,nt,np),b(nr,nt,np),c(nr,nt,np),f(nr,nt,np),fun(nr,nt,np) ! syn model 猜测模型
double precision :: uT(nr,nt,np),TableT(nr,nt,np) !true timetable 真实走时场
double precision :: u(nr,nt,np),Table(nr,nt,np) !syn timetable 猜测走时场
!double precision :: T0para(8),tau(nr,nt,np) ! multiplicative factor
! adjoint source
double precision,allocatable :: sourceADJ(:),TtimeT(:,:),Ttime(:,:) ! adjoint source 伴随源,真实走时,合成走时
double precision :: TableADJ(nr,nt,np) ! adjoint field 伴随场
double precision :: Ks(nr,nt,np),Kxi(nr,nt,np),Keta(nr,nt,np) ! event kernel
integer,parameter :: nk = 3 ! number of kernels
double precision :: all_Ks(nr,nt,np),all_Kxi(nr,nt,np),all_Keta(nr,nt,np) ! sensitivity kernel
double precision :: all_Kernel(nr,nt,np,nk) ! kernels for all parameters together
!double precision,parameter :: radius = 40 ! kernel mask radius
! inversion grid
integer,parameter :: ninvr=11,ninvt=10,ninvp=10 ! inversion grid size (coarse)
double precision,parameter :: invr1=6070.0,invr2=6400.0
double precision,parameter :: invt1=30.0/180*pi,invt2=50.0/180*pi
double precision,parameter :: invp1=15.0/180*pi,invp2=40.0/180*pi
integer,parameter :: ngrid=5 ! number of nultigrid (multigrid technique)
double precision :: invr(ngrid,ninvr),invt(ngrid,ninvt),invp(ngrid,ninvp),dinvr,dinvt,dinvp
double precision :: update_value(nr,nt,np,nk) ! parameter update value 更新参数的变化量
! model update (optimization)
double precision :: stepsize ! stepsize
integer,parameter :: MaxIter=1 ! max iteration step
double precision :: old_obj, obj, sou_obj ! 优化目标函数 objective function
double precision,allocatable :: weight_sou(:),weight_rec(:) !weight function
double precision :: dis_ij, dis_zero !weight function
logical :: weight_sou_bool=.false. , weight_rec_bool= .false.
! temp var
double precision :: depth,lat,lon
! output file
character(Len=80) :: filename,form,form2
! loop index
integer :: iir,iit,iip,idi,idj,idk,i,j,k,iter,igrid
! computing time
double precision :: time_begin,time_end,alpha
! mpi parameter
integer :: ierr,myid,nproc
! ############################# main work #############################
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world,myid,ierr)
call mpi_comm_size(mpi_comm_world,nproc,ierr)
call CPU_TIME(time_begin) ! program start
! ################### read sources and receivers 读取地震台站数据 ###################
open(10,file='ega5/input/STATIONS.txt')
read(10,*) nsou
allocate(rsou(nsou),tsou(nsou),psou(nsou),weight_sou(nsou))
do i=1,nsou
read(10,*) depth,lat,lon
rsou(i) = 6371.0-depth
tsou(i) = lat/180.0*pi
psou(i) = lon/180.0*pi
end do
close(10)
open(10,file='ega5/input/EARTHQUAKES.txt')
read(10,*) nrec
allocate(rrec(nrec),trec(nrec),prec(nrec),weight_rec(nrec))
do i=1,nrec
read(10,*) depth,lat,lon
rrec(i) = 6371.0-depth
trec(i) = lat/180.0*pi
prec(i) = lon/180.0*pi
end do
close(10)
! distance based weight -rec 台站权重
if (weight_rec_bool) then
dis_zero = 0
do i=1,nrec-1
do j=i,nrec
call Epicenter_Distance(trec(i),prec(i),trec(j),prec(j),dis_ij)
dis_zero = dis_zero + dis_ij
end do
end do
dis_zero = dis_zero/nrec/(nrec-1)*2
dis_zero = dis_zero/2
do i=1,nrec
weight_rec(i)=0.0
do j=1,nrec
call Epicenter_Distance(trec(i),prec(i),trec(j),prec(j),dis_ij)
weight_rec(i) = weight_rec(i) + exp(-(dis_ij/dis_zero)**2)
end do
weight_rec(i) = 1.0/weight_rec(i)
end do
print *, dis_zero
end if
if (weight_rec_bool) then
! distance based weight - soruce 地震权重
dis_zero = 0
do i=1,nsou-1
do j=i,nsou
call Epicenter_Distance(tsou(i),psou(i),tsou(j),psou(j),dis_ij)
dis_zero = dis_zero + dis_ij
end do
end do
dis_zero = dis_zero/nsou/(nsou-1)*2
dis_zero = dis_zero/4
do i=1,nsou
weight_sou(i)=0.0
do j=1,nsou
call Epicenter_Distance(tsou(i),psou(i),tsou(j),psou(j),dis_ij)
weight_sou(i) = weight_sou(i) + exp(-(dis_ij/dis_zero)**2)
end do
weight_sou(i) = 1.0/weight_sou(i)
end do
print *, dis_zero
end if
! ---- 记录到 matlab 数据中 matlab record ----
if (myid .eq. 0) then
open(100,file='ega5/output/STATIONS')
open(101,file='ega5/output/EARTHQUAKES')
do i=1,nsou
write(100,'(f13.7,f13.7,f13.7,f13.7)') rsou(i),tsou(i),psou(i),weight_sou(i)
end do
do i=1,nrec
write(101,'(f13.7,f13.7,f13.7,f13.7)') rrec(i),trec(i),prec(i),weight_rec(i)
end do
close(100);close(101)
end if
! ######################### 生成网格 build mesh grid #####################################
! -------- forward modeling grid ----------
dr=(rr2-rr1)/(nr-1); dt=(tt2-tt1)/(nt-1); dp=(pp2-pp1)/(np-1)
do iir=1,nr
rr(iir)=rr1+(iir-1)*dr
end do
do iit=1,nt
tt(iit)=tt1+(iit-1)*dt
end do
do iip=1,np
pp(iip)=pp1+(iip-1)*dp
end do
! -------- inversion multigrid ----------
dinvr=(invr2-invr1)/(ninvr-1); dinvt=(invt2-invt1)/(ninvt-1); dinvp=(invp2-invp1)/(ninvp-1)
do igrid=1,ngrid
do iir=1,ninvr
invr(igrid,iir)=invr1+(iir-1)*dinvr-(igrid-1)*dinvr/ngrid
end do
do iit=1,ninvt
invt(igrid,iit)=invt1+(iit-1)*dinvt-(igrid-1)*dinvt/ngrid
end do
do iip=1,ninvp
invp(igrid,iip)=invp1+(iip-1)*dinvp-(igrid-1)*dinvp/ngrid
end do
end do
! ######################### 构建背景模型 build the synthetic and true model #####################################
do iir=1,nr
do iit=1,nt
do iip=1,np
! synthetic (initial) model
eta(iir,iit,iip)=0.0;
xi(iir,iit,iip)=0.0;
zeta(iir,iit,iip)=gamma*sqrt(eta(iir,iit,iip)**2+xi(iir,iit,iip)**2)
! if (rr(iir)>6351) then ! 6371 - 6351 20 km
! fun(iir,iit,iip) = 1.0/(5.8+(6371-rr(iir))/20.0*0.7)
! elseif (rr(iir)>6336) then ! 6351 - 6336 15 km
! fun(iir,iit,iip) = 1.0/(6.5+(6351-rr(iir))/15.0*0.6)
! elseif (rr(iir)>6251) then ! 6336 - 6251 85 km
! fun(iir,iit,iip) = 1.0/(8.04+(6336-rr(iir))/85.0*0.01)
! elseif (rr(iir)>6161) then ! 6251 - 6161 90 km
! fun(iir,iit,iip) = 1.0/(8.05+(6251-rr(iir))/90.0*0.25)
! elseif (rr(iir)>5961) then ! 6161 - 5961 200 km
! fun(iir,iit,iip) = 1.0/(8.30+(6161-rr(iir))/200.0*0.73)
! else
! fun(iir,iit,iip) = 1.0/9.03
! end if
if (rr(iir)>6351) then ! 6371 - 6351 20 km
fun(iir,iit,iip) = 1.0/(5.8+(6371-rr(iir))/20.0*0.7)
elseif (rr(iir)>6336) then ! 6351 - 6336 15 km
fun(iir,iit,iip) = 1.0/(6.5+(6351-rr(iir))/15.0*0.6)
elseif (rr(iir)>5961) then ! 6351 - 6336 15 km
fun(iir,iit,iip) = 1.0/(8.0+(6336-rr(iir))/375.0*1)
else
fun(iir,iit,iip) = 1.0/9.0
end if
!read(100,*) xi(iir,iit,iip)
!read(101,*) eta(iir,iit,iip)
!read(102,*) fun(iir,iit,iip)
a(iir,iit,iip)=1.0+2*zeta(iir,iit,iip);
b(iir,iit,iip)=1.0-2*xi(iir,iit,iip);
c(iir,iit,iip)=1.0+2*xi(iir,iit,iip);
f(iir,iit,iip)=-2*eta(iir,iit,iip);
Table(iir,iit,iip) = 0
! true (target) model
if (tt(iit)>=30.0/180*pi .and. tt(iit)<=50.0/180*pi .and. &
& pp(iip)>=15.0/180*pi .and. pp(iip)<=40.0/180*pi .and. &
& rr(iir)>=6211.0 .and. rr(iir)<=6371.0 ) then
sigma1 = sin(4*pi*(tt(iit)-30.0/180*pi)/(20.0/180*pi))* &
& sin(4*pi*(pp(iip)-15.0/180*pi)/(25.0/180*pi))* &
& sin(2*pi*(rr(iir)-6211)/160.0)
else
sigma1 = 0.0
end if
if (sigma1<0) then
psi = 60.0/180*pi
elseif (sigma1 > 0) then
psi = 150.0/180*pi
else
psi = 0
end if
etaT(iir,iit,iip)=ani_p*abs(sigma1)*sin(2*psi);
xiT(iir,iit,iip)=ani_p*abs(sigma1)*cos(2*psi);
zetaT(iir,iit,iip)=gamma*sqrt(etaT(iir,iit,iip)**2+xiT(iir,iit,iip)**2)
aT(iir,iit,iip)=1.0+2*zetaT(iir,iit,iip);
bT(iir,iit,iip)=1.0-2*xiT(iir,iit,iip);
cT(iir,iit,iip)=1.0+2*xiT(iir,iit,iip);
fT(iir,iit,iip)=-2*etaT(iir,iit,iip);
funT(iir,iit,iip) = fun(iir,iit,iip)/(1+sigma1*slow_p)
!fun(iir,iit,iip) = funT(iir,iit,iip)
!eta(iir,iit,iip)=etaT(iir,iit,iip);
!xi(iir,iit,iip)=xiT(iir,iit,iip);
!zeta(iir,iit,iip)=zetaT(iir,iit,iip)
!a(iir,iit,iip)=aT(iir,iit,iip)
!b(iir,iit,iip)=bT(iir,iit,iip)
!c(iir,iit,iip)=cT(iir,iit,iip)
!f(iir,iit,iip)=fT(iir,iit,iip)
TableT(iir,iit,iip) = 0
u(iir,iit,iip) = 0
end do
end do
end do
! ---- 记录到 matlab 数据中 matlab record ----
if (myid==0) then
open(100,file='ega5/output/model_true')
open(101,file='ega5/output/model_init')
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& funT(iir,iit,iip),xiT(iir,iit,iip),etaT(iir,iit,iip)
write(101,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& fun(iir,iit,iip),xi(iir,iit,iip),eta(iir,iit,iip)
end do
end do
end do
close(100);close(101)
! --------- 多重网格 multiple grid ------
do igrid=1,Ngrid
select case(igrid)
case (1:9)
write(form,'(I1)') igrid
case (10:99)
write(form,'(I2)') igrid
end select
open(100,file='ega5/output/multigrid'//trim(form))
do iir=1,ninvr
do iit=1,ninvt
do iip=1,ninvp
write(100,'(f13.7,f13.7,f13.7)') &
& invr(igrid,iir),invt(igrid,iit),invp(igrid,iip)
end do
end do
end do
close(100)
end do
end if
! ######################### 计算真实到时 calculate true traveltimes ######################
call mpi_barrier(mpi_comm_world,ierr)
allocate(TtimeT(nsou,nrec)) ! 初始化真实到时表 initiate true traveltimes
allocate(Ttime(nsou,nrec)) ! 初始化合成到时表 initiate syn traveltimes
allocate(sourceADJ(nrec)) ! 初始化伴随源 initiate adjoint source
if (myid==0) then
print *, ' '
print *, '----------------- calculating true timetable ... ----------------'
print *, ' '
end if
do i=1,nsou ! loop sources (STATIONS)
! -------- 求解程函方程 solve eikonal equations ------------
call mpi_barrier(mpi_comm_world,ierr)
!rsou(i)=6371.0; tsou(i)=30.5/180.0*pi; psou(i)=15.5/180.0*pi
call FSM_WENO3_PS_sphe_3d_mul_mpi(rr,tt,pp,nr,nt,np,aT,bT,cT,fT,TableT,funT,rsou(i),tsou(i),psou(i),u)
call mpi_barrier(mpi_comm_world,ierr)
! -------- 得到真实走时 calculate true arrival time at receiver --------
call Linear_Interp_3D(rr,tt,pp,TableT,nr,nt,np,rrec,trec,prec,TtimeT(i,:),nrec)
!if ((myid.eq.0) .and. 1>0) then
! print *, rsou(i),tsou(i)/pi*180,psou(i)/pi*180
! open(100,file='ega5/output/arrivaltime')
! do j=1,nrec
! write(100,'(i5.3,f8.2,f6.2,f6.2,f8.3)') j, rrec(j),trec(j)/pi*180,prec(j)/pi*180,TtimeT(i,j)
! end do
! close(100)
!end if
end do
print *, '----------------- calculating true timetable over ---------------- '
! ######################## 反演开始 inversion start ##########################
! ----- 初始化反演参数 initiate inversion parameters ----
stepsize = 0.01 ! 迭代步长 stepsize
old_obj = 0; obj = 0 ! 目标函数 objective function
if (myid .eq. 0) then
print *, ' '
print *, '----------------- inversion start ... ----------------'
print *, ' '
end if
do iter = 1,MaxIter
call mpi_barrier(mpi_comm_world,ierr)
if (myid .eq. 0) then
print *, '----------------- iteration ',iter,' starting ... ----------------'
end if
! ----- 初始化参数 initiate parameters ------
all_Ks = 0; all_Kxi = 0; all_Keta = 0;
obj = 0;
if (myid .eq. 0) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
filename='ega5/output/misfit_step'//trim(form)
open(998, file=trim(filename))
end if
do i=1,nsou ! loop sources (STATIONS)
! ######################## 计算合成走时场 calculate synthetic timetable ########################
if (myid .eq. 0) then
print *, '----------------- calculating synthetic timetable ... ----------------'
end if
Table = 20
call mpi_barrier(mpi_comm_world,ierr)
call FSM_WENO3_PS_sphe_3d_mul_mpi(rr,tt,pp,nr,nt,np,a,b,c,f,Table,fun,rsou(i),tsou(i),psou(i),u)
call mpi_barrier(mpi_comm_world,ierr)
! -------- 得到合成走时 calculate synthetic arrival time at receiver --------
call Linear_Interp_3D(rr,tt,pp,Table,nr,nt,np,rrec,trec,prec,Ttime(i,:),nrec)
! -------- 构造伴随源 build adjoint source -------
call Adjoint_Source_Dt(Ttime(i,:),TtimeT(i,:),nrec,sourceADJ,sou_obj) ! absolute traveltime difference
if (myid .eq. 0) then
do j=1,nrec
write(998,'(f10.5)') Ttime(i,j)-TtimeT(i,j)
end do
end if
! weighting adjoint source
if (weight_rec_bool) then
do j=1,nrec
sourceADJ(j) = sourceADJ(j) * weight_rec(j)
end do
end if
if (1>0 .and. (myid .eq. 0)) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
select case (i)
case (1:9)
write(form2,'(I1)') i
case (10:99)
write(form2,'(I2)') i
end select
filename='ega5/output/syn_step'//trim(form)//'_event'//trim(form2)
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7)') Table(iir,iit,iip)
end do
end do
end do
close(100)
end if
!
!print *, Ttime(i,:),TtimeT(i,:)
!call Adjoint_Source_Ddt(Ttime(i,:),TtimeT(i,:),nrec,sourceADJ,sou_obj) ! double difference traveltime
!do j=1,nrec-1
! do k=j,nrec
! write(998,'(f10.5)') (Ttime(i,j)-Ttime(i,k))-(TtimeT(i,j)-TtimeT(i,k))
! end do
!end do
! ######################## 计算伴随场 adjoint field ########################
if (myid .eq. 0) then
! print *, '----------------- calculating adjoint field ... ----------------'
end if
call FSM_O1_Adj_sphe_3d(rr,tt,pp,nr,nt,np,Table,TableADJ,zeta,xi,eta,nrec,rrec,trec,prec,sourceADJ)
!call FSM_O1_Adj_PS_sphe_3d(rr,tt,pp,nr,nt,np,T0para,tau,TableADJ,zeta,xi,eta,nrec,rrec,trec,prec,sourceADJ)
! ---- 记录到 matlab 数据中 matlab record (adjoint field) ----
if (1>0 .and. (myid .eq. 0)) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
select case (i)
case (1:9)
write(form2,'(I1)') i
case (10:99)
write(form2,'(I2)') i
end select
filename='ega5/output/adj_step'//trim(form)//'_event'//trim(form2)
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7)') TableADJ(iir,iit,iip)
end do
end do
end do
close(100)
end if
! ######################## 计算敏感核 sensitivity kernel ########################
!print *, '----------------- calculating kernel ... ----------------'
call Sensitivity_Kernel(rr,tt,pp,nr,nt,np,Table,TableADJ,gamma,xi,eta,fun,Ks,Kxi,Keta)
! ------- 抹去伴随源处的大函数值 mask the source -------
!print *, '----------------- kernel mask ----------------'
!call Kernel_Mask(rr,tt,pp,nr,nt,np,Ks,rsou(i),tsou(i),psou(i),radius)
!call Kernel_Mask(rr,tt,pp,nr,nt,np,Kxi,rsou(i),tsou(i),psou(i),radius)
!call Kernel_Mask(rr,tt,pp,nr,nt,np,Keta,rsou(i),tsou(i),psou(i),radius)
call Kernel_Mask_new(rr,tt,pp,nr,nt,np,Ks,rsou(i),tsou(i),psou(i))
call Kernel_Mask_new(rr,tt,pp,nr,nt,np,Kxi,rsou(i),tsou(i),psou(i))
call Kernel_Mask_new(rr,tt,pp,nr,nt,np,Keta,rsou(i),tsou(i),psou(i))
! ---- 记录到 matlab 数据中 matlab record (adjoint field) ----
if (1<0 .and. myid .eq. 0) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
select case (i)
case (1:9)
write(form2,'(I1)') i
case (10:99)
write(form2,'(I2)') i
end select
filename='ega5/output/kernel_step'//trim(form)//'_event'//trim(form2)
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& Ks(iir,iit,iip),Kxi(iir,iit,iip),Keta(iir,iit,iip)
end do
end do
end do
close(100)
end if
! --------- 敏感核,obj叠加 sum kernels and objective function -------
if (weight_sou_bool) then ! no weight
all_Ks = all_Ks + Ks * weight_sou(i);
all_Kxi = all_Kxi + Kxi * weight_sou(i);
all_Keta = all_Keta + Keta * weight_sou(i);
obj = obj + sou_obj !* weight_sou(i)
else ! source weighted
all_Ks = all_Ks + Ks;
all_Kxi = all_Kxi + Kxi;
all_Keta = all_Keta + Keta;
obj = obj + sou_obj
end if
end do
close(998)
! ---- 记录到 matlab 数据中 matlab record (kernel) ----
if (1>0 .and. myid .eq. 0) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
filename='ega5/output/kernel_step'//trim(form)//'_sum'
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& all_Ks(iir,iit,iip),all_Kxi(iir,iit,iip),all_Keta(iir,iit,iip)
end do
end do
end do
close(100)
end if
! ##################### 模型更新 model update ####################
print *, '----------------- model updating ... ----------------'
all_Kernel(:,:,:,1) = all_Ks;
all_Kernel(:,:,:,2) = all_Kxi;
all_Kernel(:,:,:,3) = all_Keta;
! ------------ 更新速度(慢度) update velocity (slowness) ------------
call Parameter_Update_Multigrid(rr,tt,pp,nr,nt,np,all_Kernel,nk, &
& invr,invt,invp,ninvr,ninvt,ninvp,ngrid,stepsize,update_value)
if (1<0 .and. myid .eq. 0) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
filename='ega5/output/model_update_step'//trim(form)
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& -fun(iir,iit,iip)*update_value(iir,iit,iip,1), &
& -update_value(iir,iit,iip,2),-update_value(iir,iit,iip,3)
end do
end do
end do
close(100)
end if
do iir=1,nr
do iit=1,nt
do iip=1,np
fun(iir,iit,iip) = fun(iir,iit,iip)*(1-update_value(iir,iit,iip,1))
!fun(iir,iit,iip) = fun(iir,iit,iip)
end do
end do
end do
! ------------ 更新xi update xi ------------
do iir=1,nr
do iit=1,nt
do iip=1,np
xi(iir,iit,iip) = xi(iir,iit,iip)-update_value(iir,iit,iip,2)
end do
end do
end do
! ------------ 更新eta update eta ------------
do iir=1,nr
do iit=1,nt
do iip=1,np
eta(iir,iit,iip) = eta(iir,iit,iip)-update_value(iir,iit,iip,3)
end do
end do
end do
! ----------- 更新 update eikonal solver paramater -----------------
do iir=1,nr
do iit=1,nt
do iip=1,np
b(iir,iit,iip)=1.0-2*xi(iir,iit,iip);
c(iir,iit,iip)=1.0+2*xi(iir,iit,iip);
f(iir,iit,iip)=-2*eta(iir,iit,iip);
end do
end do
end do
! ##################### 调整下降步长 modify stepsize #############
if (myid .eq. 0) then
print *, ' '
end if
if (1>0 .and. (myid .eq. 0)) then
open(999,file='ega5/output/obj',access='append')
write(999,'(f9.2,f9.6)') obj, stepsize
close(999)
end if
if (iter == 1 ) then
if (myid .eq. 0) then
write(*,'(a,f9.2)') 'iter 1, obj is', obj
write(*,'(a,f9.6)') 'iter 1, stepsize is', stepsize
end if
elseif (iter >= 2 .and. obj < old_obj) then
!stepsize = min(0.01,stepsize*1.2)
if (myid .eq. 0) then
write(*,'(a,f9.2,a,f9.2)') 'objective function decreases, from', old_obj, ' to', obj
write(*,'(a,f9.6)') 'new stepsize is ', stepsize
end if
elseif (iter >= 2 .and. obj >= old_obj) then
!stepsize = max(0.002,stepsize*0.5)
if (myid .eq. 0) then
write(*,'(a,f9.2,a,f9.2)') 'objective function increases, from', old_obj, ' to', obj
write(*,'(a,f9.6)') 'new stepsize is ', stepsize
end if
end if
print *, '----------------- iteration ',iter,' over ----------------'
print *, ' '
print *, ' '
old_obj = obj
! ---- 记录到 matlab 数据中 matlab record ----
if (1>0 .and. (myid .eq. 0)) then
select case (iter)
case (1:9)
write(form,'(I1)') iter
case (10:99)
write(form,'(I2)') iter
end select
filename='ega5/output/model_step'//trim(form)
open(100,file=trim(filename))
do iir=1,nr
do iit=1,nt
do iip=1,np
write(100,'(f13.7,f13.7,f13.7,f13.7,f13.7,f13.7)') &
& rr(iir),tt(iit),pp(iip), &
& fun(iir,iit,iip),xi(iir,iit,iip),eta(iir,iit,iip)
end do
end do
end do
close(100)
end if
if (myid .eq. 0) then
call CPU_TIME(time_end)
write(*,'(f10.2)') time_end - time_begin
end if
end do
call mpi_finalize(ierr)
end program eikonal_2d

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,56 @@
version : 2
domain :
#min_max_dep : [-21.863,308.8137] # depth in km
min_max_dep : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat : [28.5,51.5] # latitude in degree
min_max_lon : [13.5,41.5] # longitude in degree
n_rtp : [55,55,55] # number of nodes
source :
#src_dep_lat_lon : [5.0,40.0,24.0] # source depth in km, latitude, longitude in degree
#src_dep_lat_lon : [5750.6370,46.0,36.0] # source depth in km, latitude, longitude in degree
src_rec_file : 'OUTPUT_FILES/src_rec_file_forward.dat' # source receiver file (if found, src_dep_lat_lon is ignored)
swap_src_rec : 1 # swap source and receiver
model :
init_model_type : '' # 'fd' (input file) or '1d_ak135'
init_model_path : './test_model_init.h5' # path to initial model file (ignored if init_model_type is '1d_*')
inversion :
run_mode : 1 # 0 for forward simulation only, 1 for inversion
n_inversion_grid : 5 # number of inversion grid sets
n_inv_dep_lat_lon : [11,10,10] # number of the base inversion grid points
min_max_dep_inv : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat_inv : [30.0,50.0] # latitude in degree
min_max_lon_inv : [15.0,40.0] # longitude in degree
#smooth_method : 1 # 0: multigrid parametrization, 1: laplacian smoothing with CG
#l_smooth_rtp : [0.9,0.9,0.9] # smoothing coefficients for each direction
max_iterations_inv : 1 # maximum number of inversion iterations
step_size : 0.01 # step size for inversion
parallel :
n_sims : 1 # number of simultaneous run
ndiv_rtp : [1,2,2] # number of subdomains
nproc_sub : 2 # number of subprocess used for each subdomain
use_gpu : 0
calculation :
convergence_tolerance : 1e-5
max_iterations : 1000
stencil_order : 3 # 1 or 3
sweep_type : 1 # 0: legacy, 1: cuthill-mckee with shm parallelization
output_setting :
is_output_source_field : 1 # output the calculated field of all sources 1 for yes; 0 for no; default: 1
is_verbose_output : 0 # output internal parameters, if no, only model parameters are out. 1 for yes; 0 for no; default: 0
is_output_model_dat : 0 # output model_parameters_inv_0000.dat or not. 1 for yes; 0 for no; default: 1
inv_strategy :
is_inv_slowness : 1
is_inv_azi_ani : 1
is_inv_rad_ani : 1

View File

@@ -0,0 +1,54 @@
version : 2
domain :
#min_max_dep : [-21.863,308.8137] # depth in km
min_max_dep : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat : [28.5,51.5] # latitude in degree
min_max_lon : [13.5,41.5] # longitude in degree
n_rtp : [55,55,55] # number of nodes
source :
#src_dep_lat_lon : [5.0,40.0,24.0] # source depth in km, latitude, longitude in degree
#src_dep_lat_lon : [5750.6370,46.0,36.0] # source depth in km, latitude, longitude in degree
src_rec_file : 'OUTPUT_FILES/src_rec_file_forward.dat' # source receiver file (if found, src_dep_lat_lon is ignored)
swap_src_rec : 1 # swap source and receiver
model :
init_model_type : '' # 'fd' (input file) or '1d_ak135'
init_model_path : './test_model_init.h5' # path to initial model file (ignored if init_model_type is '1d_*')
inversion :
run_mode : 1 # 0 for forward simulation only, 1 for inversion
n_inversion_grid : 5 # number of inversion grid sets
n_inv_dep_lat_lon : [11,10,10] # number of the base inversion grid points
min_max_dep_inv : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat_inv : [30.0,50.0] # latitude in degree
min_max_lon_inv : [15.0,40.0] # longitude in degree
#smooth_method : 1 # 0: multigrid parametrization, 1: laplacian smoothing with CG
#l_smooth_rtp : [0.9,0.9,0.9] # smoothing coefficients for each direction
max_iterations_inv : 1 # maximum number of inversion iterations
step_size : 0.01 # step size for inversion
parallel :
n_sims : 1 # number of simultaneous run
ndiv_rtp : [1,1,1] # number of subdomains
nproc_sub : 1 # number of subprocess used for each subdomain
use_gpu : 0
calculation :
convergence_tolerance : 1e-5
max_iterations : 1000
stencil_order : 3 # 1 or 3
sweep_type : 1 # 0: legacy, 1: cuthill-mckee with shm parallelization
output_setting :
it_output_suorce_field : 1
is_output_model_dat : 1
inv_strategy :
is_inv_slowness : 1
is_inv_azi_ani : 1
is_inv_rad_ani : 1

View File

@@ -0,0 +1,40 @@
version : 2
domain :
#min_max_dep : [-21.863,308.8137] # depth in km
min_max_dep : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat : [28.5,51.5] # latitude in degree
min_max_lon : [13.5,41.5] # longitude in degree
n_rtp : [55,55,55] # number of nodes
source :
#src_dep_lat_lon : [5.0,40.0,24.0] # source depth in km, latitude, longitude in degree
#src_dep_lat_lon : [5750.6370,46.0,36.0] # source depth in km, latitude, longitude in degree
src_rec_file : 'src_rec_test.dat' # source receiver file (if found, src_dep_lat_lon is ignored)
swap_src_rec : 1 # swap source and receiver
model :
init_model_type : '' # 'fd' (input file) or '1d_ak135'
init_model_path : './test_model_true.h5' # path to initial model file (ignored if init_model_type is '1d_*')
inversion :
run_mode : 0 # 0 for forward simulation only, 1 for inversion
n_inversion_grid : 1
parallel :
n_sims : 1 # number of simultaneous run
ndiv_rtp : [1,2,2] # number of subdomains
nproc_sub : 2 # number of subprocess used for each subdomain
calculation :
convergence_tolerance : 1e-4
max_iterations : 200
stencil_order : 3 # 1 or 3
sweep_type : 1 # 0: legacy, 1: cuthill-mckee with shm parallelization
output_setting :
is_output_source_field : 1 # output the calculated field of all sources 1 for yes; 0 for no; default: 1
is_verbose_output : 0 # output internal parameters, if no, only model parameters are out. 1 for yes; 0 for no; default: 0
is_output_model_dat : 0 # output model_parameters_inv_0000.dat or not. 1 for yes; 0 for no; default: 1

View File

@@ -0,0 +1,36 @@
version : 2
domain :
#min_max_dep : [-21.863,308.8137] # depth in km
min_max_dep : [-29.0, 301.0] # depth in km with R = 6371.0
min_max_lat : [28.5,51.5] # latitude in degree
min_max_lon : [13.5,41.5] # longitude in degree
n_rtp : [55,55,55] # number of nodes
source :
#src_dep_lat_lon : [5.0,40.0,24.0] # source depth in km, latitude, longitude in degree
#src_dep_lat_lon : [5750.6370,46.0,36.0] # source depth in km, latitude, longitude in degree
src_rec_file : 'src_rec_test.dat' # source receiver file (if found, src_dep_lat_lon is ignored)
swap_src_rec : 1 # swap source and receiver
model :
init_model_type : '' # 'fd' (input file) or '1d_ak135'
init_model_path : './test_model_true.h5' # path to initial model file (ignored if init_model_type is '1d_*')
inversion :
run_mode : 0 # 0 for forward simulation only, 1 for inversion
n_inversion_grid : 1
parallel :
n_sims : 1 # number of simultaneous run
ndiv_rtp : [1,1,1] # number of subdomains
nproc_sub : 1 # number of subprocess used for each subdomain
calculation :
convergence_tolerance : 1e-4
max_iterations : 200
stencil_order : 3 # 1 or 3
sweep_type : 1 # 0: legacy, 1: cuthill-mckee with shm parallelization

View File

@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# notebook for create init and true test model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import math\n",
"\n",
"# grid\n",
"R_earth = 6371.0 #6378.1370\n",
"\n",
"rr1=6070\n",
"rr2=6400\n",
"tt1=(30.0-1.5)/180*math.pi\n",
"tt2=(50.0+1.5)/180*math.pi\n",
"pp1=(15.0-1.5)/180*math.pi\n",
"pp2=(40.0+1.5)/180*math.pi\n",
"\n",
"n_rtp = [55,55,55]\n",
"dr = (rr2-rr1)/(n_rtp[0]-1)\n",
"dt = (tt2-tt1)/(n_rtp[1]-1)\n",
"dp = (pp2-pp1)/(n_rtp[2]-1)\n",
"rr = np.array([rr1 + x*dr for x in range(n_rtp[0])])\n",
"tt = np.array([tt1 + x*dt for x in range(n_rtp[1])])\n",
"pp = np.array([pp1 + x*dp for x in range(n_rtp[2])])\n",
"\n",
"# initial model\n",
"gamma = 0.0\n",
"#s0 = 1.0/6.0\n",
"slow_p=0.04\n",
"ani_p=0.03\n",
"\n",
"eta_init = np.zeros(n_rtp)\n",
"xi_init = np.zeros(n_rtp)\n",
"zeta_init = np.zeros(n_rtp)\n",
"vel_init = np.zeros(n_rtp)\n",
"fun_init = np.zeros(n_rtp)\n",
"\n",
"# true model\n",
"eta_true = np.zeros(n_rtp)\n",
"xi_true = np.zeros(n_rtp)\n",
"zeta_true = np.zeros(n_rtp)\n",
"vel_true = np.zeros(n_rtp)\n",
"fun_true = np.zeros(n_rtp)\n",
"\n",
"c=0\n",
"for ir in range(n_rtp[2]):\n",
" for it in range(n_rtp[1]):\n",
" for ip in range(n_rtp[0]):\n",
" #eta_init[ir,it,ip] = 0.0\n",
" #xi_init[ir,it,ip] = 0.0\n",
" zeta_init[ir,it,ip] = gamma*math.sqrt(eta_init[ir,it,ip]**2 + xi_init[ir,it,ip]**2)\n",
" \n",
" if (rr[ir]>6351):\n",
" fun_init[ir,it,ip] = 1.0/(5.8+(6371-rr[ir])/20.0*0.7)\n",
" elif (rr[ir]>6336):\n",
" fun_init[ir,it,ip] = 1.0/(6.5+(6351-rr[ir])/15.0*0.6)\n",
" elif (rr[ir]>5961):\n",
" fun_init[ir,it,ip] = 1.0/(8.0+(6336-rr[ir])/375.0*1) \n",
" else:\n",
" fun_init[ir,it,ip] = 1.0/9.0\n",
"\n",
" vel_init[ir,it,ip] = 1.0/fun_init[ir,it,ip]\n",
"\n",
" # true model\n",
" if (tt[it] >= 30.0/180.0*math.pi and tt[it] <= 50.0/180.0*math.pi \\\n",
" and pp[ip] >= 15.0/180.0*math.pi and pp[ip] <= 40.0/180.0*math.pi \\\n",
" and rr[ir] >= 6211.0 and rr[ir] <= 6371.0):\n",
" c+=1\n",
" sigma = math.sin(4.0*math.pi*(tt[it]-30.0/180.0*math.pi)/(20.0/180.0*math.pi)) \\\n",
" *math.sin(4.0*math.pi*(pp[ip]-15.0/180.0*math.pi)/(25.0/180.0*math.pi)) \\\n",
" *math.sin(2.0*math.pi*(rr[ir]-6211.0)/160.0)\n",
" else:\n",
" sigma = 0.0\n",
"\n",
" if sigma < 0:\n",
" psi = 60.0/180.0*math.pi\n",
" elif sigma > 0:\n",
" psi = 150.0/180.0*math.pi\n",
" else:\n",
" psi = 0.0\n",
"\n",
" eta_true[ir,it,ip] = ani_p*abs(sigma)*math.sin(2.0*psi)\n",
" xi_true[ir,it,ip] = ani_p*abs(sigma)*math.cos(2.0*psi)\n",
" zeta_true[ir,it,ip] = gamma*math.sqrt(eta_true[ir,it,ip]**2 + xi_true[ir,it,ip]**2)\n",
" fun_true[ir,it,ip] = fun_init[ir,it,ip]/(1.0+sigma*slow_p)\n",
" vel_true[ir,it,ip] = 1.0/fun_true[ir,it,ip] \n",
"\n",
"\n",
"r_earth = R_earth #6378.1370\n",
"print(\"depminmax {} {}\".format(r_earth-rr1,r_earth-rr2))\n",
"print(c)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# write out\n",
"import h5py\n",
"\n",
"fout_init = h5py.File('test_model_init.h5', 'w')\n",
"fout_true = h5py.File('test_model_true.h5', 'w')\n",
"\n",
"# write out the arrays eta_init, xi_init, zeta_init, fun_init, a_init, b_init, c_init, f_init\n",
"fout_init.create_dataset('eta', data=eta_init)\n",
"fout_init.create_dataset('xi', data=xi_init)\n",
"fout_init.create_dataset('zeta',data=zeta_init)\n",
"fout_init.create_dataset('vel', data=vel_init)\n",
"\n",
"# writeout the arrays eta_true, xi_true, zeta_true, fun_true, a_true, b_true, c_true, f_true\n",
"fout_true.create_dataset('eta', data=eta_true)\n",
"fout_true.create_dataset('xi', data=xi_true)\n",
"fout_true.create_dataset('zeta',data=zeta_true)\n",
"fout_true.create_dataset('vel', data=vel_true)\n",
"\n",
"fout_init.close()\n",
"fout_true.close()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# prepare src station file\n",
"\n",
"```\n",
" 26 1992 1 1 2 43 56.900 1.8000 98.9000 137.00 2.80 8 305644 <- src  : id_src year month day hour min sec lat lon dep_km mag num_recs id_event\n",
" 26 1 PCBI 1.8900 98.9253 1000.0000 P 10.40 18.000 <- arrival : id_src id_rec name_rec lat lon elevation_m phase epicentral_distance_km arrival_time_sec\n",
" 26 2 MRPI 1.6125 99.3172 1100.0000 P 50.84 19.400\n",
" 26 3 HUTI 2.3153 98.9711 1600.0000 P 57.84 19.200\n",
"\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"random.seed(123456789)\n",
"\n",
"# dummys\n",
"year_dummy = 1998\n",
"month_dummy = 1\n",
"day_dummy = 1\n",
"hour_dummy = 0\n",
"minute_dummy = 0\n",
"second_dummy = 0\n",
"mag_dummy = 3.0\n",
"id_dummy = 1000\n",
"st_name_dummy = 'AAAA'\n",
"phase_dummy = 'P'\n",
"arriv_t_dummy = 0.0\n",
"\n",
"tt1deg = tt1 * 180.0/math.pi\n",
"tt2deg = tt2 * 180.0/math.pi\n",
"pp1deg = pp1 * 180.0/math.pi\n",
"pp2deg = pp2 * 180.0/math.pi\n",
"\n",
"\n",
"n_src = 1\n",
"n_rec = [1 for x in range(n_src)]\n",
"\n",
"\n",
"lines = []\n",
"\n",
"pos_src=[]\n",
"pos_rec=[]\n",
"\n",
"dep_srcs=[12.902894]\n",
"lon_srcs=[16.794572]\n",
"lat_srcs=[37.503373]\n",
"\n",
"elev_recs = [0.0]\n",
"lon_recs = [29.812050]\n",
"lat_recs = [36.472809]\n",
"\n",
"\n",
"# create dummy src\n",
"for i_src in range(n_src):\n",
" # define one point in the domain (rr1 bottom, rr2 top)\n",
" dep = dep_srcs[i_src]\n",
" lon = lon_srcs[i_src]\n",
" lat = lat_srcs[i_src]\n",
"\n",
" src = [i_src, year_dummy, month_dummy, day_dummy, hour_dummy, minute_dummy, second_dummy, lat, lon, dep, mag_dummy, n_rec[i_src], id_dummy]\n",
" lines.append(src)\n",
"\n",
" pos_src.append([lon,lat,dep])\n",
"\n",
" # create dummy station\n",
" for i_rec in range(n_rec[i_src]):\n",
" #elev_rec = random.uniform(0.0,-10.0) # elevation in m\n",
" #lon_rec = random.uniform(pp1deg,pp2deg)\n",
" #lat_rec = random.uniform(tt1deg,tt2deg)\n",
"\n",
" rec = [i_src, i_rec, st_name_dummy+\"_\"+str(i_rec), lat_recs[i_rec], lon_recs[i_rec], elev_recs[i_rec], phase_dummy, arriv_t_dummy]\n",
" lines.append(rec)\n",
"\n",
" pos_rec.append([lon_recs[i_rec],lat_recs[i_rec],elev_recs[i_rec]])\n",
"\n",
"# write out ev_arrivals file\n",
"fname = 'src_rec_test.dat'\n",
"\n",
"with open(fname, 'w') as f:\n",
" for line in lines:\n",
" for elem in line:\n",
" f.write('{} '.format(elem))\n",
" f.write('\\n')\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# draw src and rec positions\n",
"import matplotlib.pyplot as plt\n",
"\n",
"for i_src in range(n_src):\n",
" plt.scatter(pos_src[i_src][1],pos_src[i_src][0],c='r',marker='o')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plot receivers\n",
"for i_rec in range(n_rec[0]):\n",
" plt.scatter(pos_rec[i_rec][1],pos_rec[i_rec][0],c='b',marker='o')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.1 64-bit ('3.9.1')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.1"
},
"vscode": {
"interpreter": {
"hash": "02f83e1f4cd9619657a6845405e2dd67c4de23753ba48bca5dce2ebf57b3813a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,215 @@
# %% [markdown]
# # notebook for create init and true test model
# %%
import numpy as np
import math
# grid
R_earth = 6371.0 #6378.1370
rr1=6070
rr2=6400
tt1=(30.0-1.5)/180*math.pi
tt2=(50.0+1.5)/180*math.pi
pp1=(15.0-1.5)/180*math.pi
pp2=(40.0+1.5)/180*math.pi
n_rtp = [55,55,55]
dr = (rr2-rr1)/(n_rtp[0]-1)
dt = (tt2-tt1)/(n_rtp[1]-1)
dp = (pp2-pp1)/(n_rtp[2]-1)
rr = np.array([rr1 + x*dr for x in range(n_rtp[0])])
tt = np.array([tt1 + x*dt for x in range(n_rtp[1])])
pp = np.array([pp1 + x*dp for x in range(n_rtp[2])])
# initial model
gamma = 0.0
#s0 = 1.0/6.0
slow_p=0.04
ani_p=0.03
eta_init = np.zeros(n_rtp)
xi_init = np.zeros(n_rtp)
zeta_init = np.zeros(n_rtp)
fun_init = np.zeros(n_rtp)
vel_init = np.zeros(n_rtp)
# true model
eta_true = np.zeros(n_rtp)
xi_true = np.zeros(n_rtp)
zeta_true = np.zeros(n_rtp)
fun_true = np.zeros(n_rtp)
vel_true = np.zeros(n_rtp)
c=0
for ir in range(n_rtp[2]):
for it in range(n_rtp[1]):
for ip in range(n_rtp[0]):
#eta_init[ir,it,ip] = 0.0
#xi_init[ir,it,ip] = 0.0
zeta_init[ir,it,ip] = gamma*math.sqrt(eta_init[ir,it,ip]**2 + xi_init[ir,it,ip]**2)
if (rr[ir]>6351):
fun_init[ir,it,ip] = 1.0/(5.8+(6371-rr[ir])/20.0*0.7)
elif (rr[ir]>6336):
fun_init[ir,it,ip] = 1.0/(6.5+(6351-rr[ir])/15.0*0.6)
elif (rr[ir]>5961):
fun_init[ir,it,ip] = 1.0/(8.0+(6336-rr[ir])/375.0*1)
else:
fun_init[ir,it,ip] = 1.0/9.0
vel_init[ir,it,ip] = 1.0/fun_init[ir,it,ip]
# true model
if (tt[it] >= 30.0/180.0*math.pi and tt[it] <= 50.0/180.0*math.pi \
and pp[ip] >= 15.0/180.0*math.pi and pp[ip] <= 40.0/180.0*math.pi \
and rr[ir] >= 6211.0 and rr[ir] <= 6371.0):
c+=1
sigma = math.sin(4.0*math.pi*(tt[it]-30.0/180.0*math.pi)/(20.0/180.0*math.pi)) \
*math.sin(4.0*math.pi*(pp[ip]-15.0/180.0*math.pi)/(25.0/180.0*math.pi)) \
*math.sin(2.0*math.pi*(rr[ir]-6211.0)/160.0)
else:
sigma = 0.0
if sigma < 0:
psi = 60.0/180.0*math.pi
elif sigma > 0:
psi = 150.0/180.0*math.pi
else:
psi = 0.0
eta_true[ir,it,ip] = ani_p*abs(sigma)*math.sin(2.0*psi)
xi_true[ir,it,ip] = ani_p*abs(sigma)*math.cos(2.0*psi)
zeta_true[ir,it,ip] = gamma*math.sqrt(eta_true[ir,it,ip]**2 + xi_true[ir,it,ip]**2)
fun_true[ir,it,ip] = fun_init[ir,it,ip]/(1.0+sigma*slow_p)
vel_true[ir,it,ip] = 1.0/fun_true[ir,it,ip]
r_earth = R_earth #6378.1370
print("depminmax {} {}".format(r_earth-rr1,r_earth-rr2))
print(c)
# %%
# write out
import h5py
fout_init = h5py.File('test_model_init.h5', 'w')
fout_true = h5py.File('test_model_true.h5', 'w')
# write out the arrays eta_init, xi_init, zeta_init, fun_init, a_init, b_init, c_init, f_init
fout_init.create_dataset('eta', data=eta_init)
fout_init.create_dataset('xi', data=xi_init)
fout_init.create_dataset('zeta',data=zeta_init)
fout_init.create_dataset('vel', data=vel_init)
# writeout the arrays eta_true, xi_true, zeta_true, fun_true, a_true, b_true, c_true, f_true
fout_true.create_dataset('eta', data=eta_true)
fout_true.create_dataset('xi', data=xi_true)
fout_true.create_dataset('zeta',data=zeta_true)
fout_true.create_dataset('vel', data=vel_true)
fout_init.close()
fout_true.close()
# %% [markdown]
# # prepare src station file
#
# ```
# 26 1992 1 1 2 43 56.900 1.8000 98.9000 137.00 2.80 8 305644 <- src  : id_src year month day hour min sec lat lon dep_km mag num_recs id_event
# 26 1 PCBI 1.8900 98.9253 1000.0000 P 10.40 18.000 <- arrival : id_src id_rec name_rec lat lon elevation_m phase epicentral_distance_km arrival_time_sec
# 26 2 MRPI 1.6125 99.3172 1100.0000 P 50.84 19.400
# 26 3 HUTI 2.3153 98.9711 1600.0000 P 57.84 19.200
#
# ```
# %%
import random
random.seed(123456789)
# dummys
year_dummy = 1998
month_dummy = 1
day_dummy = 1
hour_dummy = 0
minute_dummy = 0
second_dummy = 0
mag_dummy = 3.0
id_dummy = 1000
st_name_dummy = 'AAAA'
phase_dummy = 'P'
arriv_t_dummy = 0.0
tt1deg = tt1 * 180.0/math.pi
tt2deg = tt2 * 180.0/math.pi
pp1deg = pp1 * 180.0/math.pi
pp2deg = pp2 * 180.0/math.pi
n_src = 1
n_rec = [1 for x in range(n_src)]
lines = []
pos_src=[]
pos_rec=[]
dep_srcs=[12.902894]
lon_srcs=[16.794572]
lat_srcs=[37.503373]
elev_recs = [0.0]
lon_recs = [29.812050]
lat_recs = [36.472809]
# create dummy src
for i_src in range(n_src):
# define one point in the domain (rr1 bottom, rr2 top)
dep = dep_srcs[i_src]
lon = lon_srcs[i_src]
lat = lat_srcs[i_src]
src = [i_src, year_dummy, month_dummy, day_dummy, hour_dummy, minute_dummy, second_dummy, lat, lon, dep, mag_dummy, n_rec[i_src], id_dummy]
lines.append(src)
pos_src.append([lon,lat,dep])
# create dummy station
for i_rec in range(n_rec[i_src]):
#elev_rec = random.uniform(0.0,-10.0) # elevation in m
#lon_rec = random.uniform(pp1deg,pp2deg)
#lat_rec = random.uniform(tt1deg,tt2deg)
rec = [i_src, i_rec, st_name_dummy+"_"+str(i_rec), lat_recs[i_rec], lon_recs[i_rec], elev_recs[i_rec], phase_dummy, arriv_t_dummy]
lines.append(rec)
pos_rec.append([lon_recs[i_rec],lat_recs[i_rec],elev_recs[i_rec]])
# write out ev_arrivals file
fname = 'src_rec_test.dat'
with open(fname, 'w') as f:
for line in lines:
for elem in line:
f.write('{} '.format(elem))
f.write('\n')
# %%
# draw src and rec positions
import matplotlib.pyplot as plt
for i_src in range(n_src):
plt.scatter(pos_src[i_src][1],pos_src[i_src][0],c='r',marker='o')
# %%
# plot receivers
for i_rec in range(n_rec[0]):
plt.scatter(pos_rec[i_rec][1],pos_rec[i_rec][0],c='b',marker='o')