Esempio n. 1
0
    def matvec(self, b):
        from PyTrilinos import AztecOO
        from dolfin import GenericVector
        if not isinstance(b, GenericVector):
            return NotImplemented
        x = self.A.create_vec(dim=1)
        if len(x) != len(b):
            raise RuntimeError(
                'incompatible dimensions for AztecOO matvec, %d != %d'%(len(x),len(b)))

        solver = AztecOO.AztecOO(self.A.down_cast().mat(), x.down_cast().vec(), b.down_cast().vec())
        #solver.SetAztecDefaults()
        solver.SetAztecOption(AztecOO.AZ_solver, self.solver)
        if self.precond:
            if hasattr(self.precond, 'down_cast'):
                solver.SetPrecOperator(self.precond.down_cast())
            else:
                # doesn't seem to work very well
                solver.SetAztecOption(AztecOO.AZ_precond, self.precond)
                # the following are from the example with precond='dom_decomp'
                solver.SetAztecOption(AztecOO.AZ_subdomain_solve, AztecOO.AZ_ilu)
                solver.SetAztecOption(AztecOO.AZ_overlap, 1)
                solver.SetAztecOption(AztecOO.AZ_graph_fill, 1)

        solver.SetAztecOption(AztecOO.AZ_output, 0)
        solver.Iterate(self.maxiter, self.tolerance)
        return x
Esempio n. 2
0
def iterative_solver(List, Matrix, InputLHS, RHS, Prec):

  LHS = Epetra.MultiVector(InputLHS)
  
  Time = Epetra.Time(Matrix.Comm())

  hasConditionNumber = False;

  Solver = AztecOO.AztecOO(Matrix, LHS, RHS)
  Solver.SetPrecOperator(Prec)
  if (List['az_solver'] == "AZ_gmres"):
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres);
  elif List['az_solver'] == "AZ_cg":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cg);
  elif List['az_solver'] == "AZ_cg_condnum":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cg_condnum);
    hasConditionNumber = True
  elif List['az_solver'] == "AZ_gmres_condnum":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres_condnum);
    hasConditionNumber = True
  elif List['az_solver'] == "AZ_bicgstab":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_bicgstab);
  elif List['az_solver'] == "AZ_tfqmr":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_tfqmr);
  elif List['az_solver'] == "AZ_cgs":
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cgs);
  else:
    print "Solver type not correct, ", List['az_solver']

  Solver.SetAztecOption(AztecOO.AZ_output, 16);
  if List['iters'] < 0 | List['iters'] > MAX_ITERATIONS:
    print "Maximum number of iterations either negative of > ", MAX_ITERATIONS;
    raise("PARAMETER_ERROR");
  if List['tol'] < 1e-12:
    print "Tolerance is too small"
    raise("PARAMETER_ERROR");

  if List['az_kspace'] > 0 & List['az_kspace'] <= MAX_KSPACE:
    Solver.SetAztecOption(AztecOO.AZ_kspace, List['az_kspace']);
  else:
    print "Krylov space dimension either negative of >", MAX_KSPACE
    print "You have", List['az_kspace']
    raise("PARAMETER_ERROR");

  if List['az_output'] == "16":
    Solver.SetAztecOption(AztecOO.AZ_output, 16)
  elif List['az_output'] == "32":
    Solver.SetAztecOption(AztecOO.AZ_output, 32)
  elif List['az_output'] == "AZ_last":
    Solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_last)
  elif List['az_output'] == "AZ_none":
    Solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_none)
    
  err = Solver.Iterate(List['iters'], List['tol']) 

  if hasConditionNumber:
    ConditionNumber = Solver.GetStatus(AztecOO.AZ_condnum)
  else:
    ConditionNumber = 0.0;
  return (err, Solver.NumIters(), Time.ElapsedTime(), ConditionNumber)
Esempio n. 3
0
    def solve_linear_problem(comm, A, b):
        """
        retorna a solucao do sistema linear Ax = b
        input:
            A: matriz do sistema
            b: termo fonte
        output:
            x:solucao
        """

        n = len(b)
        assert A.NumMyCols() == A.NumMyRows()
        assert A.NumMyCols() == n

        if A.Filled():
            pass
        else:
            A.FillComplete()

        std_map = Epetra.Map(n, 0, comm)

        x = Epetra.Vector(std_map)

        linearProblem = Epetra.LinearProblem(A, x, b)
        solver = AztecOO.AztecOO(linearProblem)
        solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_warnings)
        solver.Iterate(10000, 1e-14)

        return x
 def solve_linear_problem(self, A, b, x=None, its=1000, tolerance=1e-10):
     '''
     resolve o problema Ax = b
     input:
         A: matriz quadrada esparsa do scipy
         b = termo fonte
         x: chute inicial
         its: numero maximo de iteracoes
         tolerance: tolerancia para o residuo
     output:
         res: informa se o residuo foi menor que a tolerancia
         x2: vetor resposta
     '''
     comm = self._comm
     n = len(b)
     std_map = Epetra.Map(n, 0, comm)
     x2 = Epetra.Vector(std_map)
     if x:
         x2[:] = x[:]
     b2 = Epetra.Vector(std_map)
     b2[:] = b[:]
     A2 = Epetra.CrsMatrix(Epetra.Copy, std_map, 7)
     indices = sp.find(A)
     A2.InsertGlobalValues(indices[0], indices[1], indices[2])
     irr = A2.FillComplete()
     linearProblem = Epetra.LinearProblem(A2, x2, b2)
     solver = AztecOO.AztecOO(linearProblem)
     solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_warnings)
     # solver.SetParameters(self._params)
     solver.Iterate(its, tolerance)
     x2 = np.array(x2)
     res = solver.ScaledResidual() < tolerance
     return x2
