Skip to content

Commit

Permalink
Refactor: Modify class BaseMatrix (#4239)
Browse files Browse the repository at this point in the history
* initialize some variables

* fix a semicolon

* Update sincos.cpp

* Update mathzone_add1.cpp

* Update mathzone_add1.cpp

* Update opt_DCsrch.cpp

* Update broyden_mixing.cpp

* Update pulay_mixing.cpp

* Update math_sphbes_test.cpp

* Update ORB_gen_tables.cpp

* Update ORB_table_phi.cpp

* Update math_sphbes_test.cpp

* Change some pointers to vectors

* Update formatter_contextfmt_test.cpp

* Change pointers to vectors

* Rollback

* Replace some pointers with vectors

* Change some pointers to vectors

* Remove unused function `Gint_Gamma::vl_grid_to_2D` and related variables

* Remove `gint_gamma.cpp`

* Modify base matrix
  • Loading branch information
DylanWRh authored May 27, 2024
1 parent 00f142d commit 79b9111
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
25 changes: 23 additions & 2 deletions source/module_hamilt_lcao/module_hcontainer/base_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ assert(this->value_begin != nullptr);
template <typename T>
void BaseMatrix<T>::add_array(T* array)
{
// if allocated, save data from array into matrix
// if whole matrix and 2d-block format, save data from array into matrix either
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
// if allocated, add data from array into matrix
// if whole matrix and 2d-block format, add data from array into matrix either
for (int i = 0; i < nrow_local * ncol_local; ++i)
{
value_begin[i] += array[i];
Expand All @@ -120,13 +123,19 @@ void BaseMatrix<T>::add_array(T* array)
template <typename T>
void BaseMatrix<T>::add_element(int mu, int nu, const T& value)
{
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
int index = mu * this->ncol_local + nu;
value_begin[index] += value;
}

template <typename T>
T& BaseMatrix<T>::get_value(const size_t& i_row, const size_t& j_col) const
{
#ifdef __DEBUG
assert(this->value_begin != nullptr);
#endif
int index = i_row * this->ncol_local + j_col;
return value_begin[index];
}
Expand All @@ -145,6 +154,12 @@ BaseMatrix<T>& BaseMatrix<T>::operator=(const BaseMatrix<T>& other)
{
this->nrow_local = other.nrow_local;
this->ncol_local = other.ncol_local;

if (this->allocated)
{
delete[] this->value_begin;
}

if (other.allocated)
{
this->value_begin = new T[nrow_local * ncol_local];
Expand Down Expand Up @@ -172,6 +187,12 @@ BaseMatrix<T>& BaseMatrix<T>::operator=(BaseMatrix<T>&& other) noexcept
{
this->nrow_local = other.nrow_local;
this->ncol_local = other.ncol_local;

if (this->allocated)
{
delete[] this->value_begin;
}

this->value_begin = other.value_begin;
this->allocated = other.allocated;
if (other.allocated)
Expand Down
7 changes: 5 additions & 2 deletions source/module_hamilt_lcao/module_hcontainer/base_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class BaseMatrix
void set_zero();

/**
* @brief save an array to the matrix
* @brief add an array to the matrix
*
* @param array array to be saved
* @param array array to be added
*/
void add_array(T* array);

/**
* @brief add a single element to the matrix
*
Expand All @@ -52,6 +53,7 @@ class BaseMatrix
* @param value value to be added
*/
void add_element(int mu, int nu, const T& value);

// for inside matrix
/**
* @brief get value from a whole matrix
Expand All @@ -61,6 +63,7 @@ class BaseMatrix
* @return T&
*/
T& get_value(const size_t& i_row, const size_t& j_col) const;

/**
* @brief get pointer of value from a submatrix
*/
Expand Down

0 comments on commit 79b9111

Please sign in to comment.