Define a callback function that returns the gradient and functional
temp_vp = Function(V)
temp_dj = Function(V)
def shots(xi, stops):
""" Callback function that returns gradient of control and functional to be optimized using scipy """
stops[0] = COMM_WORLD.bcast(stop[0], root=0)
if stops[0] == 0:
xi = COMM_WORLD.bcast(xi, root=0)
temp_vp.dat.data[:] = xi
Initialize a file to visualize the control each iteration
if COMM_WORLD.rank == 0:
viz_m_file = File("control.pvd", comm=COMM_SELF)
viz_m_file.write(vp_guess)
def derivative_cb(m):
""" Plots intermidate control file to disk"""
vp_guess.dat.data[:] = m
viz_m_file.write(vp_guess)
10. Call the optimization routine
if COMM_WORLD.rank==0:
stop = [0]
xi = vp_guess.dat.data
res = optimize.minimize(shots,
xi,
args = (stop, ),
method = "L-BFGS-B",
jac = True,
bounds = [(1.0, 10.0) for x in range(len(xi))],
callback = derivative_cb,
options = {"disp": True,"maxiter": 20})
stop = [1]
shots(xi, stop)
vp_guess.dat.data[:] = res.x
File("final.pvd", comm=COMM_SELF).write(vp_guess)
else:
stop = [0]
xi = vp_guess.dat.data
while stop[0] == 0:
shots(xi,stop)
############################################
When I run the problem it seems to pass through one iteration fine then errors out, I have included the output below, any suggestions?
#############################################
process_output2.log
Hi, I'm trying to run firedrake on a sample problem
Here is the problem, I can supply input files if needed
############################################ from firedrake import from firedrake.petsc import PETSc from firedrake_adjoint import
import math import numpy as np import time as ti from mpi4py import MPI from scipy import optimize
import solvers from myutils import io, utils
""" This is an example of how to use Spyro to perform Full-waveform inversion"""
-----------------------------------------------------------------------
P R O C E D U R E
-----------------------------------------------------------------------
1. Run forward problem for exact data (this is only done if you don't have real data)
2. Run forward problem for guess data.
3. Compute the gradient of the functional w.r.t. to the control (vp_guess)
4. Call SciPy optimization routine (here we're using LBFGS-B)
-----------------------------------------------------------------------
I N I T I A L I Z E
-----------------------------------------------------------------------
Import the options for your model
from inputdata import model
Create the computational environment
comm = utils.mpi_init(model)
-----------------------------------------------------------------------
F O R W A R D
-----------------------------------------------------------------------
Configure source positions based on the input dictionary.
sources = utils.create_sources(model)
Read in an external mesh and project P-wave velocity onto it
mesh,V = io.ReadMesh(model,comm) vp_exact = io.Interp(model,mesh,guess=False) vp_guess = io.Interp(model,mesh,guess=True)
Compute forward solution for all shots
p_exact = solvers.Leapfrog(model,mesh,comm,vp_exact,sources)
Compute the weights for fast interp to all receiver points
map1,map2 = utils.build_maps(model,mesh,comm)
Compute the interpolation matrix
interp_mat = utils.build_interp_matrix(model,mesh,comm,map1,map2)
vp_iterates = File("vp_iter.pvd")
dj_iterates = File("dj_iter.pvd")
Define a callback function that returns the gradient and functional
temp_vp = Function(V) temp_dj = Function(V) def shots(xi, stops): """ Callback function that returns gradient of control and functional to be optimized using scipy """ stops[0] = COMM_WORLD.bcast(stop[0], root=0) if stops[0] == 0: xi = COMM_WORLD.bcast(xi, root=0) temp_vp.dat.data[:] = xi
lets look at what's going on here
Initialize a file to visualize the control each iteration
if COMM_WORLD.rank == 0: viz_m_file = File("control.pvd", comm=COMM_SELF) viz_m_file.write(vp_guess)
def derivative_cb(m): """ Plots intermidate control file to disk""" vp_guess.dat.data[:] = m viz_m_file.write(vp_guess)
10. Call the optimization routine
if COMM_WORLD.rank==0: stop = [0] xi = vp_guess.dat.data res = optimize.minimize(shots, xi, args = (stop, ), method = "L-BFGS-B", jac = True, bounds = [(1.0, 10.0) for x in range(len(xi))], callback = derivative_cb, options = {"disp": True,"maxiter": 20}) stop = [1] shots(xi, stop) vp_guess.dat.data[:] = res.x File("final.pvd", comm=COMM_SELF).write(vp_guess) else: stop = [0] xi = vp_guess.dat.data while stop[0] == 0: shots(xi,stop) ############################################ When I run the problem it seems to pass through one iteration fine then errors out, I have included the output below, any suggestions? ############################################# process_output2.log