Esempio n. 5
0
    def solve(self):

        # parallel linear solver
        # final project has 2D differential equation
        linearProblem = Epetra.LinearProblem(self.A, self.x, self.b)
        solver = AztecOO.AztecOO(linearProblem)
        solver.Iterate(10000, 1.e-5)

        return
Esempio n. 6
0
def trilinos_solve(A, rhs, prec, x=None, tol=1e-5):
    solver = AztecOO.AztecOO(A, x, rhs)
    solver.SetPrecOperator(prec)
    solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    solver.SetAztecOption(AztecOO.AZ_conv, AztecOO.AZ_rhs)
    solver.SetAztecOption(AztecOO.AZ_output, 0)

    solver.Iterate(3000, tol)

    return x
def solve_system(operator, rhs, process_maps, tol=1E-5, prec=None):
    """Solve the global system with GMRES"""

    from PyTrilinos import AztecOO
    x = Epetra.MultiVector(process_maps.single_proc_map, 1)
    solver = AztecOO.AztecOO(operator, x, rhs)
    solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    if prec is None:
        solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_none)
    else:
        solver.SetPrecOperator(P)
    solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_last)
    solver.Iterate(500, tol)
    return x
Esempio n. 8
0
    def setup_linear_problem(self, A_name, b_name):
        """Sets a linear problem. The `A_name` and `b_name` parameters are
        related to the matrix and vectors names to be used in a linear problem
        Ax = b.

        """
        self.A = self.mesh.matrix_manager.get_matrix(A_name)
        self.b = self.mesh.matrix_manager.get_vector(b_name)

        self.x = Epetra.Vector(
            self.mesh.matrix_manager.std_map[self.solution_dim])

        self.linearProblem = Epetra.LinearProblem(self.A, self.x, self.b)
        self.solver = AztecOO.AztecOO(self.linearProblem)
        self.solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_last)
Esempio n. 9
0
    def solve_linear_problem(self, A, b, n):

        if A.Filled():
            pass
        else:
            A.FillComplete()

        std_map = Epetra.Map(n, 0, self.comm)

        x = Epetra.Vector(std_map)

        linearProblem = Epetra.LinearProblem(A, x, b)
        solver = AztecOO.AztecOO(linearProblem)
        solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_warnings)
        solver.Iterate(10000, 1e-14)

        return x
Esempio n. 10
0
    def _solve_(self, L, x, b):

        Solver = AztecOO.AztecOO(L, x, b)
        Solver.SetAztecOption(AztecOO.AZ_solver, self.solver)

        ##        Solver.SetAztecOption(AztecOO.AZ_kspace, 30)

        Solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_none)

        if self.preconditioner is not None:
            self.preconditioner._applyToSolver(solver=Solver, matrix=L)
        else:
            Solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_none)

        output = Solver.Iterate(self.iterations, self.tolerance)

        if self.preconditioner is not None:
            if hasattr(self.preconditioner, 'Prec'):
                del self.preconditioner.Prec

        if 'FIPY_VERBOSE_SOLVER' in os.environ:
            status = Solver.GetAztecStatus()

            from fipy.tools.debug import PRINT
            PRINT('iterations: %d / %d' %
                  (status[AztecOO.AZ_its], self.iterations))
            failure = {
                AztecOO.AZ_normal: 'AztecOO.AZ_normal',
                AztecOO.AZ_param: 'AztecOO.AZ_param',
                AztecOO.AZ_breakdown: 'AztecOO.AZ_breakdown',
                AztecOO.AZ_loss: 'AztecOO.AZ_loss',
                AztecOO.AZ_ill_cond: 'AztecOO.AZ_ill_cond',
                AztecOO.AZ_maxits: 'AztecOO.AZ_maxits'
            }

            PRINT('failure', failure[status[AztecOO.AZ_why]])

            PRINT('AztecOO.AZ_r:', status[AztecOO.AZ_r])
            PRINT('AztecOO.AZ_scaled_r:', status[AztecOO.AZ_scaled_r])
            PRINT('AztecOO.AZ_rec_r:', status[AztecOO.AZ_rec_r])
            PRINT('AztecOO.AZ_solve_time:', status[AztecOO.AZ_solve_time])
            PRINT('AztecOO.AZ_Aztec_version:',
                  status[AztecOO.AZ_Aztec_version])

        return output
Esempio n. 11
0
def solve_aztec(A, rhs, x=None, tol=1e-5, plist=None):
    if x is None:
        x = Epetra.Vector(A.RangeMap())

    if plist is None:
        plist = {"Solver": "bicgstab",
                 "Precond": "Dom_Decomp",
                 "subdomain_solve": "ilu",
                 "Output": 0
                 }

    solver = AztecOO.AztecOO(A, x, rhs)
    solver.SetParameters(plist, True)
    solver.SetAztecOption(AztecOO.AZ_output, 0)
    solver.SetAztecOption(AztecOO.AZ_conv, AztecOO.AZ_rhs)

    ierr = solver.Iterate(300000, tol)
    assert (ierr == 0) or (ierr == -3), ierr
    return x
Esempio n. 12
0
    def solve(self, op, rhs, tol, maxit):
        if op.dtype.char != op.dtype.char.upper():
            # Real case
            if abs(op.alpha.real) < abs(op.alpha.imag):
                op.alpha = op.alpha.imag
            else:
                op.alpha = op.alpha.real
            op.beta = op.beta.real

        epetra_op = Operator(op)
        x = Vector(rhs)
        solver = AztecOO.AztecOO(epetra_op, x, rhs)
        solver.SetParameters({
            "Solver": "GMRES",
            "Precond": "None",
            "Output": -3
        })  # Warnings. See az_aztec_defs.h
        info = solver.Iterate(maxit, tol)
        if info < 0:
            raise Exception('AztecOO returned ' + str(info))
        elif info > 0 and maxit > 1:
            warnings.warn('GMRES did not converge in ' +
                          str(solver.NumIters()) + ' iterations')
        return x
