def __init__(self, A, solver=default_solver, parameters={}): from PyTrilinos import Epetra, Amesos from dolfin import info from time import time self.A = A # Keep reference T = time() self.problem = Epetra.LinearProblem() self.problem.SetOperator(A.down_cast().mat()) self.solver = Amesos.Factory().Create(solver, self.problem) if (self.solver == None): raise RuntimeError('Failed to create Amesos solver: ' + solver) # This prevents a use-after-free crash for the MPI communicator. It # enforces that the solver is destroyed before A. self.solver.A = A self.solver.SetParameters(parameters) if self.solver is None: raise RuntimeError("Unknown solver '%s'" % solver) err = self.solver.SymbolicFactorization() if err != 0: raise RuntimeError("Amesos " + solver + " symbolic factorization failed, err=%d" % err) err = self.solver.NumericFactorization() if err != 0: raise RuntimeError("Amesos " + solver + " numeric factorization failed, err=%d" % err) info('constructed direct solver (using %s) in %.2f s' % (solver, time() - T))
def __init__(self, tolerance=1e-10, iterations=10, precon=None, maxIterations=10): """ :Parameters: - `tolerance`: The required error tolerance. - `iterations`: The maximum number of iterative steps to perform. """ iterations = min(iterations, maxIterations) TrilinosSolver.__init__(self, tolerance=tolerance, iterations=iterations, precon=None) if precon is not None: import warnings warnings.warn( "Trilinos KLU solver does not accept preconditioners.", UserWarning, stacklevel=2) self.Factory = Amesos.Factory()
def query(which=None): """Return list of available solver backends, or True/False if given a solver name""" from PyTrilinos import Amesos factory = Amesos.Factory() if which is None: avail = [] for s in serial_solvers + parallel_solvers: if factory.Query(s): avail.append(s) return avail return factory.Query(which)
def direct_solve(self, jac, rhs): '''Currently unused direct solver that was used for testing.''' A = Epetra.CrsMatrix(Epetra.Copy, self.map, 27) for i in range(len(jac.begA)-1): if i == self.dim: A[i, i] = -1 continue for j in range(jac.begA[i], jac.begA[i+1]): if jac.jcoA[j] != self.dim: A[i, jac.jcoA[j]] = jac.coA[j] A.FillComplete() rhs[self.dim] = 0 x = Vector(rhs) problem = Epetra.LinearProblem(A, x, rhs) factory = Amesos.Factory() solver = factory.Create('Klu', problem) solver.SymbolicFactorization() solver.NumericFactorization() solver.Solve() return x
def __init__(self, ckt): # Init base class _NLFunction.__init__(self) # Save ckt instance self.ckt = ckt # Make sure circuit is ready (analysis should take care) assert ckt.nD_ref # Allocate matrices/vectors # Create Epetra CrsMatrix: need communicator and map comm = Epetra.SerialComm() # Map: numelem, base index, communicator map = Epetra.Map(self.ckt.nD_dimension, 0, comm) # TODO: Find a good estimate for nnzRow nnzRow = min(10, self.ckt.nD_dimension) # G here is G1 = G + G0 in documentation self.G = Epetra.CrsMatrix(Epetra.Copy, map, nnzRow) # Jac is (G1 + di/dv) in doc self.Jac = Epetra.CrsMatrix(Epetra.Copy, map, nnzRow) # Allocate several vectors self.xVec = Epetra.Vector(map) self.iVec = Epetra.Vector(map) self.sVec = Epetra.Vector(map) self.errVec = Epetra.Vector(map) self.deltaxVec = Epetra.Vector(map) if hasattr(self.ckt, 'nD_namRClist'): # Allocate external currents vector self.extSVec = np.empty(self.ckt.nD_dimension) # Generate G and Jac sparse matrices # ---------------------------------- self._get_Gnz() self.Jacnz = [([], []) for i in xrange(self.ckt.nD_dimension)] # Insert linear contributions into G and Jac (to fix structure) for row, data in enumerate(self.Gnz): self.G.InsertGlobalValues(row, *data) self.Jac.InsertGlobalValues(row, *data) # Finalize G (which should not change any more unless parameter sweep) assert not self.G.FillComplete() assert not self.G.OptimizeStorage() # Add nonlinear contribution to Jac. Insert ones for now as the # matrix values will be discarded anyway. for elem in self.ckt.nD_nlinElem: outJac = np.ones((len(elem.csOutPorts), len(elem.controlPorts)), dtype=float) set_Jac(self.Jacnz, elem.nD_cpos, elem.nD_cneg, elem.nD_vpos, elem.nD_vneg, outJac) for row, data in enumerate(self.Jacnz): self.Jac.InsertGlobalValues(row, *data) # Finalize and symbolically factor Jac assert not self.Jac.FillComplete() assert not self.Jac.OptimizeStorage() # Create pytrilinos solver problem = Epetra.LinearProblem(self.Jac, self.deltaxVec, self.errVec) factory = Amesos.Factory() self.solver = factory.Create("Klu", problem) assert not self.solver.SymbolicFactorization()
# } # prec = ML.MultiLevelPreconditioner(P_epetra, False) # prec.SetParameterList(mlList) # prec.ComputePreconditioner() # solver = AztecOO.AztecOO(A_epetra, x_epetra, b_epetra) # solver.SetPrecOperator(prec) # solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres); # solver.SetAztecOption(AztecOO.AZ_output, 100); # err = solver.Iterate(20000, 1e-10) tic() problem = Epetra.LinearProblem(A_epetra, x_epetra, b_epetra) print '\n\n\n\n\n\n' factory = Amesos.Factory() solver = factory.Create("Amesos_Umfpack", problem) # solver = factory.Create("MUMPS", problem) amesosList = {"PrintTiming": True, "PrintStatus": True} solver.SetParameters(amesosList) solver.SymbolicFactorization() solver.NumericFactorization() solver.Solve() soln = problem.GetLHS() print "||x_computed||_2 =", soln.Norm2() # solver.PrintTiming() print '\n\n\n\n\n\n' if case == 1: ue = Expression(("20*x[0]*pow(x[1],3)", "5*pow(x[0],4)-5*pow(x[1],4)")) pe = Expression("60*pow(x[0],2)*x[1]-20*pow(x[1],3)+5")