Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Particle movement question

by koda123 (Initiate)
on Apr 23, 2017 at 22:45 UTC ( [id://1188727]=perlquestion: print w/replies, xml ) Need Help??

koda123 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, currently i'm working on "transferring" bits and pieces of a C code that I have into perl. The problem is related to finite differences, so I am creating a grid, where "particles" within each cell in the grid are moving based on my specified iterations and timesteps. The loop structures and boundary conditions are relatively straight forward (at least for my purposes). Comparing it to C, how can i set up a structure where there are say 9 particles per each grid unit?

Replies are listed 'Best First'.
Re: Particle movement question
by GrandFather (Saint) on Apr 24, 2017 at 06:48 UTC

    How do you do it in C? In the first instance you can easily transliterate the C to Perl. That's almost as simple as sticking a $ in front of each variable and changing function declarations to Perl syntax.

    Once you have the first cut of the conversion done and your Perl test suite gives the same answers as the C test suite, you can tidy up the Perl code to take advantage of Perl's greater expressiveness to end up with easier to maintain code.

    If that really is a problem for you show us the C code at least and we can help with translation. Although if your Perl is that shaky, why are you converting your working C code?

    Premature optimization is the root of all job security
Re: Particle movement question
by Anonymous Monk on Apr 23, 2017 at 23:02 UTC
    More info please... are there only 9 different particles, or are there lots of different kinds of particles and each cell can only hold up to nine at a time, etc.? What's your code so far?
    my @grid_a; # there are only 9 particles my @grid_b; # there are lots of particles for my $x (0..9) { for my $y (0..9) { for my $particle (0..8) { $grid_a[$x][$y][$particle] = 1; $grid_b[$x][$y]{$particle} = 1; } my $pcount = keys %{$grid_b[$x][$y]}; } } use Data::Dumper; print Dumper(\@grid_a, \@grid_b);

    perlreftut, perldsc

      The basis is that within a grid, there is a rectangular portion, approximately one fifth of the total x distance and the entire y distance containing a number of particles, del_x distance apart (representing a fluid). At the initial position each grid has a horizontal velocity, and a vertical velocity. I am trying to simulate a scenario where the right boundary of the rectangle is suddenly removed, releasing all of the "fluid" So by assigning each grid unit within the rectangle 9 particles, I would essentially be assigning values to the free boundary (The right and top boundary), performing my calculations, resulting in a new horizontal and vertical velocity, determine the new velocity at a time n+1, and then compute the particle position. the problem would be creating a particle that would be tracked.

        Show us your code so far.
Re: Particle movement question
by BillKSmith (Monsignor) on Apr 24, 2017 at 14:50 UTC
    I recommend searching for related modules on CPAN. If you are really lucky, you will find a module to do the job for you. Short of this, you can still learn about corner cases you may not have thought of or neat ways to do part of the job, etc.
    Bill
Re: Particle movement question
by koda123 (Initiate) on Apr 25, 2017 at 22:09 UTC

    So this would be the C code I need to implement into the code.

    if (strcmp(streakfile,"none") || strcmp(tracefile,"none")) Particlelines = SET_PARTICLES(N,pos1x,pos1y,pos2x,pos2y); Particlelines = INIT_PARTICLES(&N,imax,jmax,delx,dely,ppc,problem,U,V +); SETBCOND(U,V,P,TEMP,FLAG,imax,jmax,wW,wE,wN,wS); SETSPECBCOND(problem,U,V,P,TEMP,imax,jmax,UI,VI); struct particleline *INIT_PARTICLES (int *N,int imax,int jmax, REAL delx,REAL dely, int ppc,char *problem,REAL **U,RE +AL **V) { int i,j,ip,jp; struct particleline *Particlelines; REAL x,y; REAL height=0,rad=0,mpx=0,mpy=0,vstart=0; if((Particlelines=(struct particleline *) malloc((unsigned)(*N) * sizeof(struct particleline))) == +NULL) { printf("no memory"); exit(0); } Particlelines -= 1; /* Particlelines from 1 to N */ for (i=1;i<=*N;i++) { Particlelines[i].length = 0; Particlelines[i].Particles = PARTALLOC(-1.,-1.); } /* Set the particles */ for (i=1;i<=imax;i++) for (j=1;j<=jmax;j++) for (ip=1;ip<=ppc;ip++) { x = (i-1)*delx+(ip-.5)/((REAL)ppc)*delx; for (jp=1;jp<=ppc;jp++) { y = (j-1)*dely+(jp-.5)/((REAL)ppc)*dely; if(strcmp(problem, "dam")==0) if (x<0.2*imax*delx) SET_PART(&Particlelines[1],x,y); if(strcmp(problem, "drop")==0) { if (y<height) { SET_PART(&Particlelines[1],x,y); } else if ((x-mpx)*(x-mpx)+(y-mpy)*(y-mpy) <= rad*rad) { SET_PART(&Particlelines[2],x,y); V[i][j] = vstart; } } } } return (Particlelines); } /*----------------------------------------------------------------*/ /* Add particle to "Partline" at (x,y) */ /*----------------------------------------------------------------*/ void SET_PART(struct particleline *Partline, REAL x,REAL y) { struct particle *part; part = PARTALLOC(x,y); /* create particle +*/ part->next = (*Partline).Particles->next; /* add it to "Partline" +*/ (*Partline).Particles->next = part; /* in the first position +*/ (*Partline).length++; }
      Requirements are far more useful. What are the rules you go by to determine how a particle behaves?

        The particle will act as a fluid being confined by a free boundary. The first processing step is marking all of the cells with particles in them as fluid cells, and then marking the rest of the cells depending on their neightbor (1 empty cell, 2 empty cells etc etc.) , also specifying the direction. I have that code as well if that wll clarify things. So given an initial velocity, I will make a series of calculations that give me new velocites and pressure at Tn+1. Then I can reassign the boundary conditions for the free boundary, and then re mark the Fluid cells (particle cells) and so on. I hope that answers your question .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1188727]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found