// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/kinetic_tnm.c
// Function: Eckart_main
void Eckart_main(struct axe *axes, int N_axes, atom *atoms, int N_atoms,
		 struct Reference Ref)
{
  /******************************************************************/
  /*   Computing internal rotations and translations (Eckart frame)  */
  /******************************************************************/
  // Initialize
  int i, j, k;

 // Compute inertia tensor and center of mass for all atoms
  double **corr_sum=Allocate_mat2_d(3,3), mr_tot[3];
  for(i=0; i<3; i++)mr_tot[i]=0;
  double M_tot=Sum_inertia(mr_tot, corr_sum, atoms, Ref, 0, Ref.N_ref-1);
  double r_ave[3]; for(i=0; i<3; i++)r_ave[i]=mr_tot[i]/M_tot;
  if(START){
    printf("Center of mass: ");
    for(i=0; i<3; i++)printf("  %.7f", r_ave[i]);
    printf("   M=%.0f\n", M_tot);
    START=0;
  }

  double **inertia_tot=Allocate_mat2_d(3,3);
  Inertia_tensor(inertia_tot, corr_sum, mr_tot, M_tot);

  // Cholevsky decomposition of global inertia tensor
  double **L_mat=Allocate_mat2_d(3,3);
  choldc(L_mat, inertia_tot, 3);

  // Partial inertia tensor for all axes
  // Exploits the fact that axes are nested
  double M_part=0, mr_part[3], **inertia_part=Allocate_mat2_d(3,3);
  Empty_inertia(mr_part, corr_sum);

  struct axe *axe=axes+N_axes-1;
  int last_kin=-1, i_last=0;
  for(int k=N_axes-1; k>=0; k--){
    // Compute partial center of mass and inertia tensor
    if(axe->last_kin!=last_kin){
      // New chain, initialize counters
      M_part=Empty_inertia(mr_part, corr_sum);
      last_kin=axe->last_kin;
      i_last=last_kin;
    }
    M_part+=Sum_inertia(mr_part, corr_sum, atoms, Ref, axe->first_kin, i_last);
    Inertia_tensor(inertia_part, corr_sum, mr_part, M_part);
    i_last=axe->first_kin-1;

    // Test that at least some atom is moved
    if(M_part==0){
      printf("WARNING, axis %d does not move any reference atom!\n", k);
      printf("Reference atoms: %d - %d\n", axe->first_kin, axe->last_kin);
      printf("All atoms: %d - %d\n", axe->first_atom, axe->last_atom);
      printf("dof: %d-%d (main) ", axe->first_main, axe->last_main);
      if(axe->first_side>=0)printf(" %d-%d (side)", axe->first_side, k);
      printf("\n");
      //exit(8);
    }
 
    /*
      ECKART ROTATION AND SHIFT OPERATORS:
      Vector global rotation about axe k:
      Iner global_rot[k]=-iner_part[k]v[k]+M_k(R-Rk)X(axe[k]X Rk+tau[k])}
      Vector global shift:
      global_shift[k]= - (axe[k]X Rk+tau[k])Mk/M - global_rot[k] X R
     */

    // Center of mass displacement
    double delta_cm[3], r_shift[3], rot_tot[3], X[3], Y[3];
    for(i=0; i<3; i++)r_shift[i]=M_part*mr_tot[i]-mr_part[i];
    Vector_product_d(delta_cm, axe->rot, mr_part);
    for(i=0; i<3; i++)delta_cm[i]=delta_cm[i]/M_part+axe->shift[i];
    for(i=0; i<3; i++)r_shift[i]=M_part*r_ave[i]-mr_part[i];
    Vector_product_d(Y, r_shift, delta_cm);
    for(i=0; i<3; i++){
      for(j=0; j<3; j++)Y[i]-=inertia_part[i][j]*axe->rot[j];
    }

    // Downstream rotataion: Solve (L L^t)A_a = Y_a
    Forward_substitution(X, L_mat, Y, 3);
    Backward_substitution(axe->global_rot, L_mat, X, 3);

    // Downstream shift
    Vector_product_d(rot_tot, axe->global_rot, r_ave);
    for(i=0; i<3; i++){
      axe->global_shift[i]=-delta_cm[i]*M_part/M_tot-rot_tot[i]; //
    }

    // Downstream rotation and shift
    for(i=0; i<3; i++){
      axe->local_rot[i]  =axe->global_rot[i] + axe->rot[i];
      axe->local_shift[i]=axe->global_shift[i]+axe->shift[i];
    }

    // Store kinetic variables.
    // Remove center of mass from inertia tensor
    axe->mass=M_part;
    double mr2=Scalar_product_3_d(mr_part, mr_part)/axe->mass;
    for(i=0; i<3; i++){
      axe->MR[i]=mr_part[i];
      for(j=0; j<3; j++){
	axe->I[i][j]=inertia_part[i][j]-mr_part[i]*mr_part[j]/axe->mass;
      }
      axe->I[i][i]+=mr2;
    }

    // New axe
    axe--;
  }