Esempio n. 13
0
    def run_solver(self, interpolation_method):
        """Run solver."""
        self.interpolation_method = interpolation_method
        t0 = time.time()
        n_vertex = len(set(self.mesh_data.all_nodes) - self.dirichlet_nodes)
        print("interpolation runing...")
        self.get_nodes_weights(interpolation_method)
        print(
            "done interpolation...",
            "took {0} seconds to interpolate over {1} verts".format(
                time.time() - t0, n_vertex
            ),
        )
        print("filling the transmissibility matrix...")
        begin = time.time()

        try:
            for volume in self.volumes:
                volume_id = self.mb.tag_get_data(self.global_id_tag, volume)[
                    0
                ][0]
                RHS = self.mb.tag_get_data(self.source_tag, volume)[0][0]
                self.Q[volume_id] += RHS
                # self.Q[volume_id, 0] += RHS
        except Exception:
            pass

        for face in self.neumann_faces:
            face_flow = self.mb.tag_get_data(self.neumann_tag, face)[0][0]
            volume = self.mtu.get_bridge_adjacencies(face, 2, 3)
            volume = np.asarray(volume, dtype="uint64")
            id_volume = self.mb.tag_get_data(self.global_id_tag, volume)[0][0]
            face_nodes = self.mtu.get_bridge_adjacencies(face, 0, 0)
            node_crds = self.mb.get_coords(face_nodes).reshape([3, 3])
            face_area = geo._area_vector(node_crds, norma=True)
            RHS = face_flow * face_area
            self.Q[id_volume] += -RHS
            # self.Q[id_volume, 0] += - RHS

        id_volumes = []
        all_LHS = []
        for face in self.dirichlet_faces:
            # '2' argument was initially '0' but it's incorrect
            I, J, K = self.mtu.get_bridge_adjacencies(face, 2, 0)

            left_volume = np.asarray(
                self.mtu.get_bridge_adjacencies(face, 2, 3), dtype="uint64"
            )
            id_volume = self.mb.tag_get_data(self.global_id_tag, left_volume)[
                0
            ][0]
            id_volumes.append(id_volume)

            JI = self.mb.get_coords([I]) - self.mb.get_coords([J])
            JK = self.mb.get_coords([K]) - self.mb.get_coords([J])
            LJ = (
                self.mb.get_coords([J])
                - self.mesh_data.mb.tag_get_data(
                    self.volume_centre_tag, left_volume
                )[0]
            )
            N_IJK = np.cross(JI, JK) / 2.0
            _test = np.dot(LJ, N_IJK)
            if _test < 0.0:
                I, K = K, I
                JI = self.mb.get_coords([I]) - self.mb.get_coords([J])
                JK = self.mb.get_coords([K]) - self.mb.get_coords([J])
                N_IJK = np.cross(JI, JK) / 2.0
            tan_JI = np.cross(N_IJK, JI)
            tan_JK = np.cross(N_IJK, JK)
            self.mb.tag_set_data(self.normal_tag, face, N_IJK)

            face_area = np.sqrt(np.dot(N_IJK, N_IJK))
            h_L = geo.get_height(N_IJK, LJ)

            g_I = self.get_boundary_node_pressure(I)
            g_J = self.get_boundary_node_pressure(J)
            g_K = self.get_boundary_node_pressure(K)

            K_L = self.mb.tag_get_data(self.perm_tag, left_volume).reshape(
                [3, 3]
            )
            K_n_L = self.vmv_multiply(N_IJK, K_L, N_IJK)
            K_L_JI = self.vmv_multiply(N_IJK, K_L, tan_JI)
            K_L_JK = self.vmv_multiply(N_IJK, K_L, tan_JK)

            D_JK = self.get_cross_diffusion_term(
                tan_JK, LJ, face_area, h_L, K_n_L, K_L_JK, boundary=True
            )
            D_JI = self.get_cross_diffusion_term(
                tan_JI, LJ, face_area, h_L, K_n_L, K_L_JI, boundary=True
            )
            K_eq = (1 / h_L) * (face_area * K_n_L)

            RHS = D_JK * (g_I - g_J) - K_eq * g_J + D_JI * (g_J - g_K)
            LHS = K_eq
            all_LHS.append(LHS)

            self.Q[id_volume] += -RHS
            # self.Q[id_volume, 0] += - RHS
            # self.mb.tag_set_data(self.flux_info_tag, face,
            #                      [D_JK, D_JI, K_eq, I, J, K, face_area])

        all_cols = []
        all_rows = []
        all_values = []
        self.ids = []
        self.v_ids = []
        self.ivalues = []
        for face in self.intern_faces:
            left_volume, right_volume = self.mtu.get_bridge_adjacencies(
                face, 2, 3
            )
            L = self.mesh_data.mb.tag_get_data(
                self.volume_centre_tag, left_volume
            )[0]
            R = self.mesh_data.mb.tag_get_data(
                self.volume_centre_tag, right_volume
            )[0]
            dist_LR = R - L
            I, J, K = self.mtu.get_bridge_adjacencies(face, 0, 0)
            JI = self.mb.get_coords([I]) - self.mb.get_coords([J])
            JK = self.mb.get_coords([K]) - self.mb.get_coords([J])

            N_IJK = np.cross(JI, JK) / 2.0
            test = np.dot(N_IJK, dist_LR)

            if test < 0:
                left_volume, right_volume = right_volume, left_volume
                L = self.mesh_data.mb.tag_get_data(
                    self.volume_centre_tag, left_volume
                )[0]
                R = self.mesh_data.mb.tag_get_data(
                    self.volume_centre_tag, right_volume
                )[0]
                dist_LR = R - L

            face_area = np.sqrt(np.dot(N_IJK, N_IJK))
            tan_JI = np.cross(N_IJK, JI)
            tan_JK = np.cross(N_IJK, JK)

            K_R = self.mb.tag_get_data(self.perm_tag, right_volume).reshape(
                [3, 3]
            )
            RJ = R - self.mb.get_coords([J])
            h_R = geo.get_height(N_IJK, RJ)

            K_R_n = self.vmv_multiply(N_IJK, K_R, N_IJK)
            K_R_JI = self.vmv_multiply(N_IJK, K_R, tan_JI)
            K_R_JK = self.vmv_multiply(N_IJK, K_R, tan_JK)

            K_L = self.mb.tag_get_data(self.perm_tag, left_volume).reshape(
                [3, 3]
            )

            LJ = L - self.mb.get_coords([J])
            h_L = geo.get_height(N_IJK, LJ)

            K_L_n = self.vmv_multiply(N_IJK, K_L, N_IJK)
            K_L_JI = self.vmv_multiply(N_IJK, K_L, tan_JI)
            K_L_JK = self.vmv_multiply(N_IJK, K_L, tan_JK)

            D_JI = self.get_cross_diffusion_term(
                tan_JI,
                dist_LR,
                face_area,
                h_L,
                K_L_n,
                K_L_JI,
                h_R,
                K_R_JI,
                K_R_n,
            )
            D_JK = self.get_cross_diffusion_term(
                tan_JK,
                dist_LR,
                face_area,
                h_L,
                K_L_n,
                K_L_JK,
                h_R,
                K_R_JK,
                K_R_n,
            )

            K_eq = (K_R_n * K_L_n) / (K_R_n * h_L + K_L_n * h_R) * face_area

            id_right = self.mb.tag_get_data(self.global_id_tag, right_volume)
            id_left = self.mb.tag_get_data(self.global_id_tag, left_volume)

            col_ids = [id_right, id_right, id_left, id_left]
            row_ids = [id_right, id_left, id_left, id_right]
            values = [K_eq, -K_eq, K_eq, -K_eq]
            all_cols.append(col_ids)
            all_rows.append(row_ids)
            all_values.append(values)
            # wait for interpolation to be done
            self._node_treatment(I, id_left, id_right, K_eq, D_JK=D_JK)
            self._node_treatment(
                J, id_left, id_right, K_eq, D_JI=D_JI, D_JK=-D_JK
            )
            self._node_treatment(K, id_left, id_right, K_eq, D_JI=-D_JI)
            # self.mb.tag_set_data(self.flux_info_tag, face,
            #                      [D_JK, D_JI, K_eq, I, J, K, face_area])

        self.T.InsertGlobalValues(self.ids, self.v_ids, self.ivalues)
        # self.T[
        #     np.asarray(self.ids)[:, :, 0, 0], np.asarray(self.v_ids)
        # ] = np.asarray(self.ivalues)
        self.T.InsertGlobalValues(id_volumes, id_volumes, all_LHS)
        # self.T[
        #     np.asarray(id_volumes), np.asarray(id_volumes)
        # ] = np.asarray(all_LHS)
        self.T.InsertGlobalValues(all_cols, all_rows, all_values)
        # self.T[
        #     np.asarray(all_cols)[:, 0, 0, 0],
        #     np.asarray(all_rows)[:, 0, 0, 0]
        # ] = np.asarray(all_values)[:, 0]
        self.T.FillComplete()
        mat_fill_time = time.time() - begin
        print("matrix fill took {0} seconds...".format(mat_fill_time))
        mesh_size = len(self.volumes)
        print("running solver...")
        USE_DIRECT_SOLVER = False
        linearProblem = Epetra.LinearProblem(self.T, self.x, self.Q)
        if USE_DIRECT_SOLVER:
            solver = Amesos.Lapack(linearProblem)
            print("1) Performing symbolic factorizations...")
            solver.SymbolicFactorization()
            print("2) Performing numeric factorizations...")
            solver.NumericFactorization()
            print("3) Solving the linear system...")
            solver.Solve()
            t = time.time() - t0
            print(
                "Solver took {0} seconds to run over {1} volumes".format(
                    t, mesh_size
                )
            )
        else:
            solver = AztecOO.AztecOO(linearProblem)
            solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
            solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_none)
            # solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_Jacobi)
            # solver.SetAztecOption(AztecOO.AZ_kspace, 1251)
            # solver.SetAztecOption(AztecOO.AZ_orthog, AztecOO.AZ_modified)
            # solver.SetAztecOption(AztecOO.AZ_conv, AztecOO.AZ_Anorm)
            solver.Iterate(8000, 1e-10)
            t = time.time() - t0
            its = solver.GetAztecStatus()[0]
            solver_time = solver.GetAztecStatus()[6]
            print(
                "Solver took {0} seconds to run over {1} volumes".format(
                    t, mesh_size
                )
            )
            print(
                "Solver converged at %.dth iteration in %3f seconds."
                % (int(its), solver_time)
            )
        # self.T = self.T.tocsc()
        # self.Q = self.Q.tocsc()
        # self.x = spsolve(self.T, self.Q)
        # print(np.sum(self.T[50]), self.Q[50])
        self.mb.tag_set_data(self.pressure_tag, self.volumes, self.x)
