Skip to content
Snippets Groups Projects
MatrixVectorTraits.cpp 3.51 KiB
Newer Older
 * Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
 *            Distributed under a Modified BSD License.
 *              See accompanying file LICENSE.txt or
 *              http://www.opengeosys.org/project/license
 *
 */

#include "MatrixVectorTraits.h"
Christoph Lehmann's avatar
Christoph Lehmann committed
#include "MatrixSpecifications.h"

#ifdef USE_PETSC

namespace MathLib
{

std::unique_ptr<PETScMatrix>
MatrixVectorTraits<PETScMatrix>::
newInstance()
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<PETScMatrix>();
}

std::unique_ptr<PETScMatrix>
MatrixVectorTraits<PETScMatrix>::
newInstance(PETScMatrix const& A)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<PETScMatrix>(A);
}

std::unique_ptr<PETScMatrix>
MatrixVectorTraits<PETScMatrix>::
newInstance(MatrixSpecifications const& spec)
{
    auto const nrows = spec.nrows;
    auto const ncols = spec.ncols;
    if (spec.sparsity_pattern)
        // Assert that the misuse of the sparsity pattern is consistent.
        assert(spec.sparsity_pattern->size() == 1);
        auto const max_nonzeroes = spec.sparsity_pattern->front();

        PETScMatrixOption mat_opt;
        mat_opt.d_nz = max_nonzeroes;
        mat_opt.o_nz = max_nonzeroes;
        mat_opt.is_global_size = false;
Dmitri Naumov's avatar
Dmitri Naumov committed
        return std::make_unique<PETScMatrix>(nrows, ncols, mat_opt);
Dmitri Naumov's avatar
Dmitri Naumov committed
        return std::make_unique<PETScMatrix>(nrows, ncols);
}

std::unique_ptr<PETScVector>
MatrixVectorTraits<PETScVector>::
newInstance()
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<PETScVector>();
}

std::unique_ptr<PETScVector>
MatrixVectorTraits<PETScVector>::
newInstance(PETScVector const& x)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<PETScVector>(x);
}

std::unique_ptr<PETScVector>
MatrixVectorTraits<PETScVector>::
newInstance(MatrixSpecifications const& spec)
{
    auto const is_global_size = false;

    if (spec.ghost_indices != nullptr) {
Dmitri Naumov's avatar
Dmitri Naumov committed
        return std::make_unique<PETScVector>(spec.nrows, *spec.ghost_indices,
                                             is_global_size);
Dmitri Naumov's avatar
Dmitri Naumov committed
        return std::make_unique<PETScVector>(spec.nrows, is_global_size);
std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
    PETScVector::IndexType const length)
{
    auto const is_global_size = true;

    return std::make_unique<PETScVector>(length, is_global_size);
}

namespace MathLib
{

std::unique_ptr<EigenMatrix>
MatrixVectorTraits<EigenMatrix>::
newInstance()
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<EigenMatrix>(0, 0);  // TODO default constructor
}

std::unique_ptr<EigenMatrix>
MatrixVectorTraits<EigenMatrix>::
newInstance(EigenMatrix const& A)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<EigenMatrix>(A);
}

std::unique_ptr<EigenMatrix>
MatrixVectorTraits<EigenMatrix>::
newInstance(MatrixSpecifications const& spec)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    auto A = std::make_unique<EigenMatrix>(spec.nrows);

    if (spec.sparsity_pattern)
        setMatrixSparsity(*A, *spec.sparsity_pattern);
}

std::unique_ptr<EigenVector>
MatrixVectorTraits<EigenVector>::
newInstance()
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<EigenVector>();
}

std::unique_ptr<EigenVector>
MatrixVectorTraits<EigenVector>::
newInstance(EigenVector const& x)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<EigenVector>(x);
}

std::unique_ptr<EigenVector>
MatrixVectorTraits<EigenVector>::
newInstance(MatrixSpecifications const& spec)
{
Dmitri Naumov's avatar
Dmitri Naumov committed
    return std::make_unique<EigenVector>(spec.nrows);
std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
    Eigen::SparseMatrix<double>::Index const length)
{
    return std::make_unique<EigenVector>(length);
}