Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Agent.C
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 
29 /* Author: Gengbin Zheng */
30 
31 #include <cassert>
32 #include <fstream>
33 #include <iostream>
34 #include <sstream>
35 
36 #include "rocman.h"
37 #include "Action.h"
38 #include "Scheduler.h"
39 #include "Agent.h"
40 #include "Interpolate.h"
41 #include "basic_actions.h"
42 
48 
49 // agent's main function
50 void PhysicsAction::run(double t, double dt, double alpha)
51 {
52  MAN_DEBUG(3, ("[%d] Rocstar: Agent %s::PhysicsAction run with t:%e dt:%e alpha:%e.\n", agent->comm_rank, agent->get_agent_name().c_str(), t, dt, alpha));
53 
54  // store it, gm callback needs it
56  agent->timestamp = t;
57 
58  if (agent->get_coupling()->get_ipc() == 1)
59  agent->old_dt = dt;
60 
61  // include Interpolate::backup
62  agent->run_bcinitaction(t, dt);
63 
64  //
65  if(!agent->withgm)
67  &agent->bc_handle);
68  else
71 
72  agent->current_time = t;
73 }
74 
75 void PhysicsAction::print(FILE *f)
76 {
77  agent->print(f);
78 }
79 
81 {
82  return (char *)agent->get_agent_name().c_str();
83 }
84 
85 void AttributeBase::create(std::string bufname)
86 {
87 }
88 
89 void AttributeBase::assign(std::string bufname)
90 {
91  // check if target_window is created
92 // if (COM_get_window_handle( target_window) <= 0) {
93 // COM_new_window( target_window);
94 // COM_use_attribute( target_window, bufname+".all");
95 // }
96  COM_assertion_msg(COM_get_window_handle( target_window)>0, "ERROR: assign error.\n");
97 
99  // when to call init_done ??????????????
100 }
101 
102 NewAttribute::NewAttribute(Agent *ag, std::string target_window_, std::string attr_, char loc_, int type_, int ncomp_, const char * unit_) :
103  AttributeBase(ag, target_window_, attr_),
104  loc(loc_), type(type_), ncomp(ncomp_), unit(unit_)
105 {
106 }
107 
108 void NewAttribute::create(std::string bufname)
109 {
110  // test if already created
111  if (COM_get_attribute_handle( bufname+attr) > 0) {
112  char loc_;
113  int type_;
114  int ncomp_;
115  std::string unit_;
116  COM_get_attribute( bufname+attr, &loc_, &type_, &ncomp_, &unit_);
117  if (loc_ != loc || type_ != type || ncomp_ != ncomp || unit_ != unit) {
118  std::cerr << "Rocstar Error: NewAttribute::create(): Could not create " << bufname+attr << " in two different ways " << std::endl;
119  MPI_Abort(MPI_COMM_WORLD, -1);
120  }
121  else
122  return;
123  }
124  MAN_DEBUG(3, ("NewAttribute::create: %s%s.\n", bufname.c_str(), attr.c_str()));
125  COM_new_attribute( bufname+attr, loc, type, ncomp, unit);
126  COM_resize_array( bufname+attr);
127 }
128 
130  std::string target_window_, std::string attr_,
131  std::string parent_window_, std::string parent_attr_,
132  int wg_, const char *ptnname_, int val_):
133  AttributeBase(ag, target_window_, attr_),
134  parent_window(parent_window_), parent_attr(parent_attr_),
135  wg(wg_), val(val_), condition(cond)
136 {
137  ptnname = ptnname_?strdup(ptnname_):NULL;
138 }
139 
140 void CloneAttribute::create(std::string bufname)
141 {
142  if (parent_window=="") { // filling
144  }
145  if (COM_get_attribute_handle( bufname+attr) > 0) {
146  char loc1, loc2;
147  int type1, type2;
148  int ncomp1, ncomp2;
149  std::string unit1, unit2;
150  COM_get_attribute( bufname+attr, &loc1, &type1, &ncomp1, &unit1);
151  COM_get_attribute( parent_window+parent_attr, &loc2, &type2, &ncomp2, &unit2);
152  if (loc1 != loc2 || type1 != type2 || ncomp1 != ncomp2 || unit1 != unit2) {
153  std::cerr << "Rocstar Error: CloneAttribute::create(): Could not create " << target_window+attr << " in two different ways " << std::endl;
154  MPI_Abort(MPI_COMM_WORLD, -1);
155  }
156  else {
157  MAN_DEBUG(3, ("CloneAttribute::create: %s%s %s%s condition:%d handle:%d SKIPPED.\n", bufname.c_str(), attr.c_str(), parent_window.c_str(), parent_attr.c_str(), condition, COM_get_attribute_handle( parent_window+parent_attr)));
158  return;
159  }
160  }
161  MAN_DEBUG(3, ("CloneAttribute::create: %s%s %s%s condition:%d handle:%d.\n", bufname.c_str(), attr.c_str(), parent_window.c_str(), parent_attr.c_str(), condition, COM_get_attribute_handle( parent_window+parent_attr)));
162  if (condition)
164  if (ptnname) {
165  if (ptnname[0] == '.')
166  ptnname = strdup((agent->get_surface_window() + ptnname).c_str());
167  }
169 }
170 
171 UseAttribute::UseAttribute(Agent *ag, std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_, const char *ptnname_, int val_):
172  AttributeBase(ag, target_window_, attr_),
173  parent_window(parent_window_), parent_attr(parent_attr_),
174  wg(wg_), ptnname(ptnname_), val(val_)
175 {
176 }
177 
178 void UseAttribute::create(std::string bufname)
179 {
180  if (parent_window=="") {
182  }
183  if (COM_get_attribute_handle( bufname+attr) > 0) {
184  char loc1, loc2;
185  int type1, type2;
186  int ncomp1, ncomp2;
187  std::string unit1, unit2;
188  COM_get_attribute( bufname+attr, &loc1, &type1, &ncomp1, &unit1);
189  COM_get_attribute( parent_window+parent_attr, &loc2, &type2, &ncomp2, &unit2);
190  if (loc1 != loc2 || type1 != type2 || ncomp1 != ncomp2 || unit1 != unit2) {
191  std::cerr << "Rocstar Error: NewAttribute::create(): Could not create " << target_window+attr << " in two different ways " << std::endl;
192  MPI_Abort(MPI_COMM_WORLD, -1);
193  }
194  else
195  return;
196  }
197  MAN_DEBUG(3, ("UseAttribute::create: %s%s %s%s.\n", bufname.c_str(), attr.c_str(), parent_window.c_str(), parent_attr.c_str()));
199 }
200 
201 Agent::Agent(Coupling *cp, std::string mod, std::string obj,
202  const char *agentname, MPI_Comm com, bool wgm, bool skipio)
203  : agent_name(agentname), communicator(com), coupling(cp), action(this),
204  withgm(wgm), rocmod_name( mod), mod_instance(obj), skipInputIO(skipio)
205 {
206  MPI_Comm_rank( com, &comm_rank);
207 
208  if(comm_rank == 0)
209  MAN_DEBUG(3, ("Rocstar: Agent::Agent create window %s.\n", get_agent_name().c_str()));
210  create_window(agent_name.c_str());
211 
212  inDir = obj; inDir.append("/Rocin/");
213  outDir = obj; outDir.append("/Rocout/");
214  suffix = ".hdf";
215 
216  // set proper names for visu
217  std::string physicsname = agentname;
218  physicsname = physicsname+"-PhysicsAction";
219  action.set_name(physicsname.c_str());
220 
221  bcInitScheduler.set_name("bcInitSched");
222  gmScheduler.set_name("gmSched");
223 
225  old_dt = 0.0;
226  dobackup = 1;
227 }
228 
230 {
231 }
232 
233 // mod_instance can be RocburnAPN, etc
235 {
236  // init function handle
237  char fname[128];
238  sprintf(fname, "%s%s", mod_instance.c_str(), ".initialize");
240  COM_assertion_msg(init_handle>0, "ERROR: Agent::init_function_handles.\n");
241 
242  sprintf(fname, "%s%s", mod_instance.c_str(), ".update_solution");
244  COM_assertion_msg(update_handle>0, "ERROR: Agent::init_function_handles.\n");
245 
246  sprintf(fname, "%s%s", mod_instance.c_str(), ".finalize");
248  COM_assertion_msg(finalize_handle>0, "ERROR: Agent::init_function_handles.\n");
249 
250  // optional
251  sprintf(fname, "%s%s", mod_instance.c_str(), ".pre_hdf_output");
253 
254  // optional
255  sprintf(fname, "%s%s", mod_instance.c_str(), ".post_hdf_output");
257 
259  COM_set_profiling_barrier( update_handle, communicator);
260  COM_set_profiling_barrier( finalize_handle, communicator);
261  if (pre_hdf_handle != -1)
262  COM_set_profiling_barrier( pre_hdf_handle, communicator);
263  if (post_hdf_handle != -1)
264  COM_set_profiling_barrier( post_hdf_handle, communicator);
265 
266  sprintf(fname, "%s%s", mod_instance.c_str(), ".compute_integrals");
268 
269  read_files_handle = COM_get_function_handle( "IN.read_window");
270  read_by_control_handle = COM_get_function_handle( "IN.read_by_control_file");
271  obtain_attr_handle = COM_get_function_handle( "IN.obtain_attribute");
272  write_attr_handle = COM_get_function_handle( "OUT.write_attribute");
273  write_ctrl_handle = COM_get_function_handle( "OUT.write_rocin_control_file");
274 
279 }
280 
281 void Agent::create_window(const char *window_name)
282 {
283  // create window and global variables
284  char global[128];
285  COM_new_window(window_name);
286  sprintf(global, "%s%s", window_name, ".global");
287  COM_new_attribute(global, 'w', COM_VOID, 1, "");
288  COM_set_object(global, 0, this);
289 
290  //
291  COM_Type types[4];
292  types[0] = COM_RAWDATA;
293 
294  char window_func[128];
295 
296  sprintf(window_func, "%s.init_callback", window_name);
297  types[1] = COM_STRING;
298  types[2] = COM_STRING;
299  types[3] = COM_VOID;
300  COM_set_member_function(window_func, (Member_func_ptr)&Agent::init_callback,
301  global, "biiI", types);
302  ic_handle = COM_get_function_handle(window_func);
303 
304  types[1] = COM_DOUBLE;
305  types[2] = COM_INT;
306  sprintf(window_func, "%s.obtain_bc", window_name);
307  COM_set_member_function(window_func, (Member_func_ptr)&Agent::obtain_bc, global, "biI", types);
308  bc_handle = COM_get_function_handle(window_func);
309 
310  sprintf(window_func, "%s.obtain_gm", window_name);
311  COM_set_member_function(window_func, (Member_func_ptr)&Agent::obtain_gm, global, "bi", types);
312  gm_handle = COM_get_function_handle(window_func);
313 
314  COM_window_init_done(window_name);
315 }
316 
317 double Agent::max_timestep(double t, double dt)
318 {
319  return dt;
320 }
321 
322 void Agent::register_new_attribute(std::string target_window_, std::string attr_, char loc_, int type_, int ncomp_, const char * unit_)
323 {
324  NewAttribute *newAttr = new NewAttribute(this, target_window_, attr_, loc_, type_, ncomp_, unit_);
325  attributeList.push_back(newAttr);
326 }
327 
328 void Agent::register_clone_attribute(int cond, std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_, const char *ptnname_, int val_)
329 {
330  CloneAttribute *newAttr = new CloneAttribute(this, cond, target_window_, attr_, parent_window_, parent_attr_, wg_, ptnname_, val_);
331  attributeList.push_back(newAttr);
332 }
333 
334 void Agent::register_use_attribute(std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_, const char *ptnname_, int val_)
335 {
336  UseAttribute *newAttr = new UseAttribute(this, target_window_, attr_, parent_window_, parent_attr_, wg_, ptnname_, val_);
337  attributeList.push_back(newAttr);
338 }
339 
340 void Agent::create_registered_attributes(std::string tmpBuf)
341 {
342  unsigned int n = attributeList.size();
343  for (unsigned int i=0; i<n; i++) {
344  MAN_DEBUG(3, ("[%d] creating %s %s\n", comm_rank, attributeList[i]->target_window.c_str(), attributeList[i]->attr.c_str()));
345  attributeList[i]->create(attributeList[i]->target_window);
346  }
347  COM_window_init_done( tmpBuf);
348 }
349 
350 void Agent::create_registered_window_attributes(std::string target_window)
351 {
352  unsigned int n = attributeList.size();
353  for (unsigned int i=0; i<n; i++) {
354  if (attributeList[i]->target_window == target_window) {
355  MAN_DEBUG(3, ("[%d] creating %s %s\n", comm_rank, attributeList[i]->target_window.c_str(), attributeList[i]->attr.c_str()));
356  attributeList[i]->create(attributeList[i]->target_window);
357  }
358  }
359 }
360 
362 {
363  MAN_DEBUG(3, ("Rocstar: Agent %s::create_buffer_all called %s.\n", get_agent_name().c_str(), tmp_window.c_str()));
365 // COM_use_attribute( tmp_window, surf_window+".all", 1);
368  //create_registered_attributes(tmp_window);
371 }
372 
374 {
375  unsigned int n = attributeList.size();
376  for (unsigned int i=0; i<n; i++) {
377  attributeList[i]->assign(tmp_window);
378  }
379 }
380 
382 {
383  icScheduler.add_action(act);
384 }
385 
386 // l starts from 1
388 {
390 }
391 
392 // l starts from 1
393 void Agent::add_bcaction(Action *act, int l)
394 {
395  int cur = bcScheduler.size();
396  COM_assertion_msg(l>=1 && l <= cur+1, "ERROR: add_bcaction.\n");
397  UserScheduler *sched;
398  if (cur == l-1) {
399  sched = new UserScheduler;
400  char str[128];
401  sprintf(str, "%s%d", "bcSched", l);
402  sched->set_name(str);
403  bcScheduler.push_back(sched);
404  }
405  else
406  sched = bcScheduler[l-1];
407  COM_assertion_msg(sched, "ERROR: add_bcaction with no scheduler.\n");
408  sched->add_action(act);
409 }
410 
412 {
413  gmScheduler.add_action(act);
414 }
415 
416 void Agent::init_module(double t, double dt)
417 {
418  MAN_DEBUG(3, ("[%d] Rocstar: %s::init_module with current_time:%e current_deltatime:%e.\n", comm_rank, get_agent_name().c_str(), t, dt));
419 
421  current_deltatime = dt;
422  old_dt = 0.0;
423 }
424 
426 {
427  MAN_DEBUG(3, ("Rocstar: %sAgent::init_subscheduler called.\n", get_agent_name().c_str()));
428 
430 
431  MAN_DEBUG(3, ("Rocstar: %sAgent::init_subscheduler done.\n", get_agent_name().c_str()));
432 }
433 
435 {
436  unsigned int i;
437 
438  (bcInitScheduler.*fn)( t);
439  for (i=0; i<bcScheduler.size(); i++) {
440  (bcScheduler[i]->*fn)( t);
441  }
442  (gmScheduler.*fn)( t);
443 }
444 
445 void Agent::init_bcactions(double t)
446 {
447  for (unsigned int i=0; i<bcScheduler.size(); i++)
448  bcScheduler[i]->init_actions(t);
449 }
450 
451 void Agent::init_gmactions(double t)
452 {
454 }
455 
457 {
460  for (unsigned int i=0; i<bcScheduler.size(); i++) {
461  bcScheduler[i]->schedule();
462  }
464 }
465 
467 {
468  MAN_DEBUG(3, ("[%d] Rocstar: %s::finalize called.\n", comm_rank, get_agent_name().c_str()));
469 
472  for (unsigned int i=0; i<bcScheduler.size(); i++)
473  bcScheduler[i]->finalize_actions();
475 
476  // at restart, we don't want windows in physics modules deleted
477  if (! get_coupling()->in_restart())
479 }
480 
481 // part of INITIALIZE_XXX, customized portion
482 void Agent::init_callback( const char *surf_win, const char *vol_win,
483  void *option)
484 {
485  MAN_DEBUG(3, ("Rocstar: %s::init_callback called: surfwin: %s volwin: %s.\n", get_agent_name().c_str(), surf_win, vol_win));
486 
487  surf_window = surf_win;
488  vol_window = vol_win;
490 
491  // creat a window buffer to inherit from surface window
492  // they are like ifluid_all, iburn_all, etc
494 
495  if (icScheduler.isEmpty()) {
496  if (comm_rank==0)
497  std::cerr << "Rocstar: Warning: No actions defined for "
498  << "Initialization callback for module "
499  << rocmod_name << std::endl;
500  }
501  else {
502  MAN_DEBUG(3, ("[%d] Rocstar: %s::init_callback called IC with current_time:%e current_deltatime:%e.\n", comm_rank, get_agent_name().c_str(), current_time, current_deltatime));
503  icScheduler.set_alpha(0.0);
506  MAN_DEBUG(3, ("[%d] Rocstar: %s::init_callback called IC DONE.\n", comm_rank, get_agent_name().c_str()));
507  }
508 
509  MAN_DEBUG(3, ("Rocstar: Agent::init_callback called: surfwin: %s volwin: %s DONE.\n", surf_win, vol_win));
510 }
511 
512 /*
513 void Agent::run_initactions(double t, double dt)
514 {
515  MAN_DEBUG(3, ("Rocstar: %s::run_initactions called with t:%e dt:%e.\n", get_agent_name().c_str(), t, dt));
516  initScheduler.set_alpha(0.0);
517  //initScheduler.init_actions(t);
518  initScheduler.run_actions(t, dt);
519 }
520 */
521 
522 // called implicitly in update_solution in PhysicsAction::run()
523 // a is alpha time
524 void Agent::obtain_bc(double *a, int *l)
525 {
526  if ( l!=NULL && (unsigned int)(*l) > bcScheduler.size() && comm_rank==0) {
527  std::cerr << "Rocstar: Warning: No actions defined for "
528  << "level-" << *l << " boundary condition "
529  << "for module " << rocmod_name << std::endl;
530  return;
531  }
532 
533  int level = (l==NULL)?1:*l;
534  MAN_DEBUG(3, ("[%d] Rocstar: Agent %s::obtain_bc called at level: %d t:%e.\n", comm_rank, get_agent_name().c_str(), level, timestamp))
535  Scheduler *sched = get_bcScheduler(level);
536  if (sched) {
537  sched->set_alpha(*a);
539  }
540 }
541 
542 // callback. da is alpha time
543 void Agent::obtain_gm(double *da)
544 {
545  if (gmScheduler.isEmpty() && comm_rank==0) {
546  std::cerr << "Rocstar: Warning: No actions defined for "
547  << "Grid Motion "
548  << "for module " << rocmod_name << std::endl;
549  return;
550  }
551  MAN_DEBUG(3, ("[%d] Rocstar: Agent::obtain_gm called with alpha: %e.\n", comm_rank, *da))
552  // ???????????????
553  gmScheduler.set_alpha(*da);
555 }
556 
558 {
560 }
561 
562 void Agent::run_bcinitaction(double t, double dt)
563 {
564  MAN_DEBUG(3, ("Rocstar: %s::run_bcinitaction called t=%e dt=%e.\n", get_agent_name().c_str(), t, dt));
565 
566  // backup first
567  if (get_coupling()->get_ipc() <= 1 && dobackup == 1)
568  for (unsigned int i=0; i<interpolateList.size(); i++)
569  interpolateList[i]->backup();
570 
572 }
573 
574 // Get a string that encodes the given time. If the string is
575 // xx.yyyyyy, it means 0.yyyyyy*10^xx nanoseconds. This format
576 // ensures that sorting the strings alphabetically gives the right
577 // ordering of time. The string is null-terminated.
578 void Agent::get_time_string( double t, std::string &s) {
579  char chrstring[15];
580 
581  std::sprintf( chrstring, "%.5e", t*1.e10);
582 
583  s = ""; s.append( &chrstring[9]); s.append( ".");
584  s.append( &chrstring[0], 1); s.append( &chrstring[2], 5);
585 }
586 
587 int Agent::
588 read_by_control_file( double t, const std::string base, const std::string window) {
589 
590  if (skipInputIO) return 0; // physics module does input itself
591 
592  // Read in surface window
593  std::string fname = inDir; fname.append( base); fname.append("_in_");
594 
595  std::string timeLevel; get_time_string( t, timeLevel);
596  fname.append( timeLevel); fname.append( ".txt");
597 
598  MAN_DEBUG(3, ("[%d] Agent %s::read_by_control_file run with file: %s t:%e.\n", comm_rank, get_agent_name().c_str(), fname.c_str(), t));
599 
600  bool ctrl_exist;
601  FILE *fp = fopen( fname.c_str(), "r");
602  ctrl_exist = (fp!=NULL);
603  if ( !ctrl_exist && t!=0.0 ) {
604  fname = outDir + base + "_in_" + timeLevel + ".txt";
605  fp = fopen( fname.c_str(), "r");
606  ctrl_exist = (fp!=NULL);
607  }
608 
610  if ( ctrl_exist) {
611  if (comm_rank==0 && man_verbose > 2)
612  std::cout << "Rocstar: Found control file " << fname << std::endl;
613 
614  COM_call_function( read_by_control_handle, fname.c_str(),
615  window.c_str(), &communicator);
616  return 0;
617  }
618  else {
619  if (comm_rank==0 && man_verbose > 2)
620  std::cout << "Rocstar: Did not find control file " << fname << std::endl;
621  return -1;
622  }
623 }
624 
626 {
627  interpolateList.push_back(ip);
628 }
629 
630 void Agent::
631 write_data_files( double t, const std::string base, const std::string attr,
632  const char *ref) {
633  int separate_out = get_coupling()->get_rocmancontrol_param()->separate_out;
634  // Read in surface window
635  std::string fname = outDir;
636 /*
637  if (separate_out) {
638  std::ostringstream rank;
639  rank << comm_rank;
640  fname += rank.str();
641  fname.append( "/");
642  }
643 */
644  fname.append( base); fname.append("_");
645 
646  std::string timeLevel; get_time_string( t, timeLevel);
647  fname.append( timeLevel); fname.append("_");
648 
649  MAN_DEBUG(3, ("[%d] Rocstar: Agent %s::write_data_file with file prefix %s at t:%e.\n", comm_rank, get_agent_name().c_str(), fname.c_str(), t));
650 
651  int attr_hdl = COM_get_attribute_handle( attr);
652  COM_call_function( write_attr_handle, fname.c_str(), &attr_hdl,
653  base.c_str(), timeLevel.c_str(), ref, &communicator);
654 }
655 
656 void Agent::
657 write_control_file( double t, const std::string base, const std::string window) {
658  int separate_out = get_coupling()->get_rocmancontrol_param()->separate_out;
659  // Read in surface window
660  std::string fname; // = base; fname.append("_");
661 /*
662  if (separate_out) {
663  std::ostringstream rank;
664  rank << comm_rank;
665  fname += rank.str(); fname.append( "/");
666  }
667 */
668  fname.append( base);
669  fname.append("_");
670 
671  std::string timeLevel; get_time_string( t, timeLevel);
672  fname.append( timeLevel); fname.append("_");
673 
674  std::string ctrl_fname = outDir; ctrl_fname.append( base);
675  // If base contains '*', then remove it
676  std::string::size_type pos = ctrl_fname.find('*');
677  if ( pos != std::string::npos) ctrl_fname.erase( pos);
678  ctrl_fname.append("_in_"); ctrl_fname.append( timeLevel);
679  ctrl_fname.append(".txt");
680 
681  MAN_DEBUG(3, ("[%d] Rocstar: Agent %s::write_control_file %s with file prefix %s at t:%e.\n", comm_rank, get_agent_name().c_str(), ctrl_fname.c_str(), fname.c_str(), t));
682 
683  COM_call_function( write_ctrl_handle, window.c_str(), fname.c_str(),
684  ctrl_fname.c_str());
685 }
686 
687 // phase 1: new, use Surf_win
688 // phase 2: use
689 void Agent::
690 split_surface_window( const std::string surfAll,
691  const std::string surf_i,
692  const std::string surf_nb,
693  const std::string surf_b,
694  const std::string surf_ni) {
695  std::string bcflag = surfAll+".bcflag";
696  std::string atts = surfAll+".all";
697 
698  if (COM_get_window_handle(surf_i) <= 0)
699  COM_new_window( surf_i);
700  COM_use_attribute( surf_i, atts, 0, bcflag.c_str(), 0);
701  COM_use_attribute( surf_i, atts, 0, bcflag.c_str(), 1);
702  COM_window_init_done( surf_i);
703 
704  if (COM_get_window_handle(surf_nb) <= 0)
705  COM_new_window( surf_nb); // bcflag ==0
706  COM_use_attribute( surf_nb, atts, 1, bcflag.c_str(), 0);
707  COM_window_init_done( surf_nb);
708 
709  if (COM_get_window_handle(surf_b) <= 0)
710  COM_new_window( surf_b); // bcflag ==1
711  COM_use_attribute( surf_b, atts, 1, bcflag.c_str(), 1);
712  COM_window_init_done( surf_b);
713 
714  if (COM_get_window_handle(surf_ni) <= 0)
715  COM_new_window( surf_ni); // bcflag ==2
716  COM_use_attribute( surf_ni, atts, 1, bcflag.c_str(), 2);
717  COM_window_init_done( surf_ni);
718 }
719 
720 void Agent::init_convergence( int iPredCorr)
721 {
722  store_solutions (iPredCorr == 1);
723 }
724 
725 void Agent::store_solutions( int converged)
726 {
727  int i;
728  if(comm_rank == 0)
729  MAN_DEBUG(3, ("[%d] Agent %s::store_solutions converged=%d.\n", comm_rank, get_agent_name().c_str(), converged));
730 
731  if ( converged) { // Store internal data
732  for (i=0; i<pc_count; i++)
733  if ( pc_hdls[0][i]>0)
734  COM_copy_attribute( pc_hdls[1][i], pc_hdls[0][i]);
735  }
736  else {
737  for (i=0; i<pc_count; i++)
738  if ( pc_hdls[0][i]>0)
739  COM_copy_attribute( pc_hdls[0][i], pc_hdls[1][i]);
740  }
741 }
742 
743 int Agent::check_convergence_help( int vcur, int vpre, double tol, std::string str)
744 {
745  double nrm_val=0.0, nrm_diff=0.0, ratio;
746 
747  COM_call_function( RocBlas::sub, &vcur, &vpre, &vpre);
750 
751  if (nrm_val != 0.)
752  ratio = nrm_diff/nrm_val;
753  else
754  ratio = nrm_diff;
755  int result = (ratio <= tol);
756 
757  if (comm_rank==0 && man_verbose > 1)
758  std::cout << " Convergence ratio of " << str << " is " << ratio << std::endl;
759  return result;
760 }
761 
762 void Agent::print(FILE *f)
763 {
764  unsigned int i;
765 
766  // agent
767  fprintf(f, "graph: { title: \"%s\" label: \"%s\" \n\
768  status: folded \n\
769  display_edge_labels: yes \n\
770  layoutalgorithm: tree \n\
771  scaling: maxspect \n\
772  color : lightyellow \n\
773  node.color : lightblue \n\
774  node.textcolor : lightblue \n\
775  node.bordercolor: black \n\
776  node.borderwidth: 1 \n\
777  edge.color : lightblue \n\
778  edge.arrowsize : 7 \n\
779  edge.thickness : 2 \n\
780  edge.fontname:\"helvO08\" \n\
781  node.label: \"no type\" \n", get_agent_name().c_str(), get_agent_name().c_str());
782 
783  char *bcinit = bcInitScheduler.print(f, get_agent_name().c_str());
784 
785  // main physics
786  std::string main_action = get_agent_name() + "-main";
787  fprintf(f, "graph: { title: \"%s\" label: \"%s\" \n\
788  status: folded \n\
789  display_edge_labels: yes \n\
790  layoutalgorithm: tree \n\
791  scaling: maxspect \n\
792  color : lightred \n\
793  node.color : lightblue \n\
794  node.textcolor : black \n\
795  node.bordercolor: black \n\
796  node.borderwidth: 1 \n\
797  edge.color : lightblue \n\
798  edge.arrowsize : 7 \n\
799  edge.thickness : 2 \n\
800  edge.fontname:\"helvO08\" \n\
801  node.label: \"no type\" \n", main_action.c_str(), main_action.c_str());
802 
803  for (i=0; i<bcScheduler.size(); i++)
804  bcScheduler[i]->print(f, get_agent_name().c_str());
805 
806  gmScheduler.print(f, get_agent_name().c_str());
807 
808  std::string main_physics = get_agent_name() + "-physics";
809  fprintf(f, "node: { title:\"%s\" label:\"%s\"}\n", main_physics.c_str(), main_physics.c_str());
810 
811  fprintf(f, "}\n");
812 
813  if (bcinit) {
814  fprintf(f, "edge: { sourcename: \"%s\" targetname: \"%s\" label: \"\"}\n", bcinit, main_action.c_str());
815  free(bcinit);
816  }
817 
818  fprintf(f, "}\n");
819 }
820 
821 
822 
823 
824 
825 
826 
827 
828 
int COMMPI_Comm_rank(MPI_Comm c)
Definition: commpi.h:162
int COM_Type
Indices for derived data types.
Definition: roccom_basic.h:122
string outDir
Definition: Agent.h:142
std::string target_window
Definition: Agent.h:48
double current_deltatime
Definition: Agent.h:144
int ic_handle
Definition: Agent.h:136
here we put it at the!beginning of the common block The point to point and collective!routines know about but MPI_TYPE_STRUCT as yet does not!MPI_STATUS_IGNORE and MPI_STATUSES_IGNORE are similar objects!Until the underlying MPI library implements the C version of these are declared as arrays of MPI_STATUS_SIZE!The types and are OPTIONAL!Their values are zero if they are not available Note that!using these reduces the portability of MPI_IO INTEGER MPI_BOTTOM INTEGER MPI_DOUBLE_PRECISION INTEGER MPI_LOGICAL INTEGER MPI_2REAL INTEGER MPI_2DOUBLE_COMPLEX INTEGER MPI_LB INTEGER MPI_WTIME_IS_GLOBAL INTEGER MPI_COMM_WORLD
void run_bcinitaction(double t, double dt)
Definition: Agent.C:562
string rocmod_name
Definition: Agent.h:141
virtual void init_module(double t, double dt)
Definition: Agent.C:416
int bc_handle
Definition: Agent.h:136
void write_data_files(double t, const std::string base, const std::string attr, const char *ref=NULL)
Definition: Agent.C:631
std::string parent_window
Definition: Agent.h:73
void assign_attributes()
Definition: Agent.C:373
void create(std::string bufname)
Definition: Agent.C:108
SchdulerList bcScheduler
Definition: Agent.h:117
virtual void schedule()
Definition: Scheduler.C:408
const char * option(const char *const name, const int argc, const char *const *const argv, const char *defaut, const char *const usage=0)
Definition: CImg.h:5604
void set_name(const char *name)
Definition: Scheduler.h:50
Scheduler * get_bcScheduler(unsigned int level)
Definition: Agent.h:239
T mod(const T &x, const T &m)
Return the modulo of a number.
Definition: CImg.h:4788
void COM_get_attribute(const std::string wa_str, char *loc, int *type, int *ncomp, std::string *unit)
Definition: roccom_c++.h:269
int val
Definition: Agent.h:91
double max_timestep(double t, double dt)
Definition: Agent.C:317
int condition
Definition: Agent.h:78
#define COM_assertion_msg(EX, msg)
double s
Definition: blastest.C:80
int get_ipc() const
Definition: Coupling.h:160
std::string get_agent_name() const
Definition: Agent.h:223
void create(std::string bufname)
Definition: Agent.C:178
int gm_handle
Definition: Agent.h:136
const char * unit
Definition: Agent.h:64
InterpolateList interpolateList
Definition: Agent.h:153
virtual void assign(std::string bufname)
Definition: Agent.C:89
virtual void add_action(Action *)
Definition: Scheduler.C:401
int init_handle
Definition: Agent.h:131
void COM_set_object(const char *wa_str, int pane_id, Type *addr)
Definition: roccom_c++.h:144
int pc_hdls[2][3]
Definition: Agent.h:158
virtual ~Agent()
Definition: Agent.C:229
char loc
Definition: Agent.h:61
int COM_get_attribute_handle(const char *waname)
Definition: roccom_c++.h:412
std::string parent_attr
Definition: Agent.h:88
static int nrm2_scalar_MPI
Definition: RocBlas.h:51
double initial_time
Definition: Agent.h:144
void set_alpha(double alpha)
Definition: Scheduler.h:44
virtual void print(FILE *f)
Definition: Agent.C:75
void register_clone_attribute(int cond, std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_=1, const char *ptnname_=0, int val_=0)
Definition: Agent.C:328
void add_bcaction(Action *act, int l=1)
Definition: Agent.C:393
void add_bcinitaction(Action *act)
Definition: Agent.C:387
Definition: Action.h:32
int update_handle
Definition: Agent.h:131
bool isEmpty()
Definition: Scheduler.h:48
void register_new_attribute(std::string target_window_, std::string attr_, char loc_, int type_, int ncomp_, const char *unit_)
Definition: Agent.C:322
std::string get_surface_window() const
Definition: Agent.h:217
void callMethod(Scheduler_voidfn1_t fn, double t)
Definition: Agent.C:434
int compute_integrals_handle
Definition: Agent.h:133
int man_verbose
void get_time_string(double t, std::string &s)
Definition: Agent.C:578
void print(FILE *f)
Definition: Agent.C:762
void print(const char *fname)
Definition: Scheduler.C:244
const std::string agent_name
Definition: Agent.h:113
void obtain_bc(double *a, int *l=NULL)
Definition: Agent.C:524
string inDir
Definition: Agent.h:142
void add_gmaction(Action *act)
Definition: Agent.C:411
Definition: Agent.h:110
int wg
Definition: Agent.h:89
int ncomp
Definition: Agent.h:63
CloneAttribute(Agent *ag, int cond, std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_=1, const char *ptnname_=0, int val_=0)
Definition: Agent.C:129
void create_window(const char *window_name)
Definition: Agent.C:281
void COM_use_attribute(const char *wname, const char *attr, int wg=1, const char *ptnname=0, int val=0)
Use the subset of panes of another window of which the given pane attribute has value val...
Definition: roccom_c++.h:224
std::string vol_window
Definition: Agent.h:148
Coupling * get_coupling()
Definition: Agent.h:213
int post_hdf_handle
Definition: Agent.h:132
Agent * agent
Definition: Agent.h:47
int read_by_control_file(double t, const std::string base, const std::string window)
Definition: Agent.C:588
void init_subscheduler(double t)
Definition: Agent.C:425
void split_surface_window(const std::string surfAll, const std::string surf_i, const std::string surf_nb, const std::string surf_b, const std::string surf_ni)
Definition: Agent.C:690
virtual void create_buffer_all()
Definition: Agent.C:361
blockLoc i
Definition: read.cpp:79
static int write_ctrl_handle
Definition: Agent.h:127
UserScheduler gmScheduler
Definition: Agent.h:119
UseAttribute(Agent *ag, std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_=1, const char *ptnname_=0, int val_=0)
Definition: Agent.C:171
int check_convergence_help(int vcur, int vpre, double tol, std::string str)
Definition: Agent.C:743
void COM_set_profiling_barrier(int hdl, MPI_Comm comm)
Definition: roccom_c++.h:554
std::string surf_window
Definition: Agent.h:148
std::string tmp_window
Definition: Agent.h:149
void COM_window_init_done(const char *w_str, int pane_changed=true)
Definition: roccom_c++.h:102
static int write_attr_handle
Definition: Agent.h:126
const NT & n
virtual void create(std::string bufname)
Definition: Agent.C:85
MPI_Comm communicator
Definition: Agent.h:114
void COM_clone_attribute(const char *wname, const char *attr, int wg=1, const char *ptnname=0, int val=0)
Clone the subset of panes of another window of which the given pane attribute has value val...
Definition: roccom_c++.h:234
void finalize_actions()
Definition: Scheduler.C:351
int pre_hdf_handle
Definition: Agent.h:132
void COM_new_window(const char *wname, MPI_Comm c=MPI_COMM_NULL)
Definition: roccom_c++.h:86
void COM_call_function(const int wf, int argc,...)
Definition: roccom_c.C:48
void write_control_file(double t, const std::string base, const std::string window)
Definition: Agent.C:657
Agent * agent
Definition: Agent.h:101
static int read_by_control_handle
Definition: Agent.h:123
void init_callback(const char *surf_win, const char *vol_win, void *option=NULL)
Definition: Agent.C:482
int dobackup
Definition: Agent.h:139
void(Scheduler::* Scheduler_voidfn1_t)(double)
Definition: Scheduler.h:104
int COM_get_window_handle(const char *wname)
Definition: roccom_c++.h:404
void init_function_handles()
Definition: Agent.C:234
virtual void finalize()
Definition: Agent.C:466
void set_name(const char *name)
Definition: Action.h:54
void register_interpolate(InterpolateBase *ip)
Definition: Agent.C:625
int type
Definition: Agent.h:62
void obtain_gm(double *da)
Definition: Agent.C:543
double current_time
Definition: Agent.h:144
void COM_copy_attribute(const char *wname, const char *attr, int wg=1, const char *ptnname=0, int val=0)
Copy an attribute onto another.
Definition: roccom_c++.h:244
#define MAN_DEBUG(l, x)
Definition: rocman.h:98
void create(std::string bufname)
Definition: Agent.C:140
double current_time
Definition: Coupling.h:54
NewAttribute(Agent *ag, std::string target_window_, std::string attr_, char loc_, int type_, int ncomp_, const char *unit_)
Definition: Agent.C:102
virtual void store_solutions(int converged)
Definition: Agent.C:725
const RocmanControl_parameters * get_rocmancontrol_param()
Definition: Coupling.h:184
string suffix
Definition: Agent.h:142
int pc_count
Definition: Agent.h:160
UserScheduler bcInitScheduler
Definition: Agent.h:118
void create_registered_attributes(std::string tmpBuf)
Definition: Agent.C:340
std::string parent_window
Definition: Agent.h:87
void create_registered_window_attributes(std::string target_window)
Definition: Agent.C:350
static int read_files_handle
Definition: Agent.h:124
PhysicsAction action
Definition: Agent.h:116
bool skipInputIO
Definition: Agent.h:164
static int sub
Definition: RocBlas.h:36
void COM_new_attribute(const char *wa_str, const char loc, const int type, int ncomp, const char *unit)
Registering an attribute type.
Definition: roccom_c++.h:118
std::string attr
Definition: Agent.h:49
void COM_set_member_function(const char *wf_str, Member_func_ptr func, const char *wa_str, const char *intents, const COM_Type *types)
Definition: roccom_c++.h:330
virtual void init_convergence(int iPredCorr)
Definition: Agent.C:720
void run(double t, double dt, double alpha)
Definition: Agent.C:50
void schedule()
Definition: Agent.C:456
static int obtain_attr_handle
Definition: Agent.h:125
int finalize_handle
Definition: Agent.h:133
Agent(Coupling *cp, std::string mod, std::string obj, const char *agent_name, MPI_Comm com, bool wgm=false, bool skipio=false)
Definition: Agent.C:201
std::string parent_attr
Definition: Agent.h:74
string mod_instance
Definition: Agent.h:141
void init_bcinitaction(double t)
Definition: Agent.C:557
void * option_data
Definition: Agent.h:150
void init_actions(double t)
Definition: Scheduler.C:324
UserScheduler icScheduler
Definition: Agent.h:120
void add_icaction(Action *act)
Definition: Agent.C:381
void run_actions(double t, double dt)
Definition: Scheduler.C:339
void register_use_attribute(std::string target_window_, std::string attr_, std::string parent_window_, std::string parent_attr_, int wg_=1, const char *ptnname_=0, int val_=0)
Definition: Agent.C:334
const char * ptnname
Definition: Agent.h:76
const Control_parameters * get_control_param()
Definition: Coupling.h:183
double timestamp
Definition: Agent.h:144
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
void COM_resize_array(const char *wa_str, int pane_id=0, void **addr=NULL, int strd=-1, int cap=0)
Resize an attribute on a specific pane and return the address by setting addr.
Definition: roccom_c++.h:200
void init_bcactions(double t)
Definition: Agent.C:445
int COM_get_function_handle(const char *wfname)
Definition: roccom_c++.h:428
virtual char * name()
Definition: Agent.C:80
double old_dt
Definition: Agent.h:144
int comm_rank
Definition: Agent.h:130
void init_gmactions(double t)
Definition: Agent.C:451
AttributeList attributeList
Definition: Agent.h:156
bool withgm
Definition: Agent.h:137
const char * ptnname
Definition: Agent.h:90