problemset3

# Lrho dxmxwi0 rwimxdxrrho wimxdx0 forj0jaj

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ot; &quot; &lt;&lt; float(as[i])∗blocks/2/mx/(iter−100) &lt;&lt; endl; } } C Simple C++ code for simulating model B #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;cmath&gt; 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&lt;−4 //Value of rho for x&gt;4 //Half−width of simulation region 14 M. Z. Bazant – 18.366 Random Walks and Diﬀusion – 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&gt;20) { int r=int(sqrt(−2∗log(rnd())∗l) ∗ cos(2∗pi ∗ rnd())+0.5+l); return r&gt;0?r:0; } else { double a=rnd()∗exp(l)−1,b=1;int r=0; while(a&gt;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++&lt;lwalk) w[a++]=−mx ∗ rnd(); while(j++&lt;twalk) w[a++]=mx ∗ rnd(); for(i=0;i&lt;blocks;i++) as[i]=0; for(t=0;t&lt;iter;t++) { for(i=0;i&lt;a;i++) { j=int((w[i]+mx) ∗ isp); if (j&gt;=0&amp;&amp;j&lt;blocks&amp;&amp;t&gt;100) as[j]++; s=r=w[i]&lt;dx−mx?lrho ∗ (dx−mx−w[i]):0; r+=w[i]&gt;mx−dx?rrho ∗ (w[i]−mx+dx):0; for(j=0;j&lt;a;j++) { x=w[j]−w[i]; if (x&gt;−dx) { if (x&lt;dx) { r++;if (x&lt;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 Diﬀusion – Problem Set 3 Solutions 16 }...
View Full Document

Ask a homework question - tutors are online