// File: joraso[P]Glassius/Particles.cpp
// Function: Particles::Initialize()
void Particles::Initialize(){
    /* places the particle positions on a square lattice, each at the center of
    a unit cell. Does not initialize energies or forces (they are set to zero
    implicilty, but does randomize velocities and subtract off the cmv. */
    int xi, yi, zi, k, n; n=0;
    
    // Setting other parameters from Nside (number of particles along a side
    // of the box) and rho (number density).
    N = Nside*Nside*Nside;
    lengthscale = pow(rho, -1.0/3.0);
    sidelength = lengthscale*Nside;
    
    //std::cout << "Sidelength is " << sidelength << std::endl;
    //std::cout << "lengthscale is " << lengthscale << std::endl;
    //std::cout << "N is " << N << std::endl;
    //std::cout << "Nside is " << Nside << std::endl;
    
    // Setting up the trajectory matrices. Note the dimensionality is hard-
    // coded to 3.
    positions = Matrix(N,3);
    velocities = Matrix(N,3);
    forces = Matrix(N,3);
    r = positions.Data();
    v = velocities.Data();
    f = forces.Data();
    
    // Tracking the center-of-mass velocity
    double cmv[3];
    for(k=0;k<3;k++){cmv[k] = 0;}
    
    // initializing positions, velocities.
    for(zi=0;zi<Nside;zi++){
       //std::cout << "zi=" << zi << std::endl;
       //std::cout << "zcoord=" << lengthscale*(zi+0.5) << std::endl;
       for(yi=0;yi<Nside;yi++){
         for(xi=0;xi<Nside;xi++){
            r[n][0] = lengthscale*(xi+0.5);
            r[n][1] = lengthscale*(yi+0.5);
            r[n][2] = lengthscale*(zi+0.5);
            for(k=0;k<3;k++){
                v[n][k] = chaos::gaussian(0.0, 1.0);
                cmv[k] += v[n][k];
            }
            n++;
         }
       }
    }
    
    // Subtracting off the center-of-mass velocity, and calculating KE.
    kinetic_energy = 0;
    for(n=0;n<N;n++){
        for(k=0;k<3;k++){
            v[n][k] -= (cmv[k]/N);
            kinetic_energy += (24.0)*v[n][k]*v[n][k];
        }
    }
    return;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
movl $0x0, 0xac(%rsp)
movl 0x54(%rax), %edx
movl %edx, %ecx
imull %ecx, %ecx
imull %edx, %ecx
movq %rax, %rdx
addq $0x50, %rdx
movq %rdx, 0x18(%rsp)
movl %ecx, 0x50(%rax)
movsd 0x68(%rax), %xmm0
movsd 0x58ea(%rip), %xmm1     # 0x90f8
callq 0x2200
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rax
movsd %xmm0, 0x58(%rax)
movsd 0x58(%rax), %xmm0
cvtsi2sdl 0x54(%rax), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rax)
movl $0x3, 0x94(%rsp)
leaq 0x98(%rsp), %rdi
movq %rdi, 0x28(%rsp)
leaq 0x94(%rsp), %rdx
callq 0x3250
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
addq $0x20, %rdi
callq 0x3350
jmp 0x386f
leaq 0x98(%rsp), %rdi
callq 0x36a0
movq 0x20(%rsp), %rsi
addq $0x50, %rsi
movl $0x3, 0x6c(%rsp)
leaq 0x70(%rsp), %rdi
movq %rdi, 0x10(%rsp)
leaq 0x6c(%rsp), %rdx
callq 0x3250
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
addq $0x30, %rdi
callq 0x3350
jmp 0x38b6
leaq 0x70(%rsp), %rdi
callq 0x36a0
movq 0x20(%rsp), %rsi
addq $0x50, %rsi
movl $0x3, 0x54(%rsp)
leaq 0x58(%rsp), %rdi
movq %rdi, 0x8(%rsp)
leaq 0x54(%rsp), %rdx
callq 0x3250
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
addq $0x40, %rdi
callq 0x3350
jmp 0x38fa
leaq 0x58(%rsp), %rdi
callq 0x36a0
movq 0x20(%rsp), %rdi
addq $0x20, %rdi
callq 0x4f00
movq 0x20(%rsp), %rdi
movq %rax, 0x8(%rdi)
addq $0x30, %rdi
callq 0x4f00
movq 0x20(%rsp), %rdi
movq %rax, 0x10(%rdi)
addq $0x40, %rdi
callq 0x4f00
movq %rax, %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x18(%rax)
movl $0x0, 0xb0(%rsp)
cmpl $0x3, 0xb0(%rsp)
jge 0x39eb
movslq 0xb0(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x394d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x36a0
jmp 0x3cdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x36a0
jmp 0x3cdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x36a0
jmp 0x3cdc
movl $0x0, 0xb4(%rsp)
movq 0x20(%rsp), %rcx
movl 0xb4(%rsp), %eax
cmpl 0x54(%rcx), %eax
jge 0x3bca
movl $0x0, 0xb8(%rsp)
movq 0x20(%rsp), %rcx
movl 0xb8(%rsp), %eax
cmpl 0x54(%rcx), %eax
jge 0x3bb2
movl $0x0, 0xbc(%rsp)
movq 0x20(%rsp), %rcx
movl 0xbc(%rsp), %eax
cmpl 0x54(%rcx), %eax
jge 0x3b9a
movq 0x20(%rsp), %rax
movsd 0x58(%rax), %xmm0
cvtsi2sdl 0xbc(%rsp), %xmm1
movsd 0x56a2(%rip), %xmm2     # 0x9108
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq 0x8(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movsd %xmm0, (%rcx)
movsd 0x58(%rax), %xmm0
cvtsi2sdl 0xb8(%rsp), %xmm1
movsd 0x5670(%rip), %xmm2     # 0x9108
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq 0x8(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movsd %xmm0, 0x8(%rcx)
movsd 0x58(%rax), %xmm0
cvtsi2sdl 0xb4(%rsp), %xmm1
movsd 0x563d(%rip), %xmm2     # 0x9108
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq 0x8(%rax), %rax
movslq 0xac(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movsd %xmm0, 0x10(%rax)
movl $0x0, 0xb0(%rsp)
cmpl $0x3, 0xb0(%rsp)
jge 0x3b73
xorps %xmm0, %xmm0
movsd 0x5550(%rip), %xmm1     # 0x9058
callq 0x2ea0
movq 0x20(%rsp), %rax
movq 0x10(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0xb0(%rsp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x10(%rax), %rax
movslq 0xac(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movslq 0xb0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq 0xb0(%rsp), %rax
addsd 0x30(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x30(%rsp,%rax,8)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x3af3
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x3a36
jmp 0x3b9c
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x3a16
jmp 0x3bb4
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x39f6
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rax)
movl $0x0, 0xac(%rsp)
movq 0x20(%rsp), %rcx
movl 0xac(%rsp), %eax
cmpl 0x50(%rcx), %eax
jge 0x3cd4
movl $0x0, 0xb0(%rsp)
cmpl $0x3, 0xb0(%rsp)
jge 0x3cbc
movq 0x20(%rsp), %rax
movslq 0xb0(%rsp), %rcx
movsd 0x30(%rsp,%rcx,8), %xmm1
cvtsi2sdl 0x50(%rax), %xmm0
divsd %xmm0, %xmm1
movq 0x10(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0xb0(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, (%rcx,%rdx,8)
movq 0x10(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0xb0(%rsp), %rdx
movsd 0x548e(%rip), %xmm0     # 0x9100
mulsd (%rcx,%rdx,8), %xmm0
movq 0x10(%rax), %rcx
movslq 0xac(%rsp), %rdx
movq (%rcx,%rdx,8), %rcx
movslq 0xb0(%rsp), %rdx
movsd (%rcx,%rdx,8), %xmm2
movsd 0x78(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rax)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x3c02
jmp 0x3cbe
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x3be2
addq $0xc8, %rsp
retq
movq 0x88(%rsp), %rdi
callq 0x2210
nopl (%rax)