myplot.set_varname('m_target') myplot.plot_vtk(mtrue) myplot.set_varname('m_targetVm') myplot.plot_vtk(mtrueVm) else: myplot = None if mpirank == 0: print 'Compute noisy data' OPPS = 15 NB = OPPS + 1. Points = np.array([[[ii/NB,jj/NB] for ii in range(1,OPPS+1)]\ for jj in range(1,OPPS+1)]) Points.resize((OPPS * OPPS, 2)) ObsOp = ObsPointwise({'V': V, 'Points': Points}, mpicomm) ObsOp.noise = False goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp, [], [], [], False, mpicomm) goal.update_m(mtrue) goal.solvefwd() np.random.seed(11) noisepercent = 0.02 # e.g., 0.02 = 2% noise level UD = goal.U[0] rndnb = np.random.randn(UD.size) rndnb = rndnb / np.linalg.norm(rndnb) noiseres = noisepercent * np.linalg.norm(UD) UDnoise = UD + rndnb * noiseres if mpirank == 0: print 'noiseres={}'.format(noiseres) if PLOT: myplot.set_varname('u_target') myplot.plot_vtk(goal.u) # Define regularization:
def run_problem(): # Domain, f-e spaces and boundary conditions: mesh = UnitSquareMesh(20,20) V = FunctionSpace(mesh, 'Lagrange', 2) # space for state and adjoint variables Vm = FunctionSpace(mesh, 'Lagrange', 1) # space for medium parameter Vme = FunctionSpace(mesh, 'Lagrange', 5) # sp for target med param # Define zero Boundary conditions: def u0_boundary(x, on_boundary): return on_boundary u0 = Constant("0.0") bc = DirichletBC(V, u0, u0_boundary) # Define target medium and rhs: mtrue_exp = Expression('1 + 7*(pow(pow(x[0] - 0.5,2) +' + \ ' pow(x[1] - 0.5,2),0.5) > 0.2)') mtrue = interpolate(mtrue_exp, Vme) f = Expression("1.0") # Compute target data: noisepercent = 0.05 # e.g., 0.02 = 2% noise level ObsOp = ObsEntireDomain({'V': V,'noise':noisepercent}) goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp) goal.update_m(mtrue) goal.solvefwd() UDnoise = goal.U # Solve reconstruction problem: Regul = LaplacianPrior({'Vm':Vm,'gamma':1e-3,'beta':1e-14}) ObsOp.noise = False InvPb = ObjFctalElliptic(V, Vm, bc, bc, [f], ObsOp, UDnoise, Regul, [], False) InvPb.update_m(1.0) # Set initial medium InvPb.solvefwd_cost() # Choose between steepest descent and Newton's method: METHODS = ['sd','Newt'] meth = METHODS[1] if meth == 'sd': alpha_init = 1e3 elif meth == 'Newt': alpha_init = 1.0 nbcheck = 0 # Grad and Hessian checks nbLS = 20 # Max nb of line searches # Prepare results outputs: PP = PostProcessor(meth, Vm, mtrue) PP.getResults0(InvPb) # Get results for index 0 (before first iteration) PP.printResults() # Start iteration: maxiter = 100 for it in range(1, maxiter+1): InvPb.solveadj_constructgrad() # Check gradient and Hessian: if nbcheck and (it == 1 or it % 10 == 0): checkgradfd(InvPb, nbcheck) checkhessfd(InvPb, nbcheck) # Compute search direction: if it == 1: gradnorm_init = InvPb.getGradnorm() if meth == 'Newt': if it == 1: maxtolcg = .5 else: maxtolcg = CGresults[3] else: maxtolcg = None CGresults = compute_searchdirection(InvPb, meth, gradnorm_init, maxtolcg) # Compute line search: LSresults = bcktrcklinesearch(InvPb, nbLS, alpha_init) InvPb.plotm(it) # Plot current medium reconstruction # Print results: PP.getResults(InvPb, LSresults, CGresults) PP.printResults() if PP.Stop(): break # Stopping criterion alpha_init = PP.alpha_init() # Initialize next alpha when using sd InvPb.gatherm() # Create one plot for all intermediate reconstructions if it == maxiter: print "Max nb of iterations reached."
return on_boundary u0 = Constant("0.0") bc = DirichletBC(V, u0, u0_boundary) # Define target medium and rhs: mtrue_exp = Expression("1 + 7*(pow(pow(x[0] - 0.5,2) +" + " pow(x[1] - 0.5,2),0.5) > 0.2)") mtrue = interpolate(mtrue_exp, Vme) f = Expression("1.0") if myrank == 0: print "Compute target data".format(myrank) obspts = [[ii / 5.0, jj / 5.0] for ii in range(1, 5) for jj in range(1, 5)] noisepercent = 0.10 # e.g., 0.02 = 2% noise level ObsOp = ObsPointwise({"V": V, "Points": obspts, "noise": noisepercent}, mycomm) goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp, [], [], [], False, mycomm) goal.update_m(mtrue) goal.solvefwd() UDnoise = goal.U if myrank == 0: print "Define prior and sample from prior" m0 = Function(Vm) m0.vector()[:] = 1.0 myprior = BilaplacianPrior({"Vm": Vm, "gamma": 10.0, "beta": 10.0, "m0": m0}) filename, ext = splitext(sys.argv[0]) if isdir(filename + "/"): rmtree(filename + "/") plotprior = PlotFenics(filename + "/Prior/") plotprior.set_varname("m_prior") for ii in range(10):
# Define zero Boundary conditions: def u0_boundary(x, on_boundary): return on_boundary u0 = Constant("0.0") bc = DirichletBC(V, u0, u0_boundary) # Define target medium and rhs: mtrue_exp = Expression('1 + 7*(pow(pow(x[0] - 0.5,2) +' + \ ' pow(x[1] - 0.5,2),0.5) > 0.2)') mtrue = interpolate(mtrue_exp, Vme) f = Expression("1.0") print 'p{}: Compute target data'.format(myrank) obspts = [[ii/5.,jj/5.] for ii in range(1,5) for jj in range(1,5)] noisepercent = 0.05 # e.g., 0.02 = 2% noise level ObsOp = ObsPointwise({'V': V, 'Points':obspts,'noise':noisepercent}, mycomm) goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp, [], [], [], False, mycomm) goal.update_m(mtrue) goal.solvefwd() UDnoise = goal.U print 'p{}: Solve reconstruction problem'.format(myrank) Regul = LaplacianPrior({'Vm':Vm,'gamma':2e-8,'beta':1e-14}) plot_option = False ObsOp.noise = False InvPb = ObjFctalElliptic(V, Vm, bc, bc, [f], ObsOp, UDnoise, Regul, \ [], plot_option, mycomm) InvPb.update_m(1.0) # Set initial medium InvPb.solvefwd_cost() # Choose between steepest descent and Newton's method: METHODS = ['sd','Newt'] meth = METHODS[1]
if mpirank == 0 and isdir(filename + '/'): rmtree(filename + '/') MPI.barrier(mpicomm) myplot = PlotFenics(filename) MPI.barrier(mpicomm) myplot.set_varname('m_target') myplot.plot_vtk(mtrue) myplot.set_varname('m_targetVm') myplot.plot_vtk(mtrueVm) else: myplot = None if mpirank == 0: print 'Compute noisy data' ObsOp = ObsEntireDomain({'V': V}, mpicomm) ObsOp.noise = False goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp, [], [], [], False, mpicomm) goal.update_m(mtrue) goal.solvefwd() # noise np.random.seed(11) noisepercent = 0.1 # e.g., 0.02 = 2% noise level UD = goal.U[0] rndnb = np.random.randn(UD.size) rndnb = rndnb / np.linalg.norm(rndnb) noiseres = noisepercent * np.linalg.norm(UD) UDnoise = UD + rndnb * noiseres if mpirank == 0: print 'noiseres={}, rndnb*noiseres={}'.format(\ MPI.sum(mpicomm, noiseres), MPI.sum(mpicomm, np.linalg.norm(rndnb*noiseres))) if PLOT: myplot.set_varname('u_target') myplot.plot_vtk(goal.u)
Vme = FunctionSpace(mesh, 'Lagrange', 5) # sp for target med param # Define zero Boundary conditions: def u0_boundary(x, on_boundary): return on_boundary u0 = Constant("0.0") bc = DirichletBC(V, u0, u0_boundary) # Define target medium and rhs: mtrue_exp = Expression('1 + 7*(pow(pow(x[0] - 0.5,2) +' + \ ' pow(x[1] - 0.5,2),0.5) > 0.2)') mtrue = interpolate(mtrue_exp, Vme) f = Expression("1.0") print 'p{}: Compute target data'.format(myrank) noisepercent = 0.00 # e.g., 0.02 = 2% noise level ObsOp = ObsEntireDomain({'V': V,'noise':noisepercent}, mycomm) goal = ObjFctalElliptic(V, Vme, bc, bc, [f], ObsOp, [], [], [], False, mycomm) goal.update_m(mtrue) goal.solvefwd() print 'p{}'.format(myrank) UDnoise = goal.U print 'p{}: Solve reconstruction problem'.format(myrank) Regul = LaplacianPrior({'Vm':Vm,'gamma':1e-5,'beta':1e-14}) ObsOp.noise = False InvPb = ObjFctalElliptic(V, Vm, bc, bc, [f], ObsOp, UDnoise, Regul, [], False, mycomm) InvPb.update_m(1.0) # Set initial medium InvPb.solvefwd_cost() # Choose between steepest descent and Newton's method: METHODS = ['sd','Newt'] meth = METHODS[1] if meth == 'sd': alpha_init = 1e3