// File: /workspace/github_c_makefile_star2_O3/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, 0x1c(%rsp)
movl %r8d, 0x24(%rsp)
movl %edx, %ebp
movq %rsi, %rbx
movss %xmm0, 0x44(%rsp)
movq %rdi, 0x70(%rsp)
movslq %edx, %r15
movq %r15, %rdi
shlq $0x5, %rdi
callq 0x21e0
movq %rax, 0x28(%rsp)
movl %ebp, 0x20(%rsp)
movl %ebp, %r14d
movq %r15, 0x78(%rsp)
testl %r15d, %r15d
movq %r14, 0x58(%rsp)
jle 0x1f8b7
movss 0x44(%rsp), %xmm0
mulss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
shlq $0x5, %r14
xorl %r15d, %r15d
movq 0x28(%rsp), %r12
movl $0x0, (%r12,%r15)
movl $0x7d00, %edi           # imm = 0x7D00
callq 0x21e0
movq %rax, 0x8(%r12,%r15)
addq $0x20, %r15
cmpq %r15, %r14
jne 0x1f766
cmpl $0x0, 0x20(%rsp)
movss 0xc(%rsp), %xmm2
jle 0x1f8b7
movq 0x28(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x50(%rsp)
movq 0x58(%rsp), %rax
leaq 0x80(%rbx), %rcx
movq %rcx, 0x68(%rsp)
movl $0x1, %r14d
xorl %ecx, %ecx
movq %rbx, %r15
movq %rcx, 0x10(%rsp)
incq %rcx
movq %rcx, 0x60(%rsp)
cmpq 0x78(%rsp), %rcx
jge 0x1f896
movq 0x10(%rsp), %rcx
shlq $0x5, %rcx
addq 0x28(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x68(%rsp), %rbp
movq 0x50(%rsp), %r12
movq %r14, %r13
movsd -0x10(%rbp), %xmm0
subsd (%r15), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
ja 0x1f882
movsd -0x8(%rbp), %xmm0
subsd 0x8(%r15), %xmm0
xorps %xmm3, %xmm3
cvtsd2ss %xmm0, %xmm3
mulss %xmm3, %xmm3
addss %xmm1, %xmm3
ucomiss %xmm2, %xmm3
ja 0x1f882
movsd (%rbp), %xmm1
subsd 0x10(%r15), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
mulss %xmm0, %xmm0
addss %xmm3, %xmm0
ucomiss %xmm2, %xmm0
ja 0x1f882
movq 0x38(%rsp), %rdi
movl %r13d, %esi
movss %xmm0, 0x30(%rsp)
callq 0x1fbdf
movq %r12, %rdi
movq 0x10(%rsp), %rsi
movss 0x30(%rsp), %xmm0
callq 0x1fbdf
movss 0xc(%rsp), %xmm2
movq 0x58(%rsp), %rax
incq %r13
addq $0x20, %r12
addq $0x70, %rbp
cmpq %r13, %rax
jne 0x1f7f9
addq $0x70, %r15
incq %r14
addq $0x20, 0x50(%rsp)
addq $0x70, 0x68(%rsp)
movq 0x60(%rsp), %rcx
cmpq %rax, %rcx
jne 0x1f7c1
xorps %xmm0, %xmm0
cvtss2sd 0x1c(%rsp), %xmm0
leaq 0x38bc5(%rip), %rdi     # 0x5848c
movl 0x24(%rsp), %ebp
movl %ebp, %esi
movsd %xmm0, 0x50(%rsp)
movaps %xmm0, %xmm1
movb $0x2, %al
callq 0x20f0
cmpl $0x3, %ebp
ja 0x1fbd4
cmpl $0x0, 0x20(%rsp)
jle 0x1fb09
movss 0x1c(%rsp), %xmm0
mulss %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
xorl %r12d, %r12d
movl $0x0, 0x38(%rsp)
movl $0x0, 0x10(%rsp)
movq 0x28(%rsp), %r14
movq %r12, %rax
shlq $0x5, %rax
movq 0x10(%r14,%rax), %r13
testq %r13, %r13
je 0x1faf4
imulq $0x70, %r12, %rax
leaq (%rbx,%rax), %r15
movl 0x64(%rbx,%rax), %r14d
movq %r13, 0x30(%rsp)
movslq (%r13), %rax
cmpq %rax, %r12
jge 0x1fae7
imulq $0x70, %rax, %rbp
movl 0x64(%rbx,%rbp), %esi
cmpl %r14d, %esi
je 0x1fae7
addq %rbx, %rbp
leaq 0x48c98(%rip), %rax     # 0x68600
cmpl $0x0, (%rax)
je 0x1f983
movl %r14d, %edi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x18742
testl %eax, %eax
jne 0x1fae7
incl 0x38(%rsp)
movss 0x4(%r13), %xmm2
movl 0x24(%rsp), %eax
testl %eax, %eax
je 0x1f9e3
cmpl $0x1, %eax
je 0x1f9b8
cmpl $0x3, %eax
jne 0x1fa11
ucomiss 0x31772(%rip), %xmm2    # 0x51118
jb 0x1fa39
xorps %xmm0, %xmm0
sqrtss %xmm2, %xmm0
jmp 0x1fa4d
leaq 0x48(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movss 0xc(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x20360
jmp 0x1fa77
leaq 0x80(%rsp), %rdi
leaq 0x84(%rsp), %rsi
movss 0xc(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x201cf
jmp 0x1fa77
leaq 0x48(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movss 0x1c(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x204f5
jmp 0x1fa77
movaps %xmm2, %xmm0
movss %xmm2, 0x60(%rsp)
callq 0x21d0
movss 0x60(%rsp), %xmm2
subss 0x1c(%rsp), %xmm0
mulss %xmm0, %xmm0
leaq 0x48(%rsp), %rdi
leaq 0x4c(%rsp), %rsi
movaps %xmm0, %xmm1
movq 0x30(%rsp), %rdx
movq %r15, %rcx
movq %rbp, %r8
movq %rbx, %r9
callq 0x200ae
testl %eax, %eax
jne 0x1fae7
movslq 0x10(%rsp), %rax
imulq $0xc8, %rax, %rbp
movq 0x70(%rsp), %rcx
movl %r12d, (%rcx,%rbp)
movl (%r13), %eax
movl %eax, 0x4(%rcx,%rbp)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%r13), %xmm0
ucomisd 0x316cf(%rip), %xmm0    # 0x51178
jb 0x1fab1
sqrtsd %xmm0, %xmm0
jmp 0x1fab6
callq 0x22a0
addq 0x70(%rsp), %rbp
movsd %xmm0, 0x20(%rbp)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%rbp)
movl 0x10(%rsp), %eax
incl %eax
movl %eax, 0x10(%rsp)
leaq 0x597f9(%rip), %rcx     # 0x792d8
cmpl (%rcx), %eax
jge 0x1fbb8
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x1f940
incq %r12
cmpq 0x58(%rsp), %r12
movq 0x28(%rsp), %r14
jne 0x1f919
jmp 0x1fb1e
movl $0x0, 0x10(%rsp)
movl $0x0, 0x38(%rsp)
movq 0x28(%rsp), %r14
leaq 0x389d4(%rip), %rdi     # 0x584f9
movl 0x38(%rsp), %esi
movl 0x10(%rsp), %edx
movl 0x24(%rsp), %ecx
xorl %eax, %eax
callq 0x20f0
xorps %xmm1, %xmm1
cvtss2sd 0x44(%rsp), %xmm1
leaq 0x389ea(%rip), %rdi     # 0x58532
movsd 0x50(%rsp), %xmm0
movb $0x2, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
leaq 0x389f6(%rip), %rdi     # 0x5855c
xorl %esi, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x20(%rsp)
movq 0x58(%rsp), %rax
jle 0x1fb9a
shlq $0x5, %rax
xorl %ebx, %ebx
movq 0x8(%r14,%rbx), %rdi
movq %rax, %r15
callq 0x2050
movq %r15, %rax
addq $0x20, %rbx
cmpq %rbx, %r15
jne 0x1fb81
movq %r14, %rdi
callq 0x2050
movl 0x10(%rsp), %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x373c4(%rip), %rdi     # 0x56f83
movl 0x10(%rsp), %esi
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2280
leaq 0x388ec(%rip), %rdi     # 0x584c7
movl %ebp, %esi
jmp 0x1fbc3