Skip to content

Commit

Permalink
🎨 template deduction is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
Drewniok committed Apr 15, 2024
1 parent 73cbc44 commit 8e1d2d2
Show file tree
Hide file tree
Showing 20 changed files with 2,085 additions and 2,102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace detail
{

template <typename Lyt>
void critical_temperature(pybind11::module& m, const std::string& lattice = "")
void critical_temperature(pybind11::module& m)
{
using namespace pybind11::literals;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <string>

namespace pyfiction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <string>

namespace pyfiction
{
Expand All @@ -22,12 +21,11 @@ namespace detail
{

template <typename Lyt>
void is_ground_state(pybind11::module& m, const std::string& lattice = "")
void is_ground_state(pybind11::module& m)
{
using namespace pybind11::literals;

m.def(fmt::format("is_ground_state{}", lattice).c_str(), &fiction::is_ground_state<Lyt>, "heuristic_results"_a,
"exhaustive_results"_a, DOC(fiction_is_ground_state));
m.def("is_ground_state", &fiction::is_ground_state<Lyt>, "heuristic_results"_a, "exhaustive_results"_a, DOC(fiction_is_ground_state));
}

} // namespace detail
Expand All @@ -36,8 +34,8 @@ inline void is_ground_state(pybind11::module& m)
{
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!

detail::is_ground_state<py_sidb_100_lattice>(m, "_100");
detail::is_ground_state<py_sidb_111_lattice>(m, "_111");
detail::is_ground_state<py_sidb_100_lattice>(m);
detail::is_ground_state<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ namespace detail
{

template <typename Lyt>
void is_operational(pybind11::module& m, const std::string& lattice = "")
void is_operational(pybind11::module& m)
{
using namespace pybind11::literals;

m.def(fmt::format("is_operational{}", lattice).c_str(), &fiction::is_operational<Lyt, py_tt>, "lyt"_a, "spec"_a,
m.def("is_operational", &fiction::is_operational<Lyt, py_tt>, "lyt"_a, "spec"_a,
"params"_a = fiction::is_operational_params{}, DOC(fiction_is_operational));

m.def(fmt::format("operational_input_patterns{}", lattice).c_str(),
&fiction::operational_input_patterns<Lyt, py_tt>, "lyt"_a, "spec"_a,
m.def("operational_input_patterns", &fiction::operational_input_patterns<Lyt, py_tt>, "lyt"_a, "spec"_a,
"params"_a = fiction::is_operational_params{}, DOC(fiction_is_operational));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace detail
{

template <typename Lyt>
void minimum_energy(pybind11::module& m, const std::string lattice = "")
void minimum_energy(pybind11::module& m)
{
using namespace pybind11::literals;

m.def(
fmt::format("minimum_energy{}", lattice).c_str(), [](const std::vector<Lyt>& layouts) -> double
"minimum_energy", [](const std::vector<Lyt>& layouts) -> double
{ return fiction::minimum_energy(layouts.cbegin(), layouts.cend()); },
"layouts"_a, DOC(fiction_minimum_energy));
}
Expand All @@ -39,8 +39,8 @@ inline void minimum_energy(pybind11::module& m)
{
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!

detail::minimum_energy<py_charge_distribution_surface_100>(m, "_100");
detail::minimum_energy<py_charge_distribution_surface_111>(m, "_111");
detail::minimum_energy<py_charge_distribution_surface_100>(m);
detail::minimum_energy<py_charge_distribution_surface_111>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,50 @@ namespace detail
{

template <typename Lyt>
void quickexact(pybind11::module& m, const std::string& lattice = "")
void quickexact(pybind11::module& m)
{
namespace py = pybind11;
using namespace pybind11::literals;

py::enum_<typename fiction::quickexact_params<Lyt>::automatic_base_number_detection>(
m, fmt::format("automatic_base_number_detection{}", lattice).c_str(),
DOC(fiction_quickexact_params_automatic_base_number_detection))
.value("ON", fiction::quickexact_params<Lyt>::automatic_base_number_detection::ON,
DOC(fiction_quickexact_params_automatic_base_number_detection_ON))
.value("OFF", fiction::quickexact_params<Lyt>::automatic_base_number_detection::OFF,
DOC(fiction_quickexact_params_automatic_base_number_detection_OFF))
m.def("quickexact", &fiction::quickexact<Lyt>, "lyt"_a,
"params"_a = fiction::quickexact_params<fiction::offset::ucoord_t>{}, DOC(fiction_quickexact));
}

} // namespace detail

inline void quickexact(pybind11::module& m)
{
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!
namespace py = pybind11;
using namespace pybind11::literals;

;
py::enum_<typename fiction::quickexact_params<fiction::offset::ucoord_t>::automatic_base_number_detection>(
m, "automatic_base_number_detection", DOC(fiction_quickexact_params_automatic_base_number_detection))
.value("ON", fiction::quickexact_params<fiction::offset::ucoord_t>::automatic_base_number_detection::ON,
DOC(fiction_quickexact_params_automatic_base_number_detection_ON))
.value("OFF", fiction::quickexact_params<fiction::offset::ucoord_t>::automatic_base_number_detection::OFF,
DOC(fiction_quickexact_params_automatic_base_number_detection_OFF));

/**
* QuickExact parameters.
*/
py::class_<fiction::quickexact_params<Lyt>>(m, fmt::format("quickexact_params{}", lattice).c_str(),
DOC(fiction_quickexact_params))
py::class_<fiction::quickexact_params<fiction::offset::ucoord_t>>(m, "quickexact_params",
DOC(fiction_quickexact_params))
.def(py::init<>())
.def_readwrite(fmt::format("simulation_parameters{}", lattice).c_str(),
&fiction::quickexact_params<Lyt>::simulation_parameters,
.def_readwrite("simulation_parameters",
&fiction::quickexact_params<fiction::offset::ucoord_t>::simulation_parameters,
DOC(fiction_quickexact_params))
.def_readwrite(fmt::format("base_number_detection{}", lattice).c_str(),
&fiction::quickexact_params<Lyt>::base_number_detection,
.def_readwrite("base_number_detection",
&fiction::quickexact_params<fiction::offset::ucoord_t>::base_number_detection,
DOC(fiction_quickexact_params_base_number_detection))
.def_readwrite(fmt::format("local_external_potential{}", lattice).c_str(),
&fiction::quickexact_params<Lyt>::local_external_potential,
.def_readwrite("local_external_potential",
&fiction::quickexact_params<fiction::offset::ucoord_t>::local_external_potential,
DOC(fiction_quickexact_params_local_external_potential))
.def_readwrite(fmt::format("global_potential{}", lattice).c_str(),
&fiction::quickexact_params<Lyt>::global_potential,
DOC(fiction_quickexact_params_global_potential))

;

m.def(fmt::format("quickexact{}", lattice).c_str(), &fiction::quickexact<Lyt>, "lyt"_a,
"params"_a = fiction::quickexact_params<Lyt>{}, DOC(fiction_quickexact));
}

} // namespace detail

inline void quickexact(pybind11::module& m)
{
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!
.def_readwrite("global_potential", &fiction::quickexact_params<fiction::offset::ucoord_t>::global_potential,
DOC(fiction_quickexact_params_global_potential));

detail::quickexact<py_sidb_100_lattice>(m, "_100");
detail::quickexact<py_sidb_111_lattice>(m, "_111");
detail::quickexact<py_sidb_100_lattice>(m);
detail::quickexact<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ namespace detail
{

template <typename Lyt>
void random_layout_generator(pybind11::module& m, const std::string& lattice = "")
void random_layout_generator(pybind11::module& m)
{
using namespace pybind11::literals;

m.def(fmt::format("generate_random_sidb_layout{}", lattice).c_str(), &fiction::generate_random_sidb_layout<Lyt>,
"lyt_skeleton"_a, "params"_a = fiction::generate_random_sidb_layout_params<fiction::cell<Lyt>>{},
m.def("generate_random_sidb_layout", &fiction::generate_random_sidb_layout<Lyt>, "lyt_skeleton"_a, "params"_a = fiction::generate_random_sidb_layout_params<fiction::cell<Lyt>>{},
DOC(fiction_generate_random_sidb_layout));

m.def(fmt::format("generate_multiple_random_sidb_layouts{}", lattice).c_str(),
&fiction::generate_multiple_random_sidb_layouts<Lyt>, "lyt_skeleton"_a,
m.def("generate_multiple_random_sidb_layouts", &fiction::generate_multiple_random_sidb_layouts<Lyt>, "lyt_skeleton"_a,
"params"_a = fiction::generate_random_sidb_layout_params<fiction::cell<Lyt>>{},
DOC(fiction_generate_multiple_random_sidb_layouts));
}
Expand Down Expand Up @@ -82,8 +80,8 @@ inline void random_sidb_layout_generator(pybind11::module& m)
DOC(fiction_generate_random_sidb_layout_params_maximal_attempts_for_multiple_layouts));
// NOTE be careful with the order of the following calls! Python will resolve the first matching overload!

detail::random_layout_generator<py_sidb_100_lattice>(m, "_100");
detail::random_layout_generator<py_sidb_111_lattice>(m, "_111");
detail::random_layout_generator<py_sidb_100_lattice>(m);
detail::random_layout_generator<py_sidb_111_lattice>(m);
}

} // namespace pyfiction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_three_DBs_100_lattice(self):
heuristic_results = sidb_simulation_result_100()
heuristic_results.charge_distributions = [cds4]

self.assertTrue(is_ground_state_100(heuristic_results, exact_results))
self.assertTrue(is_ground_state(heuristic_results, exact_results))

def test_three_DBs_111_lattice(self):
layout = sidb_lattice_111((10, 10))
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_three_DBs_111_lattice(self):
heuristic_results = sidb_simulation_result_111()
heuristic_results.charge_distributions = [cds4]

self.assertTrue(is_ground_state_111(heuristic_results, exact_results))
self.assertTrue(is_ground_state(heuristic_results, exact_results))


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_three_DBs_100_lattice(self):
cds4.assign_charge_state((4, 1), sidb_charge_state.POSITIVE)
cds4.assign_charge_state((6, 1), sidb_charge_state.NEGATIVE)

result = minimum_energy_100([cds1, cds2, cds3, cds4])
result = minimum_energy([cds1, cds2, cds3, cds4])
self.assertLessEqual(result, 0)

def test_three_DBs_111_lattice(self):
Expand All @@ -44,7 +44,7 @@ def test_three_DBs_111_lattice(self):
cds4.assign_charge_state((4, 1), sidb_charge_state.POSITIVE)
cds4.assign_charge_state((6, 1), sidb_charge_state.NEGATIVE)

result = minimum_energy_111([cds1, cds2, cds3, cds4])
result = minimum_energy([cds1, cds2, cds3, cds4])
self.assertLessEqual(result, 0)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ def test_three_sidbs(self):
layout.assign_cell_type((1, 0), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((2, 0), sidb_technology.cell_type.NORMAL)

params = quickexact_params_100()
params.simulation_parameters_100.base = 2
params.simulation_parameters_100.mu_minus = -0.25
params.base_number_detection_100 = automatic_base_number_detection_100.OFF
self.assertEqual(params.simulation_parameters_100.mu_minus, -0.25)
self.assertEqual(params.base_number_detection_100, automatic_base_number_detection_100.OFF)
params = quickexact_params()
params.simulation_parameters.base = 2
params.simulation_parameters.mu_minus = -0.25
params.base_number_detection = automatic_base_number_detection.OFF
self.assertEqual(params.simulation_parameters.mu_minus, -0.25)
self.assertEqual(params.base_number_detection, automatic_base_number_detection.OFF)

cds = charge_distribution_surface_100(layout)

result = quickexact_100(cds, params)
result = quickexact(cds, params)

self.assertEqual(result.algorithm_name, "QuickExact")
self.assertLessEqual(len(result.charge_distributions), 3)

params.base_number_detection_100 = automatic_base_number_detection_100.ON
self.assertEqual(params.base_number_detection_100, automatic_base_number_detection_100.ON)
params.base_number_detection = automatic_base_number_detection.ON
self.assertEqual(params.base_number_detection, automatic_base_number_detection.ON)

result = quickexact_100(cds, params)
result = quickexact(cds, params)
self.assertLessEqual(len(result.charge_distributions), 4)

params.simulation_parameters_100.epsilon_r = 2
params.simulation_parameters_100.lambda_tf = 2
result = quickexact_100(cds, params)
params.simulation_parameters.epsilon_r = 2
params.simulation_parameters.lambda_tf = 2
result = quickexact(cds, params)
self.assertLessEqual(len(result.charge_distributions), 2)

def test_perturber_and_sidb_pair_111(self):
Expand All @@ -42,16 +42,16 @@ def test_perturber_and_sidb_pair_111(self):
layout.assign_cell_type((2, 0), sidb_technology.cell_type.NORMAL)
layout.assign_cell_type((3, 0), sidb_technology.cell_type.NORMAL)

params = quickexact_params_111()
params.simulation_parameters_111.base = 2
params.simulation_parameters_111.mu_minus = -0.32
params.base_number_detection_111 = automatic_base_number_detection_111.OFF
self.assertEqual(params.simulation_parameters_111.mu_minus, -0.32)
self.assertEqual(params.base_number_detection_111, automatic_base_number_detection_111.OFF)
params = quickexact_params()
params.simulation_parameters.base = 2
params.simulation_parameters.mu_minus = -0.32
params.base_number_detection = automatic_base_number_detection.OFF
self.assertEqual(params.simulation_parameters.mu_minus, -0.32)
self.assertEqual(params.base_number_detection, automatic_base_number_detection.OFF)

cds = charge_distribution_surface_111(layout)

result = quickexact_111(cds, params)
result = quickexact(cds, params)

self.assertEqual(result.algorithm_name, "QuickExact")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_area_with_one_coordinate_100_lattice(self):
params = generate_random_sidb_layout_params()
params.number_of_sidbs = 1
params.coordinate_pair = ((10,10), (10,10))
result_lyt = generate_random_sidb_layout_100(sidb_lattice_100(), params)
result_lyt = generate_random_sidb_layout(sidb_lattice_100(), params)
self.assertEqual(result_lyt.num_cells(), 1)
cell = (result_lyt.cells())[0]
self.assertEqual(cell.x, 10)
Expand All @@ -19,7 +19,7 @@ def test_area_with_one_coordinate_111_lattice(self):
params = generate_random_sidb_layout_params()
params.number_of_sidbs = 1
params.coordinate_pair = ((10, 10), (10, 10))
result_lyt = generate_random_sidb_layout_111(sidb_lattice_111(), params)
result_lyt = generate_random_sidb_layout(sidb_lattice_111(), params)
self.assertEqual(result_lyt.num_cells(), 1)
cell = (result_lyt.cells())[0]
self.assertEqual(cell.x, 10)
Expand Down
17 changes: 2 additions & 15 deletions cli/cmd/simulation/quickexact.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ class quickexact_command : public command
{
is_sidb_100_lattice = false;
params.simulation_parameters = physical_params;
auto cps = convert_params<Lyt>(params);
sim_result_111 = fiction::quickexact(*lyt_ptr, cps);
sim_result_111 = fiction::quickexact(*lyt_ptr, params);
}

else
Expand Down Expand Up @@ -170,7 +169,7 @@ class quickexact_command : public command
/**
* QuickExact parameters.
*/
fiction::quickexact_params<fiction::sidb_100_cell_clk_lyt> params{};
fiction::quickexact_params<fiction::offset::ucoord_t> params{};
/**
* Simulation result for H-Si(100)-2x1 surface.
*/
Expand Down Expand Up @@ -239,18 +238,6 @@ class quickexact_command : public command
sim_result_100 = {};
sim_result_111 = {};
}

template <typename LytDest, typename LytSrc>
[[nodiscard]] fiction::quickexact_params<LytDest>
convert_params(const fiction::quickexact_params<LytSrc>& ps_src) const noexcept
{
fiction::quickexact_params<LytDest> ps_dest{};

ps_dest.simulation_parameters = ps_src.simulation_parameters;
ps_dest.global_potential = ps_src.global_potential;

return ps_dest;
}
};

ALICE_ADD_COMMAND(quickexact, "Simulation")
Expand Down
Loading

0 comments on commit 8e1d2d2

Please sign in to comment.