Skip to content

Commit

Permalink
Merge pull request #13 from Becheler/develop
Browse files Browse the repository at this point in the history
docs: test code for tutorial 4 on splittable populations
  • Loading branch information
Becheler authored Jan 11, 2022
2 parents e34355f + 936cfb2 commit 35f8c15
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
43 changes: 37 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,41 @@ target_include_directories(model_3
add_test(SpatialExpansionMassBasedSerialized model_3 ${CMAKE_CURRENT_SOURCE_DIR}/data/europe_temp.tif)

### TUTORIAL TESTS
#
# add_executable(tuto_1 tutorials_test/tuto_1.cpp)
# target_link_libraries(tuto_1 LINK_PUBLIC ${GDAL_LIBRARY} )
# target_include_directories(tuto_1
# PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
# $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
# add_test(TwoDemesIndividualBased tuto_1)

add_executable(tuto_1 tutorials_test/tuto_1.cpp)
target_link_libraries(tuto_1 LINK_PUBLIC ${GDAL_LIBRARY} )
target_include_directories(tuto_1
PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
add_test(TwoDemesIndividualBased tuto_1)

# Keep test files in a separate source directory called test
file(GLOB TUTO_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} tutorials_test/*.cpp)

# Run through each source
foreach(testSrc ${TUTO_SRCS})
# Extract the filename without an extension (NAME_WE)
get_filename_component(testName ${testSrc} NAME_WE)
# Add compile target
add_executable(${testName} ${testSrc})
# Link to Boost libraries AND other targets and dependencies
target_link_libraries(${testName}
${Boost_LIBRARIES}
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
${GDAL_LIBRARY}
Boost::headers)
# I like to move testing binaries into a testBin directory
set_target_properties(${testName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/testBin)
# Specifies include directories to use when compiling a given target
target_include_directories(
${testName} PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
#Finally add it to test execution: notice the WORKING_DIRECTORY and COMMAND
add_test(NAME ${testName}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/testBin
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/build/testBin/${testName})
endforeach(testSrc)
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ int main(){
{
std::cout << "N(" << x << "," << nb_generations -1 << ") = " << N(x,t) << std::endl;
}
return 0;
} // end main
45 changes: 45 additions & 0 deletions test/tutorials_test/tuto_4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "quetzal/demography.h"
#include <iostream>
#include <random>
#include <map>
// Define a customized transition matrix
struct transition_matrix
{
// Two demes, but identified by string
using coord_type = std::string;
using time_type = int;
// Mandatory interface with the quetzal algorithms:
// Take a the coordinate of start
// Returns the vector of possible arrival coordinates
std::vector<coord_type> arrival_space(coord_type x)
{
// Whatever the coordinate to start from, redirect them either in Paris or A.A
return {"Paris", "Ann Arbor"};
}
// Second mandatory interface: what is the probability to move from x to y?
double operator()(coord_type x, coord_type y)
{
return 0.5; // 1/2 probability to change of location
}
};
int main(){
using coord_type = std::string;
using time_type = int;
using generator_type = std::mt19937;
using quetzal::demography::demographic_policy::mass_based;
using quetzal::demography::memory_policy::on_RAM;
int N0 = 100;
// Number of non-overlapping generations for the demographic simulation
int nb_generations = 10;
quetzal::demography::History<coord_type, mass_based, on_RAM> history("Paris", N0, nb_generations);
// Growth function
auto N = history.get_functor_N();
auto growth = [N](auto& gen, coord_type x, time_type t){ return 2*N(x,t) ; };
// Random number generation
generator_type gen;
// Define the transition probabilities
transition_matrix M;
// Pass everything to the simulation
history.simulate_forward(growth, M, gen);
return 0;
}

0 comments on commit 35f8c15

Please sign in to comment.