From 83ba559677e63217bac06abfaa7781a75414ca4b Mon Sep 17 00:00:00 2001 From: Drewniok Date: Fri, 10 Mar 2023 11:21:42 +0100 Subject: [PATCH 1/4] :art: added a new function ``get_chargeless_potential_between_sidbs`` to avoid confusion. --- .../charge_distribution_surface.hpp | 23 ++++++- .../charge_distribution_surface.cpp | 61 +++++++++++++++---- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/include/fiction/technology/charge_distribution_surface.hpp b/include/fiction/technology/charge_distribution_surface.hpp index 55f954b54..43258c724 100644 --- a/include/fiction/technology/charge_distribution_surface.hpp +++ b/include/fiction/technology/charge_distribution_surface.hpp @@ -391,7 +391,25 @@ class charge_distribution_surface : public Lyt return strg->dist_mat[index1][index2]; } /** - * Calculates and returns the electrostatic potential between two cells. + * Calculates and returns the chargeless electrostatic potential between two cells. + * + * @param c1 The first cell + * @param c2 The second cell + * @return The chargeless electrostatic potential between `c1` and `c2` (V_i,j / n_j). + */ + [[nodiscard]] double get_chargeless_potential_between_sidbs(const typename Lyt::cell& c1, + const typename Lyt::cell& c2) const noexcept + { + if (const auto index1 = cell_to_index(c1), index2 = cell_to_index(c2); (index1 != -1) && (index2 != -1)) + { + return strg->pot_mat[static_cast(index1)][static_cast(index2)]; + } + + return 0; + } + + /** + * Calculates and returns the electrostatic potential at one cell (c1) generated by another cell (c2). * * @param c1 The first cell * @param c2 The second cell @@ -402,7 +420,8 @@ class charge_distribution_surface : public Lyt { if (const auto index1 = cell_to_index(c1), index2 = cell_to_index(c2); (index1 != -1) && (index2 != -1)) { - return strg->pot_mat[static_cast(index1)][static_cast(index2)]; + return strg->pot_mat[static_cast(index1)][static_cast(index2)] * + charge_state_to_sign(get_charge_state(c2)); } return 0; diff --git a/test/technology/charge_distribution_surface.cpp b/test/technology/charge_distribution_surface.cpp index 4b34dd769..7788afe92 100644 --- a/test/technology/charge_distribution_surface.cpp +++ b/test/technology/charge_distribution_surface.cpp @@ -4,6 +4,8 @@ #include +#include "catch2/matchers/catch_matchers_floating_point.hpp" + #include #include #include @@ -143,7 +145,7 @@ TEMPLATE_TEST_CASE( // all SiDBs' charge states are set to neutral charge_layout.set_all_charge_states(sidb_charge_state::NEUTRAL); // - // // read SiDBs' charge states + // read SiDBs' charge states CHECK(charge_layout.get_charge_state({5, 4}) == sidb_charge_state::NEUTRAL); CHECK(charge_layout.get_charge_state({5, 5}) == sidb_charge_state::NEUTRAL); CHECK(charge_layout.get_charge_state({5, 6}) == sidb_charge_state::NEUTRAL); @@ -151,10 +153,10 @@ TEMPLATE_TEST_CASE( // charge_layout.set_all_charge_states(sidb_charge_state::NEUTRAL); // - // // all SiDBs' charge states are set to negative + // all SiDBs' charge states are set to negative charge_layout.set_all_charge_states(sidb_charge_state::NEGATIVE); // - // // read SiDBs' charge states + // read SiDBs' charge states CHECK(charge_layout.get_charge_state({5, 4}) == sidb_charge_state::NEGATIVE); CHECK(charge_layout.get_charge_state({5, 5}) == sidb_charge_state::NEGATIVE); CHECK(charge_layout.get_charge_state({5, 6}) == sidb_charge_state::NEGATIVE); @@ -207,15 +209,15 @@ TEMPLATE_TEST_CASE( lyt.assign_cell_type({1, 10, 1}, TestType::cell_type::NORMAL); charge_distribution_surface charge_layout{lyt, sidb_simulation_parameters{}}; - CHECK(charge_layout.get_potential_between_sidbs({0, 0, 0}, {0, 0, 0}) == 0.0); - CHECK(charge_layout.get_potential_between_sidbs({1, 8, 0}, {1, 8, 0}) == 0.0); - CHECK(charge_layout.get_potential_between_sidbs({1, 10, 1}, {1, 10, 1}) == 0.0); - CHECK((charge_layout.get_potential_between_sidbs({1, 8, 0}, {0, 0, 0}) - 0.0121934043) < 0.00000001); - CHECK(std::abs(charge_layout.get_potential_between_sidbs({0, 0, 0}, {1, 10, 1}) - - charge_layout.get_potential_between_sidbs({1, 10, 1}, {0, 0, 0})) < + CHECK(charge_layout.get_chargeless_potential_between_sidbs({0, 0, 0}, {0, 0, 0}) == 0.0); + CHECK(charge_layout.get_chargeless_potential_between_sidbs({1, 8, 0}, {1, 8, 0}) == 0.0); + CHECK(charge_layout.get_chargeless_potential_between_sidbs({1, 10, 1}, {1, 10, 1}) == 0.0); + CHECK((charge_layout.get_chargeless_potential_between_sidbs({1, 8, 0}, {0, 0, 0}) - 0.0121934043) < 0.00000001); + CHECK(std::abs(charge_layout.get_chargeless_potential_between_sidbs({0, 0, 0}, {1, 10, 1}) - + charge_layout.get_chargeless_potential_between_sidbs({1, 10, 1}, {0, 0, 0})) < physical_constants::POP_STABILITY_ERR); - CHECK(charge_layout.get_potential_between_sidbs({0, 0, 0}, {1, 8, 0}) > - charge_layout.get_potential_between_sidbs({1, 10, 1}, {0, 0, 0})); + CHECK(charge_layout.get_chargeless_potential_between_sidbs({0, 0, 0}, {1, 8, 0}) > + charge_layout.get_chargeless_potential_between_sidbs({1, 10, 1}, {0, 0, 0})); } // SECTION("Local Potential") @@ -372,4 +374,41 @@ TEMPLATE_TEST_CASE( charge_layout_new.increase_charge_index_by_one(); CHECK(charge_layout_new.get_charge_index().first == 15); } + + SECTION("using chargeless and normal potential function") + { + TestType lyt_new{{11, 11}}; + const sidb_simulation_parameters params{3, -0.32, 5.0 * 1E-9, 3.84 * 1E-10, 7.68 * 1E-10, 2.25 * 1E-10}; + + lyt_new.assign_cell_type({0, 0, 1}, TestType::cell_type::NORMAL); + lyt_new.assign_cell_type({1, 3, 0}, TestType::cell_type::NORMAL); + lyt_new.assign_cell_type({10, 5, 1}, TestType::cell_type::NORMAL); + + charge_distribution_surface charge_layout_new{lyt_new, params}; + + charge_layout_new.assign_charge_state({0, 0, 1}, sidb_charge_state::NEGATIVE); + charge_layout_new.assign_charge_state({1, 3, 0}, sidb_charge_state::POSITIVE); + charge_layout_new.assign_charge_state({10, 5, 1}, sidb_charge_state::NEUTRAL); + + CHECK(charge_layout_new.get_charge_state({0, 0, 1}) == sidb_charge_state::NEGATIVE); + CHECK(charge_layout_new.get_charge_state({1, 3, 0}) == sidb_charge_state::POSITIVE); + CHECK(charge_layout_new.get_charge_state({10, 5, 1}) == sidb_charge_state::NEUTRAL); + + CHECK(charge_layout_new.get_chargeless_potential_between_sidbs({0, 0, 1}, {1, 3, 0}) > 0); + CHECK(charge_layout_new.get_potential_between_sidbs({0, 0, 1}, {1, 3, 0}) > 0); + + CHECK(charge_layout_new.get_chargeless_potential_between_sidbs({0, 0, 1}, {10, 5, 1}) > 0); + CHECK(charge_layout_new.get_potential_between_sidbs({0, 0, 1}, {10, 5, 1}) == 0.0); + + CHECK(charge_layout_new.get_chargeless_potential_between_sidbs({10, 5, 1}, {0, 0, 1}) > 0); + CHECK(charge_layout_new.get_potential_between_sidbs({10, 5, 1}, {0, 0, 1}) < 0); + + CHECK_THAT(charge_layout_new.get_potential_between_sidbs({10, 5, 1}, {0, 0, 1}) + + charge_layout_new.get_chargeless_potential_between_sidbs({10, 5, 1}, {0, 0, 1}), + Catch::Matchers::WithinAbs(0.000000, 0.000001)); + + CHECK_THAT(charge_layout_new.get_potential_between_sidbs({0, 0, 1}, {1, 3, 0}) - + charge_layout_new.get_chargeless_potential_between_sidbs({0, 0, 1}, {1, 3, 0}), + Catch::Matchers::WithinAbs(0.000000, 0.000001)); + } } From 2c5a8509220d622d6585e445ab5a3d3b573728c1 Mon Sep 17 00:00:00 2001 From: Drewniok Date: Fri, 10 Mar 2023 12:39:42 +0100 Subject: [PATCH 2/4] :art: added a new function ``get_chargeless_potential_between_sidbs`` to avoid confusion. --- include/fiction/technology/charge_distribution_surface.hpp | 2 ++ test/technology/charge_distribution_surface.cpp | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/fiction/technology/charge_distribution_surface.hpp b/include/fiction/technology/charge_distribution_surface.hpp index 43258c724..a542a4840 100644 --- a/include/fiction/technology/charge_distribution_surface.hpp +++ b/include/fiction/technology/charge_distribution_surface.hpp @@ -392,6 +392,7 @@ class charge_distribution_surface : public Lyt } /** * Calculates and returns the chargeless electrostatic potential between two cells. + * Hint: Please take `get_potential_between_sidbs` if the electrostatic potential V_i,j is required. * * @param c1 The first cell * @param c2 The second cell @@ -410,6 +411,7 @@ class charge_distribution_surface : public Lyt /** * Calculates and returns the electrostatic potential at one cell (c1) generated by another cell (c2). + * Hint: Please take `get_chargeless_potential_between_sidbs` if V_i,j / n_j is required. * * @param c1 The first cell * @param c2 The second cell diff --git a/test/technology/charge_distribution_surface.cpp b/test/technology/charge_distribution_surface.cpp index 7788afe92..935004d9b 100644 --- a/test/technology/charge_distribution_surface.cpp +++ b/test/technology/charge_distribution_surface.cpp @@ -3,8 +3,7 @@ // #include - -#include "catch2/matchers/catch_matchers_floating_point.hpp" +#include #include #include From f7df6462216ebafc3d834cd7d65cd39d35f34da1 Mon Sep 17 00:00:00 2001 From: Marcel Walter Date: Mon, 13 Mar 2023 14:59:38 +0100 Subject: [PATCH 3/4] :memo: Adjusted documentation --- .../charge_distribution_surface.hpp | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/include/fiction/technology/charge_distribution_surface.hpp b/include/fiction/technology/charge_distribution_surface.hpp index a542a4840..c0ad938fe 100644 --- a/include/fiction/technology/charge_distribution_surface.hpp +++ b/include/fiction/technology/charge_distribution_surface.hpp @@ -334,7 +334,7 @@ class charge_distribution_surface : public Lyt /** * Returns the charge state of a given cell. * - * @param c cell. + * @param c The cell. * @return The charge state of the given cell. */ [[nodiscard]] sidb_charge_state get_charge_state(const typename Lyt::cell& c) const noexcept @@ -363,10 +363,10 @@ class charge_distribution_surface : public Lyt return -1; } /** - * Returns the distance between two cells + * Returns the distance between two cells. * - * @param c1 the first cell to compare - * @param c2 the second cell to compare + * @param c1 the first cell to compare. + * @param c2 the second cell to compare. * @return a constexpr double representing the distance between the two cells. */ [[nodiscard]] double get_distance_between_cells(const typename Lyt::cell& c1, @@ -391,12 +391,14 @@ class charge_distribution_surface : public Lyt return strg->dist_mat[index1][index2]; } /** - * Calculates and returns the chargeless electrostatic potential between two cells. - * Hint: Please take `get_potential_between_sidbs` if the electrostatic potential V_i,j is required. + * Returns the chargeless electrostatic potential between two cells. * - * @param c1 The first cell - * @param c2 The second cell - * @return The chargeless electrostatic potential between `c1` and `c2` (V_i,j / n_j). + * @note If the signed electrostatic potential \f$ V_{i,j} \f$ is required, use the `get_potential_between_sidbs` + * function. + * + * @param c1 The first cell. + * @param c2 The second cell. + * @return The chargeless electrostatic potential between `c1` and `c2`, i.e, \f$ \frac{V_{i,j}}{n_j} \f$. */ [[nodiscard]] double get_chargeless_potential_between_sidbs(const typename Lyt::cell& c1, const typename Lyt::cell& c2) const noexcept @@ -408,14 +410,15 @@ class charge_distribution_surface : public Lyt return 0; } - /** - * Calculates and returns the electrostatic potential at one cell (c1) generated by another cell (c2). - * Hint: Please take `get_chargeless_potential_between_sidbs` if V_i,j / n_j is required. + * Calculates and returns the electrostatic potential at one cell (`c1`) generated by another cell (`c2`). + * + * @note If the chargeless electrostatic potential \f$ \frac{V_{i,j}}{n_j} \f$ is required, use the + * `get_chargeless_potential_between_sidbs` function. * - * @param c1 The first cell - * @param c2 The second cell - * @return The electrostatic potential between `c1` and `c2`. + * @param c1 The first cell. + * @param c2 The second cell. + * @return The electrostatic potential between `c1` and `c2`, i.e., \f$ V_{i,j} \f$. */ [[nodiscard]] double get_potential_between_sidbs(const typename Lyt::cell& c1, const typename Lyt::cell& c2) const noexcept @@ -431,8 +434,8 @@ class charge_distribution_surface : public Lyt /** * Calculates and returns the potential of two indices. * - * @param index1 The first index - * @param index2 The second index + * @param index1 The first index. + * @param index2 The second index. * @return The potential between `index1` and `index2`. */ [[nodiscard]] double get_electrostatic_potential_by_indices(const uint64_t index1, @@ -471,14 +474,7 @@ class charge_distribution_surface : public Lyt const auto index1 = static_cast(cell_to_index(c1)); const auto index2 = static_cast(cell_to_index(c2)); - if (strg->dist_mat[index1][index2] == 0) - { - return 0.0; - } - - return (strg->phys_params.k / strg->dist_mat[index1][index2] * - std::exp(-strg->dist_mat[index1][index2] / strg->phys_params.lambda_tf) * - physical_constants::ELECTRIC_CHARGE); + return potential_between_sidbs_by_index(index1, index2); } /** * The function calculates the electrostatic potential for each SiDB position (local). From 0fceb87a2f8b3f8fb81a76096ef3929a46415ed7 Mon Sep 17 00:00:00 2001 From: Drewniok Date: Mon, 13 Mar 2023 19:03:41 +0100 Subject: [PATCH 4/4] :white_check_mark: added test for uncovered part --- test/technology/charge_distribution_surface.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/technology/charge_distribution_surface.cpp b/test/technology/charge_distribution_surface.cpp index 935004d9b..f871e81f2 100644 --- a/test/technology/charge_distribution_surface.cpp +++ b/test/technology/charge_distribution_surface.cpp @@ -208,6 +208,7 @@ TEMPLATE_TEST_CASE( lyt.assign_cell_type({1, 10, 1}, TestType::cell_type::NORMAL); charge_distribution_surface charge_layout{lyt, sidb_simulation_parameters{}}; + CHECK(charge_layout.get_chargeless_potential_between_sidbs({2, 8, 0}, {2, 10, 1}) == 0); CHECK(charge_layout.get_chargeless_potential_between_sidbs({0, 0, 0}, {0, 0, 0}) == 0.0); CHECK(charge_layout.get_chargeless_potential_between_sidbs({1, 8, 0}, {1, 8, 0}) == 0.0); CHECK(charge_layout.get_chargeless_potential_between_sidbs({1, 10, 1}, {1, 10, 1}) == 0.0);