3
$\begingroup$

I have been trying to simulate a simple system in which there are two set of particles. One kind of particles are localized on the boundary of a circle (shown in purple below), whereas the second kind can be either inside or outside of the boundary (delineated in blue).

I will post the simplified version of what is bugging me so as to avoid unnecessary confusion with detalied code and enable the readers to effectively answer my query.

Consider that I generate the two particles using the following commands:

particles = RandomReal[{0, 1}, {200, 2}, WorkingPrecision -> 2];

membrane = Table[SetAccuracy[RandomPoint[Circle[]], 3], 100];

particlemap = Thread[{Range@Length[particles], particles}];

we can see it visually:

Graphics[{Darker@Purple, Point@membrane, Blue, Point@particlemap}]

enter image description here

Now i can find the blue particle interacting with the purple particle using the following piece of code:

p = Reap[Function[{particle},

  particleposition = particle[[2]];
  Sow[particle[[1]], 
   Select[membrane, # === particleposition &]]] /@ particlemap
][[2]]

(* {{80}} was the index of the particle that was interacting with 
the membrane for the example posted *)

Now I can also create an association to extract the coordinates of interacting particles

association = Association@Map[#[[1]] -> #[[2]] &, particlemap];

interactingParticlePos = association[#]&/@(Flatten@p)

(* {0.57, 0.83} was the interactingParticlePos *)

plugging this value back we can find that Position can get the index back

Position[particles, interactingParticlePos]
(* {{80}} as we expected *)

However the anomaly is that we do not find the position of the point in membrane,

Position[membrane, interactingParticlePos]
(* {} *)

This should not be the case because earlier the Select command found a match between the membrane and the particlemap

Moreover,

MemberQ[membrane, interactingParticlePos]
(*False*)
MemberQ[particles,interactingParticlePos]
(*True*)

Can someone Kindly let me know how I can get the index of the position shared by particles and membrane for the membrane. Furthermore, why is Position and MemberQ not able to find a match whereas Select could?

My hunch is that it has something to do with the way i am setting WorkingPrecision and SetAccuracy but not sure.

I am trying to do a reaction diffusion simulation but this seems to be the key step. If I do not use WorkingPrecision and SetAccuracy ** then the** RandomReal and RandomPoints are generated to machineprecision which makes it impossible for the two kinds of particles to find eachother

Many thanks in return for the help !

$\endgroup$
  • 3
    $\begingroup$ Instead of messing with precisions, why not create a NearestFunction? Then use that, with some epsilon threshold, to determine when particles have in effect collided. $\endgroup$ – Daniel Lichtblau Nov 28 '16 at 15:50
  • $\begingroup$ yeah actually that might do too. thanks :) but i am really interested in why the above mentioned problem is the case $\endgroup$ – Ali Hashmi Nov 28 '16 at 16:08
  • 1
    $\begingroup$ It seems this falls into a gray area, where SameQ says one thing and whatever internals are in use by pattern matching for numbers says another. It is really not a reliable way to try to do this. Also it's slower than using a NearestFunction, which will matter if the size of these sets is scaled upward. $\endgroup$ – Daniel Lichtblau Nov 29 '16 at 2:00
  • $\begingroup$ Thanks a lot for your advice Daniel. I have implemented using the NearestFunction approach as you suggested $\endgroup$ – Ali Hashmi Nov 29 '16 at 6:01
  • $\begingroup$ Hi Daniel, could you kindly check this new post: its a different question regarding the same project. mathematica.stackexchange.com/questions/132525/… $\endgroup$ – Ali Hashmi Dec 1 '16 at 9:38
2
$\begingroup$

Try this:

Position[particles, #] & /@ interactingParticlePos

And this:

MemberQ[membrane, #] & /@ interactingParticlePos
MemberQ[particles, #] & /@ interactingParticlePos
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.