NEST  2.6.0,not_revisioned_source_dir@0
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 #include <iostream>
27 #include <utility>
28 #include <bitset>
29 #include "nest.h"
30 #include "subnet.h"
31 #include "position.h"
32 #include "dictutils.h"
33 #include "topology_names.h"
34 #include "ntree.h"
35 #include "connection_creator.h"
36 #include "selector.h"
37 
38 namespace nest
39 {
40 
44  class AbstractLayer: public Subnet
45  {
46  public:
51  {}
52 
56  virtual ~AbstractLayer();
57 
63  virtual std::vector<double_t> get_position_vector(const index sind) const = 0;
64 
72  virtual std::vector<double_t> compute_displacement(const std::vector<double_t>& from_pos,
73  const index to) const = 0;
74 
82  virtual double_t compute_distance(const std::vector<double_t>& from_pos,
83  const index to) const = 0;
84 
92  virtual void connect(AbstractLayer& target, ConnectionCreator &connector) = 0;
93 
100  static index create_layer(const DictionaryDatum&);
101 
109  virtual std::vector<index> get_global_nodes(const MaskDatum &mask, const std::vector<double_t> &anchor, bool allow_oversized) = 0;
110 
117  virtual void dump_nodes(std::ostream & os) const = 0;
118 
126  virtual void dump_connections(std::ostream & out, const Token & syn_model) = 0;
127 
128  using Subnet::local_begin;
129  using Subnet::local_end;
130 
136  std::vector<Node*>::iterator local_begin(int_t depth);
137 
143  std::vector<Node*>::iterator local_end(int_t depth);
144 
150  std::vector<Node*>::const_iterator local_begin(int_t depth) const;
151 
157  std::vector<Node*>::const_iterator local_end(int_t depth) const;
158 
159  protected:
164 
169 
174 
178  virtual void clear_ntree_cache_() const = 0;
179 
183  virtual void clear_vector_cache_() const = 0;
184 
185  };
186 
187  template<int D>
188  class MaskedLayer;
189 
193  template <int D>
194  class Layer: public AbstractLayer
195  {
196  public:
200  Layer();
201 
205  Layer(const Layer &l);
206 
210  ~Layer();
211 
217  void set_status(const DictionaryDatum&);
218 
224  void get_status(DictionaryDatum&) const;
225 
229  const Position<D> & get_lower_left() const
230  { return lower_left_; }
231 
235  const Position<D> & get_extent() const
236  { return extent_; }
237 
242  { return lower_left_ + extent_/2; }
243 
247  std::bitset<D> get_periodic_mask() const
248  { return periodic_; }
249 
255  virtual Position<D> get_position(index sind) const = 0;
256 
261  std::vector<double_t> get_position_vector(const index sind) const;
262 
270  virtual Position<D> compute_displacement(const Position<D>& from_pos,
271  const Position<D>& to_pos) const;
272 
281  const index to) const;
282 
283  std::vector<double_t> compute_displacement(const std::vector<double_t>& from_pos,
284  const index to) const;
285 
293  double_t compute_distance(const Position<D>& from_pos,
294  const index to) const;
295 
296  double_t compute_distance(const std::vector<double_t>& from_pos,
297  const index to) const;
298 
299 
304 
313 
321 
322  std::vector<std::pair<Position<D>,index> >* get_global_positions_vector(Selector filter=Selector());
323 
324  virtual std::vector<std::pair<Position<D>,index> > get_global_positions_vector(Selector filter, const MaskDatum& mask, const Position<D>& anchor, bool allow_oversized);
325 
329  std::vector<index> get_global_nodes(const MaskDatum &mask, const std::vector<double_t> &anchor, bool allow_oversized);
330 
338  void connect(AbstractLayer& target, ConnectionCreator &connector);
339 
346  void dump_nodes(std::ostream & os) const;
347 
355  void dump_connections(std::ostream & out, const Token & syn_model);
356 
362  bool is_subnet() const
363  { return false; }
364 
365  protected:
369  void clear_ntree_cache_() const;
370 
374  void clear_vector_cache_() const;
375 
377 
381  virtual void insert_global_positions_ntree_(Ntree<D,index> & tree, const Selector& filter) = 0;
382 
386  virtual void insert_global_positions_vector_(std::vector<std::pair<Position<D>,index> > &, const Selector& filter) = 0;
387 
391  virtual void insert_local_positions_ntree_(Ntree<D,index> & tree, const Selector& filter) = 0;
392 
395  std::bitset<D> periodic_;
396 
401  static std::vector<std::pair<Position<D>,index> > * cached_vector_;
403 
404  friend class MaskedLayer<D>;
405  };
406 
411  template<int D>
412  class MaskedLayer {
413  public:
414 
423  MaskedLayer(Layer<D>& layer, Selector filter, const MaskDatum& mask, bool include_global, bool allow_oversized);
424 
437  MaskedLayer(Layer<D>& layer, Selector filter, const MaskDatum& mask, bool include_global, bool allow_oversized, Layer<D>& target);
438 
439  ~MaskedLayer();
440 
447 
452 
453  protected:
454 
465  void check_mask_(Layer<D>& layer, bool allow_oversized);
466 
469  };
470 
471  template<int D>
472  inline
473  MaskedLayer<D>::MaskedLayer(Layer<D>& layer, Selector filter, const MaskDatum& maskd, bool include_global, bool allow_oversized):
474  mask_(maskd)
475  {
476  if (include_global)
477  ntree_ = layer.get_global_positions_ntree(filter);
478  else
479  ntree_ = layer.get_local_positions_ntree(filter);
480 
481  check_mask_(layer, allow_oversized);
482  }
483 
484  template<int D>
485  inline
486  MaskedLayer<D>::MaskedLayer(Layer<D>& layer, Selector filter, const MaskDatum& maskd, bool include_global, bool allow_oversized, Layer<D>& target):
487  mask_(maskd)
488  {
489  if (include_global)
490  ntree_ = layer.get_global_positions_ntree(filter, target.get_periodic_mask(), target.get_lower_left(), target.get_extent());
491  //else
492  // ntree_ = layer.get_local_positions_ntree(filter, target.get_periodic_mask(), target.get_lower_left(), target.get_extent());
493 
494  check_mask_(target, allow_oversized);
495  mask_ = new ConverseMask<D>(dynamic_cast<const Mask<D>&>(*mask_));
496  }
497 
498  template<int D>
499  inline
501  {
502  }
503 
504  template<int D>
505  inline
507  {
508  try {
509  return ntree_->masked_begin(dynamic_cast<const Mask<D>&>(*mask_),anchor);
510  } catch (std::bad_cast e) {
511  throw BadProperty("Mask is incompatible with layer.");
512  }
513  }
514 
515  template<int D>
516  inline
518  {
519  return ntree_->masked_end();
520  }
521 
522  template<int D>
523  inline
525  {
526  // Default center (0,0) and extent (1,1)
527  for(int i=0;i<D;++i) {
528  lower_left_[i] = -0.5;
529  extent_[i] = 1.0;
530  }
531  }
532 
533  template<int D>
534  inline
535  Layer<D>::Layer(const Layer &l) :
536  AbstractLayer(l),
537  lower_left_(l.lower_left_),
538  extent_(l.extent_),
539  periodic_(l.periodic_)
540  {
541  }
542 
543  template<int D>
544  inline
546  {
547  if (cached_ntree_layer_ == get_gid()) {
548  clear_ntree_cache_();
549  }
550 
551  if (cached_vector_layer_ == get_gid()) {
552  clear_vector_cache_();
553  }
554  }
555 
556  template<int D>
557  inline
559  const index to) const
560  {
561  return compute_displacement(from_pos,get_position(to));
562  }
563 
564  template<int D>
565  inline
566  std::vector<double_t> Layer<D>::compute_displacement(const std::vector<double_t>& from_pos,
567  const index to) const
568  {
569  return std::vector<double_t>(compute_displacement(Position<D>(from_pos), to));
570  }
571 
572  template<int D>
573  inline
575  const index to) const
576  {
577  return compute_displacement(from_pos, to).length();
578  }
579 
580  template<int D>
581  inline
582  double_t Layer<D>::compute_distance(const std::vector<double_t>& from_pos,
583  const index to) const
584  {
585  return compute_displacement(Position<D>(from_pos), to).length();
586  }
587 
588  template<int D>
589  inline
590  std::vector<double_t> Layer<D>::get_position_vector(const index sind) const
591  {
592  return std::vector<double_t>(get_position(sind));
593  }
594 
595  template <int D>
596  inline
598  {
599  cached_ntree_ = lockPTR<Ntree<D,index> >();
600  cached_ntree_layer_ = -1;
601  }
602 
603  template <int D>
604  inline
606  {
607  if (cached_vector_ != 0)
608  delete cached_vector_;
609  cached_vector_ = 0;
610  cached_vector_layer_ = -1;
611  }
612 
613 } // namespace nest
614 
615 #endif
vector< Node * >::iterator local_end()
Return iterator to the end of the local child-list.
Definition: subnet.h:246
size_t index
Unsigned long type for enumerations.
Definition: nest.h:109
Position< D > get_center() const
Definition: layer.h:241
This class is a representation of the dictionary of connection properties given as an argument to the...
Definition: connection_creator.h:65
lockPTR< Ntree< D, index > > ntree_
Definition: layer.h:467
int int_t
Integer number with at least 16 bit.
Definition: nest.h:95
Base class for all subnet nodes.
Definition: subnet.h:67
Position< D > lower_left_
lower left corner (minimum coordinates) of layer
Definition: layer.h:393
virtual void connect(AbstractLayer &target, ConnectionCreator &connector)=0
Connect this layer to the given target layer.
Mask oriented in the opposite direction.
Definition: mask.h:411
Ntree< D, index >::masked_iterator begin(const Position< D > &anchor)
Iterate over nodes inside mask.
Definition: layer.h:506
MaskedLayer(Layer< D > &layer, Selector filter, const MaskDatum &mask, bool include_global, bool allow_oversized)
Regular constructor.
Definition: layer.h:473
lockPTR< Ntree< D, index > > get_local_positions_ntree(Selector filter=Selector())
Get positions for local nodes in layer.
Definition: layer_impl.h:109
Definition: lockptrdatum.h:40
Iterator iterating the nodes in a Quadtree inside a Mask.
Definition: ntree.h:128
const Name layer("layer")
Definition: topology_names.h:82
Layer()
Creates an empty layer.
Definition: layer.h:524
void clear_vector_cache_() const
Clear the cache for global position information.
Definition: layer.h:605
Ntree< D, index >::masked_iterator end()
Definition: layer.h:517
void get_status(DictionaryDatum &) const
Export properties of the layer by setting entries in the status dictionary.
Definition: layer_impl.h:79
virtual void dump_nodes(std::ostream &os) const =0
Write layer data to stream.
lockPTR< Ntree< D, index > > do_get_global_positions_ntree_(const Selector &filter)
Definition: layer_impl.h:157
static lockPTR< Ntree< D, index > > cached_ntree_
Global position information for a single layer.
Definition: layer.h:400
virtual void dump_connections(std::ostream &out, const Token &syn_model)=0
Dumps information about all connections of the given type having their source in the given layer to t...
const Name extent("extent")
Definition: topology_names.h:47
const Position< D > & get_extent() const
Definition: layer.h:235
const Name lower_left("lower_left")
Definition: topology_names.h:66
~Layer()
Virtual destructor.
Definition: layer.h:545
Contains rules for selecting nodes from a layer when connecting.
Definition: selector.h:36
const Name depth("depth")
Definition: topology_names.h:46
Position< D > extent_
size of layer
Definition: layer.h:394
Class for applying masks to layers.
Definition: connection_creator.h:42
std::bitset< D > periodic_
periodic b.c.
Definition: layer.h:395
virtual void insert_global_positions_ntree_(Ntree< D, index > &tree, const Selector &filter)=0
Insert global position info into ntree.
Abstract base class for masks with given dimension.
Definition: mask.h:90
A Ntree object represents a subtree or leaf in a Ntree structure.
Definition: ntree.h:49
const Name anchor("anchor")
Definition: topology_names.h:50
void connect(AbstractLayer &target, ConnectionCreator &connector)
Connect this layer to the given target layer.
Definition: layer_impl.h:98
Abstract base class for Layer of given dimension (D=2 or 3).
Definition: connection_creator.h:39
void clear_ntree_cache_() const
Clear the cache for global position information.
Definition: layer.h:597
static Selector cached_selector_
Definition: layer.h:402
virtual Position< D > get_position(index sind) const =0
Get position of node.
const Name target("target")
Connection parameters.
Definition: nest_names.h:282
Abstract base class for Layers of unspecified dimension.
Definition: layer.h:44
bool is_subnet() const
Layers do not allow entry to the ChangeSubnet command, nodes can not be added by the user...
Definition: layer.h:362
virtual 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:43
virtual void insert_global_positions_vector_(std::vector< std::pair< Position< D >, index > > &, const Selector &filter)=0
Insert global position info into vector.
~MaskedLayer()
Definition: layer.h:500
static std::vector< std::pair< Position< D >, index > > * cached_vector_
Definition: layer.h:401
static index create_layer(const DictionaryDatum &)
Factory function for layers.
Definition: layer.cpp:41
Exception to be thrown if a status parameter is incomplete or inconsistent.
Definition: exceptions.h:420
const Position< D > & get_lower_left() const
Definition: layer.h:229
virtual void clear_vector_cache_() const =0
Clear the cache for global position information.
double double_t
Double precision floating point numbers.
Definition: nest.h:93
void dump_nodes(std::ostream &os) const
Write layer data to stream.
Definition: layer_impl.h:244
void dump_connections(std::ostream &out, const Token &syn_model)
Dumps information about all connections of the given type having their source in the given layer to t...
Definition: layer_impl.h:255
int_t depth_
number of neurons at each position
Definition: layer.h:168
static index cached_vector_layer_
GID for the single layer for which we cache global position information.
Definition: layer.h:173
virtual ~AbstractLayer()
Virtual destructor.
Definition: layer.cpp:37
virtual double_t compute_distance(const std::vector< double_t > &from_pos, const index to) const =0
Returns distance to node from given position.
AbstractLayer()
Constructor.
Definition: layer.h:50
const Name mask("mask")
Definition: topology_names.h:57
Default types used by the NEST kernel.
This template is the standard safe-pointer implementation of SYNOD.
Definition: lockptr.h:84
MaskDatum mask_
Definition: layer.h:468
std::vector< double_t > get_position_vector(const index sind) const
Definition: layer.h:590
vector< Node * >::iterator local_begin()
Return iterator to the first local child node.
Definition: subnet.h:240
void set_status(const DictionaryDatum &)
Change properties of the layer according to the entries in the dictionary.
Definition: layer_impl.h:58
std::bitset< D > get_periodic_mask() const
Definition: layer.h:247
A type-independent container for C++-types.
Definition: token.h:68
std::vector< std::pair< Position< D >, index > > * get_global_positions_vector(Selector filter=Selector())
Definition: layer_impl.h:184
lockPTR< Ntree< D, index > > get_global_positions_ntree(Selector filter=Selector())
Get positions for all nodes in layer, including nodes on other MPI processes.
Definition: layer_impl.h:119
virtual std::vector< double_t > get_position_vector(const index sind) const =0
Get position of node.
virtual std::vector< double_t > compute_displacement(const std::vector< double_t > &from_pos, const index to) const =0
Returns displacement of node from given position.
virtual void clear_ntree_cache_() const =0
Clear the cache for global position information.
static index cached_ntree_layer_
GID for the single layer for which we cache global position information.
Definition: layer.h:163
double_t compute_distance(const Position< D > &from_pos, const index to) const
Returns distance to node from given position.
Definition: layer.h:574
const double e
Definition: numerics.cpp:62
virtual std::vector< index > get_global_nodes(const MaskDatum &mask, const std::vector< double_t > &anchor, bool allow_oversized)=0
Return a vector with the GIDs of the nodes inside the mask.
virtual void insert_local_positions_ntree_(Ntree< D, index > &tree, const Selector &filter)=0
Insert local position info into ntree.
void check_mask_(Layer< D > &layer, bool allow_oversized)
Will check that the mask can be applied to the layer.
Definition: layer_impl.h:318
std::vector< index > get_global_nodes(const MaskDatum &mask, const std::vector< double_t > &anchor, bool allow_oversized)
Return a vector with the GIDs of the nodes inside the mask.
Definition: layer_impl.h:233