Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions kratos/future/containers/define_linear_algebra_mpi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Ruben Zorrilla
//

#pragma once

// System includes

// External includes

// Project includes
#include "containers/distributed_csr_matrix.h"
#include "containers/distributed_sparse_graph.h"
#include "containers/distributed_system_vector.h"

namespace Kratos::Future
{

///@addtogroup KratosCore
///@{

///@name Kratos Classes
///@{

struct DistributedLinearAlgebraTraits
{
using DataType = double;

using IndexType = std::size_t;

using MatrixType = DistributedCsrMatrix<DataType, IndexType>;

using VectorType = DistributedSystemVector<DataType, IndexType>;

using SparseGraphType = DistributedSparseGraph<IndexType>;
};

///@}
///@} addtogroup block

} // namespace Kratos::Future
51 changes: 51 additions & 0 deletions kratos/future/containers/define_linear_algebra_serial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Ruben Zorrilla
//

#pragma once

// System includes

// External includes

// Project includes
#include "containers/csr_matrix.h"
#include "containers/sparse_contiguous_row_graph.h"
#include "containers/system_vector.h"

namespace Kratos::Future
{

///@addtogroup KratosCore
///@{

///@name Kratos Classes
///@{

struct SerialLinearAlgebraTraits
{
using DataType = double;

using IndexType = std::size_t;

using MatrixType = CsrMatrix<DataType, IndexType>;

using VectorType = SystemVector<DataType, IndexType>;

using DenseMatrixType = DenseMatrix<DataType>; //TODO: think about this one

using SparseGraphType = SparseContiguousRowGraph<IndexType>;
};

///@}
///@} addtogroup block

} // namespace Kratos::Future
227 changes: 227 additions & 0 deletions kratos/future/linear_operators/linear_operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Ruben Zorrilla
//

#pragma once

// System includes

// External includes

// Project includes
#include "includes/model_part.h"

namespace Kratos::Future
{

///@addtogroup KratosCore
///@{

///@name Kratos Classes
///@{

/**
* @brief
* @class LinearOperator
* @ingroup KratosCore
* @brief Auxiliary container to store a linear operator
* @details
* This class implements a generic linear operator interface to be used
* in matrix-free algorithms. Being a generic interface, it can be derived
* to wrap different types of linear operators, including maxtrix-based ones.
* @tparam TLinearAlgebra The struct containing the linear algebra types
*/
template <class TLinearAlgebra>
class LinearOperator
{

public:
///@name Type Definitions
///@{

/// Pointer definition of LinearOperator
KRATOS_CLASS_POINTER_DEFINITION(LinearOperator);

/// Vector type definition from template parameter
using VectorType = typename TLinearAlgebra::VectorType;

/// Matrix type definition from template parameter
using MatrixType = typename TLinearAlgebra::MatrixType;

/// Data type stored in the system vector
using DataType = typename VectorType::DataType;

/// Index type used in the system vector
using IndexType = typename VectorType::IndexType;

///@}
///@name Life Cycle
///@{

/**
* @brief Default constructor.
* @details Creates an empty LinearOperator with uninitialized function objects.
*/
LinearOperator() = default;

/**
* @brief Constructor from parameters.
* @param ThisParameters Parameters containing the linear operator settings
*/
LinearOperator(Parameters ThisParameters)
{
mNumRows = ThisParameters["num_rows"].GetInt();
mNumCols = ThisParameters["num_cols"].GetInt();
}

/// Deleted copy constructor (non-copyable)
LinearOperator(const LinearOperator& rOther) = delete;

/// Defaulted move constructor
LinearOperator(LinearOperator&& rOther) = default;

/// Default destructor
virtual ~LinearOperator() = default;

///@}
///@name Operators
///@{

/// Deleted copy assignment operator (non-copyable)
LinearOperator& operator=(const LinearOperator& rOther) = delete;

/// Defaulted move assignment operator
LinearOperator& operator=(LinearOperator&& rOther) = default;

///@}
///@name Operations
///@{

/**
* @brief Performs the matrix-vector product y = A * x.
* @param rX Input vector x
* @param rY Output vector y
*/
virtual void SpMV(
const VectorType& rX,
VectorType& rY) const
{
KRATOS_ERROR << "SpMV() is not implemented in base LinearOperator class." << std::endl;
}

/**
* @brief Performs the transposed matrix-vector product y = A^T * x.
* @param rX Input vector x
* @param rY Output vector y
*/
virtual void TransposeSpMV(
const VectorType& rX,
VectorType& rY) const
{
KRATOS_ERROR << "TransposeSpMV() is not implemented in base LinearOperator class." << std::endl;
}

/**
* @brief Clear the operator data.
* @details Resets the sizes and function objects to null.
*/
virtual void Clear()
{
mNumRows = 0;
mNumCols = 0;
}

///@}
///@name Access
///@{

virtual MatrixType& GetMatrix()
{
KRATOS_ERROR << "GetMatrix() is not implemented in base LinearOperator class." << std::endl;
}

virtual const MatrixType& GetMatrix() const
{
KRATOS_ERROR << "GetMatrix() is not implemented in base LinearOperator class." << std::endl;
}

/**
* @brief Set the number of rows.
* @param NumRows Number of rows
*/
void SetNumRows(std::size_t NumRows)
{
mNumRows = NumRows;
}

/**
* @brief Set the number of columns.
* @param NumCols Number of columns
*/
void SetNumCols(std::size_t NumCols)
{
mNumCols = NumCols;
}

///@}
///@name Inquiry
///@{

/**
* @brief Get the number of rows.
* @return Number of rows of the operator
*/
std::size_t NumRows() const
{
return mNumRows;
}

/**
* @brief Get the number of columns.
* @return Number of columns of the operator
*/
std::size_t NumCols() const
{
return mNumCols;
}

/**
* @brief Check if the operator is matrix-free.
* @return True if the operator is matrix-free, false otherwise
*/
virtual bool IsMatrixFree() const
{
return true;
}

///@}
private:

///@name Member Variables
///@{

/// Number of rows of the operator
std::size_t mNumRows = 0;

/// Number of columns of the operator
std::size_t mNumCols = 0;

///@}
}; // class LinearOperator

///@}
///@name Input and output
///@{

///@}
///@} addtogroup block

} // namespace Kratos::Future
Loading
Loading