Skip to content

Commit

Permalink
Add ForwardDemesGraph::number_of_demes() const. (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Sep 2, 2022
1 parent 6c21cd1 commit 09e53e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cpptests/test_forward_demes_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ BOOST_FIXTURE_TEST_CASE(test_zero_length_single_deme_model, SingleDemeModel)
BOOST_FIXTURE_TEST_CASE(single_deme_model_with_burn_in, SingleDemeModel)
{
fwdpy11_core::ForwardDemesGraph g(yaml, 10);
BOOST_REQUIRE_EQUAL(g.number_of_demes(), 1);
fwdpy11::DiploidPopulation pop(100, 1.0);
g.initialize_model(pop.generation);
auto end_time = g.model_end_time();
Expand All @@ -43,6 +44,7 @@ BOOST_FIXTURE_TEST_CASE(single_deme_model_with_burn_in_THE_CODE_WE_WANT_TO_WRITE
SingleDemeModel)
{
fwdpy11_core::ForwardDemesGraph g(yaml, 10);
BOOST_REQUIRE_EQUAL(g.number_of_demes(), 1);
fwdpy11::DiploidPopulation pop(100, 1.0);
g.initialize_model(pop.generation);
auto end_time = g.model_end_time();
Expand Down
1 change: 1 addition & 0 deletions lib/core/demes/forward_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ namespace fwdpy11_core
bool iterating_model() const;
void iterate_state();
std::uint32_t model_end_time() const;
std::ptrdiff_t number_of_demes() const;
};
}
17 changes: 16 additions & 1 deletion lib/demes/forward_graph.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cassert>
Expand All @@ -14,13 +15,21 @@ namespace fwdpy11_core
struct ForwardDemesGraph::forward_graph_implementation
{
graph_ptr_t graph;
std::ptrdiff_t number_of_demes;

forward_graph_implementation(const std::string &yaml, std::uint32_t burnin)
: graph(demes_forward_graph_allocate(), &demes_forward_graph_deallocate)
: graph(demes_forward_graph_allocate(), &demes_forward_graph_deallocate),
number_of_demes{-1}
{
auto code = demes_forward_graph_initialize_from_yaml(
yaml.c_str(), static_cast<double>(burnin), graph.get());
handle_error_code(code);
number_of_demes = demes_forward_graph_number_of_demes(graph.get());
if (number_of_demes < 1)
{
throw fwdpy11::discrete_demography::DemographyError(
"number of demes must be >= 1");
}
}

void handle_error_code(std::int32_t code) const;
Expand Down Expand Up @@ -135,4 +144,10 @@ namespace fwdpy11_core
pimpl->update_internal_state(*t);
}
};

std::ptrdiff_t
ForwardDemesGraph::number_of_demes() const
{
return pimpl->number_of_demes;
}
}

0 comments on commit 09e53e3

Please sign in to comment.