// File: /workspace/github_cpp_makefile_star2_O3/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 0x84364
leaq 0x130(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x84364
movq %rbx, 0x70(%rsp)
movq 0x90(%r14), %rcx
movq 0x98(%r14), %rdx
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
cmpq %rcx, %rdx
je 0x71093
leaq 0xc0(%r14), %rax
movq %rax, 0x98(%rsp)
leaq 0xd8(%r14), %rax
movq %rax, 0x90(%rsp)
leaq 0xf0(%r14), %rax
movq %rax, 0x88(%rsp)
movq 0xa8(%r14), %rax
xorl %edi, %edi
leaq 0xe8(%rsp), %rbp
leaq 0x100(%rsp), %r15
leaq 0xd0(%rsp), %rbx
leaq 0x118(%rsp), %r12
movq %r14, 0x80(%rsp)
movq %rdi, 0x78(%rsp)
leaq (%rdi,%rdi,2), %rdi
movq (%rax,%rdi,8), %rsi
movq %rdi, 0x38(%rsp)
cmpq %rsi, 0x8(%rax,%rdi,8)
je 0x71075
movl $0x1, %edi
xorl %ecx, %ecx
movq 0x38(%rsp), %rdx
movl %edi, 0x18(%rsp)
movq %r12, %r13
movq %rbx, %r12
movq %r15, %rbx
movq %rbp, %r15
movslq (%rsi,%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 0x84364
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 0x84364
movq %r15, %rbp
movq %rbx, %r15
movq %r12, %rbx
movq %r13, %r12
leaq 0xb8(%rsp), %r14
movq 0x38(%rsp), %r13
movsd 0x58(%rsp), %xmm0
movq %rbp, %rdi
movq 0x98(%rsp), %rsi
callq 0x84416
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
movq %rbp, %rdx
callq 0x84642
movsd 0x60(%rsp), %xmm0
movq %rbx, %rdi
movq 0x90(%rsp), %rsi
callq 0x84416
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x84642
movsd 0x68(%rsp), %xmm0
movq %r14, %rdi
movq 0x88(%rsp), %rsi
callq 0x84416
leaq 0xa0(%rsp), %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x84642
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 0x70fea
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsi)
movaps 0x40(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rsp)
jmp 0x70ff7
movq %rsp, %rdi
leaq 0x40(%rsp), %rdx
callq 0x69b82
leaq 0xa0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x84642
movl 0x18(%rsp), %edi
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movupd 0xa0(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
movl %edi, %ecx
movq 0x80(%rsp), %r14
movq 0xa8(%r14), %rax
movq (%rax,%r13,8), %rsi
movq 0x8(%rax,%r13,8), %rdx
subq %rsi, %rdx
sarq $0x2, %rdx
incl %edi
cmpq %rcx, %rdx
movq %r13, %rdx
ja 0x70eb5
movq 0x90(%r14), %rcx
movq 0x98(%r14), %rdx
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x78(%rsp), %rdi
incl %edi
movq %rdx, %rsi
subq %rcx, %rsi
sarq $0x2, %rsi
imulq %r13, %rsi
cmpq %rdi, %rsi
ja 0x70e8c
movq 0x68(%r14), %rax
subq 0x60(%r14), %rax
sarq $0x3, %rax
movabsq $-0x71c71c71c71c71c7, %rcx # imm = 0x8E38E38E38E38E39
imulq %rax, %rcx
movq %rcx, %xmm0
punpckldq 0xe3556(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xe355e(%rip), %xmm0    # 0x154620
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0xdbf82(%rip), %xmm0    # 0x14d058
divsd %xmm1, %xmm0
leaq 0x58(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x84416
movq 0x68(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x58(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movq (%rsp), %rdi
cmpq %rdi, 0x8(%rsp)
je 0x7116d
movl $0x1, %ebp
xorl %eax, %eax
leaq 0x20(%rsp), %rbx
leaq (%rax,%rax,2), %r15
leaq (%rdi,%r15,8), %rsi
cmpl $0x1, %ebp
je 0x71140
movq %rbx, %rdi
callq 0x8468c
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7114e
shlq $0x3, %r15
addq (%rsp), %r15
movq %r15, %rsi
movq %rbx, %rdi
callq 0x8468c
movsd %xmm0, 0x18(%rsp)
movl %ebp, %eax
movq (%rsp), %rdi
movq 0x8(%rsp), %rcx
subq %rdi, %rcx
sarq $0x3, %rcx
imulq %r13, %rcx
incl %ebp
cmpq %rax, %rcx
ja 0x71114
jmp 0x7116d
cmpl $0x0, 0x108(%r14)
movq 0x70(%rsp), %rbx
jle 0x71198
movq 0x123e3d(%rip), %rdi    # 0x194fc0
leaq 0xe367e(%rip), %rsi     # 0x154808
movl $0x6c, %edx
callq 0x44e0
movq (%rsp), %rdi
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rbx)
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rbx)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rbx)
testq %rdi, %rdi
je 0x711c6
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 0x711eb
jmp 0x711eb
jmp 0x711eb
jmp 0x711eb
jmp 0x711eb
jmp 0x711eb
jmp 0x711eb
jmp 0x711eb
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x71204
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780