From 83d22e3dcfe528612209f52ab83b8ce8d37f80e9 Mon Sep 17 00:00:00 2001 From: Amrita Goswami Date: Thu, 18 Apr 2024 16:57:50 +0000 Subject: [PATCH] NetworkGeneration: Square lattice Now you can make a square lattice UndirectedNetwork. TODO : implement copy constructor to convert an UndirectedNetwork to a DirectedNetwork. --- include/network_generation.hpp | 66 +++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/include/network_generation.hpp b/include/network_generation.hpp index 8c84d98..af7d0de 100644 --- a/include/network_generation.hpp +++ b/include/network_generation.hpp @@ -1,5 +1,6 @@ #pragma once #include "directed_network.hpp" +#include "undirected_network.hpp" #include #include #include @@ -300,4 +301,67 @@ generate_square_lattice( size_t n_edge, typename DirectedNetwork::Wei return network; } -} // namespace Seldon::DirectedNetworkGeneration \ No newline at end of file + +} // namespace Seldon::DirectedNetworkGeneration + +namespace Seldon::UndirectedNetworkGeneration +{ +/* Constructs a new network on a square lattice of edge length n_edge (with PBCs)*/ +template +UndirectedNetwork +generate_square_lattice( size_t n_edge, typename UndirectedNetwork::WeightT weight = 0.0 ) +{ + using UndirectedNetworkT = UndirectedNetwork; + using WeightT = typename UndirectedNetworkT::WeightT; + auto n_agents = n_edge * n_edge; + + // Create an empty DirectedNetwork + auto network = UndirectedNetworkT( n_agents ); + + auto wrap_edge_index = [&]( int k ) + { + if( k >= int( n_edge ) ) + { + return k - int( n_edge ); + } + else if( k < 0 ) + { + return int( n_edge ) + k; + } + else + { + return k; + } + }; + + auto linear_index = [&]( int i, int j ) + { + auto idx = wrap_edge_index( i ) + n_edge * wrap_edge_index( j ); + return idx; + }; + + for( int i = 0; i < int( n_edge ); i++ ) + { + // other edge + for( int j = 0; j < int( n_edge ); j++ ) + { + // Central agent + auto central_index = linear_index( i, j ); + + // clang-format off + std::vector neighbours = { + linear_index( i - 1, j ), + linear_index( i, j - 1 ), + }; + + // clang-format on + for( size_t j_idx = 0; j_idx < neighbours.size(); j_idx++ ) + { + network.push_back_neighbour_and_weight( central_index, neighbours[j_idx], weight ); + } + } + } + + return network; +} +} // namespace Seldon::UndirectedNetworkGeneration \ No newline at end of file