if mpirank == 0: print 'Check gradient and Hessian against finite-difference' InvPb.update_m(mtrueVm) #InvPb.update_m(1.0) InvPb.Regul.update_Parameters({ 'Vm': Vm, 'gamma': 0.0, 'beta': 0.0, 'm0': 1.0 }) InvPb.solvefwd_cost() InvPb.solveadj_constructgrad() nbcheck = 5 MedPert = np.zeros((nbcheck, InvPb.m.vector().local_size())) for ii in range(nbcheck): smoothperturb = dl.Expression('sin(n*pi*x[0])*sin(n*pi*x[1])', n=ii + 1) smoothperturb_fn = dl.interpolate(smoothperturb, Vm) MedPert[ii, :] = smoothperturb_fn.vector().array() checkgradfd_med(InvPb, MedPert, 1e-6, [1e-4, 1e-5, 1e-6], True, mpicomm) print '' checkhessfd_med(InvPb, MedPert, 1e-6, [1e-4, 1e-5, 1e-6], True, mpicomm) sys.exit(0) # Solve inverse problem if mpirank == 0: print 'Solve inverse problem' InvPb.inversion(1.0, mtrueVm, mpicomm, {'maxnbNewtiter': 200}, myplot=myplot)
#InvPb.update_m(mtrueVm) #InvPb.update_m(1.0) InvPb.update_m(dl.interpolate(dl.Expression("sin(x[0])*sin(x[1])"), Vm)) InvPb.regparam = 0.0 InvPb.solvefwd_cost() InvPb.solveadj_constructgrad() checkgradfd_med(InvPb, MedPert, 1e-6, [1e-4, 1e-5, 1e-6], True, mpicomm) print '' #checkhessfd_med(InvPb, MedPert, 1e-6, [1e-1, 1e-2, 1e-3, 1e-4, 1e-5], False, mpicomm) checkhessfd_med(InvPb, MedPert, 1e-6, [1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8], False, mpicomm) sys.exit(0) #### Solve inverse problem if mpirank == 0: print 'Solve inverse problem' InvPb.inversion(minit, mtrueVm, mpicomm, {'maxnbNewtiter': 5000}, myplot=myplot) InvPb.regparam = 0.0 InvPb.solvefwd_cost() InvPb.solveadj_constructgrad() checkgradfd_med(InvPb, MedPert, 1e-6, [1e-4, 1e-5, 1e-6], True, mpicomm) print '' checkhessfd_med(InvPb, MedPert, 1e-6, [1e-1, 1e-2, 1e-3, 1e-4, 1e-5], False, mpicomm)