Commit b2cfc7cf authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

#984 Introduce an advanced way to use MatSetValues.

parent 437226b8
......@@ -32,7 +32,7 @@ int main(int argc, char ** argv)
{ 0 },
{ 1, 2 },
{ 3 },
{ 1, 2 }
{ 2 }
};
......@@ -50,13 +50,14 @@ int main(int argc, char ** argv)
std::vector<PetscInt> row { 1, 3 };
std::vector<PetscInt> col { 1, 2 };
std::vector<PetscScalar> val { 1., 1. };
std::vector<PetscScalar> val { 1., 1., 0., 1. };
matrix.SetValues(row,
col,
val.data(),
INSERT_VALUES,
__FILE__, __LINE__);
__FILE__, __LINE__,
Wrappers::Petsc::ignore_zero_entries::yes);
matrix.Assembly(__FILE__, __LINE__);
......
......@@ -265,9 +265,20 @@ namespace HappyHeart
void Matrix::SetValues(const std::vector<PetscInt>& row_indexing,
const std::vector<PetscInt>& column_indexing,
const PetscScalar* values, InsertMode insertOrAppend,
const char* invoking_file, int invoking_line)
const char* invoking_file, int invoking_line,
ignore_zero_entries do_ignore_zero_entries)
{
int error_code;
if (do_ignore_zero_entries == ignore_zero_entries::yes)
{
int error_code = MatSetValues(Internal(),
error_code = MatSetOption(Internal(), MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE);
if (error_code)
throw ExceptionNS::Exception(error_code, "MatSetOption - MAT_IGNORE_ZERO_ENTRIES - PETSC_TRUE",
invoking_file, invoking_line);
}
error_code = MatSetValues(Internal(),
static_cast<PetscInt>(row_indexing.size()),
row_indexing.data(),
static_cast<PetscInt>(column_indexing.size()),
......@@ -277,6 +288,15 @@ namespace HappyHeart
if (error_code)
throw ExceptionNS::Exception(error_code, "MatSetValues", invoking_file, invoking_line);
if (do_ignore_zero_entries == ignore_zero_entries::yes)
{
error_code = MatSetOption(Internal(), MAT_IGNORE_ZERO_ENTRIES, PETSC_FALSE);
if (error_code)
throw ExceptionNS::Exception(error_code, "MatSetOption - MAT_IGNORE_ZERO_ENTRIES - PETSC_TRUE",
invoking_file, invoking_line);
}
}
......
......@@ -73,6 +73,14 @@ namespace HappyHeart
{
/*!
* \brief Enum which enable to ignore zero entries in SetValues() calls.
*
* It is advanced use; default value of no is used.
*/
enum class ignore_zero_entries { yes, no };
/*!
* \brief A wrapper class over Petsc Mat objects.
......@@ -318,7 +326,8 @@ namespace HappyHeart
* \note Petsc documentation says you should use this over MatSetValue, but usage is actually not that
* wide: you actually define here a block of values. So for instance you choose \a row_indexing = { 1, 2 }
* and \a col_indexing = { 3, 5 }, you modify the four values (1, 3), (1, 5), (2, 3), (2, 5)... (which
* might fail if some of it are actually not non zero values!)
* might fail if some of it are actually not non zero values! See however \a ignore_zero_entries
* for a possible workaround here).
* To my knowledge there are no way to modify just (1, 3) and (2, 5) in one call! I skimmed a bit through
* petsc-user help list, and they seem to suggest MatSetValues should be called for one row at a time.
*
......@@ -331,11 +340,17 @@ namespace HappyHeart
* \param [in] insertOrAppend Petsc ADD_VALUES or INSERT_VALUES (see Petsc documentation).
* \param[in] invoking_file File that invoked the function or class; usually __FILE__.
* \param[in] invoking_line File that invoked the function or class; usually __LINE__.
* \param[in] do_ignore_zero_entries If yes, you might specify a block which encompass locations that
* are not within the pattern. For instance in our case above, you may specify \a row_indexing = { 1, 2 }
* and \a col_indexing = { 3, 5 } even if pattern says there are no value at (1, 5). In this case,
* the associated value in \a values MUST be 0.; any other value will rightfully trigger a Petsc error.
*
*/
void SetValues(const std::vector<PetscInt>& row_indexing,
const std::vector<PetscInt>& column_indexing,
const PetscScalar* values, InsertMode insertOrAppend,
const char* invoking_file, int invoking_line);
const char* invoking_file, int invoking_line,
ignore_zero_entries do_ignore_zero_entries = ignore_zero_entries::no);
/*!
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment