// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/screened_interactions.c
// Function: Screened_Interactions
int Screened_Interactions(struct interaction *Int_list,
			  float THR, atom *atoms, int N_atoms, int nres,
			  int TYPE, float S_THR)
{
  // For each pair of atoms ij, determine e_ij such that
  // e_ij=min_k(max(d_ik,d_jk) (minimum of the maximum distance).
  // C_ij=exp(e_ij-d_ij)/(1+exp(e_ij-d_ij))-> 0 if e_ij << d_ij
  // Fast algorithm: for each i store the first RMAX neighbors d<thr
  // For each i and j in list of neighbors, find e_ij

  int i1, i2, i;
  atom *atom1=atoms, *atom2;
  float Thr=THR, thr2=Thr*Thr, d, d2;

  // Allocate and rank RMAX neighbors, including bonded
  struct ranked *rank=malloc(N_atoms*sizeof(struct ranked));
  for(i=0; i<N_atoms; i++){
    rank[i].num=0;
    rank[i].store=malloc(RMAX*sizeof(struct store));
  }
  for(i1=0; i1<N_atoms; i1++){
    atom2=atom1+1;
    struct ranked *rank1=rank+i1;
    for(i2=i1+1; i2<N_atoms; i2++){
      // Interatomic distance
      d=atom2->r[0]-atom1->r[0]; d2 =d*d; if(d2>thr2)goto next;
      d=atom2->r[1]-atom1->r[1]; d2+=d*d; if(d2>thr2)goto next;
      d=atom2->r[2]-atom1->r[2]; d2+=d*d; if(d2>thr2)goto next;
      Rank(rank1,  i2, d2, RMAX);
      Rank(rank+i2,i1, d2, RMAX);
    next:
      atom2++;
    }
    atom1++;
  }

  // Thresholds
  float S_BOND=S_THR;
  if(RELAX_COV){
    if(TYPE==0){S_BOND=S_THR+(1.-S_THR)/2;} // cosine
    else if(TYPE==1){S_BOND=0.5*S_THR;} // distance from axis
    else if(TYPE==2){S_BOND=0.5*S_THR;} // Shadow
    else if(TYPE==3){S_BOND=2*S_THR;}   // SCR
  }
  float S2_NB=S_THR*S_THR, S2_BO=S_BOND*S_BOND; 
  //float delta_nb=DTHR*S_THR;
  //float delta_bo=DTHR*S_BOND;
  printf("Screened interactions type %d D=%.3f (nobond) %.3f (bond)\n",
	 TYPE, S_THR, S_BOND);
  if((TYPE<0)||(TYPE>3)){
    printf("ERROR, undefined type of screened interaction %d\n", TYPE);
    exit(8);
  }

  // Protect from screening interactions that satisfy the MIN criterion
  int **protect=NULL;
  if(PROTECT)protect=Protect_interactions(rank, atoms, N_atoms, nres);
  // WARNING! store is a linked list !!!!!

  int N_int=0, tested=0, Nprotect=0;
  float e2=1;
  for(i1=0; i1<N_atoms; i1++){
    atom1=atoms+i1;
    int res1=atom1->res, n=-1;
    struct store *store=rank[i1].first;
    while(store != NULL){
      if(protect)n++;
      i2=store->i;
      if(i2<=i1)goto No_int;
      atom2=atoms+i2;
      int res2=atom2->res;
      if(res2==res1)goto No_int;
      if(NOCOV && Covalent(res1, res2, atom1, atom2))goto No_int;
      if((protect)&&(protect[i1][n])){Nprotect++; goto store;}
      tested++;

      float d2_ij= store->x; 
      float d2_bo=100, d2_nb=d2_bo;  // Bonded and non-bonded, squared

      if(TYPE==3){
	// SCR interactions
	float d_ij=sqrt(d2_ij);
	float D2_NB=d_ij-S_THR;  D2_NB*=D2_NB;
	float D2_BO=d_ij-S_BOND; D2_BO*=D2_BO;
	//Screening_distance(&e2_nb,&e2_bo,res2,rank[i2].first, atom1, atoms);
	// Change 27/3/2015: weight SCR=0,1
	if(Screening_distance(&d2_nb, &d2_bo, D2_NB, D2_BO,
			      rank[i1].first, d2_ij, atom1, atom2, atoms))
	  goto No_int;

	/* 
	e2_nb=sqrt(d2_ij)-sqrt(e2_nb);
	if(e2_nb > delta_nb)goto No_int;
	e2_bo=sqrt(d2_ij)-sqrt(e2_bo);
	if(e2_bo > delta_bo)goto No_int;
	if(S_THR > 0){
	  e2_nb=exp(-e2_nb/S_THR);
	  e2_bo=exp(-e2_bo/S_BOND);
	  if(e2_nb<e2_bo){e2=e2_nb/(1.+e2_nb);}
	  else{e2=e2_bo/(1.+e2_bo);}
	} */

      }else if(TYPE==0){
	// Screening based on cosine
	float cos2_bo=0, cos2_nb=0;
	if(Shadow_cosine(&cos2_nb, &cos2_bo, S2_NB, S2_BO,
			 rank[i1].first, d2_ij, atom1, atom2, atoms))
	  goto No_int;

      }else if(TYPE==1){
	// Screening based on distance from axis
	if(Axis_distance(&d2_nb, &d2_bo, S2_NB, S2_BO,
			 rank[i1].first, d2_ij, atom1, atom2, atoms))
	  goto No_int;
	//w=exp(-d_min/S_THR); w=(1-w)/(1+w); if(w<0.005)goto No_int;

      }else if(TYPE==2){
	// Screening based on Onuchic radius
	if(Axis_distance_S(&d2_nb, &d2_bo, S_THR, S_BOND,
			   rank[i1].first, d2_ij, atom1, atom2, atoms))
	  goto No_int;
      }

    store:
      Int_list[N_int].i1=i1; Int_list[N_int].i2=store->i;
      Int_list[N_int].r0=sqrt(store->x); Int_list[N_int].sec_der=e2;
      N_int++;
      if(N_int >= INT_MAX){
	printf("ERROR, too many interactions %d\n", N_int); exit(8);
      }

    No_int:
      store=store->next;
    }
  }

  // INform
  printf("%d pairs tested, %d screened interactions type %d found,",
	 tested, N_int, TYPE);
  printf(" screening par=%.4f cut-off distance=%.1f", S_THR, THR);
  //if(TYPE==3)printf("cut-off for screening=%.2g", 2*exp(-DTHR));
  printf("\n");
  printf("%d pairs protected from screening because of MIN\n", Nprotect);

  for(i=0; i<N_atoms; i++)free(rank[i].store);
  free(rank);

  return(N_int);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm1, 0xc(%rsp)
movl %r8d, 0x3c(%rsp)
movl %edx, %ebx
movq %rsi, 0x10(%rsp)
movss %xmm0, 0x38(%rsp)
movq %rdi, 0x70(%rsp)
movslq %edx, %r15
movq %r15, %rdi
shlq $0x5, %rdi
callq 0x21d0
movq %rax, %r13
xorl %r14d, %r14d
testl %r15d, %r15d
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x60(%rsp)
shlq $0x5, %rax
movq %rax, 0x68(%rsp)
cmpq %r14, 0x68(%rsp)
je 0x1dc0a
andl $0x0, (%r13,%r14)
movl $0x7d00, %edi           # imm = 0x7D00
callq 0x21d0
movq %rax, 0x8(%r13,%r14)
addq $0x20, %r14
jmp 0x1dbe8
movss 0x38(%rsp), %xmm3
mulss %xmm3, %xmm3
leaq 0x20(%r13), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rsp), %r14
leaq 0x80(%r14), %r15
pushq $0x1
popq %rbp
xorl %r12d, %r12d
movq %r13, 0x58(%rsp)
movss 0xc(%rsp), %xmm2
movss %xmm3, 0x1c(%rsp)
cmpq 0x60(%rsp), %r12
je 0x1dd31
leaq 0x70(%r14), %rax
movq %rax, 0x78(%rsp)
movq %r12, 0x20(%rsp)
shlq $0x5, %r12
addq %r13, %r12
movq %r15, 0x50(%rsp)
movl %ebp, 0x48(%rsp)
movq 0x30(%rsp), %r13
cmpl %ebx, %ebp
jge 0x1dd05
movsd -0x10(%r15), %xmm0
subsd (%r14), %xmm0
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm3, %xmm1
ja 0x1dcf6
movsd -0x8(%r15), %xmm0
subsd 0x8(%r14), %xmm0
cvtsd2ss %xmm0, %xmm4
mulss %xmm4, %xmm4
addss %xmm1, %xmm4
ucomiss %xmm3, %xmm4
ja 0x1dcf6
movsd (%r15), %xmm1
subsd 0x10(%r14), %xmm1
cvtsd2ss %xmm1, %xmm0
mulss %xmm0, %xmm0
addss %xmm4, %xmm0
ucomiss %xmm3, %xmm0
ja 0x1dcf6
movq %r12, %rdi
movl %ebp, %esi
movss %xmm0, 0x28(%rsp)
callq 0x1e04c
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movss 0x28(%rsp), %xmm0
callq 0x1e04c
movss 0x1c(%rsp), %xmm3
movss 0xc(%rsp), %xmm2
addq $0x20, %r13
incl %ebp
addq $0x70, %r15
jmp 0x1dc6e
movq 0x20(%rsp), %r12
incq %r12
addq $0x20, 0x30(%rsp)
movl 0x48(%rsp), %ebp
incl %ebp
movq 0x50(%rsp), %r15
addq $0x70, %r15
movq 0x78(%rsp), %r14
movq 0x58(%rsp), %r13
jmp 0x1dc40
cvtss2sd %xmm2, %xmm0
leaq 0x346b0(%rip), %rdi     # 0x523ec
movl 0x3c(%rsp), %r14d
movl %r14d, %esi
movsd %xmm0, 0x48(%rsp)
movaps %xmm0, %xmm1
movb $0x2, %al
callq 0x20f0
cmpl $0x3, %r14d
ja 0x1e040
movss 0xc(%rsp), %xmm1
movaps %xmm1, %xmm0
mulss %xmm1, %xmm0
movss %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %r9
cmpq 0x60(%rsp), %r13
je 0x1dfa2
imulq $0x70, %r13, %rax
leaq (%r9,%rax), %rcx
movq %rcx, 0x28(%rsp)
movl 0x64(%r9,%rax), %r15d
movq %r13, %rax
shlq $0x5, %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%rax), %rbx
addq $0x10, %rbx
movq %rbx, 0x20(%rsp)
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x1df9a
movslq (%rbx), %rax
cmpq %rax, %r13
jge 0x1df91
imulq $0x70, %rax, %rbp
movl 0x64(%r9,%rbp), %esi
cmpl %r15d, %esi
je 0x1df91
addq %r9, %rbp
leaq 0x45812(%rip), %rax     # 0x63600
cmpl $0x0, (%rax)
je 0x1de16
movl %r15d, %edi
movq 0x28(%rsp), %rdx
movq %rbp, %rcx
callq 0x171db
movq 0x10(%rsp), %r9
movss 0xc(%rsp), %xmm1
testl %eax, %eax
jne 0x1df91
incl 0x1c(%rsp)
movss 0x4(%rbx), %xmm2
testl %r14d, %r14d
je 0x1de73
cmpl $0x1, %r14d
je 0x1de46
cmpl $0x3, %r14d
jne 0x1dea3
ucomiss 0x2d1d5(%rip), %xmm2    # 0x4b00c
jb 0x1dec7
sqrtss %xmm2, %xmm0
jmp 0x1dee6
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x40(%rsp), %rdi
leaq 0x44(%rsp), %rsi
movss 0x30(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x28(%rsp), %rcx
movq %rbp, %r8
callq 0x1e621
jmp 0x1df10
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x80(%rsp), %rdi
leaq 0x84(%rsp), %rsi
movss 0x30(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x28(%rsp), %rcx
movq %rbp, %r8
callq 0x1e4d7
jmp 0x1df10
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x40(%rsp), %rdi
leaq 0x44(%rsp), %rsi
movaps %xmm1, %xmm0
movq 0x28(%rsp), %rcx
movq %rbp, %r8
callq 0x1e770
jmp 0x1df10
movaps %xmm2, %xmm0
movss %xmm2, 0x50(%rsp)
callq 0x21c0
movss 0x50(%rsp), %xmm2
movq 0x10(%rsp), %r9
movss 0xc(%rsp), %xmm1
subss %xmm1, %xmm0
mulss %xmm0, %xmm0
movq 0x20(%rsp), %rax
movq (%rax), %rdx
leaq 0x40(%rsp), %rdi
leaq 0x44(%rsp), %rsi
movaps %xmm0, %xmm1
movq 0x28(%rsp), %rcx
movq %rbp, %r8
callq 0x1e3d7
testl %eax, %eax
je 0x1df21
movss 0xc(%rsp), %xmm1
movq 0x10(%rsp), %r9
jmp 0x1df91
movslq %r12d, %rax
imulq $0xc8, %rax, %r14
movq 0x70(%rsp), %rcx
movl %r13d, (%rcx,%r14)
movl (%rbx), %eax
movl %eax, 0x4(%rcx,%r14)
cvtss2sd 0x4(%rbx), %xmm0
ucomisd 0x2d108(%rip), %xmm0    # 0x4b050
jb 0x1df50
sqrtsd %xmm0, %xmm0
jmp 0x1df55
callq 0x2290
movss 0xc(%rsp), %xmm1
movq 0x10(%rsp), %r9
addq 0x70(%rsp), %r14
movsd %xmm0, 0x20(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r14)
incl %r12d
leaq 0x56355(%rip), %rax     # 0x742d8
cmpl (%rax), %r12d
jge 0x1e027
movl 0x3c(%rsp), %r14d
addq $0x8, %rbx
jmp 0x1ddba
incq %r13
jmp 0x1dd84
leaq 0x344b0(%rip), %rdi     # 0x52459
xorl %ebx, %ebx
movl 0x1c(%rsp), %esi
movl %r12d, %edx
movl %r14d, %ecx
xorl %eax, %eax
callq 0x20f0
cvtss2sd 0x38(%rsp), %xmm1
leaq 0x344c9(%rip), %rdi     # 0x52492
movsd 0x48(%rsp), %xmm0
movb $0x2, %al
callq 0x20f0
pushq $0xa
popq %rdi
callq 0x2060
leaq 0x344d7(%rip), %rdi     # 0x524bc
xorl %esi, %esi
xorl %eax, %eax
callq 0x20f0
movq 0x58(%rsp), %r14
cmpq %rbx, 0x68(%rsp)
je 0x1e00a
movq 0x8(%r14,%rbx), %rdi
callq 0x2050
addq $0x20, %rbx
jmp 0x1dff3
movq %r14, %rdi
callq 0x2050
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x32ea5(%rip), %rdi     # 0x50ed3
movl %r12d, %esi
xorl %eax, %eax
callq 0x20f0
pushq $0x8
popq %rdi
callq 0x2270
leaq 0x343e0(%rip), %rdi     # 0x52427
movl %r14d, %esi
jmp 0x1e031