GPUE  v1.0
GPU Gross-Pitaevskii Equation numerical solver for Bose-Einstein condensates
fileIO.cu
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <cuda_runtime.h>
6 #include "../include/fileIO.h"
7 
8 namespace FileIO{
9 
10  /*
11  * Reads datafile into memory.
12  */
13  double2* readIn(std::string fileR, std::string fileI,
14  int gSize){
15  FILE *f;
16  f = fopen(fileR.c_str(),"r");
17  int i = 0;
18  double2 *arr = (double2*) malloc(sizeof(double2)*gSize);
19  double line;
20  while(fscanf(f,"%lE",&line) > 0){
21  arr[i].x = line;
22  ++i;
23  }
24  fclose(f);
25  f = fopen(fileI.c_str(),"r");
26  i = 0;
27  while(fscanf(f,"%lE",&line) > 0){
28  arr[i].y = line;
29  ++i;
30  }
31  fclose(f);
32  return arr;
33  }
34 
35  /*
36  * Writes out the parameter file.
37  */
38  void writeOutParam(std::string buffer, Grid &par, std::string file){
39  par.write(file);
40  /*
41  FILE *f;
42  sprintf((char *)buffer.c_str(), "%s", file.c_str());
43  f = fopen(file.c_str(),"w");
44  fprintf(f,"[Params]\n");
45  for (size_t i = 0; i < arr.used(); ++i){
46  fprintf(f,"%s=",arr.array[i].title);
47  fprintf(f,"%e\n",arr.array[i].data);
48  }
49  fclose(f);
50  */
51  }
52 
53  /*
54  * Writes out double2 complex data files.
55  */
56  void writeOut(std::string buffer, std::string file, double2 *data,
57  int length, int step){
58  FILE *f;
59  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
60  f = fopen (buffer.c_str(),"w");
61  int i;
62  for (i = 0; i < length; i++)
63  fprintf (f, "%.16e\n",data[i].x);
64  fclose (f);
65 
66  sprintf ((char *)buffer.c_str(), "%si_%d", file.c_str(), step);
67  f = fopen (buffer.c_str(),"w");
68  for (i = 0; i < length; i++)
69  fprintf (f, "%.16e\n",data[i].y);
70  fclose (f);
71 
72  }
73 
74  /*
75  * Writes out double type data files.
76  */
77  void writeOutDouble(std::string buffer, std::string file, double *data,
78  int length, int step){
79  FILE *f;
80  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
81  f = fopen (buffer.c_str(),"w");
82  int i;
83  for (i = 0; i < length; i++)
84  fprintf (f, "%.16e\n",data[i]);
85  fclose (f);
86  }
87 
88  /*
89  * Writes out bool type data files.
90  */
91  void writeOutBool(std::string buffer, std::string file, bool *data,
92  int length, int step){
93  FILE *f;
94  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
95  f = fopen (buffer.c_str(),"w");
96  int i;
97  for (i = 0; i < length; i++)
98  fprintf (f, "%u\n",data[i]);
99  fclose (f);
100  }
101 
102  /*
103  * Writes out int type data files.
104  */
105  void writeOutInt(std::string buffer, std::string file, int *data,
106  int length, int step){
107  FILE *f;
108  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
109  f = fopen (buffer.c_str(),"w");
110  int i;
111  for (i = 0; i < length; i++)
112  fprintf (f, "%d\n",data[i]);
113  fclose (f);
114  }
115 
116  /*
117  * Writes out int2 data type.
118  */
119  void writeOutInt2(std::string buffer, std::string file, int2 *data,
120  int length, int step){
121  FILE *f;
122  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
123  f = fopen (buffer.c_str(),"w");
124  int i;
125  for (i = 0; i < length; i++)
126  fprintf (f, "%d,%d\n",data[i].x,data[i].y);
127  fclose (f);
128  }
129 
130  /*
131  * Writes out tracked vortex data.
132  */
133  void writeOutVortex(std::string buffer, std::string file,
134  std::vector<std::shared_ptr<Vtx::Vortex>> &data, int step){
135  FILE *f;
136  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
137 
138  f = fopen (buffer.c_str(),"w");
139  int i;
140 
141  fprintf (f, "#UID,X,Xd,Y,Yd,WINDING,isOn\n");
142  for (i = 0; i < data.size(); i++)
143  //fprintf (f, "%d,%d,%e,%d,%e,%d\n",data[i]->getUID(),data[i]->getCoords().x,data[i]->getCoordsD().x,data[i]->getCoords().y,data[i]->getCoordsD().y,data[i]->getWinding());
144  fprintf (f, "%d,%e,%d,%e,%d\n",data[i]->getCoords().x,data[i]->getCoordsD().x,data[i]->getCoords().y,data[i]->getCoordsD().y,data[i]->getWinding());
145  fclose (f);
146  }
147 
148  /*
149  * Opens and closes file. Nothing more. Nothing less.
150  */
151  int readState(std::string name){
152  FILE *f;
153  f = fopen(name.c_str(),"r");
154  fclose(f);
155  return 0;
156  }
157 
158  /*
159  * Outputs the adjacency matrix to a file
160  */
161  void writeOutAdjMat(std::string buffer, std::string file, int *mat, unsigned int *uids, int dim, int step){
162  FILE *f;
163  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
164  f = fopen (buffer.c_str(),"w");
165  fprintf (f, "(*");
166  for(int ii = 0; ii<dim; ++ii){
167  fprintf (f, "%d",uids[ii]);
168  }
169  fprintf (f, "*)\n");
170  fprintf (f, "{\n");
171  for(int ii = 0; ii < dim; ++ii){
172  fprintf (f, "{");
173  for(int jj = 0; jj < dim; ++jj){
174  fprintf (f, "%i",mat[ii*dim + jj]);
175  if(jj<dim-1)
176  fprintf (f, ",");
177  else
178  fprintf (f, "}");
179  }
180  if(ii<dim-1)
181  fprintf (f, ",");
182  fprintf (f, "\n");
183  }
184  fprintf (f, "}\n");
185  fclose(f);
186  }
187  void writeOutAdjMat(std::string buffer, std::string file, double *mat,
188  unsigned int *uids, int dim, int step){
189  FILE *f;
190  sprintf ((char *)buffer.c_str(), "%s_%d", file.c_str(), step);
191  f = fopen (buffer.c_str(),"w");
192  fprintf (f, "(*");
193  for(int ii = 0; ii<dim; ++ii){
194  fprintf (f, "%d",uids[ii]);
195  if(ii!=dim-1)
196  /* I am not sure what Lee wants here, but I think...
197  fprintf (f, ",",uids[ii]); */
198  fprintf (f, ",");
199 
200  }
201  fprintf (f, "*)\n");
202  fprintf (f, "{\n");
203  for(int ii = 0; ii < dim; ++ii){
204  fprintf (f, "{");
205  for(int jj = 0; jj < dim; ++jj){
206  fprintf (f, "%e",mat[ii*dim + jj]);
207  if(jj<dim-1)
208  fprintf (f, ",");
209  else
210  fprintf (f, "}");
211  }
212  if(ii<dim-1)
213  fprintf (f, ",");
214  fprintf (f, "\n");
215  }
216  fprintf (f, "}\n");
217  fclose(f);
218  }
219 }