Esempio n. 14
0
def runit(size, prec, diff):
    # builds the linear system matrix and sets up starting solution and
    # right-hand side
    Comm = Epetra.PyComm()

    print "SIZE = ", size
    print "PREC = ", prec
    print "DIFF = ", diff

    GaleriList = {
        "nx": size,
        "ny": size,
        "mx": 1,
        "my": 1,
        "conv": 1.0,
        "diff": diff
    }

    Map = Galeri.CreateMap("Cartesian2D", Comm, GaleriList)
    Matrix = Galeri.CreateCrsMatrix("BentPipe2D", Map, GaleriList)

    Util = Epetra.Util()
    Util.SetSeed(0)
    LHS = Epetra.Vector(Map)
    RHS = Epetra.Vector(Map)
    n = LHS.MyLength()
    for i in xrange(0, n):
        LHS[i] = sin(i * 3.1415 / n) * sin(i * 3.1415 / n)
    Matrix.Multiply(False, LHS, RHS)
    LHS.PutScalar(0.0)

    # sets up the parameters for ML using a python dictionary
    if prec == "SA":
        MLList = {
            "max levels": 10,
            "output": 10,
            "increasing or decreasing": "increasing",
            "aggregation: type": "Uncoupled-MIS",
            "smoother: type (level 0)": "symmetric Gauss-Seidel",
            "smoother: damping factor": 0.67,
            "smoother: sweeps": 1,
            "smoother: pre or post": "both",
            "PDE equations": 1,
            "aggregation: damping factor": 1.333,
            "eigen-analysis: type": "power-method"
        }
    elif prec == "NSA":
        MLList = {
            "max levels": 10,
            "output": 10,
            "increasing or decreasing": "increasing",
            "aggregation: type": "Uncoupled-MIS",
            "smoother: type (level 0)": "symmetric Gauss-Seidel",
            "smoother: damping factor": 0.67,
            "smoother: sweeps": 1,
            "smoother: pre or post": "both",
            "PDE equations": 1,
            "aggregation: damping factor": 0.0000,
            "eigen-analysis: type": "power-method"
        }
    elif prec == "NSR":
        MLList = {
            "max levels": 10,
            "output": 10,
            "increasing or decreasing": "increasing",
            "aggregation: type": "Uncoupled-MIS",
            "smoother: type (level 0)": "symmetric Gauss-Seidel",
            "smoother: damping factor": 0.67,
            "smoother: sweeps": 1,
            "smoother: pre or post": "both",
            "PDE equations": 1,
            "aggregation: use tentative restriction": True,
            "aggregation: damping factor": 1.0000,
            "eigen-analysis: type": "power-method"
        }
    elif prec == "EMIN":
        MLList = {
            "max levels": 10,
            "output": 10,
            "increasing or decreasing": "increasing",
            "aggregation: type": "Uncoupled-MIS",
            "smoother: type (level 0)": "symmetric Gauss-Seidel",
            "smoother: damping factor": 0.67,
            "smoother: sweeps": 1,
            "smoother: pre or post": "both",
            "PDE equations": 1,
            "energy minimization: enable": True,
            "energy minimization: type": 2
        }

    # creates the preconditioner and computes it
    Prec = ML.MultiLevelPreconditioner(Matrix, False)
    Prec.SetParameterList(MLList)
    Prec.ComputePreconditioner()

    # sets up the solver, specifies Prec as preconditioner, and
    # solves using CG.
    Solver = AztecOO.AztecOO(Matrix, LHS, RHS)
    Solver.SetPrecOperator(Prec)
    Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    Solver.SetAztecOption(AztecOO.AZ_kspace, 30)
    Solver.SetAztecOption(AztecOO.AZ_output, 32)
    Solver.Iterate(400, 1e-8)

    del Prec
