Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TransferFunctions.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 #include <iostream>
24 #include <fstream>
25 #include <string>
26 #include <list>
27 #include <map>
28 #include <sstream>
29 #include <iomanip>
30 #include <vector>
31 
32 #include <unistd.h>
33 
34 //#ifdef _TRAIL_MPI_
35 #include "mpi.h"
36 //#else
37 //typedef int MPI_Comm;
38 //#endif
39 
40 #include "GEM.H"
41 #include "TRAIL.H"
42 #include "TRAIL_Flu.H"
43 #include "TRAIL_Remesh.H"
44 
45 //#ifdef _ROCSTAR_X_
46 #include "roccom.h"
47 //#endif
48 
49 COM_EXTERN_MODULE( Rocface);
54 
55 using namespace std;
56 
57 
58 
59 void
60 read_file( const string &fname,
61  const string &wname,
62  vector<int> &bcflags,
63  MPI_Comm comm,
64  bool apply_disp,
65  bool all,
66  bool with_ghost)
67 {
68  // char *lastdot=strrchr( fname, '.');
69 
70  COM_new_window( wname.c_str());
71  // Read in HDF files or a Rocin control file
72  std::cout << "Reading file " << fname << "..." << std::endl;
73 
74  // Read in HDF format
76 
77  int IN_read;
78  // Read in HDF format using Rocin::read_window or ::read_by_control_file
79  IN_read = COM_get_function_handle( "TRAILIN.read_by_control_file");
80 
81  // Pass MPI_COMM_NULL to Rocin so that the rank becomes a wildcard.
82  // MPI_Comm comm_null = MPI_COMM_NULL;
83  std::string bufwin("bufwin");
84  COM_call_function( IN_read, fname.c_str(), bufwin.c_str(), &comm);
85  int IN_obtain = COM_get_function_handle( "TRAILIN.obtain_attribute");
86 
87  if(!bcflags.empty()){
88  // Check whether bcflag exists. If so, retain only the panes with flag<=1.
89  int bcflag = COM_get_attribute_handle((bufwin+".bcflag").c_str());
90  if (bcflag > 0) {
91  // Read in bcflags.
92  COM_call_function( IN_obtain, &bcflag, &bcflag);
93 
94  // Obtain the IDs of the panes of the window
95  int npanes, *pane_ids;
96  COM_get_panes( bufwin.c_str(), &npanes, &pane_ids);
97 
98  // Loop through the panes to remove those with bcflag >1.
99  for ( int i=0; i<npanes; ++i) {
100  int *flag;
101  COM_get_array( (bufwin+".bcflag").c_str(), pane_ids[i], &flag);
102  if ( flag==NULL )
103  COM_delete_pane( bufwin.c_str(),pane_ids[i]);
104  bool delite = true;
105  vector<int>::iterator bcfi = bcflags.begin();
106  while(bcfi != bcflags.end() && delite)
107  if(*bcfi++ == *flag)
108  delite = false;
109  if(delite)
110  COM_delete_pane( bufwin.c_str(), pane_ids[i]);
111  }
112  // remove buffers.
113  COM_free_buffer( &pane_ids);
114  }
115  }
116  if(apply_disp){
117  // This is NOT correct for problems with regression.
118  int disp_hndl = COM_get_attribute_handle((bufwin+".uhat").c_str());
119  if(disp_hndl > 0){
120  std::cout << "Applying total displacements..." << std::endl;
122  int add = COM_get_function_handle( "BLAS.add");
123  COM_call_function(IN_obtain,&disp_hndl,&disp_hndl);
124  int nc_hndl = COM_get_attribute_handle( bufwin + ".nc");
125  COM_call_function( add, &disp_hndl, &nc_hndl, &nc_hndl);
127  }
128  }
129  // Read in the mesh.
130  int buf_atts;
131  if(all)
132  buf_atts = COM_get_attribute_handle((bufwin+".all").c_str());
133  else
134  buf_atts = COM_get_attribute_handle((bufwin+".mesh").c_str());
135  COM_call_function( IN_obtain, &buf_atts, &buf_atts);
137 
138  if(!all)
139  // Remove all attributes except for the mesh
140  COM_delete_attribute( (bufwin+".atts").c_str());
141 
142 
143  std::cout << "Obtained window " << wname
144  << " from file " << fname << std::endl;
145 
146  // Change the memory layout to contiguous.
147  if(all)
148  COM_clone_attribute( (wname+".all").c_str(),
149  (bufwin+".all").c_str(), (with_ghost ? 1 : 0));
150  else
151  COM_clone_attribute( (wname+".mesh").c_str(),
152  (bufwin+".mesh").c_str(), (with_ghost ? 1 : 0));
153 
154  COM_delete_window( bufwin.c_str());
155 }
156 
157 
158 // A serial program, do not call this on mulitiple procs simultaneously
159 bool
161  const string &src,
162  double t,
163  MPI_Comm comm)
164 {
165  // src will help specify the source window's file names:
166  // src_in_<timestring>.txt
167  std::string timestring(TRAIL_TimeString(t));
168  std::string srcfile(src + "_in_" + timestring + ".txt");
169  // the target window's files are specified by
170  // gp.surface_window_in_<timestring>.txt
171  std::string trgfile(gp.surface_window+"_in_"+timestring+".txt");
172  int rank = 0;
173  MPI_Comm_rank(comm,&rank);
174  std::string srcwin(src+"_coup");
175  std::string trgwin(gp.surface_window+"_coup");
176  vector<int> bcflags(2);
177  bcflags[0] = 0;
178  bcflags[1] = 1;
179  const char *format = "HDF";
180 
181 
182 
183  // Serial step to create the common refinement
184  if(!rank){
185  read_file(trgfile,trgwin,bcflags,MPI_COMM_NULL,false,false,false);
186  read_file(srcfile,srcwin,bcflags,MPI_COMM_NULL,false,false,false);
187 
188  COM_LOAD_MODULE_STATIC_DYNAMIC( Rocface, "RFC");
189 
190  int RFC_overlay = COM_get_function_handle( "RFC.overlay");
191  int RFC_write = COM_get_function_handle( "RFC.write_overlay");
192  int src_mesh = COM_get_attribute_handle( (srcwin+".mesh").c_str());
193  int trg_mesh = COM_get_attribute_handle( (trgwin+".mesh").c_str());
194 
195  if(gp._debug && gp._out)
196  *gp._out << "TRAIL_AutoSurfer: Creating overlay for coupled surfaces."
197  << std::endl;
198  COM_call_function( RFC_overlay, &src_mesh, &trg_mesh);
199  if(gp._debug && gp._out)
200  *gp._out << "TRAIL_AutoSurfer: Writing overlay for coupled surfaces."
201  << std::endl;
202  COM_call_function( RFC_write, &src_mesh, &trg_mesh,
203  srcwin.c_str(), trgwin.c_str(), format);
204 
205  COM_UNLOAD_MODULE_STATIC_DYNAMIC( Rocface, "RFC");
206  }
207 
208 
209  // Now we have a common refinement, use it to transfer data from the old
210  // surface to the new surface
211  COM_LOAD_MODULE_STATIC_DYNAMIC( Rocface, "RFC");
212  int RFC_transfer = COM_get_function_handle("RFC.least_squares_transfer");
213  int RFC_read = COM_get_function_handle( "RFC.read_overlay");
214 
215  string r_trgwin("new_surf_b");
216  read_file(trgfile,trgwin,bcflags,comm,false,true,false);
217  read_file(srcfile,srcwin,bcflags,comm,false,true,false);
218  read_file(trgfile,r_trgwin,bcflags,comm,false,true,true);
219 
220  int srcmesh = COM_get_attribute_handle( (srcwin+".mesh").c_str());
221  int trgmesh = COM_get_attribute_handle( (trgwin+".mesh").c_str());
222  std::vector<int> pane_id;
223 
224  if(gp._debug && gp._out)
225  *gp._out << "TRAIL_AutoSurfer: Reading mesh overlay for coupled surfaces."
226  << std::endl;
227  COM_call_function( RFC_read, &srcmesh, &trgmesh, &comm,
228  srcwin.c_str(),trgwin.c_str(),format);
229 
230  int num_attributes;
231  string names;
232  COM_get_attributes( srcwin.c_str(),&num_attributes,names);
233  char loc;
234  COM_Type comtype;
235  int ncomp;
236  std::string unit;
237  istringstream Istr(names);
238  for(int i = 0;i < num_attributes;i++){
239  string aname;
240  Istr >> aname;
241  COM_get_attribute(srcwin+"."+aname,&loc,&comtype,&ncomp,&unit);
242  if((loc == 'e' || loc == 'n') && comtype == COM_DOUBLE){
243  if(!rank)
244  cout << "Transferring attribute: " << aname << " on "
245  << (loc == 'e' ? "elements" : "nodes") << "." << endl;
246  COM_resize_array((srcwin+"."+aname).c_str());
247  COM_new_attribute((trgwin+"."+aname).c_str(),(char)loc,
248  COM_DOUBLE,(int)ncomp,unit.c_str());
249  COM_new_attribute((r_trgwin+"."+aname).c_str(),(char)loc,
250  COM_DOUBLE,(int)ncomp,unit.c_str());
251  COM_resize_array((r_trgwin+"."+aname).c_str());
252  COM_resize_array((trgwin+"."+aname).c_str());
253  int src_ahdl = COM_get_attribute_handle((srcwin+"."+aname).c_str());
254  int trg_ahdl = COM_get_attribute_handle((trgwin+"."+aname).c_str());
255  COM_call_function( RFC_transfer, &src_ahdl, &trg_ahdl);
256  int *srcpane_ids;
257  int npanes;
258  COM_get_panes( trgwin.c_str(), &npanes, &srcpane_ids);
259  pane_id.resize(npanes);
260  for(int i = 0;i < npanes;i++)
261  pane_id[i] = srcpane_ids[i];
262  // These are no longer necessary as we've duped the info into
263  // a locally allocated array
264  COM_free_buffer( &srcpane_ids);
265  for(int p = 0;p < npanes;p++){
266  void *src_ptr = NULL;
267  int src_std = 0;
268  int src_cap = 0;
269  void *trg_ptr = NULL;
270  int trg_std = 0;
271  int trg_cap = 0;
272  COM_get_array((trgwin+"."+aname).c_str(),pane_id[p],
273  &src_ptr,&src_std,&src_cap);
274  COM_get_array((r_trgwin+"."+aname).c_str(),pane_id[p],
275  &trg_ptr,&trg_std,&trg_cap);
276  if(src_ptr && trg_ptr && (trg_std*trg_cap == src_std*src_cap))
277  memcpy(trg_ptr,src_ptr,sizeof(double)*src_std*src_cap);
278  else
279  if(gp._out)
280  *gp._out << "TRAIL_AutoSurfer: WARNING: non matching sizes for "
281  << aname << " on pane " << pane_id[p] << "."
282  << std::endl;
283  }
284  }
285  }
286 
287  // Write new surface and Rocin control files for it
289  int OUT_set_option = COM_get_function_handle( "Rocout.set_option");
290  std::string rankstr("0");
291  COM_call_function( OUT_set_option, "rankwidth", rankstr.c_str());
292  std::ostringstream Ostr;
293  Ostr << r_trgwin << "_" << timestring << "_" << setw(5)
294  << setfill('0') << rank+1;
295  int whand = COM_get_function_handle("Rocout.write_attribute");
296  int all = COM_get_attribute_handle((r_trgwin+".all"));
297  COM_call_function(whand,Ostr.str().c_str(),&all,
298  r_trgwin.c_str(),timestring.c_str());
299  std::ofstream Ouf;
300  string controlfilename(Ostr.str() + "_in.txt");
301  Ouf.open(controlfilename.c_str());
302  Ouf << "@Proc: " << rank << endl
303  << "@Files: " << Ostr.str() << ".hdf" << std::endl;
304  Ouf.clear();
305  Ouf << "@Panes: ";
306  std::vector<int>::iterator pii = pane_id.begin();
307  while(pii != pane_id.end())
308  Ouf << *pii++ << " ";
309  Ouf << endl;
310  Ouf.close();
311  Ouf.clear();
312 
313  // Merge Rocin control files
314  if(!rank){
315  Ostr.clear();
316  Ostr.str("");
317  Ostr << r_trgwin << "_" << timestring << "_";
318  ostringstream OutS;
319  unsigned int id = 1;
320  while(id <= gp._npart){
321  ifstream Inf;
322  ostringstream Ofn;
323  Ofn << Ostr.str() << setw(5) << setfill('0')
324  << id++;
325  string filename(Ofn.str() + "_in.txt");
326  Inf.open(filename.c_str());
327  if(Inf){
328  OutS << Inf.rdbuf() << endl;
329  Inf.close();
330  unlink(filename.c_str());
331  }
332  }
333  ofstream Ouf2;
334  Ouf2.open((r_trgwin+"_in_"+timestring+".txt").c_str());
335  Ouf2 << OutS.str();
336  Ouf2.close();
337  Ostr.clear();
338  Ostr.str("");
339  }
340 
342  COM_UNLOAD_MODULE_STATIC_DYNAMIC(Rocface,"RFC");
343  return(true);
344 }
345 
346 
347 
348 
349 
350 
int COM_Type
Indices for derived data types.
Definition: roccom_basic.h:122
void COM_delete_window(const char *wname)
Definition: roccom_c++.h:94
void COM_get_attribute(const std::string wa_str, char *loc, int *type, int *ncomp, std::string *unit)
Definition: roccom_c++.h:269
void read_file(const char *fname, const string &wname, double alpha)
Definition: autosurfer.C:39
This file contains the prototypes for Roccom API.
void TRAIL_AutoSurfer(const std::string &src, const std::string &trg, const std::string &srcpath=".", const std::string &trgpath=".", const std::string &destpath=".", double t=0, MPI_Comm comm=MPI_COMM_NULL, std::ostream *=NULL)
Definition: TRAIL.C:2544
void COM_get_array(const char *wa_str, int pane_id, void **addr, int *strd, int *cap)
Get the address for an attribute on a specific pane.
int COM_get_attribute_handle(const char *waname)
Definition: roccom_c++.h:412
unsigned int _npart
Definition: GEM.H:285
void COM_delete_attribute(const char *wa_str)
Delete an existing attribute.
Definition: roccom_c++.h:128
std::string TRAIL_TimeString(double t)
Definition: TRAIL.C:85
#define COM_UNLOAD_MODULE_STATIC_DYNAMIC(moduleName, windowString)
Definition: roccom_basic.h:113
void COM_get_attributes(const char *wname, int *na, std::string &names)
Definition: roccom_c++.h:360
std::ostream * _out
Definition: GEM.H:308
Definition: Rocin.h:64
Definition: Rocout.h:81
blockLoc i
Definition: read.cpp:79
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 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 COM_delete_pane(const char *str, int pid)
Definition: roccom_c++.h:110
bool _debug
Definition: GEM.H:310
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
void COM_get_panes(const char *wname, std::vector< int > &pane_ids, int rank=-2)
Definition: roccom_c++.h:350
void COM_free_buffer(int **buf)
Definition: roccom_c++.h:397
static int rank
Definition: advectest.C:66
std::string surface_window
Definition: GEM.H:319
#define COM_LOAD_MODULE_STATIC_DYNAMIC(moduleName, windowString)
Definition: roccom_basic.h:111
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
int COM_get_function_handle(const char *wfname)
Definition: roccom_c++.h:428
#define COM_EXTERN_MODULE(moduleName)
Definition: roccom_basic.h:116