NEST main@caf0ae8
 
Loading...
Searching...
No Matches
layer.h
Go to the documentation of this file.
1/*
2 * layer.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef LAYER_H
24#define LAYER_H
25
26// C++ includes:
27#include <bitset>
28#include <iostream>
29#include <utility>
30
31// Includes from nestkernel:
32#include "kernel_manager.h"
33#include "nest_names.h"
34#include "nest_types.h"
35
36
37// Includes from spatial:
38#include "connection_creator.h"
39#include "ntree.h"
40#include "position.h"
41
42namespace nest
43{
44
45class AbstractLayer;
46typedef std::shared_ptr< AbstractLayer > AbstractLayerPTR;
47
52{
53public:
56 {
57 }
58
59 virtual ~AbstractLayer();
60
66 virtual void set_status( const Dictionary& ) = 0;
67
75 virtual void get_status( Dictionary&, NodeCollection const* const ) const = 0;
76
77 virtual unsigned int get_num_dimensions() const = 0;
78
85 virtual std::vector< double > get_position_vector( const size_t lid ) const = 0;
86
95 virtual std::vector< double > compute_displacement( const std::vector< double >& from_pos,
96 const size_t to ) const = 0;
97 virtual double compute_displacement( const std::vector< double >& from_pos,
98 const std::vector< double >& to_pos,
99 const unsigned int dimension ) const = 0;
100
109 virtual double compute_distance( const std::vector< double >& from_pos, const size_t lid ) const = 0;
110 virtual double compute_distance( const std::vector< double >& from_pos,
111 const std::vector< double >& to_pos ) const = 0;
112
123 virtual void connect( NodeCollectionPTR source_nc,
124 AbstractLayerPTR target,
125 NodeCollectionPTR target_nc,
126 ConnectionCreator& connector ) = 0;
127
135
145 virtual std::vector< size_t > get_global_nodes( const MaskPTR mask,
146 const std::vector< double >& anchor,
147 bool allow_oversized,
148 NodeCollectionPTR node_collection ) = 0;
149
157 virtual void dump_nodes( std::ostream& os ) const = 0;
158
170 virtual void dump_connections( std::ostream& out,
171 NodeCollectionPTR node_collection,
172 AbstractLayerPTR target_layer,
173 const std::string& syn_model ) = 0;
174
177
178protected:
183
188
193
197 virtual void clear_ntree_cache_() const = 0;
198
202 virtual void clear_vector_cache_() const = 0;
203
208};
209
210template < int D >
211class MaskedLayer;
212
216template < int D >
217class Layer : public AbstractLayer
218{
219public:
223 Layer();
224
225 Layer( const Layer& other_layer );
226
227 ~Layer() override;
228
235 void set_status( const Dictionary& ) override;
236
238 void get_status( Dictionary&, NodeCollection const* const ) const override;
239
240 unsigned int
241 get_num_dimensions() const override
242 {
243 return D;
244 }
245
249 const Position< D >&
251 {
252 return lower_left_;
253 }
254
258 const Position< D >&
260 {
261 return extent_;
262 }
263
269 {
270 return lower_left_ + extent_ / 2;
271 }
272
276 std::bitset< D >
278 {
279 return periodic_;
280 }
281
288 virtual Position< D > get_position( size_t sind ) const = 0;
289
294 std::vector< double > get_position_vector( const size_t sind ) const override;
295
304 Position< D > compute_displacement( const Position< D >& from_pos, const Position< D >& to_pos ) const;
305 double compute_displacement( const std::vector< double >& from_pos,
306 const std::vector< double >& to_pos,
307 const unsigned int dimension ) const override;
308
317 Position< D > compute_displacement( const Position< D >& from_pos, const size_t to ) const;
318
319 std::vector< double > compute_displacement( const std::vector< double >& from_pos, const size_t to ) const override;
320
329 double compute_distance( const Position< D >& from_pos, const size_t lid ) const;
330
331 double compute_distance( const std::vector< double >& from_pos, const size_t lid ) const override;
332
333 double compute_distance( const std::vector< double >& from_pos, const std::vector< double >& to_pos ) const override;
334
335
344 std::shared_ptr< Ntree< D, size_t > > get_global_positions_ntree( NodeCollectionPTR node_collection );
345
353 std::shared_ptr< Ntree< D, size_t > > get_global_positions_ntree( std::bitset< D > periodic,
354 Position< D > lower_left,
355 Position< D > extent,
356 NodeCollectionPTR node_collection );
357
358 std::vector< std::pair< Position< D >, size_t > >* get_global_positions_vector( NodeCollectionPTR node_collection );
359
360 virtual std::vector< std::pair< Position< D >, size_t > > get_global_positions_vector( const MaskPTR mask,
361 const Position< D >& anchor,
362 bool allow_oversized,
363 NodeCollectionPTR node_collection );
364
368 std::vector< size_t > get_global_nodes( const MaskPTR mask,
369 const std::vector< double >& anchor,
370 bool allow_oversized,
371 NodeCollectionPTR node_collection ) override;
372
383 void connect( NodeCollectionPTR source_nc,
384 AbstractLayerPTR target,
385 NodeCollectionPTR target_nc,
386 ConnectionCreator& connector ) override;
387
395 void dump_nodes( std::ostream& os ) const override;
396
409 void dump_connections( std::ostream& out,
410 NodeCollectionPTR node_collection,
411 AbstractLayerPTR target_layer,
412 const std::string& syn_model ) override;
413
414protected:
418 void clear_ntree_cache_() const override;
419
423 void clear_vector_cache_() const override;
424
425 std::shared_ptr< Ntree< D, size_t > > do_get_global_positions_ntree_( NodeCollectionPTR node_collection );
426
430 virtual void insert_global_positions_ntree_( Ntree< D, size_t >& tree, NodeCollectionPTR node_collection ) = 0;
431
435 virtual void insert_global_positions_vector_( std::vector< std::pair< Position< D >, size_t > >&,
436 NodeCollectionPTR ) = 0;
437
441 std::bitset< D > periodic_;
442
446 static std::shared_ptr< Ntree< D, size_t > > cached_ntree_;
447 static std::vector< std::pair< Position< D >, size_t > >* cached_vector_;
448
449 friend class MaskedLayer< D >;
450};
451
456template < int D >
458{
459public:
469 MaskedLayer( Layer< D >& layer, const MaskPTR mask, bool allow_oversized, NodeCollectionPTR node_collection );
470
483 MaskedLayer( Layer< D >& layer,
484 const MaskPTR mask,
485 bool allow_oversized,
486 Layer< D >& target,
487 NodeCollectionPTR node_collection );
488
489 ~MaskedLayer();
490
499
504
505protected:
517 void check_mask_( Layer< D >& layer, bool allow_oversized );
518
519 std::shared_ptr< Ntree< D, size_t > > ntree_;
521};
522
523inline void
525{
526 node_collection_ = node_collection;
527}
528
529
535
536template < int D >
538 const MaskPTR maskd,
539 bool allow_oversized,
540 NodeCollectionPTR node_collection )
541 : mask_( maskd )
542{
543 ntree_ = layer.get_global_positions_ntree( node_collection );
544
545 check_mask_( layer, allow_oversized );
546}
547
548template < int D >
550 const MaskPTR maskd,
551 bool allow_oversized,
552 Layer< D >& target,
553 NodeCollectionPTR node_collection )
554 : mask_( maskd )
555{
557 target.get_periodic_mask(), target.get_lower_left(), target.get_extent(), node_collection );
558
559 check_mask_( target, allow_oversized );
560 mask_ = MaskPTR( new ConverseMask< D >( dynamic_cast< const Mask< D >& >( *mask_ ) ) );
561}
562
563template < int D >
567
568template < int D >
571{
572 try
573 {
574 return ntree_->masked_begin( dynamic_cast< const Mask< D >& >( *mask_ ), anchor );
575 }
576 catch ( std::bad_cast& e )
577 {
578 throw BadProperty( "Mask is incompatible with layer." );
579 }
580}
581
582template < int D >
585{
586 return ntree_->masked_end();
587}
588
589template < int D >
591{
592 // Default center (0,0) and extent (1,1)
593 for ( int i = 0; i < D; ++i )
594 {
595 lower_left_[ i ] = -0.5;
596 extent_[ i ] = 1.0;
597 }
598}
599
600template < int D >
601inline Layer< D >::Layer( const Layer& other_layer )
602 : AbstractLayer( other_layer )
603 , lower_left_( other_layer.lower_left_ )
604 , extent_( other_layer.extent_ )
605 , periodic_( other_layer.periodic_ )
606{
607}
608
609template < int D >
611{
612 if ( cached_ntree_md_ == get_metadata() )
613 {
614 clear_ntree_cache_();
615 }
616
617 if ( cached_vector_md_ == get_metadata() )
618 {
619 clear_vector_cache_();
620 }
621}
622
623template < int D >
624inline Position< D >
625Layer< D >::compute_displacement( const Position< D >& from_pos, const size_t to_lid ) const
626{
627 return compute_displacement( from_pos, get_position( to_lid ) );
628}
629
630template < int D >
631inline std::vector< double >
632Layer< D >::compute_displacement( const std::vector< double >& from_pos, const size_t to_lid ) const
633{
634 return std::vector< double >( compute_displacement( Position< D >( from_pos ), to_lid ).get_vector() );
635}
636
637template < int D >
638inline double
639Layer< D >::compute_distance( const Position< D >& from_pos, const size_t lid ) const
640{
641 return compute_displacement( from_pos, lid ).length();
642}
643
644template < int D >
645inline double
646Layer< D >::compute_distance( const std::vector< double >& from_pos, const size_t lid ) const
647{
648 return compute_displacement( Position< D >( from_pos ), lid ).length();
649}
650
651template < int D >
652inline double
653Layer< D >::compute_distance( const std::vector< double >& from_pos, const std::vector< double >& to_pos ) const
654{
655 double squared_displacement = 0;
656 for ( unsigned int i = 0; i < D; ++i )
657 {
658 const double displacement = compute_displacement( from_pos, to_pos, i );
659 squared_displacement += displacement * displacement;
660 }
661 return std::sqrt( squared_displacement );
662}
663
664template < int D >
665inline std::vector< double >
666Layer< D >::get_position_vector( const size_t sind ) const
667{
668 return get_position( sind ).get_vector();
669}
670
671template < int D >
672inline void
674{
675 cached_ntree_ = std::shared_ptr< Ntree< D, size_t > >();
676 cached_ntree_md_ = NodeCollectionMetadataPTR( nullptr );
677}
678
679template < int D >
680inline void
682{
683 if ( cached_vector_ != 0 )
684 {
685 delete cached_vector_;
686 }
687 cached_vector_ = 0;
688 cached_vector_md_ = NodeCollectionMetadataPTR( nullptr );
689}
690
691} // namespace nest
692
693#endif
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
Abstract base class for Layers of unspecified dimension.
Definition layer.h:52
AbstractLayer()
Definition layer.h:54
virtual double compute_distance(const std::vector< double > &from_pos, const size_t lid) const =0
Returns distance to node from given position.
virtual void set_status(const Dictionary &)=0
Change properties of the layer according to the entries in the dictionary.
virtual void clear_vector_cache_() const =0
Clear the cache for global position information.
static NodeCollectionMetadataPTR cached_ntree_md_
Metadata for the layer for which we cache global position information.
Definition layer.h:187
virtual std::vector< double > compute_displacement(const std::vector< double > &from_pos, const size_t to) const =0
Returns displacement of node from given position.
NodeCollectionPTR node_collection_
The NodeCollection to which the layer belongs.
Definition layer.h:182
void set_node_collection(NodeCollectionPTR)
Definition layer.h:524
NodeCollectionMetadataPTR get_metadata() const
Gets metadata of the NodeCollection to which this layer belongs.
Definition layer.cpp:159
virtual std::vector< double > get_position_vector(const size_t lid) const =0
Get position of node.
virtual ~AbstractLayer()
Definition layer.cpp:47
virtual std::vector< size_t > get_global_nodes(const MaskPTR mask, const std::vector< double > &anchor, bool allow_oversized, NodeCollectionPTR node_collection)=0
Return a vector with the node IDs of the nodes inside the mask.
virtual void get_status(Dictionary &, NodeCollection const *const) const =0
Export properties of the layer by setting entries in the status dictionary, respects slicing of given...
virtual double compute_distance(const std::vector< double > &from_pos, const std::vector< double > &to_pos) const =0
virtual void connect(NodeCollectionPTR source_nc, AbstractLayerPTR target, NodeCollectionPTR target_nc, ConnectionCreator &connector)=0
Connect this layer to the given target layer.
static NodeCollectionPTR create_layer(const Dictionary &)
Factory function for layers.
Definition layer.cpp:52
NodeCollectionPTR get_node_collection()
Definition layer.h:531
virtual unsigned int get_num_dimensions() const =0
virtual void dump_nodes(std::ostream &os) const =0
Write layer data to stream.
virtual double compute_displacement(const std::vector< double > &from_pos, const std::vector< double > &to_pos, const unsigned int dimension) const =0
static NodeCollectionMetadataPTR cached_vector_md_
Metadata for the layer for which we cache global position information.
Definition layer.h:192
virtual void dump_connections(std::ostream &out, NodeCollectionPTR node_collection, AbstractLayerPTR target_layer, const std::string &syn_model)=0
Dumps information about all connections of the given type having their source in the given layer to t...
virtual void clear_ntree_cache_() const =0
Clear the cache for global position information.
Exception to be thrown if a status parameter is incomplete or inconsistent.
Definition exceptions.h:680
This class is a representation of the dictionary of connection properties given as an argument to the...
Definition connection_creator.h:63
Mask oriented in the opposite direction.
Definition mask.h:633
Abstract base class for Layer of given dimension (D=2 or 3).
Definition layer.h:218
void clear_vector_cache_() const override
Clear the cache for global position information.
Definition layer.h:681
std::vector< double > get_position_vector(const size_t sind) const override
Definition layer.h:666
double compute_distance(const Position< D > &from_pos, const size_t lid) const
Returns distance to node from given position.
Definition layer.h:639
virtual void insert_global_positions_ntree_(Ntree< D, size_t > &tree, NodeCollectionPTR node_collection)=0
Insert global position info into ntree.
virtual Position< D > get_position(size_t sind) const =0
Get position of node.
std::shared_ptr< Ntree< D, size_t > > get_global_positions_ntree(NodeCollectionPTR node_collection)
Get positions for all nodes in layer, including nodes on other MPI processes.
Definition layer_impl.h:140
void clear_ntree_cache_() const override
Clear the cache for global position information.
Definition layer.h:673
Position< D > extent_
size of layer
Definition layer.h:440
Layer()
Creates an empty layer.
Definition layer.h:590
Position< D > get_center() const
Definition layer.h:268
Position< D > compute_displacement(const Position< D > &from_pos, const Position< D > &to_pos) const
Returns displacement of a position from another position.
Definition layer_impl.h:46
~Layer() override
Definition layer.h:610
static std::vector< std::pair< Position< D >, size_t > > * cached_vector_
Definition layer.h:447
std::shared_ptr< Ntree< D, size_t > > do_get_global_positions_ntree_(NodeCollectionPTR node_collection)
Definition layer_impl.h:189
unsigned int get_num_dimensions() const override
Definition layer.h:241
void set_status(const Dictionary &) override
Change properties of the layer according to the entries in the dictionary.
Definition layer_impl.h:80
void connect(NodeCollectionPTR source_nc, AbstractLayerPTR target, NodeCollectionPTR target_nc, ConnectionCreator &connector) override
Connect this layer to the given target layer.
Definition layer_impl.h:117
void dump_nodes(std::ostream &os) const override
Write layer data to stream.
Definition layer_impl.h:283
std::bitset< D > get_periodic_mask() const
Definition layer.h:277
std::vector< size_t > get_global_nodes(const MaskPTR mask, const std::vector< double > &anchor, bool allow_oversized, NodeCollectionPTR node_collection) override
Return a vector with the node IDs of the nodes inside the mask.
Definition layer_impl.h:267
const Position< D > & get_extent() const
Definition layer.h:259
void get_status(Dictionary &, NodeCollection const *const) const override
Retrieve status, slice according to node collection if given.
Definition layer_impl.h:93
std::bitset< D > periodic_
periodic b.c.
Definition layer.h:441
static std::shared_ptr< Ntree< D, size_t > > cached_ntree_
Global position information for a single layer.
Definition layer.h:446
void dump_connections(std::ostream &out, NodeCollectionPTR node_collection, AbstractLayerPTR target_layer, const std::string &syn_model) override
Dumps information about all connections of the given type having their source in the given layer to t...
Definition layer_impl.h:297
std::vector< std::pair< Position< D >, size_t > > * get_global_positions_vector(NodeCollectionPTR node_collection)
Definition layer_impl.h:210
const Position< D > & get_lower_left() const
Definition layer.h:250
virtual void insert_global_positions_vector_(std::vector< std::pair< Position< D >, size_t > > &, NodeCollectionPTR)=0
Insert global position info into vector.
Position< D > lower_left_
lower left corner (minimum coordinates) of layer
Definition layer.h:439
Abstract base class for masks with given dimension.
Definition mask.h:101
Class for applying masks to layers.
Definition layer.h:458
void check_mask_(Layer< D > &layer, bool allow_oversized)
Will check that the mask can be applied to the layer.
Definition layer_impl.h:355
~MaskedLayer()
Definition layer.h:564
Ntree< D, size_t >::masked_iterator begin(const Position< D > &anchor)
Iterate over nodes inside mask.
Definition layer.h:570
Ntree< D, size_t >::masked_iterator end()
Definition layer.h:584
MaskedLayer(Layer< D > &layer, const MaskPTR mask, bool allow_oversized, NodeCollectionPTR node_collection)
Regular constructor.
Definition layer.h:537
std::shared_ptr< Ntree< D, size_t > > ntree_
Definition layer.h:519
MaskPTR mask_
Definition layer.h:520
Superclass for NodeCollections.
Definition node_collection.h:565
Iterator iterating the nodes in a Quadtree inside a Mask.
Definition ntree.h:153
A Ntree object represents a subtree or leaf in a Ntree structure.
Definition ntree.h:55
Definition position.h:57
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
std::shared_ptr< AbstractMask > MaskPTR
Definition mask.h:44
std::shared_ptr< AbstractLayer > AbstractLayerPTR
Definition layer.h:46
std::shared_ptr< NodeCollectionMetadata > NodeCollectionMetadataPTR
Definition node_collection.h:51
std::vector< std::vector< double > > displacement(NodeCollectionPTR layer_to_nc, NodeCollectionPTR layer_from_nc)
Definition spatial.cpp:137
Dictionary get_metadata(const NodeCollectionPTR nc)
Definition nest.cpp:410
std::shared_ptr< NodeCollection > NodeCollectionPTR
Definition node_collection.h:50
std::vector< std::vector< double > > get_position(NodeCollectionPTR layer_nc)
Definition spatial.cpp:83