// File: /workspace/github_cpp_makefile_star2_O2/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 $0x168, %rsp            # imm = 0x168
movq %rsi, %r14
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0x10(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
movq %rbx, 0x68(%rsp)
leaq 0x150(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x6402e
leaq 0xc0(%r14), %rax
movq %rax, 0x90(%rsp)
leaq 0xd8(%r14), %rax
movq %rax, 0x88(%rsp)
leaq 0xf0(%r14), %rax
movq %rax, 0x80(%rsp)
xorl %ecx, %ecx
leaq 0x108(%rsp), %r15
leaq 0x120(%rsp), %rbp
leaq 0xf0(%rsp), %r13
leaq 0x138(%rsp), %rbx
movq %r14, 0x78(%rsp)
movl %ecx, %ecx
movq 0x98(%r14), %rax
subq 0x90(%r14), %rax
cqto
pushq $0xc
popq %rsi
idivq %rsi
cmpq %rcx, %rax
jbe 0x539e3
xorl %eax, %eax
imulq $0x18, %rcx, %rdx
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x70(%rsp)
imulq $0xc, %rcx, %rcx
movq %rcx, 0x98(%rsp)
movl %eax, %eax
movq %rax, 0x8(%rsp)
movq 0xa8(%r14), %rdi
movq 0xa0(%rsp), %rcx
movq 0x8(%rdi,%rcx), %rax
subq (%rdi,%rcx), %rax
sarq $0x2, %rax
cmpq 0x8(%rsp), %rax
movq 0x8(%rsp), %rsi
jbe 0x539d7
addq %rcx, %rdi
callq 0xac00
movq %rbx, %r12
movq %r13, %rbx
movq %rbp, %r13
movq %r15, %rbp
movslq (%rax), %r15
movq 0x90(%r14), %rax
movq 0x98(%rsp), %rcx
cvtsi2sdl (%rax,%rcx), %xmm0
cvtsi2sdl 0x4(%rax,%rcx), %xmm1
cvtsi2sdl 0x8(%rax,%rcx), %xmm2
leaq 0x50(%rsp), %rdi
callq 0x6402e
movq 0x60(%r14), %rax
imulq $0x48, %r15, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd 0x10(%rax,%rcx), %xmm1
movsd 0x18(%rax,%rcx), %xmm2
leaq 0xc0(%rsp), %r14
movq %r14, %rdi
callq 0x6402e
movq %rbp, %r15
movq %r13, %rbp
movq %rbx, %r13
movq %r12, %rbx
leaq 0xd8(%rsp), %r12
movsd 0x50(%rsp), %xmm0
movq %r15, %rdi
movq 0x90(%rsp), %rsi
callq 0x640ba
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x642d2
movsd 0x58(%rsp), %xmm0
movq %r13, %rdi
movq 0x88(%rsp), %rsi
callq 0x640ba
movq %rbx, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x642d2
movsd 0x60(%rsp), %xmm0
movq %r12, %rdi
movq 0x80(%rsp), %rsi
callq 0x640ba
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x642d2
movq 0xb8(%rsp), %rax
movq %rax, 0xd0(%rsp)
movupd 0xa8(%rsp), %xmm0
movapd %xmm0, 0xc0(%rsp)
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0x4beaa
leaq 0xa8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x642d2
movq 0xb8(%rsp), %rax
movq %rax, 0x20(%rsp)
movupd 0xa8(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movq 0x8(%rsp), %rax
incl %eax
movq 0x78(%rsp), %r14
jmp 0x53843
movq 0x70(%rsp), %rcx
incl %ecx
jmp 0x53803
movq 0x68(%r14), %rax
subq 0x60(%r14), %rax
pushq $0x48
popq %rcx
cqto
idivq %rcx
movq %rax, %xmm0
punpckldq 0xafb70(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xafb78(%rip), %xmm0    # 0x103580
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0xa863c(%rip), %xmm0    # 0xfc058
divsd %xmm1, %xmm0
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x640ba
movq 0x60(%rsp), %rax
leaq 0x10(%rsp), %rbx
movq %rax, 0x10(%rbx)
movups 0x50(%rsp), %xmm0
movaps %xmm0, (%rbx)
xorl %r15d, %r15d
pushq $0x18
popq %r12
movl %r15d, %r15d
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rax
subq %rsi, %rax
cqto
idivq %r12
cmpq %r15, %rax
jbe 0x53aa1
imulq $0x18, %r15, %r13
addq %r13, %rsi
testq %r15, %r15
je 0x53a8e
movq %rbx, %rdi
callq 0x6431c
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x53a9c
addq 0x30(%rsp), %r13
movq %r13, %rsi
movq %rbx, %rdi
callq 0x6431c
movsd %xmm0, 0x8(%rsp)
incl %r15d
jmp 0x53a4c
cmpl $0x0, 0x108(%r14)
jle 0x53abe
movq 0xfc50e(%rip), %rdi     # 0x14ffc0
leaq 0xafca7(%rip), %rsi     # 0x103760
callq 0x4480
movq 0x20(%rsp), %rax
movq 0x68(%rsp), %rbx
movq %rax, 0x10(%rbx)
movaps 0x10(%rsp), %xmm0
movups %xmm0, (%rbx)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x18(%rbx)
leaq 0x30(%rsp), %rdi
callq 0xb132
movq %rbx, %rax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
jmp 0x53b12
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xb132
movq %rbx, %rdi
callq 0x4780
nop