Esempio n. 15
0
    # b_epetra = NullSpace(bb,'vec')
    # x_epetra = NullSpace(x.vector(),'vec')
    print '\n\n\n DoF = ', Nb[0], '\n\n\n'
    DoF[xx - 1] = Nb[0] - 1
    mlList = {
        "max levels": 200,
        "output": 10,
        "smoother: type": "symmetric Gauss-Seidel",
        "aggregation: type": "Uncoupled"
    }

    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(1550, 1e-5)

    print 'done'

    # Ap = sp.save(A)

    # SaveEpertaMatrix(A,"A")
    # SaveEpertaMatrix(P.down_cast().mat(),"P")
    # tic()

    # solve(a==L1,uu,bcs)
    # # solver = KrylovSolver("gmres", "amg")
Esempio n. 16
0
    def iterative_solve(self,
                        A,
                        rhs,
                        x0,
                        tol=1.e-5,
                        max_iter=200,
                        n_smooth=10,
                        smoother="gmres",
                        conv_history=False,
                        with_multiscale=True,
                        adapt_smoothing=True,
                        verbose=False):

        history = dict()
        history["n_smooth"] = []
        history["residual"] = []

        assert smoother in ["gmres", "ilu", "jacobi"]

        rhs_sum = rhs.Comm().SumAll(np.sum(rhs[:]))
        if not abs(rhs_sum) < abs(rhs.NormInf() * 1e-8):
            logger.warn("sum of rhs does not equal to zero")

        residual = Epetra.Vector(A.RangeMap())

        ref_pressure = Epetra.Vector(A.RangeMap())
        ref_residual = Epetra.Vector(A.RangeMap())

        temp = Epetra.Vector(A.RangeMap())
        A_diagonal = Epetra.Vector(A.RangeMap())
        A.ExtractDiagonalCopy(A_diagonal)

        ref_pressure[:] = rhs[:] / A_diagonal[:]
        A.Multiply(False, ref_pressure, temp)
        ref_residual[:] = rhs[:] - temp[:]
        ref_residual_norm = ref_residual.NormInf()

        AP = mat_multiply(A, self.P)

        RAP_msfv = mat_multiply(self.R, AP, transpose_1=True)
        RAP_msfe = mat_multiply(self.P, AP, transpose_1=True)

        residual = self.__compute_residual(A, rhs, x0, residual)

        error = Epetra.Vector(self.P.RangeMap())
        if with_multiscale:
            error = self.__solve_one_step(residual, RAP_msfv, self.R)
            x0[:] += error[:]

        if smoother == "ilu":
            ilu = IFPACK.ILU(self.A)
            ilu.Initialize()
            ilu.Compute()

        residual_prev_norm = 1.e50

        solver = AztecOO.AztecOO()
        if smoother == "ilu":
            solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_fixed_pt)
            solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_dom_decomp)
            solver.SetAztecOption(AztecOO.AZ_subdomain_solve, AztecOO.AZ_ilu)

        if smoother == "gmres":
            solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
            solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_dom_decomp)
            solver.SetAztecOption(AztecOO.AZ_subdomain_solve, AztecOO.AZ_ilu)

        if smoother == "jacobi":
            solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_fixed_pt)
            solver.SetAztecOption(AztecOO.AZ_precond, AztecOO.AZ_Jacobi)

        solver.SetAztecOption(AztecOO.AZ_conv, AztecOO.AZ_rhs)
        solver.SetAztecOption(AztecOO.AZ_output, 0)

        for iteration in xrange(max_iter):
            residual = self.__compute_residual(A, rhs, x0, residual)

            logger.debug("Residual at iteration %d: %g", iteration,
                         residual.NormInf()[0] / ref_residual_norm)

            error[:] = 0.0
            solver.Iterate(A, error, residual, n_smooth, 1e-20)
            x0[:] += error[:]

            residual = self.__compute_residual(A, rhs, x0, residual)

            if with_multiscale:
                error = self.__solve_one_step(residual, RAP_msfe, self.P)
                x0[:] += error[:]

            if residual.NormInf()[0] / ref_residual_norm < tol:
                logger.debug("Number of iterations for convergence: %d",
                             iteration)
                break

            if residual.NormInf(
            )[0] >= 1.01 * residual_prev_norm and adapt_smoothing:
                n_smooth = int(1.4 * n_smooth)
                logger.warn(
                    "Solver stagnated, increasing number of smoothing steps to %d",
                    n_smooth)

            residual_prev_norm = residual.NormInf()

            history["n_smooth"].append(n_smooth)
            history["residual"].append(residual_prev_norm[0] /
                                       ref_residual_norm)

            if verbose:
                print iteration, history["residual"][-1]

        residual = self.__compute_residual(A, rhs, x0, residual)
        error[:] = 0.0

        solver.Iterate(A, error, residual, n_smooth, 1e-20)
        x0[:] += error[:]

        residual = self.__compute_residual(A, rhs, x0, residual)
        error = self.__solve_one_step(residual, RAP_msfv, self.R)
        x0[:] += error[:]

        history["n_smooth"].append(n_smooth)
        history["residual"].append(residual.NormInf()[0] / ref_residual_norm)

        # Check for convergence.:

        lhs_fine = Epetra.Vector(A.DomainMap())
        rhs_coarse = Epetra.Vector(self.R.DomainMap())
        lhs_coarse = Epetra.Vector(self.R.DomainMap())
        error = Epetra.Vector(self.R.DomainMap())

        self.R.Multiply(True, rhs, rhs_coarse)
        A.Multiply(False, x0, lhs_fine)
        self.R.Multiply(True, lhs_fine, lhs_coarse)

        max_lhs = lhs_coarse.NormInf()
        max_rhs = rhs_coarse.NormInf()
        error[:] = lhs_coarse[:] - rhs_coarse[:]
        max_err = error.NormInf()
        tol = max(max_lhs, max_rhs) * 1.e-4
        assert np.allclose(
            rhs_coarse[:], lhs_coarse[:],
            atol=tol), "max_lhs:%e max_rhs:%e, max_error:%e " % (
                max_lhs, max_rhs, max_err)

        if conv_history:
            return x0, history
        else:
            return x0
Esempio n. 17
0
linearProblem = Epetra.LinearProblem(A, x, b)

if USE_DIRECT_SOLVER:
    solver = Amesos.Lapack(linearProblem)

    print "1) Performing symbolic factorizations..."
    solver.SymbolicFactorization()

    print "2) Performing numeric factorizations..."
    solver.NumericFactorization()
    print "3) Solving the linear system..."
    ierr = solver.Solve()
else:

    solver = AztecOO.AztecOO(linearProblem)

    ierr = solver.Iterate(1000, 1e-9)

print "   solver.Solve() return code = ", ierr

if ierr <= 1e-9:

    print
    print "|--------------------|"
    print "|convergence achieved|"
    print "|--------------------|"
    print

mb.tag_set_data(pres_tag, volumes, np.asarray(x[v_ids]))
Esempio n. 18
0
    def upscale_perm_flow_based(self, domain, dim, boundary_meshset):
        self.average_method = 'flow-based'
        area = (
            self.block_size[1] * self.block_size[2],
            self.block_size[0] * self.block_size[2],
            self.block_size[0] * self.block_size[1],
        )
        pres_tag = self.mb.tag_get_handle("Pressure", 1, types.MB_TYPE_DOUBLE,
                                          types.MB_TAG_SPARSE, True)
        std_map = Epetra.Map(len(domain), 0, self.comm)
        linear_vals = np.arange(0, len(domain))
        id_map = dict(zip(domain, linear_vals))
        boundary_elms = set()

        b = Epetra.Vector(std_map)
        x = Epetra.Vector(std_map)

        A = Epetra.CrsMatrix(Epetra.Copy, std_map, 3)

        for elem in boundary_meshset:
            if elem in boundary_elms:
                continue
            boundary_elms.add(elem)
            idx = id_map[elem]
            A.InsertGlobalValues(idx, [1], [idx])
            b[idx] = self.mb.tag_get_data(self.boundary_dir[dim],
                                          elem,
                                          flat=True)

        self.mb.tag_set_data(pres_tag, domain, np.repeat(0.0, len(domain)))

        for elem in (set(domain) ^ boundary_elms):

            adj_volumes = self.mesh_topo_util.get_bridge_adjacencies(
                np.asarray([elem]), 2, 3, 0)
            adj_volumes = [elems for elems in adj_volumes if elems in domain]
            adj_volumes_set = set(adj_volumes)

            elem_center = self.mesh_topo_util.get_average_position(
                np.asarray([elem]))
            K1 = self.mb.tag_get_data(self.perm_tag, [elem], flat=True)
            adj_perms = []
            for adjacencies in range(len(adj_volumes)):
                adj_perms.append(
                    self.mb.tag_get_data(self.perm_tag, adj_volumes,
                                         flat=True)[adjacencies *
                                                    9:(adjacencies + 1) * 9])
            values = []
            ids = []
            for K2, adj in zip(adj_perms, adj_volumes_set):
                adj_center = self.mesh_topo_util.get_average_position(
                    np.asarray([adj]))
                N = elem_center - adj_center
                N = N / np.sqrt(N[0]**2 + N[1]**2 + N[2]**2)
                K1proj = np.dot(np.dot(N, K1.reshape([3, 3])), N)
                K2proj = np.dot(np.dot(N, K2.reshape([3, 3])), N)
                dl = np.linalg.norm((elem_center - adj_center) / 2)
                K_eq = (2 * K1proj * K2proj) / (K1proj * dl + K2proj * dl)
                values.append(-K_eq)
                if adj in id_map:
                    ids.append(id_map[adj])
            values.append(-sum(values))
            idx = id_map[elem]
            ids.append(idx)
            A.InsertGlobalValues(idx, values, ids)
        A.FillComplete()
        """
        prec = ML.MultiLevelPreconditioner(A, False)
        prec.SetParameterList(mlList)
        solver = AztecOO.AztecOO(A, x, b)
        solver.SetPrecOperator(prec)
        solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cg)
        solver.SetAztecOption(AztecOO.AZ_output, 16)
        solver.Iterate(1550, 1e-5)
        """

        linearProblem = Epetra.LinearProblem(A, x, b)
        solver = AztecOO.AztecOO(linearProblem)
        solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_warnings)
        solver.Iterate(200, 1e-9)

        self.mb.tag_set_data(pres_tag, domain, np.asarray(x))

        # Get the flux - should break down in another part
        flow_rate = 0.0
        total_area = 0.0
        for elem in boundary_meshset:
            elem_center = self.mesh_topo_util.get_average_position(
                np.asarray([elem]))
            adj_volumes = self.mesh_topo_util.get_bridge_adjacencies(
                np.asarray([elem]), 2, 3)
            adj_volumes_set = set(adj_volumes).intersection(set(domain))
            adj_to_boundary_volumes = set()
            for el in adj_volumes_set:
                if el in boundary_meshset:
                    adj_to_boundary_volumes.add(el)
            adj_volumes_set = adj_volumes_set - adj_to_boundary_volumes
            for adj in adj_volumes_set:
                adj_center = self.mesh_topo_util.get_average_position(
                    np.asarray([adj]))
                N = elem_center - adj_center
                N = N / np.sqrt(N[0]**2 + N[1]**2 + N[2]**2)
                adj_pres = self.mb.tag_get_data(pres_tag, adj)
                adj_perm = np.dot(
                    N,
                    np.dot(
                        self.mb.tag_get_data(self.perm_tag,
                                             adj).reshape([3, 3]), N))
                elem_perm = np.dot(
                    N,
                    np.dot(
                        self.mb.tag_get_data(self.perm_tag,
                                             elem).reshape([3, 3]), N))
                dl = np.linalg.norm((elem_center - adj_center) / 2)
                K_equiv = (2 * adj_perm * elem_perm) / (adj_perm * dl +
                                                        elem_perm * dl)

                flow_rate = flow_rate + area[dim] * K_equiv * adj_pres / dl
                total_area = total_area + area[dim]
            perm = flow_rate * dl / total_area
        return perm
