Hi,

I hope somebody here can help me with this one.

I’m building a Monte Carlo code for a system of particles. Position coordinates of these N particles are stored in a 3xN array of coordinates r[:,:], like for instance for N=5

‘’’

0.0719552 0.820621 0.169617 0.336838 0.620763

0.218954 0.198641 0.793058 0.661784 0.32143

0.448448 0.377976 0.533512 0.278445 0.821139

‘’’

where each particle sits in a different column, and rows indicate x,y and z for that particle, respectively.

Now I need to define an object (called a bead) for each of these particles, which I want to define in a mutable struct.That’s because every particle has some simulation parameters corresponding to it, and its own coordinates, together with the coordinates of its left particle, and the coordinates of its right particle. Then, along the simulation, the particle positions change according to a Metropolis criteria. What I would like to do, however, is to perform these coordinates changes in r[:,:], but such that the changes automatically apply to the coordinates in the beads. I have tried to define the beads as in

‘’’

mutable struct Bead{T}

δt :: T;

rleft :: Array{T};

rright :: Array{T};

Bead{T}() where T = new();

end

‘’’

so that I can do

‘’’

kind=Float32

b1 = Bead{kind}();

b2 = Bead{kind}();

b1.δt = b2.δt = 0.01;

b1.rleft = view(r,:,2);

b1.rright = view(r,:,3);

b2.rleft = view(r,:,3);

b2.rright = view(r,:,4);

‘’’

hoping that, being a view what I use, changes in r[:,:] are automaticaly updated to b1.rleft, b1.rright, b2.rleft and b2.rright.

But that does not really work, and I think that this is becasue I define

rleft and rright in the Bead struct as arrays, which copy the value they get

at the point I initialize them, but do not get updated when r[:,:] is updated.

If I was writting the code in C, I could define the rleft and rright fields in the Bead struct as pointers to r[:,:], which would then solve the problem. But in Julia… I don’t know.

So the question is: is there a way to implement this pointer-like behaviour?

Something that would automatically update the rleft and rright fields in b1 and b2 when changes in r[:,:] are prodced…

Thanks in advance,

Ferran.