コード例 #1
0
class ElasticitySolver:
    """docstring for ElasticitySolver"""
    def __init__(self, energy, state, bcs, parameters={}):
        super(ElasticitySolver, self).__init__()
        solver_name = 'elasticity'
        self.problem = ElasticityProblem(energy, state, bcs)
        # Set the solver
        self.solver = PETScSNESSolver()
        snes = self.solver.snes()

        prefix = "elasticity_"
        snes.setOptionsPrefix(prefix)
        print(parameters)
        for parameter, value in parameters.items():
            log(LogLevel.DEBUG,
                "DEBUG: Set: {} = {}".format(prefix + parameter, value))
            PETScOptions.set(prefix + parameter, value)

        snes.setFromOptions()

    def solve(self):
        log(LogLevel.INFO,
            '________________________ EQUILIBRIUM _________________________')
        log(LogLevel.INFO, 'Solving elasticity')
        problem = self.problem

        u = problem.state["u"].vector()
        self.solver.solve(problem, u)
        return (self.solver.snes().getIterationNumber(),
                self.solver.snes().getConvergedReason())
コード例 #2
0
    def set_solver(self):
        solver = PETScSNESSolver()
        snes = solver.snes()
        snes.setType("newtontr")
        ksp = snes.getKSP()
        ksp.setType("preonly")
        pc = ksp.getPC()
        pc.setType("lu")
        snes_solver_parameters = {
            # "linear_solver": "lu",
            "linear_solver": "umfpack",
            # "linear_solver": "mumps",
            "maximum_iterations": 300,
            "report": True,
            # "monitor": True,
            "line_search": "basic",
            "method": "newtonls",
            "absolute_tolerance": 1e-5,
            "relative_tolerance": 1e-5,
            "solution_tolerance": 1e-5
        }

        solver.parameters.update(snes_solver_parameters)
        info(solver.parameters, True)
        # import pdb; pdb.set_trace()
        return solver
コード例 #3
0
    def __init__(self, problem, parameters={}, lb=None):
        super(DamageSolverSNES, self).__init__()
        self.problem = problem
        self.energy = problem.energy
        self.state = problem.state
        self.alpha = problem.state['alpha']
        self.alpha_dvec = as_backend_type(self.alpha.vector())
        self.alpha_pvec = self.alpha_dvec.vec()

        self.bcs = problem.bcs
        self.parameters = parameters
        comm = self.alpha.function_space().mesh().mpi_comm()
        self.comm = comm
        V = self.alpha.function_space()
        self.V = V
        self.Ealpha = derivative(
            self.energy, self.alpha,
            dolfin.TestFunction(self.alpha.ufl_function_space()))
        self.dm = self.alpha.function_space().dofmap()
        solver = PETScSNESSolver()
        snes = solver.snes()

        if lb == None:
            lb = interpolate(Constant(0.), V)
        ub = interpolate(Constant(1.), V)

        prefix = "damage_"
        snes.setOptionsPrefix(prefix)
        for option, value in self.parameters["snes"].items():
            PETScOptions.set(prefix + option, value)
            log(LogLevel.DEBUG,
                "DEBUG: Set: {} = {}".format(prefix + option, value))

        snes.setFromOptions()

        (J, F, bcs_alpha) = (problem.J, problem.F, problem.bcs)
        self.ass = SystemAssembler(J, F, bcs_alpha)
        self.b = self.init_residual()
        snes.setFunction(self.residual, self.b.vec())
        self.A = self.init_jacobian()
        snes.setJacobian(self.jacobian, self.A.mat())
        snes.ksp.setOperators(self.A.mat())

        snes.setVariableBounds(self.problem.lb.vec(), self.problem.ub.vec())  #

        # snes.solve(None, Function(V).vector().vec())

        self.solver = snes
コード例 #4
0
    def set_solver_u(self):
        for option, value in self.parameters["solver_u"].items():
            print("setting ", option, value)
            PETScOptions.set(option, value)
        solver = PETScSNESSolver()
        snes = solver.snes()
        snes.setOptionsPrefix("u_")
        snes.setType(self.parameters["solver_u"]["u_snes_type"])
        ksp = snes.getKSP()
        ksp.setType("preonly")
        pc = ksp.getPC()
        pc.setType("lu")

        # Namespace mismatch between petsc4py 3.7 and 3.9.
        if hasattr(pc, 'setFactorSolverType'):
            pc.setFactorSolverType("mumps")
        elif hasattr(pc, 'setFactorSolverPackage'):
            pc.setFactorSolverPackage('mumps')
        else:
            ColorPrint.print_warn('Could not configure preconditioner')
        solver.set_from_options()
        snes.setFromOptions()
        self.solver_u = solver