Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /tests/lamont-tests/unit-sphere-bug2.diderot
ViewVC logotype

Annotation of /tests/lamont-tests/unit-sphere-bug2.diderot

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4712 - (view) (download)

1 : glk 4712 // unit-sphere
2 :     //
3 :     // Demo of distributing particles on the unit sphere
4 :     //
5 :     input vec3{} initPos ("initial positions for all particles") = load("vec3.nrrd");
6 :     input int iterMax ("maximum number of iterations to run") = 100;
7 :     input real rad ("radius of particle's energy support") = 0.1;
8 :     input int pcp ("periodicity of population control (if non-zero)") = 5;
9 :     input real mvmtEps ("convergence threshold on particle system movement") = 0.001;
10 :     input bool noDie ("all strands stabilize instead of die at iterMax") = false;
11 :    
12 :     real hhInit = 10; // initial step size, can be big
13 :     real newDist = 0.9*rad; // how far away to birth new particles
14 :     real stepMax = 2*rad; // limit on distance to travel per iter
15 :    
16 :     int iter = 0; // which iteration we're on
17 :    
18 :     // inter-particle energy, and its derivative
19 :     //function real φ(real r) = (1 - r)^4;
20 :     //function real φ'(real r) = -4*(1 - r)^3;
21 :     function real φ(real r) = (1/r)*(1-r)^3;
22 :     function real φ'(real r) = 3 - 1/(r^2) - 2*r;
23 :     function real enr(vec3 x) = φ(|x|/rad);
24 :     function vec3 frc(vec3 x) = φ'(|x|/rad) * (1/rad) * x/|x|; // chain rule
25 :    
26 :     // returns a non-zero vector perpendicular to given non-zero vector v
27 :     function vec3 perp3(vec3 v) {
28 :     int c = 0;
29 :     if (|v[0]| < |v[1]|) {
30 :     c = 1;
31 :     }
32 :     // can't say v[c] because tensors can only be indexed by constants
33 :     if (|v[1] if 1==c else v[0]| < |v[2]|) {
34 :     c = 2;
35 :     }
36 :     // now c is index of v component with largest absolute value
37 :     vec3 ret = ([v[1] - v[2], -v[0], v[0]] if (c == 0) else
38 :     [-v[1], v[0] - v[2], v[1]] if (c == 1) else
39 :     [-v[2], v[2], v[0] - v[1]]);
40 :     return ret;
41 :     }
42 :    
43 :     strand Particle (vec3 pos0, real hh0) {
44 :     vec3 pos = pos0/|pos0|;
45 :     real hh = hh0;
46 :     output vec3 outPos = pos;
47 :     /* putting this here allows it to be accessed by the global update,
48 :     where it is used in a convergence test */
49 :     vec3 delta = [0,0,0];
50 :     update {
51 :     // compute energy and forces on us
52 :     int ncount = 0;
53 :     real energyLast = 0;
54 :     vec3 force = [0,0,0];
55 :     foreach (Particle p_j in sphere(rad)) {
56 :     ncount += 1;
57 :     vec3 r_ij = p_j.pos - pos;
58 :     energyLast += enr(r_ij);
59 :     force += frc(r_ij);
60 :     }
61 :     vec3 norm = normalize(pos); // surface normal (for unit circle)
62 :     if (ncount == 0 && (pcp > 0 && 0 == iter % pcp)) {
63 :     vec3 npos = pos + newDist*rad*normalize(perp3(norm));
64 :     new Particle(npos, hh);
65 :     continue;
66 :     }
67 :     // else update position based on force from neighbors.
68 :     // first, project force onto tangent surface
69 :     force = (identity[3] - norm⊗norm)•force;
70 :     delta = hh*force;
71 :     if (|delta| > stepMax) {
72 :     // decrease hh by factor by which step was too big
73 :     hh *= stepMax/|delta|;
74 :     // and find smaller step
75 :     delta = hh*force; // == stepMax*normalize(force);
76 :     }
77 :     vec3 posLast = pos;
78 :     // take step and re-apply constraint
79 :     pos = normalize(pos + delta);
80 :     real energy = 0;
81 :     ncount = 0;
82 :     real minz = 2; // like infinity
83 :     foreach (Particle p_j in sphere(rad)) {
84 :     energy += enr(p_j.pos - pos);
85 :     ncount += 1;
86 :     minz = min(minz, fmod(p_j.pos[0]*1000, 1));
87 :     }
88 :     if (energy - energyLast > 0.5*(pos - posLast)•(-force)) {
89 :     hh *= 0.5; // backtrack
90 :     pos = posLast;
91 :     } else {
92 :     hh *= 1.1;
93 :     /* putting new particle creation here avoids possibility of
94 :     starting multiple particles at same position, while this
95 :     particle is not moving due to backtracking (above) */
96 :     if (pcp > 0 && 0 == iter % pcp) {
97 :     if (ncount < 5) {
98 :     vec3 npos = pos + newDist*rad*normalize(force);
99 :     new Particle(npos, hh);
100 :     } else if (ncount > 8) {
101 :     if (fmod(pos[0]*1000, 1) < minz) { die; }
102 :     }
103 :     }
104 :     }
105 :     outPos = pos;
106 :     }
107 :     }
108 :    
109 :     global {
110 :     print("(iter ", iter, ") hello from global: ", numActive(), " points \n");
111 :     real mvmt = max { |P.delta|/rad | P in Particle.all};
112 :     if (mvmt < mvmtEps // there is very little movement
113 :     && (pcp == 0 // and, either there is no population control
114 :     || iter > 2*pcp)) { // or new particles should be moving by now
115 :     print("Stabilizing ", numActive(), " points with movement ", mvmt,
116 :     " < ", mvmtEps, " (iter ", iter, ")\n");
117 :     stabilize;
118 :     }
119 :     iter += 1;
120 :     if (iter >= iterMax && iterMax > 0) {
121 :     print("Stabilizing (due to noDie) " if noDie else "STOPPING ",
122 :     numActive(), " points at iter ", iter, " >= ", iterMax,
123 :     " (movement ", mvmt, ")\n");
124 :     if (noDie) {
125 :     stabilize;
126 :     } else {
127 :     die;
128 :     }
129 :     }
130 :     }
131 :    
132 :     initially {Particle(initPos[ii], hhInit)
133 :     | ii in 0 .. length(initPos)-1 };
134 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0