Esempio n. 19
0
  "aggregation: type" : "Uncoupled",
  "smoother: type (level 0)"    : "Aztec",
  "smoother: damping factor": 0.67,
  "smoother: sweeps": 1,
  "smoother: pre or post": "post",
  "smoother: type (level 1)"    : "Aztec",
  "PDE equations": 5,
  "aggregation: use tentative restriction": False,
  "aggregation: damping factor": 0.000,
  "eigen-analysis: type": "power-method",
  "aggregation: block scaling": False,
  "energy minimization: enable": False,
  "energy minimization: type": 2
}

# creates the preconditioner and computes it
Prec = ML.MultiLevelPreconditioner(Matrix, False)
Prec.SetParameterList(MLList)
Prec.ComputePreconditioner()

# sets up the solver, specifies Prec as preconditioner, and
# solves using CG.
Solver = AztecOO.AztecOO(Matrix, LHS, RHS)
Solver.SetPrecOperator(Prec)
Solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres);
Solver.SetAztecOption(AztecOO.AZ_kspace, 30);
Solver.SetAztecOption(AztecOO.AZ_output, 1);
Solver.Iterate(200, 1e-8)

del Prec
Esempio n. 20
0
 def solve_linear_problem(self):
     linearProblem = Epetra.LinearProblem(self.T, self.x, self.Q)
     solver = AztecOO.AztecOO(linearProblem)
     solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
     solver.SetAztecOption(AztecOO.AZ_output, AztecOO.AZ_none)
     solver.Iterate(2000, 1e-16)
