-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrite EHVec in new file * Adjust include headers that need EHVec class * Fix tests for EHVec * Fix tests, fix seg-fault bug in constructor * Add docstrings to EHVec class
- Loading branch information
1 parent
b51fbf0
commit f673023
Showing
9 changed files
with
112 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @file eh_vec.h | ||
* @author William Graham ([email protected]) | ||
* @brief Declaration for the storage class used when performing Fourier | ||
* transforms via fftw | ||
*/ | ||
#pragma once | ||
|
||
#include <fftw3.h> | ||
|
||
/** | ||
* @brief Storage class for Fourier transform values, obtained when performing | ||
* the pseudo-spectral timestep. | ||
* @details Storage is organised as a 2D buffer of fftw_complex types, of size | ||
* n_rows_ by n_cols. Elements can be accessed via the [] operator. | ||
*/ | ||
class EHVec { | ||
protected: | ||
int n_rows_ = 0; | ||
int n_cols_ = 0; | ||
|
||
fftw_complex **data_ = nullptr; | ||
|
||
/** @brief Free the memory assigned to this instance */ | ||
void free_memory(); | ||
|
||
public: | ||
EHVec() = default; | ||
|
||
fftw_complex *operator[](int col_index) { return data_[col_index]; } | ||
|
||
/** | ||
* @brief Allocate storage for this instance. Existing storage will be lost | ||
* (if present). | ||
* @details Reallocation of an already-allocated EHVec frees the current | ||
* buffer before reassigning the new buffer. Any values stored in the old | ||
* buffer are lost, EVEN IF THE NEW BUFFER IS OF GREATER SIZE THAN THE OLD | ||
* BUFFER. | ||
* @param n_rows,n_cols Shape of storage to allocate. | ||
*/ | ||
void allocate(int n_rows, int n_cols); | ||
|
||
/** @brief Determine whether the instance has memory allocated. */ | ||
bool is_allocated() const { return data_ != nullptr; } | ||
|
||
~EHVec() { free_memory(); } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "arrays/eh_vec.h" | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
void EHVec::free_memory() { | ||
if (is_allocated()) { | ||
for (int i = 0; i < n_rows_; i++) { fftw_free(data_[i]); } | ||
free(data_); | ||
} | ||
data_ = nullptr; | ||
} | ||
|
||
void EHVec::allocate(int n_rows, int n_cols) { | ||
// If already allocated we need to clean up old memory before resizing! | ||
if (is_allocated()) { | ||
spdlog::warn("EHVec is already assigned - freeing old memory before " | ||
"reallocating!"); | ||
free_memory(); | ||
} | ||
|
||
n_rows_ = n_rows; | ||
n_cols_ = n_cols; | ||
|
||
data_ = (fftw_complex **) malloc(n_rows_ * sizeof(fftw_complex *)); | ||
for (int i = 0; i < n_rows_; i++) { | ||
data_[i] = (fftw_complex *) malloc(n_cols_ * sizeof(fftw_complex)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include "arrays/eh_vec.h" | ||
#include "unit_test_utils.h" | ||
|
||
using tdms_tests::is_close; | ||
|
||
TEST_CASE("EHVec") { | ||
const int REAL = 0, IMAG = 1; | ||
const int n_rows = 4, n_cols = 8; | ||
EHVec eh; | ||
|
||
// allocate memory | ||
REQUIRE(!eh.is_allocated()); | ||
eh.allocate(n_rows, n_cols); | ||
REQUIRE(eh.is_allocated()); | ||
spdlog::info("Completed assignment"); | ||
|
||
// check that we an assign fftw_complexes to the elements | ||
eh[0][0][REAL] = 1.; | ||
eh[0][0][IMAG] = 0.; | ||
eh[0][1][REAL] = 0.; | ||
eh[0][1][IMAG] = 1.; | ||
fftw_complex fftw_unit{1., 0.}; | ||
fftw_complex fftw_imag_unit{0., 1.}; | ||
|
||
bool elements_set_correctly = | ||
is_close(eh[0][0][REAL], fftw_unit[REAL]) && | ||
is_close(eh[0][0][IMAG], fftw_unit[IMAG]) && | ||
is_close(eh[0][1][REAL], fftw_imag_unit[REAL]) && | ||
is_close(eh[0][1][IMAG], fftw_imag_unit[IMAG]); | ||
REQUIRE(elements_set_correctly); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,13 @@ | |
* @file test_Matrix.cpp | ||
* @author William Graham ([email protected]) | ||
* @brief Tests for the Matrix class and its subclasses (Vertices, | ||
* GratingStructure, Pupil, EHVec) | ||
* GratingStructure, Pupil) | ||
*/ | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include "array_test_class.h" | ||
#include "arrays.h" | ||
#include "unit_test_utils.h" | ||
|
||
using tdms_tests::is_close; | ||
|
||
void MatrixTest::test_correct_construction() { | ||
// create a Matrix via the default constructor | ||
|
@@ -162,36 +159,10 @@ void PupilTest::test_correct_construction() { | |
} | ||
} | ||
|
||
void EHVecTest::test_other_methods() { | ||
const int REAL = 0, IMAG = 1; | ||
EHVec eh; | ||
|
||
// allocate memory | ||
eh.allocate(n_rows, n_cols); | ||
REQUIRE(eh.has_elements()); | ||
|
||
// check that we an assign fftw_complexes to the elements | ||
eh[0][0][REAL] = 1.; | ||
eh[0][0][IMAG] = 0.; | ||
eh[0][1][REAL] = 0.; | ||
eh[0][1][IMAG] = 1.; | ||
fftw_complex fftw_unit{1., 0.}; | ||
fftw_complex fftw_imag_unit{0., 1.}; | ||
|
||
bool elements_set_correctly = | ||
is_close(eh[0][0][REAL], fftw_unit[REAL]) && | ||
is_close(eh[0][0][IMAG], fftw_unit[IMAG]) && | ||
is_close(eh[0][1][REAL], fftw_imag_unit[REAL]) && | ||
is_close(eh[0][1][IMAG], fftw_imag_unit[IMAG]); | ||
REQUIRE(elements_set_correctly); | ||
} | ||
|
||
TEST_CASE("Matrix") { MatrixTest().run_all_class_tests(); } | ||
|
||
TEST_CASE("Vertices") { VerticesTest().run_all_class_tests(); } | ||
|
||
TEST_CASE("GratingStructure") { GratingStructureTest().run_all_class_tests(); } | ||
|
||
TEST_CASE("Pupil") { PupilTest().run_all_class_tests(); } | ||
|
||
TEST_CASE("EHVec") { EHVecTest().run_all_class_tests(); } |