// File: /workspace/github_c_makefile_star2_O1/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 $0x78, %rsp
movss %xmm1, 0x10(%rsp)
movl %r8d, 0x2c(%rsp)
movl %edx, %ebp
movq %rsi, 0x8(%rsp)
movss %xmm0, 0x48(%rsp)
movq %rdi, 0x70(%rsp)
movslq %edx, %rbx
movq %rbx, %rdi
shlq $0x5, %rdi
callq 0x21d0
movq %rax, %r15
movl %ebp, %eax
movq %rax, 0x40(%rsp)
testl %ebx, %ebx
jle 0x1feb8
movq 0x40(%rsp), %rbx
shlq $0x5, %rbx
xorl %r14d, %r14d
movl $0x0, (%r15,%r14)
movl $0x7d00, %edi           # imm = 0x7D00
callq 0x21d0
movq %rax, 0x8(%r15,%r14)
addq $0x20, %r14
cmpq %r14, %rbx
jne 0x1fe98
movq %r15, 0x50(%rsp)
movl %ebp, 0x4c(%rsp)
testl %ebp, %ebp
movq 0x8(%rsp), %r14
jle 0x1fff1
movss 0x48(%rsp), %xmm2
mulss %xmm2, %xmm2
movq 0x50(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x58(%rsp)
movq 0x40(%rsp), %rax
leaq 0x80(%r14), %rbp
movl $0x1, %ebx
xorl %ecx, %ecx
movss %xmm2, 0x14(%rsp)
movq %rcx, 0x18(%rsp)
incq %rcx
movq %rcx, 0x60(%rsp)
cmpq %rax, %rcx
jae 0x1ffd2
movq 0x18(%rsp), %rcx
shlq $0x5, %rcx
addq 0x50(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq %rbp, %r15
movq 0x58(%rsp), %r12
movq %rbx, %r13
movsd -0x10(%r15), %xmm0
subsd (%r14), %xmm0
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
mulss %xmm1, %xmm1
ucomiss %xmm2, %xmm1
ja 0x1ffbe
movsd -0x8(%r15), %xmm0
subsd 0x8(%r14), %xmm0
xorps %xmm3, %xmm3
cvtsd2ss %xmm0, %xmm3
mulss %xmm3, %xmm3
addss %xmm1, %xmm3
ucomiss %xmm2, %xmm3
ja 0x1ffbe
movsd (%r15), %xmm1
subsd 0x10(%r14), %xmm1
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
mulss %xmm0, %xmm0
addss %xmm3, %xmm0
ucomiss %xmm2, %xmm0
ja 0x1ffbe
movq 0x38(%rsp), %rdi
movl %r13d, %esi
movss %xmm0, 0x20(%rsp)
callq 0x20340
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movss 0x20(%rsp), %xmm0
callq 0x20340
movss 0x14(%rsp), %xmm2
movq 0x40(%rsp), %rax
incq %r13
addq $0x20, %r12
addq $0x70, %r15
cmpq %r13, %rax
jne 0x1ff33
addq $0x70, %r14
incq %rbx
addq $0x20, 0x58(%rsp)
addq $0x70, %rbp
movq 0x60(%rsp), %rcx
cmpq %rax, %rcx
jne 0x1feff
xorps %xmm0, %xmm0
cvtss2sd 0x10(%rsp), %xmm0
leaq 0x384cb(%rip), %rdi     # 0x584cc
movl 0x2c(%rsp), %ebx
movl %ebx, %esi
movsd %xmm0, 0x58(%rsp)
movaps %xmm0, %xmm1
movb $0x2, %al
callq 0x20f0
cmpl $0x3, %ebx
ja 0x20335
movl 0x4c(%rsp), %eax
testl %eax, %eax
jle 0x20279
movss 0x10(%rsp), %xmm0
mulss %xmm0, %xmm0
movss %xmm0, 0x14(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x38(%rsp)
xorl %ebp, %ebp
movq 0x50(%rsp), %r15
movq 0x8(%rsp), %r9
movq %r13, %rax
shlq $0x5, %rax
movq 0x10(%r15,%rax), %r12
testq %r12, %r12
je 0x20269
imulq $0x70, %r13, %rcx
leaq (%r9,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movl 0x64(%r9,%rcx), %ebx
addq %r15, %rax
addq $0x10, %rax
movq %rax, 0x18(%rsp)
movslq (%r12), %rax
cmpq %rax, %r13
jge 0x2025b
imulq $0x70, %rax, %r14
movl 0x64(%r9,%r14), %esi
cmpl %ebx, %esi
je 0x2025b
addq %r9, %r14
leaq 0x48552(%rip), %rax     # 0x68600
cmpl $0x0, (%rax)
je 0x200cf
movl %ebx, %edi
movq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x18e79
movq 0x8(%rsp), %r9
testl %eax, %eax
jne 0x2025b
incl 0x38(%rsp)
movss 0x4(%r12), %xmm2
movl $0x42c80000, %eax       # imm = 0x42C80000
movl %eax, 0x34(%rsp)
movl %eax, 0x30(%rsp)
movl 0x2c(%rsp), %eax
testl %eax, %eax
je 0x2013f
cmpl $0x1, %eax
je 0x20112
cmpl $0x3, %eax
jne 0x20173
ucomiss 0x30f0c(%rip), %xmm2    # 0x5100c
jb 0x2019d
xorps %xmm0, %xmm0
sqrtss %xmm2, %xmm0
jmp 0x201b6
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x30(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movss 0x14(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x20(%rsp), %rcx
movq %r14, %r8
callq 0x209d0
jmp 0x201e2
xorl %eax, %eax
movl %eax, 0x6c(%rsp)
movl %eax, 0x68(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x68(%rsp), %rdi
leaq 0x6c(%rsp), %rsi
movss 0x14(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x20(%rsp), %rcx
movq %r14, %r8
callq 0x20829
jmp 0x201e2
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x30(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movss 0x10(%rsp), %xmm0
movaps %xmm0, %xmm1
movq 0x20(%rsp), %rcx
movq %r14, %r8
callq 0x20b7b
jmp 0x201e2
movaps %xmm2, %xmm0
movss %xmm2, 0x60(%rsp)
callq 0x21c0
movss 0x60(%rsp), %xmm2
movq 0x8(%rsp), %r9
subss 0x10(%rsp), %xmm0
mulss %xmm0, %xmm0
movq 0x18(%rsp), %rax
movq (%rax), %rdx
leaq 0x30(%rsp), %rdi
leaq 0x34(%rsp), %rsi
movaps %xmm0, %xmm1
movq 0x20(%rsp), %rcx
movq %r14, %r8
callq 0x206fc
testl %eax, %eax
je 0x201ed
movq 0x8(%rsp), %r9
jmp 0x2025b
movslq %ebp, %rax
imulq $0xc8, %rax, %r14
movq 0x70(%rsp), %rcx
movl %r13d, (%rcx,%r14)
movl (%r12), %eax
movl %eax, 0x4(%rcx,%r14)
xorps %xmm0, %xmm0
cvtss2sd 0x4(%r12), %xmm0
ucomisd 0x30e45(%rip), %xmm0    # 0x51060
jb 0x20223
sqrtsd %xmm0, %xmm0
jmp 0x20228
callq 0x2290
movq 0x8(%rsp), %r9
addq 0x70(%rsp), %r14
movsd %xmm0, 0x20(%r14)
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
movq %rax, 0x28(%r14)
movl %ebp, %eax
incl %eax
movl %eax, %ebp
leaq 0x59085(%rip), %rcx     # 0x792d8
cmpl (%rcx), %eax
jge 0x2031b
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x20086
incq %r13
cmpq 0x40(%rsp), %r13
jne 0x20053
jmp 0x20288
xorl %ebp, %ebp
movl $0x0, 0x38(%rsp)
movq 0x50(%rsp), %r15
leaq 0x382aa(%rip), %rdi     # 0x58539
movl 0x38(%rsp), %esi
movl %ebp, %edx
movl 0x2c(%rsp), %ecx
xorl %eax, %eax
callq 0x20f0
xorps %xmm1, %xmm1
cvtss2sd 0x48(%rsp), %xmm1
leaq 0x382c2(%rip), %rdi     # 0x58572
movsd 0x58(%rsp), %xmm0
movb $0x2, %al
callq 0x20f0
movl $0xa, %edi
callq 0x2060
leaq 0x382ce(%rip), %rdi     # 0x5859c
xorl %esi, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, 0x4c(%rsp)
movq 0x40(%rsp), %rax
jle 0x20302
shlq $0x5, %rax
xorl %ebx, %ebx
movq 0x8(%r15,%rbx), %rdi
movq %rax, %r14
callq 0x2050
movq %r14, %rax
addq $0x20, %rbx
cmpq %rbx, %r14
jne 0x202e9
movq %r15, %rdi
callq 0x2050
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x36ca1(%rip), %rdi     # 0x56fc3
movl %ebp, %esi
xorl %eax, %eax
callq 0x20f0
movl $0x8, %edi
callq 0x2270
leaq 0x381cb(%rip), %rdi     # 0x58507
movl %ebx, %esi
jmp 0x20324