// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/interactions_tnm.c
// Function: Compute_sec_der
void Compute_sec_der(struct interaction *Int_list, int N_int, char *nameout)
{
  // Normalize such that the average contact interaction does
  // not depend on C and E

  // Set parameters for force constant computation
  printf("Computing force constant\n");

  C1_THR=C_THR; //-0.025; // Lower threshold for interpolation
  printf("Interpolating to zero between %.3f and %.3f\n", C1_THR, C_THR);
  if(C_THR>C1_THR){K1=KAPPA/(C_THR-C1_THR);}
  else{K1=0;}

  if(POW){ // power law: C0^-e
    K0=KAPPA*pow(C0,EXP_HESSIAN); 
    K1*=pow((C1_THR/C0),-EXP_HESSIAN);
    printf("Power law k= %.3g (r/%.1f)^-%.1f\n", KAPPA, C0, EXP_HESSIAN);
  }else{
    K0=KAPPA*exp(C0*EXP_HESSIAN);
    K1*=exp((C0-C1_THR)*EXP_HESSIAN);
    printf("Exponential law k= %.3g exp(-(r/%.1f)*%.1f\n",
	   KAPPA, C0, EXP_HESSIAN);
  }

  char name_out[200];
  if(nameout){
    sprintf(name_out,"%s.Inter_Force_constant.dat",nameout);
    // YYY change output file name
    //if(POW){sprintf(name_out, "%s_POW_%.1f.dat", name_out, EXP_HESSIAN);}
    //else{sprintf(name_out, "%s_EXP_%.1f.dat", name_out, EXP_HESSIAN);}
    FILE *file_out=fopen(name_out, "w");
    fprintf(file_out, "# POW= %d EXPO= %.2f C0= %.2f KAPPA= %.2f\n",
	    POW, EXP_HESSIAN, C0, KAPPA);
    
    if(POW){ // power law: C0^-e
      fprintf(file_out, "# f(r)= %.2f (%.2f/r)^%.2f if r< %.2f\n",
	      KAPPA, C0, EXP_HESSIAN, C1_THR);
    }else{ // Exponential
      fprintf(file_out, "# f(r)= %.2f exp((%.2f-r)%.2f) if r< %.2f\n",
	      KAPPA, C0, EXP_HESSIAN, C1_THR);
    }
    fprintf(file_out, "# f(r)= 0 r>= %.2f\n", C_THR);
    fprintf(file_out, "# f(r)= linear interpolation %.2f < r < %.2f\n",
	    C1_THR, C_THR);
    
    for(float r=2.0; r<C_THR; r+=0.05){
      float kappa=Force_constant(r);
      fprintf(file_out, "%.2f %.3g\n", r, kappa);
    }
    fclose(file_out);
  }

  for(int n=0; n<N_int; n++){
    if(Int_list[n].r0<=0){
      printf("ERROR, d2= %.2f\n", Int_list[n].r0); exit(8);
    }
    Int_list[n].sec_der*=Force_constant(Int_list[n].r0);
  }
  if(0){
    printf("\nExpo= %.2f\nV''(r^PDB):\n", EXP_HESSIAN);
    for(int n=0; n<N_int; n++){
      printf("%d-%d %.2g %.2g\n", Int_list[n].res1, Int_list[n].res2,
	     Int_list[n].r0, Int_list[n].sec_der);
    }
    exit(8);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r15
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
leaq 0x3a3cb(%rip), %rdi     # 0x511d0
callq 0x2090
leaq 0x4d257(%rip), %r13     # 0x64068
movss (%r13), %xmm0
leaq 0x4d252(%rip), %r12     # 0x64070
movss %xmm0, (%r12)
cvtss2sd %xmm0, %xmm0
leaq 0x3a170(%rip), %rdi     # 0x50f9f
movaps %xmm0, %xmm1
movb $0x2, %al
callq 0x20f0
movss (%r13), %xmm1
movss (%r12), %xmm2
ucomiss %xmm2, %xmm1
leaq 0x4d229(%rip), %rax     # 0x64078
movss (%rax), %xmm0
ja 0x16e5a
xorps %xmm1, %xmm1
jmp 0x16e69
subss %xmm2, %xmm1
movaps %xmm0, %xmm2
divss %xmm1, %xmm2
cvtss2sd %xmm2, %xmm1
leaq 0x4d218(%rip), %r12     # 0x64088
leaq 0x4c775(%rip), %rax     # 0x635ec
cmpl $0x0, (%rax)
movsd %xmm1, (%r12)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x4d1e4(%rip), %rax     # 0x64074
movss (%rax), %xmm0
je 0x16ef3
cvtss2sd %xmm0, %xmm0
leaq 0x4d1cb(%rip), %rbp     # 0x6406c
cvtss2sd (%rbp), %xmm1
callq 0x20a0
mulsd (%rsp), %xmm0
leaq 0x4d1c9(%rip), %rax     # 0x64080
movsd %xmm0, (%rax)
leaq 0x4d1ae(%rip), %rax     # 0x64070
movss (%rax), %xmm0
leaq 0x4d1a7(%rip), %rax     # 0x64074
divss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movss (%rbp), %xmm1
xorps 0x3997f(%rip), %xmm1    # 0x50860
cvtss2sd %xmm1, %xmm1
callq 0x20a0
leaq 0x3a0db(%rip), %rdi     # 0x50fcc
jmp 0x16f43
leaq 0x4d172(%rip), %rbp     # 0x6406c
mulss (%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x22a0
mulsd (%rsp), %xmm0
leaq 0x4d16c(%rip), %rax     # 0x64080
leaq 0x4d159(%rip), %rcx     # 0x64074
movss (%rcx), %xmm1
leaq 0x4d14a(%rip), %rcx     # 0x64070
subss (%rcx), %xmm1
movsd %xmm0, (%rax)
mulss (%rbp), %xmm1
cvtss2sd %xmm1, %xmm0
callq 0x22a0
leaq 0x3a0ab(%rip), %rdi     # 0x50fee
mulsd (%r12), %xmm0
leaq 0x4d128(%rip), %r14     # 0x64078
cvtss2sd (%r14), %xmm3
leaq 0x4d118(%rip), %rax     # 0x64074
cvtss2sd (%rax), %xmm1
movsd %xmm0, (%r12)
leaq 0x4d0ff(%rip), %rbp     # 0x6406c
cvtss2sd (%rbp), %xmm2
movaps %xmm3, %xmm0
movb $0x3, %al
callq 0x20f0
testq %r15, %r15
je 0x170b6
leaq 0x3a08e(%rip), %rsi     # 0x5101a
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x2260
leaq 0x3a1ab(%rip), %rsi     # 0x51150
movq %r12, %rdi
callq 0x2210
movq %rax, %r15
leaq 0x4c635(%rip), %r12     # 0x635ec
movl (%r12), %edx
cvtss2sd (%rbp), %xmm0
leaq 0x4d0ad(%rip), %rbp     # 0x64074
cvtss2sd (%rbp), %xmm1
cvtss2sd (%r14), %xmm2
leaq 0x3a05e(%rip), %rsi     # 0x51036
movq %rax, %rdi
movb $0x3, %al
callq 0x2190
cmpl $0x0, (%r12)
cvtss2sd (%r14), %xmm0
cvtss2sd (%rbp), %xmm1
leaq 0x4d074(%rip), %rax     # 0x6406c
cvtss2sd (%rax), %xmm2
leaq 0x4d06d(%rip), %r12     # 0x64070
cvtss2sd (%r12), %xmm3
leaq 0x3a078(%rip), %rax     # 0x51088
leaq 0x3a04a(%rip), %rsi     # 0x51061
cmoveq %rax, %rsi
movq %r15, %rdi
movb $0x4, %al
callq 0x2190
cvtss2sd (%r13), %xmm0
leaq 0x3a081(%rip), %rsi     # 0x510b3
movq %r15, %rdi
movb $0x1, %al
callq 0x2190
cvtss2sd (%r12), %xmm0
cvtss2sd (%r13), %xmm1
leaq 0x3a078(%rip), %rsi     # 0x510c7
movq %r15, %rdi
movb $0x2, %al
callq 0x2190
movss 0x39da3(%rip), %xmm0    # 0x50e04
leaq 0x3a08d(%rip), %r12     # 0x510f5
movss (%r13), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x170ae
movss %xmm0, (%rsp)
callq 0x178fd
cvtss2sd (%rsp), %xmm2
movsd %xmm2, (%rsp)
cvtss2sd %xmm0, %xmm1
movq %r15, %rdi
movq %r12, %rsi
movaps %xmm2, %xmm0
movb $0x2, %al
callq 0x2190
movsd (%rsp), %xmm0
addsd 0x33f80(%rip), %xmm0    # 0x4b028
cvtsd2ss %xmm0, %xmm0
jmp 0x17068
movq %r15, %rdi
callq 0x20c0
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
testl %eax, %eax
cmovlel %r15d, %eax
imulq $0xc8, %rax, %r14
cmpq %r15, %r14
je 0x17104
movsd 0x20(%rbx,%r15), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x17116
cvtsd2ss %xmm0, %xmm0
callq 0x178fd
cvtss2sd %xmm0, %xmm0
mulsd 0x28(%rbx,%r15), %xmm0
movsd %xmm0, 0x28(%rbx,%r15)
addq $0xc8, %r15
jmp 0x170cb
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x39fe3(%rip), %rdi     # 0x51100
movb $0x1, %al
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270