Skip to content

Commit

Permalink
feat: integrating geography spatial graph with C++23
Browse files Browse the repository at this point in the history
  • Loading branch information
Becheler committed Jan 7, 2024
1 parent 10b934b commit 19712ab
Show file tree
Hide file tree
Showing 11 changed files with 592 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/Dockerfile-ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ RUN apt-get update -y
RUN apt-get install -y --no-install-recommends\
vim \
git \
gcc-12 \
g++-12 \
gcc-13 \
g++-13 \
build-essential \
libboost-all-dev \
cmake \
Expand Down
4 changes: 2 additions & 2 deletions docs/4-tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -853,11 +853,11 @@ Quetzal incorporates various kernel types available in the `quetzal::demography:
**Input**
@include{lineno} dispersal_kernel.cpp
@include{lineno} demography_dispersal_kernel.cpp
**Output**
@include{lineno} dispersal_kernel.txt
@include{lineno} demography_dispersal_kernel.txt
---
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ foreach(testSrc ${SRCS})
# Add compile target
add_executable(${testName} ${testSrc})
# Require the standard
target_compile_features(${testName} PUBLIC cxx_std_20)
target_compile_features(${testName} PUBLIC cxx_std_23)
# Ignore warnings about subtle ABI change
target_compile_options(${testName} PUBLIC "-Wno-psabi")
# Link to Boost libraries AND other targets and dependencies
Expand Down
File renamed without changes.
25 changes: 13 additions & 12 deletions example/spatial_graph.cpp → example/geography_graph_complete.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "quetzal/quetzal.hpp"
#include <filesystem>
#include <cassert>

using namespace quetzal;
//#include <print>

int main()
{
Expand All @@ -18,15 +17,17 @@ int main()
auto env = quetzal::geography::landscape<>::from_files( { {"bio1", file1}, {"bio12", file2} }, times );
std::cout << env << std::endl;

// // Embed default demographic quantities along vertices (N_{xt}) and edges (Phi_{xyt})
// auto graph1 = env.to_4_neighborhood_graph<>();
// auto graph2 = env.to_8_neighborhood_graph<>();
// auto graph3 = env.to_complete_graph<>();
// We convert the grid to a fully connected graph
auto graph = env.to_complete_graph<>();

// A little helper function
auto constexpr sphere_distance = [&]( auto const& p1, auto const& p2 ){
return env.to_latlon( p1 ).great_circle_distance( env.to_latlon( p2 ) );}

auto v = graph.edges()
| std::transform( [&](auto const& e){ sphere_distance( graph.source( e ), graph.target( e ) );} )
| std::transform( [&](auto const& d){ return quetzal::demography::dispersal_kernel::exponential_power( d, {.a=1., .b=5.5} ) ;} )
| std::ranges::to<std::vector>();

// auto weights graph.edges()
// | [](const & e){ return { graph.source(e), graph.target(e)}; }
// | [](const & p){ return { env.to_latlon(s), graph.to_latlon(t)}; }
// | [](const & p){ return x.great_circle_distance_to(y);}
// | [](const & d){ return quetzal::demography::dispersal_kernel::ExponentialPower(d, {.a=1., .b=5.5});}
// | []()
//std::println("{}", v);
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ target_include_directories(${PROJECT_NAME}
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# If we have compiler requirements for this library, list them here
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_23)

###########################
# INSTALLATION
Expand Down
1 change: 1 addition & 0 deletions src/include/quetzal/geography.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "geography/landscape.hpp"
#include "geography/coordinates.hpp"
#include "geography/graph/graph.hpp"
///
/// @brief Geospatial data formatting and processing
///
Expand Down
33 changes: 33 additions & 0 deletions src/include/quetzal/geography/graph/detail/adl_resolution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Arnaud Becheler <abechele@umich.edu>

/////////////////////////////////////////////////////////////////////////// *
/// This program is free software; you can redistribute it and/or modify ///
/// it under the terms of the GNU General Public License as published by ///
/// the Free Software Foundation; either version 2 of the License, or ///
/// (at your option) any later version. ///
/// ///
///////////////////////////////////////////////////////////////////////////

#pragma once

namespace quetzal::geography::detail
{
namespace adl_resolution
{
void add_edge() = delete;
void add_left_edge() = delete;
void add_right_edge() = delete;
void add_vertex() = delete;
void out_degree() = delete;
void root() = delete;
void is_left_successor() = delete;
void is_right_successor() = delete;
void has_predecessor() = delete;
void predecessor() = delete;
void depth_first_search() = delete;
void isomorphism() = delete;
void in_edges() = delete;
void degree() = delete;
void edge() = delete;
}
}
55 changes: 55 additions & 0 deletions src/include/quetzal/geography/graph/detail/graph_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2021 Arnaud Becheler <abechele@umich.edu>

/////////////////////////////////////////////////////////////////////////// *
/// This program is free software; you can redistribute it and/or modify ///
/// it under the terms of the GNU General Public License as published by ///
/// the Free Software Foundation; either version 2 of the License, or ///
/// (at your option) any later version. ///
/// ///
///////////////////////////////////////////////////////////////////////////

#pragma once

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_matrix.hpp>

namespace quetzal::geography::detail
{
///
/// @brief Defines the common desired graph properties and constraints for a complete and sparse spatial graph
///
struct common_graph_traits
{
/// @brief We want to enforce avoiding multi-graphs (edges with same end nodes)
using out_edge_list_type = boost::setS;

/// @brief We don't allow for inserting vertices except at the end and we don't remove vertices.
/// This means that neither reallocation cost nor stability are reasons for preferring listS to vecS.
using vertex_list_type = boost::vecS;

/// @brief We need bidirectionality for in-edges access
/// @remark Bidirectionality trades off storage/insertion overhead for the ability to get in-edges
/// and in-degree without having to enumerate all vertices or edges
using directed_type = boost::bidirectionalS;
};

///
/// @brief Defines the desired graph properties and constraints for a complete spatial graph
///
struct dense_graph_traits : public common_graph_traits
{
/// @brief Better to use the adjacency_matrix for dense graphs (E is close to V^2) rather than adjacency_list
template <class... Types>
using model = boost::adjacency_matrix<Types...>;
};

///
/// @brief Defines the desired graph properties and constraints for a 8 or 4 neighbors spatial graph
///
struct sparse_graph_traits : public common_graph_traits
{
/// @brief Better to use the adjacency_list for sparse graphs (E is closer to 4*V or 8*V than V^2)
template <class... Types>
using model = boost::adjacency_matrix<Types...>;
};
}
Loading

0 comments on commit 19712ab

Please sign in to comment.