// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/nma.c
// Function: Convert_torsion2cart
void Convert_torsion2cart(float *Cart_mode, atom *atoms, float *u_a,
			  struct axe *axes, int N_axes,
			  struct Reference Ref, int ia)
{
  // Computing derivative:
  // dr_ia ~ (sum_k u_a,k*rot_ik) X r_i + (sum_k u_a,k*shift_ik)
  int  i, j, k, jj;

  double dr_daxe_rot_global[3], dr_daxe_shift_global[3];
  for(j=0; j<3; j++){
    dr_daxe_rot_global[j]=0;
    dr_daxe_shift_global[j]=0;
  }
  float *u_ak=u_a;
  struct axe *axe=axes;
  for(k=0; k<N_axes; k++){
    for(j=0; j<3; j++){
      dr_daxe_shift_global[j]+=(*u_ak)*axe->global_shift[j];
      dr_daxe_rot_global[j]  +=(*u_ak)*axe->global_rot[j];
    }
    u_ak++; axe++;
  }

  double dr_daxe_rot[Ref.N_Cart], dr_daxe_shift[Ref.N_Cart];
  for(j=0; j<Ref.N_Cart; j++){
    dr_daxe_rot[j]=0;
    dr_daxe_shift[j]=0;
  }
  u_ak=u_a; axe=axes;
  for(k=0; k<N_axes; k++){
    jj=3*axe->first_kin;
    for(i=axe->first_kin; i<=axe->last_kin; i++){
      for(j=0; j<3; j++){
	dr_daxe_rot[jj]  +=(*u_ak)*axe->rot[j];
	dr_daxe_shift[jj]+=(*u_ak)*axe->shift[j];
	jj++;
      }
    }
    u_ak++; axe++;
  }

  double r[3], dd[3], rot[3]; jj=0;
  for(i=0; i<Ref.N_ref; i++){
    atom *atom=atoms+Ref.atom_num[i];
    for(j=0; j<3; j++){
      r[j]= atom->r[j];
      rot[j]=dr_daxe_rot[jj+j]+dr_daxe_rot_global[j];
    }
    Vector_product_d(dd, rot, r);
    for(j=0; j<3; j++){
      Cart_mode[jj]=dd[j]+dr_daxe_shift[jj]+dr_daxe_shift_global[j];
      jj++;
    }
  }
  Normalize_vector_weighted(Cart_mode, Ref.mass_coord, Ref.N_Cart);

  // UGO 10/08/22 r_i=sqrt(m_i)*x_i; It may induce mistakes
  //float norm=sqrt(Ref.mass_sum/Ref.N_ref);
  //for(j=0; j<Ref.N_Cart; j++)Cart_mode[j]*=norm; //Ref.mass_sqrt[j];