  if(DEBUG){
    double *v, q;
    printf("Axis res local_rot local_shift global_rot global_shift");
    printf(" first_ref first_rotable_atom type\n");
    for(k=0; k<N_axes; k++){
      axe=axes+k;
      printf("%3d %3d ", k, axe->bond->atom->res);
      v=axe->local_rot;
      q=Scalar_product_3_d(v,v); printf(" %.3f ", sqrt(q));
      v=axe->local_shift;
      q=Scalar_product_3_d(v,v); printf("%4.1f ", sqrt(q));
      v=axe->global_rot;
      q=Scalar_product_3_d(v,v); printf(" %.3f ", sqrt(q));
      v=axe->global_shift;
      q=Scalar_product_3_d(v,v); printf("%4.1f ", sqrt(q));
      printf("%3d %3d %c\n",axe->first_kin,axe->first_atom,axe->type);
    }
  }

  // Cleaning
  Empty_matrix_d(corr_sum, 3);
  Empty_matrix_d(inertia_part, 3);
  Empty_matrix_d(inertia_tot, 3);
  Empty_matrix_d(L_mat, 3);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rdx, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
movq %rdi, 0x30(%rsp)
pushq $0x3
popq %rdi
movl %edi, %esi
callq 0x43878
movq %rax, 0x40(%rsp)
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1f322
andq $0x0, 0xd0(%rsp,%rax,8)
incq %rax
jmp 0x1f30e
leaq 0x1c0(%rsp), %rax
movl (%rax), %r8d
decl %r8d
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movupd 0x20(%rax), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
leaq 0xd0(%rsp), %rdi
xorl %ebx, %ebx
movq 0x40(%rsp), %rsi
movq 0x68(%rsp), %rdx
xorl %ecx, %ecx
callq 0x20914
cvtss2sd %xmm0, %xmm1
cmpq $0x3, %rbx
je 0x1f38b
movsd 0xd0(%rsp,%rbx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp,%rbx,8)
incq %rbx
jmp 0x1f36a
movsd %xmm1, 0x50(%rsp)
cmpl $0x0, 0x44b14(%rip)     # 0x63eac
je 0x1f3ec
leaq 0x332eb(%rip), %rdi     # 0x5268c
xorl %r15d, %r15d
xorl %eax, %eax
callq 0x20f0
leaq 0x332eb(%rip), %rbx     # 0x5269d
cmpq $0x3, %r15
je 0x1f3d1
movsd 0x110(%rsp,%r15,8), %xmm0
movq %rbx, %rdi
movb $0x1, %al
callq 0x20f0
incq %r15
jmp 0x1f3b2
leaq 0x332cc(%rip), %rdi     # 0x526a4
movsd 0x50(%rsp), %xmm0
movb $0x1, %al
callq 0x20f0
andl $0x0, 0x44ac0(%rip)     # 0x63eac
pushq $0x3
popq %rbp
movl %ebp, %edi
movl %ebp, %esi
callq 0x43878
movq %rax, %r15
leaq 0xd0(%rsp), %rdx
movq %rax, %rdi
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movsd 0x50(%rsp), %xmm0
callq 0x20681
movl %ebp, %edi
movl %ebp, %esi
callq 0x43878
movq %rax, 0x70(%rsp)
movq %rax, %rdi
movq %r15, 0x78(%rsp)
movq %r15, %rsi
movl %ebp, %edx
callq 0x438e8
movl %ebp, %edi
movl %ebp, %esi
callq 0x43878
movq %rax, %rbp
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x209a9
movq 0x60(%rsp), %rcx
movslq %ecx, %rax
imulq $0x158, %rax, %rax      # imm = 0x158
movq 0x30(%rsp), %rdx
leaq (%rdx,%rax), %r12
leaq (%rdx,%rax), %r9
addq $-0x48, %r9
pushq $-0x1
popq %rax
movl %eax, 0x48(%rsp)
leaq 0x90(%rsp), %r14
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
movl %ecx, %r15d
testl %r15d, %r15d
jle 0x1f8ab
movq %r9, 0x88(%rsp)
movl 0x48(%rsp), %eax
cmpl %eax, -0x13c(%r12)
je 0x1f4d0
movq %r13, %rdi
movq 0x40(%rsp), %rsi
callq 0x209a9
movl -0x13c(%r12), %eax
xorpd %xmm0, %xmm0
movl %eax, 0x48(%rsp)
movl %eax, %r8d
movsd %xmm0, 0x38(%rsp)
decl %r15d
movl %r15d, 0x4c(%rsp)
movl -0x140(%r12), %ecx
leaq 0x1c0(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movupd 0x20(%rax), %xmm2
movupd %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %r13, %rdi
movq 0x40(%rsp), %r15
movq %r15, %rsi
movq 0x68(%rsp), %rdx
callq 0x20914
cvtss2sd %xmm0, %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
movl 0x4c(%rsp), %r15d
movq %r13, %rdx
callq 0x20681
movsd 0x38(%rsp), %xmm1
movl -0x140(%r12), %ebx
ucomisd 0x2baf9(%rip), %xmm1    # 0x4b050
jne 0x1f5f9
jp 0x1f5f9
leaq 0x33145(%rip), %rdi     # 0x526af
movl %r15d, %esi
xorl %eax, %eax
callq 0x20f0
movl -0x140(%r12), %esi
movl -0x13c(%r12), %edx
leaq 0x33158(%rip), %rdi     # 0x526e3
xorl %eax, %eax
callq 0x20f0
movl -0x148(%r12), %esi
movl -0x144(%r12), %edx
leaq 0x33154(%rip), %rdi     # 0x526fd
xorl %eax, %eax
callq 0x20f0
movl -0x138(%r12), %esi
movl -0x134(%r12), %edx
leaq 0x3314a(%rip), %rdi     # 0x52711
xorl %eax, %eax
callq 0x20f0
movl -0x130(%r12), %esi
testl %esi, %esi
js 0x1f5eb
leaq 0x33143(%rip), %rdi     # 0x52724
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
pushq $0xa
popq %rdi
callq 0x2060
movsd 0x38(%rsp), %xmm1
leaq -0x158(%r12), %rax
movq %rax, 0x80(%rsp)
decl %ebx
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1f637
movsd 0xd0(%rsp,%rax,8), %xmm0
mulsd %xmm1, %xmm0
subsd 0xb0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x130(%rsp,%rax,8)
incq %rax
jmp 0x1f60d
movl %ebx, 0x5c(%rsp)
leaq -0x110(%r12), %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0xf8f7
movsd 0x38(%rsp), %xmm1
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1f681
movsd 0x90(%rsp,%rax,8), %xmm0
divsd %xmm1, %xmm0
addsd -0xf8(%r12,%rax,8), %xmm0
movsd %xmm0, 0x90(%rsp,%rax,8)
incq %rax
jmp 0x1f656
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1f6ad
movsd 0x110(%rsp,%rax,8), %xmm0
mulsd %xmm1, %xmm0
subsd 0xb0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x130(%rsp,%rax,8)
incq %rax
jmp 0x1f683
leaq 0xf0(%rsp), %rdi
leaq 0x130(%rsp), %rsi
movq %r14, %rdx
callq 0xf8f7
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1f709
movq (%rbp,%rax,8), %rcx
movsd 0xf0(%rsp,%rax,8), %xmm0
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x1f704
movsd (%rcx,%rdx,8), %xmm1
mulsd -0x110(%r12,%rdx,8), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp,%rax,8)
incq %rdx
jmp 0x1f6dd
incq %rax
jmp 0x1f6c7
leaq 0x150(%rsp), %rdi
movq 0x70(%rsp), %r14
movq %r14, %rsi
leaq 0xf0(%rsp), %rdx
movq %r13, %rbx
pushq $0x3
popq %r13
movl %r13d, %ecx
callq 0x43a6b
leaq -0xb0(%r12), %r15
movq %r15, %rdi
movq %r14, %rsi
leaq 0x150(%rsp), %rdx
movl %r13d, %ecx
movq %rbx, %r13
callq 0x43abd
leaq 0x170(%rsp), %rdi
movq %r15, %rsi
movl 0x4c(%rsp), %r15d
leaq 0x110(%rsp), %rdx
callq 0xf8f7
xorl %eax, %eax
movsd 0x50(%rsp), %xmm1
movapd 0x2b942(%rip), %xmm2    # 0x4b0c0
movsd 0x38(%rsp), %xmm3
cmpq $0x3, %rax
je 0x1f7b7
movsd 0x90(%rsp,%rax,8), %xmm0
xorpd %xmm2, %xmm0
mulsd %xmm3, %xmm0
divsd %xmm1, %xmm0
subsd 0x170(%rsp,%rax,8), %xmm0
movsd %xmm0, -0xc8(%r12,%rax,8)
incq %rax
jmp 0x1f784
pushq $-0x10
popq %rax
leaq 0x90(%rsp), %r14
cmpq $-0xd, %rax
je 0x1f7f9
movsd -0x48(%r12,%rax,8), %xmm0
movsd -0x30(%r12,%rax,8), %xmm1
addsd -0x90(%r12,%rax,8), %xmm1
movsd %xmm1, (%r12,%rax,8)
addsd -0x78(%r12,%rax,8), %xmm0
movsd %xmm0, -0x18(%r12,%rax,8)
incq %rax
jmp 0x1f7c2
movsd %xmm3, -0x68(%r12)
movq %r13, %rdi
movq %r13, %rsi
callq 0xf96f
divsd -0x68(%r12), %xmm0
leaq -0x48(%r12), %rax
movq 0x88(%rsp), %r9
movq %r9, %rcx
xorl %edx, %edx
movl 0x5c(%rsp), %r8d
cmpq $0x3, %rdx
je 0x1f891
movsd 0xb0(%rsp,%rdx,8), %xmm1
movsd %xmm1, -0x60(%r12,%rdx,8)
movq (%rbp,%rdx,8), %rsi
xorl %edi, %edi
cmpq $0x3, %rdi
je 0x1f873
movsd 0xb0(%rsp,%rdi,8), %xmm2
mulsd %xmm1, %xmm2
divsd -0x68(%r12), %xmm2
movsd (%rsi,%rdi,8), %xmm3
subsd %xmm2, %xmm3
movsd %xmm3, (%rcx,%rdi,8)
incq %rdi
jmp 0x1f846
imulq $0x18, %rdx, %rsi
addq %rax, %rsi
movsd (%rsi,%rdx,8), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
addq $0x18, %rcx
jmp 0x1f829
addq $-0x158, %r9            # imm = 0xFEA8
movq 0x80(%rsp), %r12
movsd 0x38(%rsp), %xmm0
jmp 0x1f491
leaq 0x54b3a(%rip), %rax     # 0x743ec
cmpl $0x0, (%rax)
je 0x1fa15
leaq 0x32e70(%rip), %rdi     # 0x52732
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x20f0
leaq 0x33053(%rip), %rdi     # 0x52925
callq 0x2090
movq 0x60(%rsp), %r15
testl %r15d, %r15d
cmovlel %ebx, %r15d
addq $0x90, 0x30(%rsp)
leaq 0x32e7f(%rip), %r12     # 0x52772
leaq 0x32e7f(%rip), %r13     # 0x52779
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0x1fa15
movq 0x30(%rsp), %r14
movq -0x88(%r14), %rax
movq (%rax), %rax
movl 0x64(%rax), %edx
leaq 0x32e4b(%rip), %rdi     # 0x52769
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x48(%r14), %rdi
movq %rdi, %rsi
callq 0xf96f
ucomisd 0x2b715(%rip), %xmm0    # 0x4b050
jb 0x1f943
sqrtsd %xmm0, %xmm0
jmp 0x1f948
callq 0x2290
movq %r12, %rdi
movb $0x1, %al
callq 0x20f0
movq 0x30(%rsp), %rax
leaq 0x30(%rax), %rdi
movq %rdi, %rsi
callq 0xf96f
ucomisd 0x2b6e5(%rip), %xmm0    # 0x4b050
jb 0x1f973
sqrtsd %xmm0, %xmm0
jmp 0x1f978
callq 0x2290
movq %r13, %rdi
movb $0x1, %al
callq 0x20f0
movq 0x30(%rsp), %rax
leaq 0x18(%rax), %rdi
movq %rdi, %rsi
callq 0xf96f
ucomisd 0x2b6b5(%rip), %xmm0    # 0x4b050
jb 0x1f9a3
sqrtsd %xmm0, %xmm0
jmp 0x1f9a8
callq 0x2290
movq %r12, %rdi
movb $0x1, %al
callq 0x20f0
movq 0x30(%rsp), %rdi
movq %rdi, %rsi
callq 0xf96f
ucomisd 0x2b689(%rip), %xmm0    # 0x4b050
jb 0x1f9cf
sqrtsd %xmm0, %xmm0
jmp 0x1f9d4
callq 0x2290
movq %r13, %rdi
movb $0x1, %al
callq 0x20f0
movq 0x30(%rsp), %r14
movl -0x80(%r14), %edx
movl -0x78(%r14), %esi
movsbl -0x90(%r14), %ecx
leaq 0x32d86(%rip), %rdi     # 0x52780
xorl %eax, %eax
callq 0x20f0
incq %rbx
addq $0x158, %r14            # imm = 0x158
movq %r14, 0x30(%rsp)
jmp 0x1f8fc
pushq $0x3
popq %rbx
movq 0x40(%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
movq %rbp, %rdi
movl %ebx, %esi
callq 0x4372a
movq 0x78(%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
movq 0x70(%rsp), %rdi
movl %ebx, %esi
callq 0x4372a
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq