1 /*
2  *  sp_manager.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 /*
24  * File:   sp_updater.h
25  * Author: naveau
26  *
27  * Created on November 26, 2013, 2:28 PM
28  */
29 
30 #ifndef SP_MANAGER_H
31 #define SP_MANAGER_H
32 
33 // C++ includes:
34 #include <vector>
35 
36 // Includes from libnestutil:
37 #include "manager_interface.h"
38 
39 // Includes from nestkernel:
40 #include "node_collection.h"
41 #include "growth_curve_factory.h"
42 #include "nest_time.h"
43 #include "nest_types.h"
44 
45 // Includes from sli:
46 #include "arraydatum.h"
47 #include "dict.h"
48 #include "dictdatum.h"
49 
50 namespace nest
51 {
52 class Node;
53 
54 class SPBuilder;
55 
56 /**
57  * The SPManager class is in charge of managing the dynamic creation and
58  * deletion of synapses in the simulation when structural plasticity is enabled.
59  * Otherwise it behaves as the normal ConnectionManager.
60  * @param
61  */
62 class SPManager : public ManagerInterface
63 {
64 
65 public:
66   SPManager();
67   virtual ~SPManager();
68 
69   virtual void initialize();
70   virtual void finalize();
71 
72   virtual void get_status( DictionaryDatum& );
73   virtual void set_status( const DictionaryDatum& );
74 
75   DictionaryDatum& get_growthcurvedict();
76 
77   /**
78    * Create a new Growth Curve object using the GrowthCurve Factory
79    * @param name which defines the type of NC to be created
80    * @return a new Growth Curve object of the type indicated by name
81    */
82   GrowthCurve* new_growth_curve( Name name );
83 
84   /**
85    * Add a growth curve for MSP
86    */
87   template < typename GrowthCurve >
88   void register_growth_curve( const std::string& name );
89 
90   /**
91    * Disconnect two collections of nodes.  The connection is
92    * established on the thread/process that owns the target node.
93    *
94    * \param sources Node collection of the source Nodes.
95    * \param targets Node collection of the target Nodes.
96    * \param connectivityParams connectivity Dictionary
97    * \param synapseParams synapse parameters Dictionary
98    */
99   void disconnect( NodeCollectionPTR, NodeCollectionPTR, DictionaryDatum&, DictionaryDatum& );
100 
101   /**
102    * Disconnect two nodes.
103    * The source node is defined by its global ID.
104    * The target node is defined by the node. The connection is
105    * established on the thread/process that owns the target node.
106    *
107    * \param snode_id node ID of the sending Node.
108    * \param target Pointer to target Node.
109    * \param target_thread Thread that hosts the target node.
110    * \param syn_id The synapse model to use.
111    */
112   void disconnect( const index snode_id, Node* target, thread target_thread, const index syn_id );
113 
114   void update_structural_plasticity();
115   void update_structural_plasticity( SPBuilder* );
116 
117   /**
118    * Enable structural plasticity
119    */
120   void enable_structural_plasticity();
121 
122   /**
123    * Disable structural plasticity
124    */
125   void disable_structural_plasticity();
126 
127   bool is_structural_plasticity_enabled() const;
128 
129   double get_structural_plasticity_update_interval() const;
130 
131   /**
132    * Returns the minimum delay of all SP builders.
133    * This influences the min_delay of the kernel, as the connections
134    * are build during the simulation. Hence, the
135    * ConnectionManager::min_delay() methods have to respect this delay
136    * as well.
137    */
138   delay builder_min_delay() const;
139 
140   /**
141    * Returns the maximum delay of all SP builders.
142    * This influences the max_delay of the kernel, as the connections
143    * are build during the simulation. Hence, the
144    * ConnectionManager::max_delay() methods have to respect this delay
145    * as well.
146    */
147   delay builder_max_delay() const;
148 
149   // Creation of synapses
150   bool create_synapses( std::vector< index >& pre_vacant_id,
151     std::vector< int >& pre_vacant_n,
152     std::vector< index >& post_vacant_id,
153     std::vector< int >& post_vacant_n,
154     SPBuilder* sp_conn_builder );
155   // Deletion of synapses on the pre synaptic side
156   void delete_synapses_from_pre( const std::vector< index >& pre_deleted_id,
157     std::vector< int >& pre_deleted_n,
158     const index synapse_model,
159     const std::string& se_pre_name,
160     const std::string& se_post_name );
161   // Deletion of synapses on the postsynaptic side
162   void delete_synapses_from_post( std::vector< index >& post_deleted_id,
163     std::vector< int >& post_deleted_n,
164     index synapse_model,
165     std::string se_pre_name,
166     std::string se_post_name );
167   // Deletion of synapses
168   void delete_synapse( index source, index target, long syn_id, std::string se_pre_name, std::string se_post_name );
169 
170   void get_synaptic_elements( std::string se_name,
171     std::vector< index >& se_vacant_id,
172     std::vector< int >& se_vacant_n,
173     std::vector< index >& se_deleted_id,
174     std::vector< int >& se_deleted_n );
175 
176   void serialize_id( std::vector< index >& id, std::vector< int >& n, std::vector< index >& res );
177   void global_shuffle( std::vector< index >& v );
178   void global_shuffle( std::vector< index >& v, size_t n );
179 
180 private:
181   /**
182    * Time interval for structural plasticity update (creation/deletion of
183    * synapses).
184    */
185   double structural_plasticity_update_interval_;
186 
187   /**
188    * Indicates whether the Structrual Plasticity functionality is On (True) of
189    * Off (False).
190    */
191   bool structural_plasticity_enabled_;
192   std::vector< SPBuilder* > sp_conn_builders_;
193 
194   /** @BeginDocumentation
195 
196    Name: growthcurvedict - growth curves for Model of Structural Plasticity
197 
198    Description:
199    This dictionary provides indexes for the growth curve factory
200    */
201   DictionaryDatum growthcurvedict_; //!< Dictionary for growth rules.
202 
203   /**
204    * GrowthCurve factories, indexed by growthcurvedict_ elements.
205    */
206   std::vector< GenericGrowthCurveFactory* > growthcurve_factories_;
207 };
208 
209 inline DictionaryDatum&
get_growthcurvedict()210 SPManager::get_growthcurvedict()
211 {
212   return growthcurvedict_;
213 }
214 
215 inline GrowthCurve*
new_growth_curve(Name name)216 SPManager::new_growth_curve( Name name )
217 {
218   const long nc_id = ( *growthcurvedict_ )[ name ];
219   return growthcurve_factories_.at( nc_id )->create();
220 }
221 
222 inline bool
is_structural_plasticity_enabled()223 SPManager::is_structural_plasticity_enabled() const
224 {
225   return structural_plasticity_enabled_;
226 }
227 
228 inline double
get_structural_plasticity_update_interval()229 SPManager::get_structural_plasticity_update_interval() const
230 {
231   return structural_plasticity_update_interval_;
232 }
233 }
234 #endif /* SP_MANAGER_H */
235