// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rsi, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x85982
movq %rbx, 0x70(%rsp)
leaq 0x130(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x85982
movq 0x98(%r14), %rax
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
cmpq 0x90(%r14), %rax
je 0x71d4a
leaq 0xc0(%r14), %rax
movq %rax, 0x98(%rsp)
leaq 0xd8(%r14), %rax
movq %rax, 0x90(%rsp)
leaq 0xf0(%r14), %rax
movq %rax, 0x88(%rsp)
xorl %edx, %edx
leaq 0x100(%rsp), %rbp
leaq 0x118(%rsp), %r15
leaq 0xe8(%rsp), %rbx
movq %r14, 0x80(%rsp)
movq 0xa8(%r14), %rcx
movq %rdx, 0x78(%rsp)
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rax
cmpq %rax, 0x8(%rcx,%rdx,8)
je 0x71d1a
movl $0x1, %r13d
xorl %ecx, %ecx
movq %rdx, 0x18(%rsp)
movq %rbx, %r12
movq %r15, %rbx
movq %rbp, %r15
movslq (%rax,%rcx,4), %rbp
movq 0x90(%r14), %rax
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rdx,4), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x4(%rax,%rdx,4), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl 0x8(%rax,%rdx,4), %xmm2
leaq 0x58(%rsp), %rdi
callq 0x85982
movq 0x60(%r14), %rax
leaq (,%rbp,8), %rcx
addq %rbp, %rcx
movsd 0x8(%rax,%rcx,8), %xmm0
movsd 0x10(%rax,%rcx,8), %xmm1
movsd 0x18(%rax,%rcx,8), %xmm2
leaq 0x40(%rsp), %rdi
callq 0x85982
leaq 0xd0(%rsp), %r14
movq %r15, %rbp
movq %rbx, %r15
movq %r12, %rbx
movsd 0x58(%rsp), %xmm0
movq %rbp, %rdi
movq 0x98(%rsp), %rsi
callq 0x85a34
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq 0x85dd4
movsd 0x60(%rsp), %xmm0
movq %rbx, %rdi
movq 0x90(%rsp), %rsi
callq 0x85a34
leaq 0xb8(%rsp), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x85dd4
movsd 0x68(%rsp), %xmm0
movq %r14, %rdi
movq 0x88(%rsp), %rsi
callq 0x85a34
leaq 0xa0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movq %r14, %rdx
callq 0x85dd4
movq 0xb0(%rsp), %rax
movq %rax, 0x50(%rsp)
movups 0xa0(%rsp), %xmm0
movaps %xmm0, 0x40(%rsp)
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x71ca4
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsi)
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rsp)
jmp 0x71cb1
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x6a5be
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x85dd4
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movupd 0xa0(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movl %r13d, %ecx
movq 0x80(%rsp), %r14
movq 0xa8(%r14), %rdx
movq 0x18(%rsp), %rsi
movq (%rdx,%rsi,8), %rax
movq 0x8(%rdx,%rsi,8), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
incl %r13d
cmpq %rcx, %rdx
movq %rsi, %rdx
ja 0x71b74
movq 0x98(%r14), %rax
subq 0x90(%r14), %rax
movq 0x78(%rsp), %rdx
incl %edx
sarq $0x2, %rax
movabsq $-0x5555555555555555, %r12 # imm = 0xAAAAAAAAAAAAAAAB
imulq %r12, %rax
cmpq %rdx, %rax
ja 0x71b48
movq 0x68(%r14), %rax
subq 0x60(%r14), %rax
sarq $0x3, %rax
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rax, %rcx
movq %rcx, %xmm0
punpckldq 0xe152f(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xe1537(%rip), %xmm0    # 0x1532b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0xdd2cb(%rip), %xmm0    # 0x14f058
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x85a34
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x58(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
je 0x71e24
movl $0x1, %ebp
xorl %ecx, %ecx
leaq 0x20(%rsp), %rbx
leaq (%rcx,%rcx,2), %r15
leaq (%rax,%r15,8), %rsi
cmpl $0x1, %ebp
je 0x71df7
movq %rbx, %rdi
callq 0x85e28
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x71e05
shlq $0x3, %r15
addq (%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdi
callq 0x85e28
movsd %xmm0, 0x18(%rsp)
movl %ebp, %ecx
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r12, %rdx
incl %ebp
cmpq %rcx, %rdx
ja 0x71dcb
jmp 0x71e24
cmpl $0x0, 0x108(%r14)
jle 0x71e46
movq 0x12518b(%rip), %rdi    # 0x196fc0
leaq 0xe48b4(%rip), %rsi     # 0x1566f0
movl $0x6c, %edx
callq 0x44e0
movq 0x30(%rsp), %rax
movq 0x70(%rsp), %rbx
movq %rax, 0x10(%rbx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rbx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rbx)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x71e7d
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
jmp 0x71ea4
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x71ebd
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780
nop