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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4656 - (view) (download)

1 : glk 4652 // 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 : glk 4656 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 : glk 4652 }
42 :    
43 :     strand Particle (int parent_, int ii, vec3 pos0, real hh0) {
44 :     int parent = parent_;
45 :     vec3 pos = pos0/|pos0|;
46 :     real hh = hh0;
47 :     output vec3 outPos = pos;
48 :     vec3 delta = [0,0,0];
49 :     int id = ii;
50 :     int born = 0;
51 :     real energyLast = ii; // HEY: can do convergence test based on variance of energies
52 :     vec3 force = [0,0,0]; // or can test convergence based on sum of |force|
53 :     bool first = true;
54 :     int ncount = 0;
55 :     update {
56 :     if (first) {
57 :     //print("(iter ", iter, ") hello from ", ii, "\n");
58 :     first = false; // HEY remove me
59 :     }
60 :     // compute energy and forces on us
61 :     energyLast = 0;
62 :     force = [0,0,0];
63 :     foreach (Particle p_j in sphere(rad)) {
64 :     ncount += 1;
65 :     vec3 r_ij = p_j.pos - pos;
66 :     energyLast += enr(r_ij);
67 :     force += frc(r_ij);
68 :     }
69 :     vec3 norm = normalize(pos); // surface normal (for unit circle)
70 :     if (ncount == 0 && (pcp > 0 && 0 == iter % pcp)) {
71 :     vec3 npos = pos + newDist*rad*normalize(perp3(norm));
72 :     new Particle(ii, born, npos, hh);
73 :     born += 1;
74 :     continue;
75 :     }
76 :     // else update position based on force from neighbors.
77 :     // first, project force onto tangent surface
78 :     force = (identity[3] - norm⊗norm)•force;
79 :     delta = hh*force;
80 :     if (|delta| > stepMax) {
81 :     // decrease hh by factor by which step was too big
82 :     hh *= stepMax/|delta|;
83 :     // and find smaller step
84 :     delta = hh*force; // == stepMax*normalize(force);
85 :     }
86 :     vec3 posLast = pos;
87 :     // take step and re-apply constraint
88 :     pos = normalize(pos + delta);
89 :     real energy = 0;
90 :     ncount = 0;
91 :     foreach (Particle p_j in sphere(rad)) {
92 :     energy += enr(p_j.pos - pos);
93 :     ncount += 1;
94 :     }
95 :     if (energy - energyLast > 0.5*(pos - posLast)•(-force)) {
96 :     hh *= 0.5; // backtrack
97 :     pos = posLast;
98 :     } else {
99 :     hh *= 1.1;
100 :     /* putting new particle creation here avoids possibility of
101 :     starting multiple particles at same position while not
102 :     moving due to backtracking */
103 :     if (ncount < 5 && (pcp > 0 && 0 == iter % pcp)) {
104 :     vec3 npos = pos + newDist*rad*normalize(force);
105 :     new Particle(ii, born, npos, hh);
106 :     born += 1;
107 :     }
108 :     }
109 :     outPos = pos;
110 :     }
111 :     }
112 :    
113 :     global {
114 :     //print("(iter ", iter, ") hello from global\n");
115 :     real mvmt = max { |P.delta|/rad | P in Particle.all};
116 :     if (mvmt < mvmtEps // there is very little movement
117 :     && (pcp == 0 // and, either there is no population control
118 :     || iter > 2*pcp)) { // or new particles should be moving by now
119 :     print("Stabilizing with movement ", mvmt, " < ", mvmtEps, " (iter ", iter, ")\n");
120 :     stabilize;
121 :     }
122 :     iter += 1;
123 :     if (iter >= iterMax) {
124 :     print("Stabilizing (due to noDie)" if noDie else "STOPPING",
125 :     " at iter ", iter, " >= ", iterMax, " (movement ", mvmt, ")\n");
126 :     if (noDie) {
127 :     stabilize;
128 :     } else {
129 :     die;
130 :     }
131 :     }
132 :     }
133 :    
134 :     initially {Particle(-1, ii, initPos[ii], hhInit)
135 :     | ii in 0 .. length(initPos)-1 };
136 :    

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