Skip to content

Commit

Permalink
address src on grid point
Browse files Browse the repository at this point in the history
  • Loading branch information
inverseproblem committed Jan 31, 2025
1 parent b6bb9bb commit af5b42f
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 197 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Both forward and gradient (adjoint) computations can be run in parallel using ei

This code is part of a larger project `G⁻¹Lab` (a superset of HMCLab) targeting probabilistic geophysical inverse problems. Please cite the following papers if you use this code:

* Andrea Zunino, Scott Keating, Andreas Fichtner (2025), **A discrete adjoint method for deterministic and probabilistic eikonal-equation-based inversion of traveltime for velocity and source location, arXiv preprint arXiv:2501.13532, [https://arxiv.org/abs/2501.13532v1](https://arxiv.org/abs/2501.13532v1).
* Andrea Zunino, Scott Keating, Andreas Fichtner (2025), **A discrete adjoint method for deterministic and probabilistic eikonal-equation-based inversion of traveltime for velocity and source location**, arXiv preprint arXiv:2501.13532, [https://arxiv.org/abs/2501.13532v1](https://arxiv.org/abs/2501.13532v1).

* Andrea Zunino, Lars Gebraad, Alessandro Ghirotto, Andreas Fichtner (2023), **HMCLab: a framework for solving diverse geophysical inverse problems using the Hamiltonian Monte Carlo method**, Geophysical Journal International, Volume 235, Issue 3, Pages 2979–2991, [https://doi.org/10.1093/gji/ggad403](https://doi.org/10.1093/gji/ggad403)

Expand Down
27 changes: 3 additions & 24 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Documenter, EikonalSolvers

makedocs(modules = [EikonalSolvers],
repo=Remotes.GitLab("JuliaGeoph","EikonalSolvers.jl"), #"https://gitlab.com/JuliaGeoph/EikonalSolvers.jl/blob/{commit}{path}#{line}",
repo=Remotes.GitHub("GinvLab","EikonalSolvers.jl"),
sitename="EikonalSolvers.jl",
authors = "Andrea Zunino",
format = Documenter.HTML(prettyurls=get(ENV,"CI",nothing)=="true"),
Expand All @@ -14,30 +14,9 @@ makedocs(modules = [EikonalSolvers],
)

deploydocs(
repo="gitlab.com/JuliaGeoph/EikonalSolvers.jl.git",
repo="github.com/GinvLab/EikonalSolvers.jl.git",
devbranch = "main",
deploy_config = Documenter.GitLab(),
deploy_config = Documenter.GitHubActions(),
branch = "gl-pages",
)


###########################################################
# GitLab <: DeployConfig

# GitLab implementation of DeployConfig.

# The following environment variables influence the build when using the GitLab configuration:

# DOCUMENTER_KEY: must contain the Base64-encoded SSH private key for the repository. This variable should be set in the GitLab settings. Make sure this variable is marked NOT to be displayed in the build log.

# CI_COMMIT_BRANCH: the name of the commit branch.

# CI_EXTERNAL_PULL_REQUEST_IID: Pull Request ID from GitHub if the pipelines are for external pull requests.

# CI_PROJECT_PATH_SLUG: The namespace with project name. All letters lowercased and non-alphanumeric characters replaced with -.

# CI_COMMIT_TAG: The commit tag name. Present only when building tags.

# CI_PIPELINE_SOURCE: Indicates how the pipeline was triggered.

# The CI_* variables are set automatically on GitLab. More information on how GitLab sets the CI_* variables can be found in the GitLab documentation.
282 changes: 151 additions & 131 deletions examples/test_fwd_grad/test_grad_source_loc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,145 +5,165 @@ using GLMakie



# create the Grid2D struct
hgrid = 180.73
grd = Grid2DCart(hgrid=hgrid,xinit=0.0,yinit=0.0,nx=380,ny=260)
# nsrc = 4
# coordsrc = [hgrid.*LinRange(10.0,190.0,nsrc) hgrid.*100.0.*ones(nsrc)] # coordinates of the sources (4 sources)

nsrc = 1
nrec = 5
#coordrec = [[hgrid.*LinRange(5.73,grd.nx-7.34,nrec) hgrid.*5.7.*ones(nrec)] for i=1:nsrc] # coordinates of the receivers (10 receivers)

nrec = 5
coordrec = [[hgrid.*LinRange(5.73,grd.nx-7.34,nrec) hgrid.*5.7.*ones(nrec)] for i=1:nsrc] # coordinates of the receivers (10 receivers)
#@show coordrec
coordrec = [vcat(coordrec...,
[[hgrid.*LinRange(5.73,grd.nx-7.34,nrec) hgrid.*(grd.ny-10)*ones(nrec)] for i=1:nsrc]... )]


@show coordrec

velmod = 2500.0 .* ones(grd.nx,grd.ny) # velocity model
if true
# ## increasing velocity with depth...
for i=1:grd.ny
velmod[:,i] = 0.034 * i .+ velmod[:,i]
end
# velmod = velmod[:,end:-1:1]
# velmod .+= 1.0.*rand(size(velmod))
# for i=1:grd.nx
# velmod[i,:] = 0.034 * i .+ velmod[i,:]
# end
else
println("\n=== Constant velocity model ===\n")
end

gradtype = :gradvelandsrcloc #:gradvel # :gradvelandsrcloc # :gradvel
parallelkind = :serial #:sharedmem


for refinesrc in [false,true]

extraparams=ExtraParams(parallelkind=parallelkind,
refinearoundsrc=refinesrc,
radiussmoothgradsrc=0)

println("\n######## Refinement of the source region? $refinesrc ###########")

#coordsrc = [hgrid*15.0 hgrid*(grd.ny-15.0)]
#coordsrc = [hgrid*5.4 hgrid*(grd.ny-3.2)]
coordsrc1 = [hgrid*37.8 hgrid*(grd.ny-28.4)]
#coordsrc = [hgrid*5.39123 hgrid*(grd.ny-3.1123)]

println("\n-------------- forward ----------------")
# run the traveltime computation with default algorithm ("ttFMM_hiord")
ttpicks = eiktraveltime(velmod,grd,coordsrc1,coordrec,
extraparams=extraparams)


# standard deviation of error on observed data
stdobs = [0.05.*ones(size(ttpicks[1])) for i=1:nsrc]
# generate a "noise" array to simulate real data
#noise = [stdobs[i].^2 .* randn(size(stdobs[i])) for i=1:nsrc]
# add the noise to the synthetic traveltime data
dobs = ttpicks #.+ noise


# # create a guess/"current" model
#vel0 = copy(velmod)
vel0 = copy(velmod)
@assert velmod==vel0
# vel0 = 2.3 .* ones(grd.nx,grd.ny)
# nsrc = 1
coordsrc2 = [hgrid*22.53 hgrid*(grd.ny-38.812)]
#coordsrc = [hgrid*5.4 hgrid*(grd.ny-3.2)]


println("\n-------------- gradient ----------------")
## calculate the gradient of the misfit function
gradvel,∂χ∂xysrc = eikgradient(vel0,grd,coordsrc2,coordrec,dobs,stdobs,
gradtype,extraparams=extraparams)
function rungradsrcloc()
# create the Grid2D struct
hgrid = 10.0
grd = Grid2DCart(hgrid=hgrid,cooinit=(0.0,0.0),grsize=(380,260))


#nsrc = 3
# nrec = 2
# coordrec = [[hgrid.*LinRange(5.73,grd.grsize[1]-7.34,nrec) hgrid.*5.7.*ones(nrec)] for i=1:nsrc]
#coordrec = [vcat(coordrec...,
# [[hgrid.*LinRange(5.73,grd.grsize[1]-7.34,nrec) hgrid.*(grd.grsize[2]-10)*ones(nrec)] for i=1:nsrc]... )]

println("\n-------------- misfit ----------------")

dh = 0.00001

coordsrc_plusdx = coordsrc2 .+ [dh 0.0]
misf_pdx = eikttimemisfit(vel0,dobs,stdobs,coordsrc_plusdx,coordrec,grd;
extraparams=extraparams)

coordsrc_minusdx = coordsrc2 .+ [-dh 0.0]
misf_mdx = eikttimemisfit(vel0,dobs,stdobs,coordsrc_minusdx,coordrec,grd;
extraparams=extraparams)

coordsrc_plusdy = coordsrc2 .+ [0.0 dh]
misf_pdy = eikttimemisfit(vel0,dobs,stdobs,coordsrc_plusdy,coordrec,grd;
extraparams=extraparams)

coordsrc_minusdy = coordsrc2 .+ [0.0 -dh]
misf_mdy = eikttimemisfit(vel0,dobs,stdobs,coordsrc_minusdy,coordrec,grd;
extraparams=extraparams)


∂χ∂x_src_FD = (misf_pdx-misf_mdx)/(2*dh)
∂χ∂y_src_FD = (misf_pdy-misf_mdy)/(2*dh)
∂χ∂xysrc_FD = [∂χ∂x_src_FD ∂χ∂y_src_FD]

# @show misf_ref
# @show misf_pdx,misf_mdx
# @show misf_pdy,misf_mdy


if ∂χ∂xysrc!=nothing
@show ∂χ∂xysrc
@show ∂χ∂xysrc_FD
@show ∂χ∂xysrc.-∂χ∂xysrc_FD

velmod = 2500.0 .* ones(grd.grsize...) # velocity model
if true
# ## increasing velocity with depth...
for i=1:grd.grsize[2]
velmod[:,i] = 0.034 * i .+ velmod[:,i]
end
# velmod = velmod[:,end:-1:1]
# velmod .+= 1.0.*rand(size(velmod))
# for i=1:grd.nx
# velmod[i,:] = 0.034 * i .+ velmod[i,:]
# end
else
println("No ∂χ∂xysrc requested.")
println("\n=== Constant velocity model ===\n")
end

# println()
# @show extrema(gradvel)

##
if true
fig = Figure(size=(1000,800))
ax1 = Axis(fig[1,1],title="Refinement $refinesrc")
vmax = maximum(abs.(gradvel))
colorrange = (-vmax,vmax)
hm = heatmap!(ax1,gradvel,colormap=:balance,colorrange=colorrange)
Colorbar(fig[1,2], hm)
ax1.yreversed = true
gradtype = :gradvelandsrcloc #:gradvel # :gradvelandsrcloc # :gradvel
parallelkind = :serial #:sharedmem

@show extrema(grd.x)
@show extrema(grd.y)

for refinesrc in [false,true]

extraparams=ExtraParams(parallelkind=parallelkind,
refinearoundsrc=refinesrc,
radiussmoothgradsrc=0)

println("\n######## Refinement of the source region? $refinesrc ###########")

coordsrc1 = [#hgrid*127.8 hgrid*(grd.grsize[2]-28.4);
#hgrid*245.39123 hgrid*(grd.grsize[2]-193.1123);
hgrid*315.120 hgrid*(grd.grsize[2]-15.230)]

nrec = 4
coordrec = [[hgrid.*LinRange(5.73,grd.grsize[1]-7.34,nrec) hgrid.*5.7.*ones(nrec)]
for i=1:size(coordsrc1,1)]
#coordrec = [[hgrid.*grd.grsize[1]-300.34 hgrid.*5.7.*ones(1)] for i=1:size(coordsrc1,1)]

println("\n-------------- forward ----------------")
# run the traveltime computation with default algorithm ("ttFMM_hiord")
ttpicks = eiktraveltime(velmod,grd,coordsrc1,coordrec,
extraparams=extraparams)

# standard deviation of error on observed data
stdobs = [0.05.*ones(size(ttpicks[1])) for i=1:size(coordsrc1,1)]
# generate a "noise" array to simulate real data
#noise = [stdobs[i] .* randn(size(stdobs[i])) for i=1:nsrc]
# add the noise to the synthetic traveltime data
dobs = ttpicks #.+ noise


# # create a guess/"current" model
#vel0 = copy(velmod)
vel0 = copy(velmod)
@assert velmod==vel0
# vel0 = 2.3 .* ones(grd.nx,grd.ny)
# nsrc = 1
coordsrc2 = [#hgrid*22.53 hgrid*(grd.grsize[2]-38.812);
#hgrid*260.4 hgrid*(grd.grsize[2]-123.2);
#hgrid*280.230 hgrid*(grd.grsize[2]-28.0);
hgrid*280.0 2320.00]
@show coordsrc2

#display(GLMakie.Screen(),fig)
display(fig)
#sleep(5)
println("\n-------------- gradient ----------------")
## calculate the gradient of the misfit function
gradvel,∂χ∂xysrc,misf = eikgradient(vel0,grd,coordsrc2,coordrec,dobs,stdobs,
gradtype,extraparams=extraparams)


@show coordsrc1
@show coordsrc2

println("\n-------------- misfit ----------------")

dh = 0.0001
@show dh

∂χ∂xysrc_FD = zeros(size(coordsrc2,1),2)
for s=1:size(coordsrc2,1)

@show s
@show coordsrc2[s:s,:].-coordsrc1[s:s,:]
@show dobs[s:s]
@show stdobs[s:s]

coordsrc_plusdx = coordsrc2[s:s,:] .+ [dh 0.0]
misf_pdx = eikttimemisfit(vel0,grd,coordsrc_plusdx,coordrec[s:s],dobs[s:s],stdobs[s:s];
extraparams=extraparams)

coordsrc_minusdx = coordsrc2[s:s,:] .+ [-dh 0.0]
misf_mdx = eikttimemisfit(vel0,grd,coordsrc_minusdx,coordrec[s:s],dobs[s:s],stdobs[s:s];
extraparams=extraparams)

coordsrc_plusdy = coordsrc2[s:s,:] .+ [0.0 dh]
misf_pdy = eikttimemisfit(vel0,grd,coordsrc_plusdy,coordrec[s:s],dobs[s:s],stdobs[s:s];
extraparams=extraparams)

coordsrc_minusdy = coordsrc2[s:s,:] .+ [0.0 -dh]
misf_mdy = eikttimemisfit(vel0,grd,coordsrc_minusdy,coordrec[s:s],dobs[s:s],stdobs[s:s];
extraparams=extraparams)

∂χ∂x_src_FD = (misf_pdx-misf_mdx)/(2*dh)
∂χ∂y_src_FD = (misf_pdy-misf_mdy)/(2*dh)
∂χ∂xysrc_FD[s,:] .= (∂χ∂x_src_FD,∂χ∂y_src_FD)

@show dh,grd.hgrid
@show misf_pdx,misf_mdx
@show misf_pdy,misf_mdy

end

# @show misf_ref
# @show misf_pdx,misf_mdx
# @show misf_pdy,misf_mdy


println("∂χ∂xysrc:")
display(∂χ∂xysrc)
println("∂χ∂xysrc_FD:")
display(∂χ∂xysrc_FD)
println("∂χ∂xysrc.-∂χ∂xysrc_FD:")
display(∂χ∂xysrc.-∂χ∂xysrc_FD)


##
if true
fig = Figure(size=(1000,800))
ax1 = Axis(fig[1,1],title="Refinement $refinesrc")
vmax = maximum(abs.(gradvel))
colorrange = (-vmax,vmax)
hm = heatmap!(ax1,grd.x,grd.y,gradvel,colormap=:balance,colorrange=colorrange)
Colorbar(fig[1,2], hm)

for i=1:size(coordsrc1,1)
scatter!(ax1,coordsrc1[i,1],coordsrc1[i,2],color=:black)
scatter!(ax1,coordsrc2[i,1],coordsrc2[i,2],color=:red)
end

#ax1.yreversed = true

#display(GLMakie.Screen(),fig)
display(fig)
sleep(3)
end

end

return
end


Loading

0 comments on commit af5b42f

Please sign in to comment.