  if(0){
    // Test Eckart conditions. 15/03/19 I verified that they are fulfilled
    jj=0; int violation=0; float eps=0.001;
    double d[3], M_tot=0, Md_sum[3]; for(j=0; j<3; j++)Md_sum[j]=0;
    double MRd[3], MRd_sum[3]; for(j=0; j<3; j++)MRd_sum[j]=0;
    for(i=0; i<Ref.N_ref; i++){
      double *ri=atoms[Ref.atom_num[i]].r;
      float m= Ref.mass_atom[i];
      M_tot+=m;
      for(j=0; j<3; j++){
	r[j]=ri[j]; d[j]=Cart_mode[jj];
	Md_sum[j]+=m*Cart_mode[jj]; jj++;	
      }
      Vector_product_d(MRd, r, d);
      for(j=0; j<3; j++){MRd_sum[j]+=m*MRd[j];}
    }
    float eps_m=eps*M_tot;
    for(j=0; j<3; j++){
      if((fabs(Md_sum[j])>eps_m)||(fabs(MRd_sum[j])>eps_m)){
	violation++;
	printf("Eckart violation for mode %d! Md_sum= %.4f MRd_sum= %.4f\n",
	       ia, Md_sum[j]/M_tot, MRd_sum[j]/M_tot);
      }
    }
    if(violation){
      printf("Exiting the program\n"); exit(8);
    }else{
      printf("Mode %d ok\n", ia);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %r8d, %r13d
movq %rcx, %r15
movq %rdx, %r14
movq %rsi, -0x50(%rbp)
movq %rdi, -0x40(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
xorl %eax, %eax
movq %rax, -0x80(%rbp)
movaps %xmm0, -0x70(%rbp)
movq %rax, -0x60(%rbp)
testl %r8d, %r8d
jle 0x26123
movq %r15, %rcx
movq %r14, %rdx
xorps %xmm0, %xmm0
cvtss2sd (%rdx), %xmm0
xorl %esi, %esi
movsd 0x90(%rcx,%rsi,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0x70(%rbp,%rsi,8), %xmm1
movsd %xmm1, -0x70(%rbp,%rsi,8)
movsd 0xa8(%rcx,%rsi,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0x90(%rbp,%rsi,8), %xmm1
movsd %xmm1, -0x90(%rbp,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x260d0
addq $0x4, %rdx
addq $0x158, %rcx            # imm = 0x158
incl %eax
cmpl %r13d, %eax
jne 0x260c7
movq %rsp, -0x48(%rbp)
leaq 0x10(%rbp), %rax
movl 0x4(%rax), %edx
movq %rsp, %r12
leaq 0xf(,%rdx,8), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
testl %edx, %edx
movq %rdx, -0x38(%rbp)
jle 0x2617c
leaq (,%rdx,8), %rdx
movq %rdx, -0x30(%rbp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x2110
movq %rbx, %rdi
xorl %esi, %esi
movq -0x30(%rbp), %rdx
callq 0x2110
movq -0x38(%rbp), %rdx
testl %r13d, %r13d
jle 0x26200
xorl %eax, %eax
movl 0x18(%r15), %r10d
movl 0x1c(%r15), %ecx
cmpl %ecx, %r10d
jg 0x261ee
leal (%r10,%r10,2), %esi
xorps %xmm0, %xmm0
cvtss2sd (%r14), %xmm0
movslq %esi, %rdi
leaq (%r12,%rdi,8), %r8
leaq (%rbx,%rdi,8), %r9
xorl %edi, %edi
movsd 0x48(%r15,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r8,%rdi,8), %xmm1
movsd %xmm1, (%r8,%rdi,8)
movsd 0x60(%r15,%rdi,8), %xmm1
mulsd %xmm0, %xmm1
addsd (%r9,%rdi,8), %xmm1
movsd %xmm1, (%r9,%rdi,8)
incq %rdi
cmpq $0x3, %rdi
jne 0x261a9
addl %edi, %esi
leal 0x1(%r10), %edi
cmpl %ecx, %r10d
movl %edi, %r10d
jne 0x2619c
addq $0x4, %r14
addq $0x158, %r15            # imm = 0x158
incl %eax
cmpl %r13d, %eax
jne 0x26183
leaq 0x10(%rbp), %rax
movslq (%rax), %r14
testq %r14, %r14
jle 0x262cf
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq -0x30(%rbp), %rax
movslq (%rax,%r13,4), %rax
imulq $0x70, %rax, %rax
movq -0x50(%rbp), %rdx
movq 0x10(%rdx,%rax), %rcx
movq %rcx, -0xa0(%rbp)
movups (%rdx,%rax), %xmm0
movaps %xmm0, -0xb0(%rbp)
movslq %r15d, %r15
leaq (%r12,%r15,8), %rax
xorl %ecx, %ecx
movsd (%rax,%rcx,8), %xmm0
addsd -0x90(%rbp,%rcx,8), %xmm0
movsd %xmm0, -0xd0(%rbp,%rcx,8)
incq %rcx
cmpq $0x3, %rcx
jne 0x2624e
leaq -0xf0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0xb0(%rbp), %rdx
callq 0x1086b
leaq (%rbx,%r15,8), %rcx
movq -0x40(%rbp), %rax
leaq (%rax,%r15,4), %rdx
xorl %eax, %eax
movsd -0xf0(%rbp,%rax,8), %xmm0
addsd (%rcx,%rax,8), %xmm0
addsd -0x70(%rbp,%rax,8), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%rdx,%rax,4)
incq %rax
cmpq $0x3, %rax
jne 0x26296
incq %r13
addq %rax, %r15
cmpq %r14, %r13
movq -0x38(%rbp), %rdx
jne 0x2621e
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x40(%rbp), %rdi
callq 0x10d14
movq -0x48(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq