def run_L2tracking_optimization(write_output=False): """ Test template for fsz.LevelsetFunctional.""" # tool for developing new tests, allows storing shape iterates if write_output: out = fd.File("domain.pvd") def cb(*args): out.write(Q.mesh_m.coordinates) cb() else: cb = None # setup problem mesh = fd.UnitSquareMesh(30, 30) Q = fs.FeControlSpace(mesh) inner = fs.ElasticityInnerProduct(Q) q = fs.ControlVector(Q, inner) # setup PDE constraint mesh_m = Q.mesh_m e = PoissonSolver(mesh_m) # create PDEconstrained objective functional J_ = L2trackingObjective(e, Q, cb=cb) J = fs.ReducedObjective(J_, e) # ROL parameters params_dict = { 'General': { 'Secant': { 'Type': 'Limited-Memory BFGS', 'Maximum Storage': 10 } }, 'Step': { 'Type': 'Line Search', 'Line Search': { 'Descent Method': { 'Type': 'Quasi-Newton Step' } }, }, 'Status Test': { 'Gradient Tolerance': 1e-4, 'Step Tolerance': 1e-5, 'Iteration Limit': 15 } } # assemble and solve ROL optimization problem params = ROL.ParameterList(params_dict, "Parameters") problem = ROL.OptimizationProblem(J, q) solver = ROL.OptimizationSolver(problem, params) solver.solve() # verify that the norm of the gradient at optimum is small enough state = solver.getAlgorithmState() assert (state.gnorm < 1e-4)
def test_equality_constraint(pytestconfig): mesh = fs.DiskMesh(0.05, radius=2.) Q = fs.FeControlSpace(mesh) inner = fs.ElasticityInnerProduct(Q, direct_solve=True) mesh_m = Q.mesh_m (x, y) = fd.SpatialCoordinate(mesh_m) q = fs.ControlVector(Q, inner) if pytestconfig.getoption("verbose"): out = fd.File("domain.pvd") def cb(*args): out.write(Q.mesh_m.coordinates) else: cb = None f = (pow(2 * x, 2)) + pow(y - 0.1, 2) - 1.2 J = fsz.LevelsetFunctional(f, Q, cb=cb) vol = fsz.LevelsetFunctional(fd.Constant(1.0), Q) e = fs.EqualityConstraint([vol]) emul = ROL.StdVector(1) params_dict = { 'Step': { 'Type': 'Augmented Lagrangian', 'Augmented Lagrangian': { 'Subproblem Step Type': 'Line Search', 'Penalty Parameter Growth Factor': 2., 'Initial Penalty Parameter': 1., 'Subproblem Iteration Limit': 20, }, 'Line Search': { 'Descent Method': { 'Type': 'Quasi-Newton Step' } }, }, 'General': { 'Secant': { 'Type': 'Limited-Memory BFGS', 'Maximum Storage': 5 } }, 'Status Test': { 'Gradient Tolerance': 1e-4, 'Step Tolerance': 1e-10, 'Iteration Limit': 10 } } params = ROL.ParameterList(params_dict, "Parameters") problem = ROL.OptimizationProblem(J, q, econ=e, emul=emul) solver = ROL.OptimizationSolver(problem, params) solver.solve() state = solver.getAlgorithmState() assert (state.gnorm < 1e-4) assert (state.cnorm < 1e-6)
R = 1.0 r = 0.5 print("Harmonic map exists for r^*/R^* = %.2f" % ((0.5 * (R / r + r / R))**-1)) Rs = 1.0 rs = args.rstar Q = fs.FeControlSpace(mesh) d = distance_function(Q.get_space_for_inner()[0].mesh(), boundary_ids=[1, 2]) if args.weighted: mu_base = 0.01 / (0.01 + d) else: mu_base = fd.Constant(1.) # mu_base = fd.Constant(1.0) if args.base_inner == "elasticity": inner = fs.ElasticityInnerProduct(Q, mu=mu_base, direct_solve=True) elif args.base_inner == "laplace": inner = fs.LaplaceInnerProduct(Q, mu=mu_base, direct_solve=True) else: raise NotImplementedError if args.alpha is not None: mu_cr = mu_base / args.alpha inner = CauchyRiemannAugmentation(mu_cr, inner) mesh_m = Q.mesh_m (x, y) = fd.SpatialCoordinate(mesh_m) r = fd.sqrt(x**2 + y**2) expr = (r - fd.Constant(rs)) * (r - fd.Constant(Rs)) J = 0.1 * fsz.LevelsetFunctional(expr, Q, quadrature_degree=5)
import firedrake as fd import fireshape as fs import fireshape.zoo as fsz import ROL dim = 2 mesh = fs.DiskMesh(0.4) Q = fs.FeMultiGridControlSpace(mesh, refinements=4, order=2) # Q = fs.FeControlSpace(mesh) # inner = fs.SurfaceInnerProduct(Q) inner = fs.ElasticityInnerProduct(Q) extension = fs.ElasticityExtension(Q.V_r, direct_solve=True) mesh_m = Q.mesh_m if dim == 2: (x, y) = fd.SpatialCoordinate(mesh_m) f = (pow(x, 2)) + pow(0.5 * y, 2) - 1. else: (x, y, z) = fd.SpatialCoordinate(mesh_m) f = (pow(x - 0.5, 2)) + pow(y - 0.5, 2) + pow(z - 0.5, 2) - 2. q = fs.ControlVector(Q, inner, boundary_extension=extension) out = fd.File("domain.pvd") J = fsz.LevelsetFunctional(f, Q, cb=lambda: out.write(mesh_m.coordinates)) J.cb() g = q.clone() J.update(q, None, 1) J.gradient(g, q, None) g.scale(-0.3) J.update(g, None, 1)
parser.add_argument("--base-inner", type=str, default="elasticity", choices=["elasticity", "laplace"]) parser.add_argument("--alpha", type=float, default=None) parser.add_argument("--clscale", type=float, default=0.1) parser.add_argument("--maxiter", type=int, default=50) args = parser.parse_args() mesh = fs.DiskMesh(args.clscale, radius=3, smooth=10) Q = fs.FeControlSpace(mesh) mu_base = fd.Constant(1.0) if args.base_inner == "elasticity": inner = fs.ElasticityInnerProduct(Q, mu=mu_base, direct_solve=True) elif args.base_inner == "laplace": inner = fs.LaplaceInnerProduct(Q, mu=mu_base, direct_solve=True) else: raise NotImplementedError if args.alpha is not None: mu_cr = mu_base / args.alpha inner = CauchyRiemannAugmentation(mu_cr, inner) mesh_m = Q.mesh_m (x, y) = fd.SpatialCoordinate(mesh_m) a = 0.8 b = 2.0
parser = argparse.ArgumentParser() parser.add_argument("--use_cr", type=int, default=0) parser.add_argument("--base_inner", type=str, default="elasticity") args = parser.parse_args() use_cr = bool(args.use_cr) base_inner = args.base_inner mesh = fd.Mesh("Sphere2D.msh") Q = fs.FeControlSpace(mesh) d = distance_function(Q.get_space_for_inner()[0].mesh(), eps=fd.Constant(0.1)) mu_base = 0.01 / (0.01 + d) if base_inner == "elasticity": inner = fs.ElasticityInnerProduct(Q, fixed_bids=[1, 2, 3], mu=mu_base, direct_solve=True) elif base_inner == "laplace": inner = fs.LaplaceInnerProduct(Q, fixed_bids=[1, 2, 3], mu=mu_base, direct_solve=True) else: raise NotImplementedError if use_cr: mu_cr = 100.0 * mu_base inner = CauchyRiemannAugmentation(mu_cr, inner) mesh_m = Q.mesh_m (x, y) = fd.SpatialCoordinate(mesh_m)
import firedrake as fd import fireshape as fs import fireshape.zoo as fsz import ROL mesh = fd.Mesh("Sphere2D.msh") # Q = fs.FeControlSpace(mesh) Q = fs.FeMultiGridControlSpace(mesh, refinements=1, order=1) inner = fs.ElasticityInnerProduct(Q, fixed_bids=[1, 2, 3]) mesh_m = Q.mesh_m (x, y) = fd.SpatialCoordinate(mesh_m) inflow_expr = fd.Constant((1.0, 0.0)) e = fsz.StokesSolver(mesh_m, inflow_bids=[1, 2], inflow_expr=inflow_expr, noslip_bids=[4]) e.solve() out = fd.File("u.pvd") def cb(*args): out.write(e.solution.split()[0]) cb() Je = fsz.EnergyObjective(e, Q, cb=cb) Jr = 1e-2 * fs.ReducedObjective(Je, e) Js = fsz.MoYoSpectralConstraint(10., fd.Constant(0.7), Q)