Memosa-FVM  0.2
MPM_Particles.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "MPM_Particles.h"
6 
7 #include "StorageSite.h"
8 #include "CRConnectivity.h"
9 
10 
11 
12 typedef MPM::VecD3 VecD3;
14 
15 MPM::MPM(string fileName):
16  _particles(0),
17  _coordinates(),
18  _velocities(),
19  _types(),
20  _temperatures()
21 {
22  char* file;
23  file = &fileName[0];
24 
25  // MPM::setandwriteParticles(file);
26  //get coordinate
27  const shared_ptr<VecD3Array> MPM_Coordinates = MPM::readCoordinates(file);
28 
29  //get velocity
30  const shared_ptr<VecD3Array> MPM_Velocities = MPM::readVelocities(file);
31 
32  //get type
33  const shared_ptr<Array<int> > MPM_Types = MPM::readTypes(file);
34 
35  //get temperature
36  const shared_ptr<Array<double> > MPM_Temperatures = MPM::readTemperatures(file);
37  //store all the information in MPM class
38  MPM::Init(MPM_Coordinates, MPM_Velocities, MPM_Types, MPM_Temperatures);}
39 
41  _particles(0),
42  _coordinates(),
43  _velocities(),
44  _types(),
45  _temperatures()
46 {}
47 
48 
49 MPM::~MPM() { }
50 
51 
52 void MPM::setandwriteParticles(const char *file)
53 {
54 
55  FILE *fp;
56 
57  VecD3 center;
58  center[0]=0.0;
59  center[1]=0.0;
60  center[2]=0.0;
61 
62  int count=0;
63 
64 #if 0
65  //set up particle cartesian coordinate
66  VecD3 temp;
67 
68  const double innerSide = 1.0;
69  const double midSide = 3.0;
70  const double outerSide = 4.2;
71 
72  int nX=20, nY=20, nZ=1;
73 
74  double gapX=outerSide/(nX), gapY=outerSide/(nY), gapZ=0;
75  const int nMPM = (nX+1)*(nY+1)*nZ;
76  Array<VecD3> solidPoint(nMPM);
77  Array<VecD3> solidVelocity(nMPM);
78  Array<int> type(nMPM);
79  Array<double> solidTemperature(nMPM);
80  for(int i=0; i<=nX; i++){
81  for(int j=0; j<=nY; j++){
82  for(int k=0; k<nZ; k++){
83  temp[0]=i*gapX-outerSide/2.;
84  temp[1]=j*gapY-outerSide/2.;
85  temp[2]=k*gapZ;
86  //inner square
87  if( temp[0]>=(-innerSide/2.) && temp[0]<=(innerSide/2.) && temp[1]>=(-innerSide/2.) && temp[1]<=(innerSide/2.))
88  {
89  //surface particles
90  type[count] = 0;
91  if( temp[0]> (innerSide/2.0-gapX) || temp[0]<(-innerSide/2.0+gapX))
92  type[count]=1;
93  if( temp[1]> (innerSide/2.0-gapY) || temp[1]<(-innerSide/2.0+gapY))
94  type[count]=1;
95  //rotate
96  double alfa = atan(1.);
97  solidPoint[count][0]=temp[0]*cos(alfa)-temp[1]*sin(alfa);
98  solidPoint[count][1]=temp[1]*cos(alfa)+temp[0]*sin(alfa);
99  solidPoint[count][2]=temp[2];
100 
101  count+=1;
102  }
103 
104  //outer square
105  if( !(temp[0]>(-midSide/2.) && temp[0]<(midSide/2.) && temp[1]>(-midSide/2.) && temp[1]<(midSide/2.)))
106  {
107  type[count] = 0;
108  //surface particles
109 
110  if( temp[0]< (midSide/2.0+gapX) && temp[0]>(midSide/2.0-gapX) && temp[1]<(midSide/2.0+gapY) && temp[1]>(-midSide/2.0-gapY))
111  type[count]=1;
112  if( temp[0]> (-midSide/2.0-gapX) && temp[0]<(-midSide/2.0+gapX) && temp[1]<(midSide/2.0+gapY) && temp[1]>(-midSide/2.0-gapY))
113  type[count]=1;
114  if( temp[1]< (midSide/2.0+gapY) && temp[1]>(midSide/2.0-gapY) && temp[0]<(midSide/2.0+gapX) && temp[0]>(-midSide/2.0-gapX))
115  type[count]=1;
116  if( temp[1]> (-midSide/2.0-gapY) && temp[1]<(-midSide/2.0+gapY) && temp[0]<(midSide/2.0+gapX) && temp[0]>(-midSide/2.0-gapX))
117  type[count]=1;
118 
119  double alfa = atan(1);
120  solidPoint[count][0]=temp[0]*cos(alfa)-temp[1]*sin(alfa);
121  solidPoint[count][1]=temp[1]*cos(alfa)+temp[0]*sin(alfa);
122  solidPoint[count][2]=temp[2];
123  count+=1;
124  }
125 
126  }
127  }
128  }
129 
130 
131 #endif
132 
133 #if 0
134  int nX=81, nY=400, nZ=1;
135  double radius1=0., radius2=0.2;
136  double gapR=(radius2-radius1)/(nX-1), gapAngle=2*3.1415926/nY, gapZ=1.0/nZ;
137 
138  const int nMPM = nX*nY*nZ;
139  Array<VecD3> solidPoint(nMPM);
140  Array<VecD3> solidVelocity(nMPM);
141  Array<int> type(nMPM);
142  Array<double> solidTemperature(nMPM);
143 
144 
145  //polar coordinate
146  for(int i=0; i<nX; i++){
147  for(int j=0; j<nY; j++){
148  for(int k=0; k<nZ; k++){
149  solidPoint[count][0]=(radius1+i*gapR)*(cos(j*gapAngle))+center[0];
150  solidPoint[count][1]=(radius1+i*gapR)*(sin(j*gapAngle))+center[1];
151  solidPoint[count][2]=k*gapZ+center[2];
152 
153  if(i!=(nX-1)) type[count] = 0; //internal particles
154  if(i==(nX-1)){
155  type[count] = 1; //surface particles
156  }
157  count+=1;
158  }
159  }
160  }
161 
162  /*
163  radius1=0.9, radius2=1.5;
164  gapR=(radius2-radius1)/nX, gapAngle=2*3.1415926/nY, gapZ=1.0/nZ;
165 
166  //polar coordinate
167  for(int i=0; i<nX; i++){
168  for(int j=0; j<nY; j++){
169  for(int k=0; k<nZ; k++){
170  solidPoint[count][0]=(radius1+i*gapR)*(cos(j*gapAngle))+center[0];
171  solidPoint[count][1]=(radius1+i*gapR)*(sin(j*gapAngle))+center[1];
172  solidPoint[count][2]=k*gapZ+center[2];
173 
174  if(i!=0) type[count] = 0; //internal particles
175  if(i==0){
176  type[count] = 1; //surface particles
177  }
178  count+=1;
179  }
180  }
181  }
182  */
183 #endif
184 #if 1
185  int nX=21, nY=300, nZ=400;
186  const double pi = atan(1.0)*4.0;
187  double radius1=0., radius2=10;
188  double gapR=(radius2-radius1)/(nX-1), gapAlfa=pi/nY, gapBeta=2*pi/nZ;
189  const int nMPM = nX*nY*nZ;
190  Array<VecD3> solidPoint(nMPM);
191  Array<VecD3> solidVelocity(nMPM);
192  Array<int> type(nMPM);
193  Array<double> solidTemperature(nMPM);
194  //polar coordinate
195  for(int i=0; i<nX; i++){
196  for(int j=0; j<nY; j++){
197  for(int k=0; k<nZ; k++){
198  solidPoint[count][0]=(radius1+i*gapR)*sin(j*gapAlfa)*(cos(k*gapBeta))+center[0];
199  solidPoint[count][1]=(radius1+i*gapR)*sin(j*gapAlfa)*(sin(k*gapBeta))+center[1];
200  solidPoint[count][2]=(radius1+i*gapR)*cos(j*gapAlfa)+center[2];
201 
202  type[count] = 0; //internal particles
203  if(i==nX-1){
204  type[count] = 1; //surface particles
205  }
206  count+=1;
207  }
208  }
209  }
210 
211 #endif
212  //set up particle velocity
213 #if 1
214  for(int p=0; p<count; p++){
215  solidVelocity[p][0]=0.0;
216  solidVelocity[p][1]=0.0;
217  solidVelocity[p][2]=0.0;
218  }
219 #endif
220 
221 #if 0
222  //set up rotating cylinder velcity
223  const double angV = 1;
224  for (int p=0; p<count; p++){
225  double r = mag(solidPoint[p]-center);
226  double angle = atan2(solidPoint[p][1]-center[1],solidPoint[p][0]-center[0]);
227  solidVelocity[p][0] = -angV*r*sin(angle);
228  solidVelocity[p][1] = angV*r*cos(angle);
229  solidVelocity[p][2] = 0.0;
230  }
231 #endif
232 
233  //set up temperature
234  const double InitT=300.0;
235  for (int p=0; p<count; p++){
236  solidTemperature[p]=InitT;
237  }
238 
239 
240  cout<<"count of particles is "<<count<<endl;
241  //write out coordinate and velocity and particle type into file
242  fp=fopen(file,"w");
243  fprintf(fp,"%i\n",count);
244  for(int p=0; p<count; p++){
245  fprintf(fp, "%e\t%e\t%e\n", solidPoint[p][0],solidPoint[p][1],solidPoint[p][2]);
246  }
247  for(int p=0; p<count; p++){
248  fprintf(fp, "%e\t%e\t%e\n", solidVelocity[p][0],solidVelocity[p][1],solidVelocity[p][2]);
249  }
250  for(int p=0; p<count; p++){
251  fprintf(fp, "%i\n", type[p]);
252  }
253  for(int p=0; p<count; p++){
254  fprintf(fp, "%f\n", solidTemperature[p]);
255  }
256  fclose(fp);
257 
258 }
259 
260 
261 const shared_ptr<Array<VecD3> > MPM::readCoordinates(const char *file)
262 
263 {
264  FILE *fp;
265  int nMPM;
266  double x=0, y=0, z=0;
267 
268  fp=fopen(file,"r");
269  fscanf(fp,"%i\n",&nMPM);
270  cout<<"number of particles is"<<nMPM<<endl;
271 
272  shared_ptr<Array<VecD3> > MPM_Points ( new Array<VecD3> (nMPM));
273  //read in coordinate
274  for(int i=0; i<nMPM; i++){
275  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
276  (*MPM_Points)[i][0]=x;
277  (*MPM_Points)[i][1]=y;
278  (*MPM_Points)[i][2]=z;
279  }
280  fclose(fp);
281  return (MPM_Points);
282 }
283 
284 const shared_ptr<Array<VecD3> > MPM::readVelocities(const char *file)
285 
286 {
287  FILE *fp;
288  int nMPM;
289  double vx=0, vy=0, vz=0;
290  double x=0, y=0, z=0;
291  fp=fopen(file,"r");
292  fscanf(fp,"%i\n",&nMPM);
293 
294  shared_ptr<Array<VecD3> > MPM_Points ( new Array<VecD3> (nMPM));
295  //read in cooridnate and skip
296  for(int i=0; i<nMPM; i++){
297  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
298  }
299  //read in velocity
300  for(int i=0; i<nMPM; i++){
301  fscanf(fp,"%lf\t%lf\t%lf\n", &vx, &vy, &vz);
302  (*MPM_Points)[i][0]=vx;
303  (*MPM_Points)[i][1]=vy;
304  (*MPM_Points)[i][2]=vz;
305  }
306  fclose(fp);
307  return (MPM_Points);
308 }
309 
310 
311 const shared_ptr<Array<int> > MPM::readTypes(const char *file)
312 
313 {
314  FILE *fp;
315  int nMPM;
316  double x=0, y=0, z=0;
317  int t=0;
318  fp=fopen(file,"r");
319  fscanf(fp,"%i\n",&nMPM);
320 
321  shared_ptr<Array<int> > MPM_Points ( new Array<int> (nMPM));
322  //read in cooridnate and skip
323  for(int i=0; i<nMPM; i++){
324  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
325  }
326  //read in velocity and skip
327  for(int i=0; i<nMPM; i++){
328  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
329  }
330  //read in type
331  for(int i=0; i<nMPM; i++){
332  fscanf(fp,"%i\n", & t);
333  (*MPM_Points)[i]=t;
334  }
335  fclose(fp);
336  return (MPM_Points);
337 }
338 const shared_ptr<Array<double> > MPM::readTemperatures(const char *file)
339 
340 {
341  FILE *fp;
342  int nMPM;
343  double x=0, y=0, z=0;
344  int t=0;
345  double temperature=0.0;
346  fp=fopen(file,"r");
347  fscanf(fp,"%i\n",&nMPM);
348 
349  shared_ptr<Array<double> > MPM_Points ( new Array<double> (nMPM));
350  //read in cooridnate and skip
351  for(int i=0; i<nMPM; i++){
352  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
353  }
354  //read in velocity and skip
355  for(int i=0; i<nMPM; i++){
356  fscanf(fp,"%lf\t%lf\t%lf\n", &x, &y, &z);
357  }
358  //read in type
359  for(int i=0; i<nMPM; i++){
360  fscanf(fp,"%i\n", & t);
361  }
362  //read in temperature
363  for(int i=0; i<nMPM; i++){
364  fscanf(fp,"%lf\n", & temperature);
365  (*MPM_Points)[i]=temperature;
366  }
367  fclose(fp);
368  return (MPM_Points);
369 }
370 
371 void MPM::Init(const shared_ptr<Array<VecD3> > coordinates,
372  const shared_ptr<Array<VecD3> > velocities,
373  const shared_ptr<Array<int> > types,
374  const shared_ptr<Array<double> > temperatures)
375 {
376 
377  const int n = (*coordinates).getLength(); //number of particles
378  _particles.setCount(n);
379 
380  MPM::setCoordinates(coordinates);
381  MPM::setVelocities(velocities);
382  MPM::setTypes(types);
383  MPM::setTemperatures(temperatures);
384 
385 }
386 
void Init(const shared_ptr< Array< VecD3 > > coordinates, const shared_ptr< Array< VecD3 > > velocities, const shared_ptr< Array< int > > types, const shared_ptr< Array< double > > temperatures)
const shared_ptr< Array< double > > readTemperatures(const char *file)
const shared_ptr< Array< VecD3 > > readCoordinates(const char *file)
T mag(const Vector< T, 3 > &a)
Definition: Vector.h:260
void setCount(const int selfCount, const int nGhost=0)
Definition: StorageSite.h:42
StorageSite _particles
Definition: MPM_Particles.h:71
void setTemperatures(const shared_ptr< ArrayBase > t)
Definition: MPM_Particles.h:51
MPM::VecD3Array VecD3Array
void setCoordinates(const shared_ptr< ArrayBase > x)
Definition: MPM_Particles.h:42
MPM::VecD3 VecD3
void setandwriteParticles(const char *file)
Tangent sin(const Tangent &a)
Definition: Tangent.h:307
Definition: Array.h:14
void setTypes(const shared_ptr< ArrayBase > type)
Definition: MPM_Particles.h:48
const shared_ptr< Array< VecD3 > > readVelocities(const char *file)
const shared_ptr< Array< int > > readTypes(const char *file)
void setVelocities(const shared_ptr< ArrayBase > v)
Definition: MPM_Particles.h:45