// File: Luthaf[P]zeopp/channel.cc
// Function: PORE::getCenterOfMass()
pair <XYZ, double> PORE::getCenterOfMass(){

/*
 Pore variables
    std::vector<DELTA_POS> unitCells;    // List of displacements of unitcells in single PORE unit
    std::vector< std::vector<int> > ucNodes;  // List of nodes contained in each unit cell
    XYZ v_a, v_b, v_c;              // Unit cell vectors

*/

 vector<XYZ> reconstructedNodeCoords;
 XYZ centerOfMass (0,0,0);
 double dist; // distance from the center of mass to the nearest node

 XYZ tempPt;
 for(int unsigned i=0; i<unitCells.size(); i++)
    {
    for(int unsigned j=0; j<ucNodes[i].size(); j++)
       {
       int nd = ucNodes[i].at(j); // current node

       XYZ deltapos(double(unitCells[i].x), double(unitCells[i].y), double(unitCells[i].z));

       XYZ node(nodes[nd].x, nodes[nd].y, nodes[nd].z);

       node = node + v_a.scale(deltapos.x) + v_b.scale(deltapos.y) + v_c.scale(deltapos.z) ;

       reconstructedNodeCoords.push_back(node);
       centerOfMass = centerOfMass + node;
       };
    };

 centerOfMass = centerOfMass.scale(1.0/double(nodes.size()));

 for(int unsigned i=0; i<reconstructedNodeCoords.size(); i++)
    {
    if(i==0)
      {
      dist = centerOfMass.euclid_dist(reconstructedNodeCoords[i]);
      }
    else
      {
      if(centerOfMass.euclid_dist(reconstructedNodeCoords[i]) < dist) dist = centerOfMass.euclid_dist(reconstructedNodeCoords[i]);
      };
    };

 if(dimensionality>0) cout << "Center of Mass calculation: PORE dimensionality>0; results may be buggy due to PORE reconstruction handling\n";

/* DEBUGGING: Printing out coordinates
 for(int unsigned i=0; i<reconstructedNodeCoords.size(); i++)
    {
    cout << "C    " << reconstructedNodeCoords[i].x << "  " << reconstructedNodeCoords[i].y << "  " << reconstructedNodeCoords[i].z << "\n";
    };
*/

 pair<XYZ, double> p(centerOfMass, dist);
 return p;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x190(%rbp)
movq %rdi, -0x188(%rbp)
movq %rsi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xada60
leaq -0x38(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
jmp 0xc6865
leaq -0x68(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
jmp 0xc6879
movl $0x0, -0x6c(%rbp)
movq -0x180(%rbp), %rdi
movl -0x6c(%rbp), %eax
movq %rax, -0x198(%rbp)
addq $0x90, %rdi
callq 0x706e0
movq %rax, %rcx
movq -0x198(%rbp), %rax
cmpq %rcx, %rax
jae 0xc6b80
movl $0x0, -0x70(%rbp)
movq -0x180(%rbp), %rdi
movl -0x70(%rbp), %eax
movq %rax, -0x1a0(%rbp)
addq $0xa8, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x10890
movq %rax, %rdi
callq 0x10090
movq %rax, %rcx
movq -0x1a0(%rbp), %rax
cmpq %rcx, %rax
jae 0xc6b70
movq -0x180(%rbp), %rdi
addq $0xa8, %rdi
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x10890
movq %rax, %rdi
movl -0x70(%rbp), %eax
movl %eax, %esi
callq 0x101d0
movq %rax, -0x1a8(%rbp)
jmp 0xc6922
movq -0x180(%rbp), %rdi
movq -0x1a8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
addq $0x90, %rdi
movq %rdi, -0x1c0(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x658f0
movq -0x1c0(%rbp), %rdi
cvtsi2sdl (%rax), %xmm0
movsd %xmm0, -0x1b8(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x658f0
movq -0x1c0(%rbp), %rdi
cvtsi2sdl 0x4(%rax), %xmm0
movsd %xmm0, -0x1b0(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, %esi
callq 0x658f0
movsd -0x1b8(%rbp), %xmm0
movsd -0x1b0(%rbp), %xmm1
cvtsi2sdl 0x8(%rax), %xmm2
leaq -0x90(%rbp), %rdi
callq 0xea8e0
jmp 0xc69ab
movq -0x180(%rbp), %rdi
addq $0x60, %rdi
movq %rdi, -0x1d8(%rbp)
movslq -0x74(%rbp), %rsi
callq 0x2a4c0
movq -0x1d8(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1d0(%rbp)
movslq -0x74(%rbp), %rsi
callq 0x2a4c0
movq -0x1d8(%rbp), %rdi
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x1c8(%rbp)
movslq -0x74(%rbp), %rsi
callq 0x2a4c0
movsd -0x1d0(%rbp), %xmm0
movsd -0x1c8(%rbp), %xmm1
movsd 0x18(%rax), %xmm2
leaq -0xb0(%rbp), %rdi
callq 0xea8e0
jmp 0xc6a23
movq -0x180(%rbp), %rsi
addq $0xc0, %rsi
movsd -0x90(%rbp), %xmm0
leaq -0x110(%rbp), %rdi
callq 0xea9f0
jmp 0xc6a47
leaq -0xf8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0xeae50
jmp 0xc6a63
movq -0x180(%rbp), %rsi
addq $0xd8, %rsi
movsd -0x88(%rbp), %xmm0
leaq -0x128(%rbp), %rdi
callq 0xea9f0
jmp 0xc6a87
leaq -0xe0(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
leaq -0x128(%rbp), %rdx
callq 0xeae50
jmp 0xc6aa3
movq -0x180(%rbp), %rsi
addq $0xf0, %rsi
movsd -0x80(%rbp), %xmm0
leaq -0x140(%rbp), %rdi
callq 0xea9f0
jmp 0xc6ac4
leaq -0xc8(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
leaq -0x140(%rbp), %rdx
callq 0xeae50
jmp 0xc6ae0
movq -0xb8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movups -0xc8(%rbp), %xmm0
movaps %xmm0, -0xb0(%rbp)
leaq -0x20(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xafc20
jmp 0xc6b0e
leaq -0x158(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0xeae50
jmp 0xc6b27
movq -0x158(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x150(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0xc68b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x20(%rbp), %rdi
callq 0x12210
jmp 0xc6d44
jmp 0xc6b72
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0xc6880
movq -0x180(%rbp), %rdi
addq $0x60, %rdi
callq 0x10100
movq %rax, %xmm1
movaps 0x12f7c4(%rip), %xmm0   # 0x1f6360
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x12f7c8(%rip), %xmm0   # 0x1f6370
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x12b491(%rip), %xmm0   # 0x1f2050
divsd %xmm1, %xmm0
leaq -0x170(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0xea9f0
jmp 0xc6bd5
movq -0x170(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x168(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x160(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x174(%rbp)
movl -0x174(%rbp), %eax
movq %rax, -0x1e0(%rbp)
leaq -0x20(%rbp), %rdi
callq 0xae590
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
cmpq %rcx, %rax
jae 0xc6cee
cmpl $0x0, -0x174(%rbp)
jne 0xc6c68
movl -0x174(%rbp), %eax
movl %eax, %esi
leaq -0x20(%rbp), %rdi
callq 0xb3b70
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0xeaf00
movsd %xmm0, -0x1e8(%rbp)
jmp 0xc6c59
movsd -0x1e8(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0xc6cd8
movl -0x174(%rbp), %eax
movl %eax, %esi
leaq -0x20(%rbp), %rdi
callq 0xb3b70
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0xeaf00
movsd %xmm0, -0x1f0(%rbp)
jmp 0xc6c8f
movsd -0x1f0(%rbp), %xmm1
movsd -0x50(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc6cd6
movl -0x174(%rbp), %eax
movl %eax, %esi
leaq -0x20(%rbp), %rdi
callq 0xb3b70
movq %rax, %rsi
leaq -0x38(%rbp), %rdi
callq 0xeaf00
movsd %xmm0, -0x1f8(%rbp)
jmp 0xc6cc9
movsd -0x1f8(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0xc6cd8
jmp 0xc6cda
movl -0x174(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x174(%rbp)
jmp 0xc6c00
movq -0x180(%rbp), %rax
cmpl $0x0, 0x108(%rax)
jle 0xc6d15
movq 0x1ac2bb(%rip), %rdi    # 0x272fc0
leaq 0x13474c(%rip), %rsi    # 0x1fb458
callq 0x5540
jmp 0xc6d13
jmp 0xc6d15
movq -0x190(%rbp), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0xcd580
jmp 0xc6d2b
leaq -0x20(%rbp), %rdi
callq 0x12210
movq -0x188(%rbp), %rax
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x58f0
nopl (%rax)