Swarm-NG  1.1
stop_on_close_encounter.hpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) 2011 by Eric Ford and the Swarm-NG Development Team *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 3 of the License. *
7  * *
8  * This program is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11  * GNU General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU General Public License *
14  * along with this program; if not, write to the *
15  * Free Software Foundation, Inc., *
16  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17  ************************************************************************/
18 
26 #pragma once
27 
28 #include <limits>
29 
30 namespace swarm { namespace monitors {
31 
41  double dmin;
42  bool deactivate_on, log_on, verbose_on;
46  {
47  dmin = cfg.optional("close_approach",0.0);
48  deactivate_on = cfg.optional("deactivate_on_close_encounter",false);
49  log_on = cfg.optional("log_on_close_encounter",false);
50  verbose_on = cfg.optional("verbose_on_close_encounter",false);
51  }
52 };
53 
64 template<class log_t>
66  public:
68 
69  private:
70  params _params;
71  bool need_full_test, condition_met;
72  ensemble::SystemRef& _sys;
73  log_t& _log;
74 
75 
76  public:
77 
78  template<class T>
79  static GENERIC int thread_per_system(T compile_time_param){
80  return 1;
81  }
82 
83  template<class T>
84  static GENERIC int shmem_per_system(T compile_time_param) {
85  return 0;
86  }
87 
88  GPUAPI bool is_deactivate_on() { return _params.deactivate_on; };
89  GPUAPI bool is_log_on() { return _params.log_on; };
90  GPUAPI bool is_verbose_on() { return _params.verbose_on; };
91  GPUAPI bool is_any_on() { return is_deactivate_on() || is_log_on() || is_verbose_on() ; }
92  GPUAPI bool is_condition_met () { return ( condition_met ); }
93  GPUAPI bool need_to_log_system ()
94  { return (is_log_on() && is_condition_met() ); }
95  GPUAPI bool need_to_deactivate ()
96  { return ( is_deactivate_on() && is_condition_met() ); }
97 
98  GPUAPI void log_system() { log::system(_log, _sys); }
99 
101  GPUAPI bool pass_one (int thread_in_system)
102  {
103  need_full_test = false;
104  condition_met = false;
105  if(is_any_on()&&(thread_in_system==0))
106  {
107  // Chcek for close encounters
108  for(int b = 2; b < _sys.nbod(); b++)
109  for(int d = 1; d < b; d++)
110  condition_met = condition_met || check_close_encounters(b,d);
111  if( condition_met && is_log_on() )
112  { need_full_test = true; }
113 
114  }
115  return need_full_test;
116  }
117 
119  GPUAPI int pass_two (int thread_in_system)
120  {
121  if (need_to_deactivate() && (thread_in_system==0) )
122  { _sys.set_disabled(); }
123  return _sys.state();
124  }
125 
126 
127  GPUAPI bool check_close_encounters(const int& i, const int& j){
128 
129  double d = _sys.distance_between(i,j);
130  double _GM = _sys[0].mass(); // remove _ if ok to keep
131  // double rH = pow((_sys[i].mass()+_sys[j].mass())/(3.*_GM),1./3.);
132  // bool close_encounter = d < _p.dmin * rH;
133  double a = 0.5*(_sys[i].distance_to_origin()+_sys[i].distance_to_origin());
134  double rH3 = (_sys[i].mass()+_sys[j].mass())/(3.*_GM)*a*a*a;
135  bool close_encounter = d*d*d < _params.dmin*_params.dmin*_params.dmin * rH3;
136 
137  if( close_encounter )
138  if(is_verbose_on() )
139  lprintf(_log, "Close apporach detected: "
140  "sys=%d, T=%f j=%d i=%d d=%lg.\n"
141  , _sys.number(), _sys.time(), j, i,d);
142 
143  return close_encounter;
144  }
145 
146  GPUAPI void operator () (const int thread_in_system)
147  {
148  pass_one(thread_in_system);
149  pass_two(thread_in_system);
150  if(need_to_log_system() && (thread_in_system==0) )
151  log::system(_log, _sys);
152  }
153 
154 #if 0
155  // GPUAPI void operator () ()
156  GPUAPI void operator () (int thread_in_system)
157  {
158  if(!is_any_on()) return;
159  bool stopit = false;
160 
161  if(thread_in_system==0)
162  {
163  // Chcek for close encounters
164  for(int b = 2; b < _sys.nbod(); b++)
165  for(int d = 1; d < b; d++)
166  stopit = stopit || check_close_encounters(b,d);
167 
168  if(stopit) {
169  if(is_log_on())
170  log::system(_log, _sys);
171  if(is_deactivate_on())
172  _sys.set_disabled();
173  }
174  }
175  }
176 #endif
177 
178  GPUAPI stop_on_close_encounter(const params& p,ensemble::SystemRef& s,log_t& l)
179  :_params(p),_sys(s),_log(l){}
180 
181 };
182 
183 } } // end namespace monitors :: swarm