Lrho dxmxwi0 rwimxdxrrho wimxdx0 forj0jaj

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ot; " << float(as[i])∗blocks/2/mx/(iter−100) << endl; } } C Simple C++ code for simulating model B #include <string> #include <iostream> #include <cstdio> #include <cmath> using namespace std; const const const const const const int iter=400; float dx=0.2; float rhomax=100; float lrho=25; float rrho=75; float mx=4; //Total number of iterations //Width for calculating local density //Value of rho max //Value of rho for x<−4 //Value of rho for x>4 //Half−width of simulation region 14 M. Z. Bazant – 18.366 Random Walks and Diffusion – Problem Set 3 Solutions 15 const float udt=0.01; //u max ∗ dt; const float ddt=0.0025; //D ∗ dt; const int blocks=40; //Number of blocks to save out const const const const const const const float pi=3.1415926535897932384626433832795; int lwalk=int(mx ∗ lrho); //Total initial walkers on LHS int twalk=int(mx ∗ (lrho+rrho)); //Total initial walkers float lspeed=udt∗(1−lrho/rhomax); float lrate=lspeed ∗ lrho; //L. walker intro rate int mem=twalk∗2; //Total memory float isp=float(blocks)/mx∗0.5; //Inverse block spacing inline float rnd() { return (float(rand())+0.5)/RAND MAX; } inline int poisson(float l) { if (l>20) { int r=int(sqrt(−2∗log(rnd())∗l) ∗ cos(2∗pi ∗ rnd())+0.5+l); return r>0?r:0; } else { double a=rnd()∗exp(l)−1,b=1;int r=0; while(a>0) { a−=b ∗ =l/++r; } return r; } } int main() { int a=0,i,j=0,t;float w[mem],v[mem],as[blocks],r,s,x; while(j++<lwalk) w[a++]=−mx ∗ rnd(); while(j++<twalk) w[a++]=mx ∗ rnd(); for(i=0;i<blocks;i++) as[i]=0; for(t=0;t<iter;t++) { for(i=0;i<a;i++) { j=int((w[i]+mx) ∗ isp); if (j>=0&&j<blocks&&t>100) as[j]++; s=r=w[i]<dx−mx?lrho ∗ (dx−mx−w[i]):0; r+=w[i]>mx−dx?rrho ∗ (w[i]−mx+dx):0; for(j=0;j<a;j++) { x=w[j]−w[i]; if (x>−dx) { if (x<dx) { r++;if (x<0) s++; } } } s=(r−2∗s)/(dx ∗ dx); r/=2∗dx; v[i]=udt∗(1−r/rhomax)−ddt ∗ s/r; M. Z. Bazant – 18.366 Random Walks and Diffusion – Problem Set 3 Solutions 16 }...
View Full Document

Ask a homework question - tutors are online