// File: /workspace/github_c_makefile_star2_O2/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
movq %rsi, -0x50(%rbp)
movq %rdi, -0x30(%rbp)
leaq 0x10(%rbp), %r13
xorl %eax, %eax
cmpq $0x3, %rax
je 0x235df
andq $0x0, -0x90(%rbp,%rax,8)
andq $0x0, -0x70(%rbp,%rax,8)
incq %rax
jmp 0x235c5
xorl %eax, %eax
testl %r8d, %r8d
cmovlel %eax, %r8d
movq %rdx, %rsi
movq %rcx, %rdi
cmpl %r8d, %eax
je 0x23652
cvtss2sd (%rsi), %xmm0
xorl %r9d, %r9d
cmpq $0x3, %r9
je 0x23643
movsd 0x90(%rdi,%r9,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0x70(%rbp,%r9,8), %xmm1
movsd %xmm1, -0x70(%rbp,%r9,8)
movsd 0xa8(%rdi,%r9,8), %xmm1
mulsd %xmm0, %xmm1
addsd -0x90(%rbp,%r9,8), %xmm1
movsd %xmm1, -0x90(%rbp,%r9,8)
incq %r9
jmp 0x235fa
addq $0x4, %rsi
addq $0x158, %rdi            # imm = 0x158
incl %eax
jmp 0x235ee
movq %rsp, -0x40(%rbp)
movl 0x4(%r13), %edi
movq %rsp, %r14
leaq 0xf(,%rdi,8), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rsp, %rbx
subq %rax, %rbx
movq %rbx, %rsp
xorl %eax, %eax
testl %edi, %edi
movl $0x0, %esi
movq %rdi, -0x38(%rbp)
cmovgl %edi, %esi
cmpq %rax, %rsi
je 0x2369c
andq $0x0, (%r14,%rax,8)
andq $0x0, (%rbx,%rax,8)
incq %rax
jmp 0x23688
xorl %eax, %eax
pushq $-0x3
popq %rsi
cmpl %r8d, %eax
je 0x2371e
movl 0x18(%rcx), %edi
movl 0x1c(%rcx), %r9d
leal (%rdi,%rdi,2), %r10d
cmpl %r9d, %edi
jg 0x2370f
cvtss2sd (%rdx), %xmm0
movslq %r10d, %r11
leaq (%r14,%r11,8), %r15
leaq (%rbx,%r11,8), %r12
movq %rsi, %r11
testq %r11, %r11
je 0x23704
movsd 0x60(%rcx,%r11,8), %xmm1
mulsd %xmm0, %xmm1
addsd 0x18(%r15,%r11,8), %xmm1
movsd %xmm1, 0x18(%r15,%r11,8)
movsd 0x78(%rcx,%r11,8), %xmm1
mulsd %xmm0, %xmm1
addsd 0x18(%r12,%r11,8), %xmm1
movsd %xmm1, 0x18(%r12,%r11,8)
incq %r11
jmp 0x236c8
addl %r11d, %r10d
addl $0x3, %r10d
incl %edi
jmp 0x236b1
addq $0x4, %rdx
addq $0x158, %rcx            # imm = 0x158
incl %eax
jmp 0x236a1
movl (%r13), %r12d
movq 0x28(%r13), %rax
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
testl %r12d, %r12d
cmovlel %r15d, %r12d
xorl %r13d, %r13d
cmpq %r12, %r15
je 0x237e4
movq -0x48(%rbp), %rax
movslq (%rax,%r15,4), %rax
imulq $0x70, %rax, %rax
addq -0x50(%rbp), %rax
movslq %r13d, %r13
leaq (%r14,%r13,8), %rcx
xorl %edx, %edx
cmpq $0x3, %rdx
je 0x23789
movsd (%rax,%rdx,8), %xmm0
movsd %xmm0, -0xf0(%rbp,%rdx,8)
movsd (%rcx,%rdx,8), %xmm0
addsd -0x90(%rbp,%rdx,8), %xmm0
movsd %xmm0, -0xb0(%rbp,%rdx,8)
incq %rdx
jmp 0x23759
leaq -0xd0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0xf8f7
leaq (%rbx,%r13,8), %rcx
movq -0x30(%rbp), %rax
leaq (%rax,%r13,4), %rdx
xorl %eax, %eax
cmpq $0x3, %rax
je 0x237d9
movsd -0xd0(%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
jmp 0x237b1
incq %r15
addq %rax, %r13
jmp 0x23737
leaq 0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rdx
callq 0xfd9c
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq