// File: /workspace/github_c_makefile_star2_O3/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, %r14
movl %esi, 0xc(%rsp)
movq %rdi, %rbx
leaq 0x3ef63(%rip), %rdi     # 0x57280
callq 0x2090
leaq 0x50d3f(%rip), %r12     # 0x69068
movss (%r12), %xmm0
leaq 0x50d3a(%rip), %r15     # 0x69070
movss %xmm0, (%r15)
cvtss2sd %xmm0, %xmm0
leaq 0x3ed09(%rip), %rdi     # 0x5704f
movaps %xmm0, %xmm1
movb $0x2, %al
callq 0x20f0
movss (%r12), %xmm1
movss (%r15), %xmm2
ucomiss %xmm2, %xmm1
leaq 0x50d13(%rip), %rax     # 0x69078
movss (%rax), %xmm0
ja 0x18370
xorps %xmm1, %xmm1
jmp 0x18382
subss %xmm2, %xmm1
movaps %xmm0, %xmm2
divss %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtss2sd %xmm2, %xmm1
leaq 0x50cff(%rip), %r15     # 0x69088
leaq 0x5025c(%rip), %rax     # 0x685ec
cmpl $0x0, (%rax)
movsd %xmm1, (%r15)
cvtss2sd %xmm0, %xmm0
movsd %xmm0, (%rsp)
leaq 0x50ccc(%rip), %rax     # 0x69074
movss (%rax), %xmm0
je 0x1840e
cvtss2sd %xmm0, %xmm0
leaq 0x50cb3(%rip), %rbp     # 0x6906c
xorps %xmm1, %xmm1
cvtss2sd (%rbp), %xmm1
callq 0x20a0
mulsd (%rsp), %xmm0
leaq 0x50cae(%rip), %rax     # 0x69080
movsd %xmm0, (%rax)
leaq 0x50c93(%rip), %rax     # 0x69070
movss (%rax), %xmm0
leaq 0x50c8c(%rip), %rax     # 0x69074
divss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movss (%rbp), %xmm1
xorps 0x38c34(%rip), %xmm1    # 0x51030
cvtss2sd %xmm1, %xmm1
callq 0x20a0
leaq 0x3ec70(%rip), %rdi     # 0x5707c
jmp 0x18461
leaq 0x50c57(%rip), %rbp     # 0x6906c
mulss (%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x22b0
mulsd (%rsp), %xmm0
leaq 0x50c51(%rip), %rax     # 0x69080
leaq 0x50c3e(%rip), %rcx     # 0x69074
movss (%rcx), %xmm1
leaq 0x50c2f(%rip), %rcx     # 0x69070
subss (%rcx), %xmm1
movsd %xmm0, (%rax)
mulss (%rbp), %xmm1
xorps %xmm0, %xmm0
cvtss2sd %xmm1, %xmm0
callq 0x22b0
leaq 0x3ec3d(%rip), %rdi     # 0x5709e
mulsd (%r15), %xmm0
leaq 0x50c0b(%rip), %r13     # 0x69078
cvtss2sd (%r13), %xmm3
leaq 0x50bfa(%rip), %rax     # 0x69074
xorps %xmm1, %xmm1
cvtss2sd (%rax), %xmm1
movsd %xmm0, (%r15)
leaq 0x50bdf(%rip), %rbp     # 0x6906c
xorps %xmm2, %xmm2
cvtss2sd (%rbp), %xmm2
movaps %xmm3, %xmm0
movb $0x3, %al
callq 0x20f0
testq %r14, %r14
je 0x18608
leaq 0x3ec1b(%rip), %rsi     # 0x570ca
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2270
leaq 0x3ed38(%rip), %rsi     # 0x57200
movq %r15, %rdi
callq 0x2220
movq %rax, %r14
leaq 0x50112(%rip), %r15     # 0x685ec
movl (%r15), %edx
xorps %xmm0, %xmm0
cvtss2sd (%rbp), %xmm0
leaq 0x50b88(%rip), %rbp     # 0x69074
xorps %xmm1, %xmm1
cvtss2sd (%rbp), %xmm1
xorps %xmm2, %xmm2
cvtss2sd (%r13), %xmm2
leaq 0x3ebe2(%rip), %rsi     # 0x570e6
movq %rax, %rdi
movb $0x3, %al
callq 0x21a0
cmpl $0x0, (%r15)
xorps %xmm0, %xmm0
cvtss2sd (%r13), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%rbp), %xmm1
leaq 0x50b42(%rip), %rax     # 0x6906c
xorps %xmm2, %xmm2
cvtss2sd (%rax), %xmm2
leaq 0x50b38(%rip), %r15     # 0x69070
xorps %xmm3, %xmm3
cvtss2sd (%r15), %xmm3
leaq 0x3ebf1(%rip), %rax     # 0x57138
leaq 0x3ebc3(%rip), %rsi     # 0x57111
cmoveq %rax, %rsi
movq %r14, %rdi
movb $0x4, %al
callq 0x21a0
xorps %xmm0, %xmm0
cvtss2sd (%r12), %xmm0
leaq 0x3ebf7(%rip), %rsi     # 0x57163
movq %r14, %rdi
movb $0x1, %al
callq 0x21a0
xorps %xmm0, %xmm0
cvtss2sd (%r15), %xmm0
xorps %xmm1, %xmm1
cvtss2sd (%r12), %xmm1
leaq 0x3ebe9(%rip), %rsi     # 0x57177
movq %r14, %rdi
movb $0x2, %al
callq 0x21a0
movss (%r12), %xmm0
ucomiss 0x3e90f(%rip), %xmm0    # 0x56eb4
jbe 0x18600
movss 0x3e905(%rip), %xmm0    # 0x56eb4
leaq 0x3ebef(%rip), %r15     # 0x571a5
movss %xmm0, (%rsp)
callq 0x18eec
xorps %xmm2, %xmm2
cvtss2sd (%rsp), %xmm2
movsd %xmm2, (%rsp)
xorps %xmm1, %xmm1
cvtss2sd %xmm0, %xmm1
movq %r14, %rdi
movq %r15, %rsi
movaps %xmm2, %xmm0
movb $0x2, %al
callq 0x21a0
movsd (%rsp), %xmm0
addsd 0x38b3f(%rip), %xmm0    # 0x51130
cvtsd2ss %xmm0, %xmm0
movss (%r12), %xmm1
ucomiss %xmm0, %xmm1
ja 0x185b6
movq %r14, %rdi
callq 0x20c0
movl 0xc(%rsp), %eax
testl %eax, %eax
jle 0x18653
movl %eax, %eax
imulq $0xc8, %rax, %r14
xorl %r15d, %r15d
movsd 0x20(%rbx,%r15), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x18665
cvtsd2ss %xmm0, %xmm0
callq 0x18eec
cvtss2sd %xmm0, %xmm0
mulsd 0x28(%rbx,%r15), %xmm0
movsd %xmm0, 0x28(%rbx,%r15)
addq $0xc8, %r15
cmpq %r15, %r14
jne 0x1861c
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3eb44(%rip), %rdi     # 0x571b0
movb $0x1, %al
callq 0x20f0
movl $0x8, %edi
callq 0x2280