Esempio n. 21
0
    "smoother: type (level 4)": "Hiptmair",
    "smoother: type (level 5)": "Hiptmair",
    "smoother: type (level 6)": "Hiptmair",
    "smoother: Hiptmair efficient symmetric": True,
    "subsmoother: type": "Chebyshev",
    "subsmoother: Chebyshev alpha": 27.0,
    "subsmoother: node sweeps": 4,
    "subsmoother: edge sweeps": 4,
    "coarse: type": "Amesos-KLU",
    "coarse: max size": 128,
    "coarse: pre or post": "post",
    "coarse: sweeps": 1
}

comm = Epetra.PyComm()
C = scipy_csr_matrix2CrsMatrix(System["C"].tocsr(), comm)
CurlCurl = scipy_csr_matrix2CrsMatrix(System["CurlCurl"].tocsr(), comm)
node = scipy_csr_matrix2CrsMatrix(System["node"].tocsr(), comm)

ML_Hiptmair = ML.MultiLevelPreconditioner(CurlCurl, C, node, MLList)
ML_Hiptmair.ComputePreconditioner()
x = System["rhs"][0]
b_epetra = TrilinosIO._numpyToTrilinosVector(x)
x_epetra = TrilinosIO._numpyToTrilinosVector(System["rhs"] * 0)

solver = AztecOO.AztecOO(CurlCurl, x_epetra, b_epetra)
solver.SetPrecOperator(ML_Hiptmair)
solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cg)
solver.SetAztecOption(AztecOO.AZ_output, 50)
err = solver.Iterate(155000, 1e-10)
Esempio n. 22
0
    bcs.apply(PP)
    # bcs.apply(BB)make
    P = as_backend_type(PP).mat()
    B = as_backend_type(BB).mat()
    L = as_backend_type(LL).mat()
    b = as_backend_type(bb).vec()
    x = Epetra.Vector(0 * bb.array())
    # exit
    # A = as_backend_type(AA).mat()
    print toc()

    ML_Hiptmair = ml.MultiLevelPreconditioner(P, B, L, MLList)

    ML_Hiptmair.ComputePreconditioner()
    # ML_Hiptmair.ComputePreconditioner()
    solver = AztecOO.AztecOO(A, x, b)
    solver.SetPrecOperator(ML_Hiptmair)
    solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_gmres)
    solver.SetAztecOption(AztecOO.AZ_output, 16)
    err = solver.Iterate(1550, 1e-5)

    # if (Solving == 'Direct'):
    #     ksp = PETSc.KSP().create()
    #     ksp.setOperators(A)

    #     ksp.setFromOptions()

    #     ksp.setType(ksp.Type.PREONLY)
    #     pc = ksp.getPC()
    #     pc.setFromOptions()
    #     pc.setType("lu")
Esempio n. 23
0
        if pol:
            DOp = QUDestripeOperator(pix, tmap_local, tmap_glob, umap_local,
                                     umap_glob, hits_glob, baseline_lengths,
                                     data, comm, num_baselines)
        else:
            DOp = TDestripeOperator(pix, tmap_local, tmap_glob, hits_glob,
                                    baseline_lengths, comm)

        # Create the preconditioning operator
        PrecOp = PrecOperator(baseline_lengths, comm)

        # Aggregate the Destriping Operator, first guess and RHS in Linear Problem object
        LinProb = Epetra.LinearProblem(DOp, baselines, RHS)

        # Solve the problem iteratively using GMRES
        IterSolver = AztecOO.AztecOO(LinProb)
        IterSolver.SetPrecOperator(PrecOp)
        with tm.TimeMonitor("Iterations"):
            IterSolver.Iterate(gmres_iterations, gmres_residual)

        # Write the baselines to disk, remove baselines from data, bin and write destriped maps

        with tm.TimeMonitor("After destriping"):
            if pol:
                mpi_writer.write("baselinesQ.bin",
                                 baselines.array[0][:num_baselines])
                mpi_writer.write("baselinesU.bin",
                                 baselines.array[0][num_baselines:])

                data['Q'] -= np.repeat(baselines.array[0][:num_baselines],
                                       baseline_lengths)
Esempio n. 24
0
scipy.io.savemat( "System.mat", {"CurlCurl":Acurl.sparray(),"node":Anode.sparray(),"C":C,"rhs":b.array()},oned_as='row')
scipy.io.savemat( "node.mat", {"node":Anode.sparray()},oned_as='row')
scipy.io.savemat( "rhs.mat", {"rhs":b.array()},oned_as='row')

C = scipy_csr_matrix2CrsMatrix(C, comm)
Acurl = scipy_csr_matrix2CrsMatrix(Acurl.sparray(), comm)
Anode = scipy_csr_matrix2CrsMatrix(Anode.sparray(), comm)
# Acurl = as_backend_type(Acurl).mat()
# Anode = as_backend_type(Anode).mat()

ML_Hiptmair = ML.MultiLevelPreconditioner(Acurl,C,Anode,MLList,True)
ML_Hiptmair.ComputePreconditioner()
x = Function(V)

b_epetra = x_epetra = TrilinosIO._numpyToTrilinosVector(b.array())
x_epetra = TrilinosIO._numpyToTrilinosVector(x.vector().array())

tic()
#u = M.SolveSystem(A,b,V,"cg","amg",1e-6,1e-6,1)
print toc()

import PyTrilinos.AztecOO as AztecOO
solver = AztecOO.AztecOO(Acurl, x_epetra, b_epetra)
solver.SetPrecOperator(ML_Hiptmair)
solver.SetAztecOption(AztecOO.AZ_solver, AztecOO.AZ_cg);
solver.SetAztecOption(AztecOO.AZ_output, 50);
err = solver.Iterate(155000, 1e-10)

x = Function(V)
x.vector()[:] = TrilinosIO._trilinosToNumpyVector(x_epetra)
    def solve(self):

        linear_problem = Epetra.LinearProblem(self.A, self.x, self.b)
        solver = AztecOO.AztecOO(linear_problem)
        solver.Iterate(10000, 1.e-5)
        return