// File: /workspace/github_c_makefile_star2_O2/ugobas[P]tnm/simulation.c
// Function: Simulate_ensemble_Cart
void Simulate_ensemble_Cart(int N_struct, float factor, char *name,
			    struct Para_simul Para_simul,
			    atom *atoms, int natoms,
			    struct axe *axe, int naxes,
			    struct bond *bonds,
			    struct residue *seq, int nres,
			    int N_diso, struct Normal_Mode NM,
			    struct Jacobian J, struct Reference Ref,
			    struct interaction *Int_list, int N_int,
			    char *INT_TYPE, float s0, char *nameout,
			    int anhar)
{
  float *sigma2;
  if(anhar){sigma2=NM.sigma2_anhar;}
  else{sigma2=NM.sigma2;}

  if(natoms!=Ref.N_ref){
    printf("WARNING, cannot simulate Cartesian ensemble\n"); return;
  }
  idum=IDUM;
  int i, k, n3=3*natoms, N_Cart=Ref.N_Cart, N_ref=Ref.N_ref;

  atom atoms_sim[natoms]; float mass[natoms];
  for(i=0; i<natoms; i++){
    atoms_sim[i]=atoms[i];
    mass[i]=Mass(atoms+i);
  }
  float coord_all_1[n3], coord_all[n3];
  Write_all_coord_atom(coord_all_1, atoms, natoms);
  float coord_ref_1[N_Cart]; // coord_ref[N_Cart];
  Write_ref_coord_atom(coord_ref_1, N_ref, atoms, Ref.atom_num);

  // Open files
  char name_pdb[100], name_out[200];
  sprintf(name_pdb, "%s_%s_ensemble_Cart.pdb", nameout, name);
  FILE *file_pdb=fopen(name_pdb, "w");
  sprintf(name_out, "%s_%s_ensemble_Cart.dat", nameout, name);
  FILE *file_out=fopen(name_out, "w");
  fprintf(file_out, "#struct rmsd\n");
  if(anhar){
    fprintf(file_out, "# Omega2 corrected for anharmonicity\n");
    fprintf(file_pdb, "REMARK Omega2 corrected for anharmonicity\n");
  }

  // Initialization for Examine_confchange
  if(Ini_ch){
    Allocate_tors(&Diff_s, naxes, N_Cart, NM.N);
    ali_sim.alignres=malloc(nres*sizeof(int));
    Ini_ch=0;
  }
  ali_sim.seq_id=100; ali_sim.mammoth=0;
  for(i=0; i<nres; i++)ali_sim.alignres[i]=i;
  char summary3[400];
  sprintf(summary3, "Summary_%s_ensemble.dat", nameout);

  struct ali_atoms ali_a; Copy_ali(&ali_a, Ref);
  float dx[N_Cart], sigma[NM.N];
  for(k=0; k<NM.N; k++){
    if(sigma2[k]>0){sigma[k]=sqrt(sigma2[k]);}
    else{sigma[k]=0;}
  }
  // Write initial PDB
  Print_PDB(file_pdb, atoms, natoms, coord_all_1, seq, 0, 0.00);
  fprintf(file_out, "%d %.3f\n", 0, 0.0);
  for(k=1; k<=N_struct; k++){
    Extract_dx(dx, NM.Cart, sigma, NM.N, N_Cart, factor, &idum);
    for(i=0; i<N_Cart; i++){
      coord_all[i]=coord_ref_1[i]+dx[i];
    }
    float rmsd=rmsd_mclachlan_f(coord_all_1, coord_all, mass, natoms);
    fprintf(file_out, "%d %.3f\n", k, rmsd);
    Print_PDB(file_pdb, atoms, natoms, coord_all, seq, k, rmsd);
    Put_atoms(atoms_sim, coord_all, natoms);
    /*Examine_confchange(&Diff_s, bonds, axe, NULL, 0, summary3, nameout, "",
		       atoms, coord_ref_1, seq, nres, N_diso, natoms,
		       atoms_sim, coord_ref, seq, nres, N_diso, natoms,  //&J, 
		       Ref, ali_a, Int_list, N_int, INT_TYPE, s0, NM.N, NM,
		       k, ali_sim, Para_simul, k, NULL, NULL, NULL, 0, anhar);
    */
  }
  printf("Distances printed in %s\n", name_out); fclose(file_out);
  printf("Simulated structures printed in %s\n", name_pdb);
  fclose(file_pdb);
  if(Ini_ch==0){Empty_tors(Diff_s); free(ali_sim.alignres); Ini_ch=1;}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movq %rdx, -0x68(%rbp)
movss %xmm0, -0x80(%rbp)
movl %edi, -0x2c(%rbp)
movl 0x1d8(%rbp), %edi
testl %edi, %edi
leaq 0x98(%rbp), %rdx
leaq 0xf8(%rbp), %rax
cmoveq %rdx, %rax
cmpl %ecx, 0x188(%rbp)
jne 0x3035d
movq %rsi, -0x78(%rbp)
movl %edi, -0x38(%rbp)
movl %r9d, -0x7c(%rbp)
movq %rsp, -0xa0(%rbp)
movq 0x1d0(%rbp), %rdx
movq %rdx, -0x40(%rbp)
leaq 0x188(%rbp), %rsi
movl 0x60(%rbp), %r14d
movq 0x58(%rbp), %rdx
movq %rdx, -0x90(%rbp)
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq $-0xb2a3, 0x4423c(%rip) # imm = 0xFFFF4D5D
movl 0x4(%rsi), %eax
movq %rax, -0x50(%rbp)
movl %ecx, %eax
movq %rsp, %rsi
imulq $0x70, %rax, %rdx
subq %rdx, %rsi
movq %rsi, -0x98(%rbp)
movq %rsi, %rsp
movq %rsp, %r12
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r12
movq %r12, %rsp
xorl %r15d, %r15d
testl %ecx, %ecx
movl $0x0, %eax
movq %rcx, -0x48(%rbp)
cmovgl %ecx, %eax
imulq $0x70, %rax, %r13
pushq $0xe
popq %rbx
movq %r12, -0xa8(%rbp)
cmpq %r15, %r13
je 0x30377
movq -0x98(%rbp), %rax
leaq (%rax,%r15), %rdi
movq -0x68(%rbp), %rax
addq %r15, %rax
movq %rbx, %rcx
movq %rax, %rsi
rep  movsq (%rsi), %es:(%rdi)
movq %rax, %rdi
callq 0x233c9
movss %xmm0, (%r12)
addq $0x70, %r15
addq $0x4, %r12
jmp 0x30325
leaq 0x2580f(%rip), %rdi     # 0x55b73
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2090
movq -0x48(%rbp), %rbx
leal (%rbx,%rbx,2), %eax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, %rsp
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %rdi, -0x88(%rbp)
movq -0x68(%rbp), %r12
movq %r12, %rsi
movl %ebx, %edx
callq 0x2fa25
movq %rsp, %r15
movq -0x50(%rbp), %rax
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
movq %rax, -0x70(%rbp)
subq %rax, %r15
movq %r15, %rsp
leaq 0x188(%rbp), %rax
movq 0x28(%rax), %rcx
movq %r15, %rdi
movl %ebx, %esi
movq %r12, %rdx
callq 0x2f97d
leaq 0x24bed(%rip), %rsi     # 0x54fda
leaq -0x110(%rbp), %rdi
movq -0x40(%rbp), %rbx
movq %rbx, %rdx
movq -0x78(%rbp), %r12
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
leaq 0x20d40(%rip), %rsi     # 0x51150
leaq -0x110(%rbp), %rdi
callq 0x2210
movq %rax, -0x58(%rbp)
leaq 0x24bcb(%rip), %rsi     # 0x54ff2
leaq -0x1e0(%rbp), %rdi
movq %rbx, %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2260
leaq -0x1e0(%rbp), %rdi
leaq 0x20d07(%rip), %rsi     # 0x51150
movq -0x58(%rbp), %rbx
callq 0x2210
movq %rax, %r12
leaq 0x24bae(%rip), %rdi     # 0x5500a
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x2280
cmpl $0x0, -0x38(%rbp)
je 0x3049a
leaq 0x24a45(%rip), %rdi     # 0x54ebc
pushq $0x25
popq %rsi
pushq $0x1
popq %rdx
movq %r12, %rcx
callq 0x2280
leaq 0x24a56(%rip), %rdi     # 0x54ee2
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
movq %rbx, %rcx
callq 0x2280
movq %r12, -0x38(%rbp)
cmpl $0x0, 0x33a1b(%rip)     # 0x63ec0
je 0x304dc
leaq 0x70(%rbp), %rax
movl (%rax), %ecx
leaq 0x44064(%rip), %rdi     # 0x74518
movl -0x7c(%rbp), %esi
movq -0x50(%rbp), %rdx
callq 0x10444
movslq %r14d, %rdi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, 0x4409d(%rip)     # 0x74570
andl $0x0, 0x339e6(%rip)     # 0x63ec0
jmp 0x304e3
movq 0x4408d(%rip), %rax     # 0x74570
movl $0x42c80000, 0x4406b(%rip) # imm = 0x42C80000
andl $0x0, 0x44084(%rip)     # 0x74578
xorl %ecx, %ecx
testl %r14d, %r14d
cmovlel %ecx, %r14d
cmpq %rcx, %r14
je 0x3050a
movl %ecx, (%rax,%rcx,4)
incq %rcx
jmp 0x304fd
leaq 0x24b07(%rip), %rsi     # 0x55018
xorl %ebx, %ebx
leaq -0x370(%rbp), %rdi
movq -0x40(%rbp), %rdx
xorl %eax, %eax
callq 0x2260
movq %rsp, %r14
subq -0x70(%rbp), %r14
movq %r14, %rsp
leaq 0x70(%rbp), %rax
movl (%rax), %ecx
movq %rsp, %rdx
leaq 0xf(,%rcx,4), %rax
andq $-0x10, %rax
subq %rax, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
testl %ecx, %ecx
movl $0x0, %r12d
movq %rcx, -0x78(%rbp)
cmovgl %ecx, %r12d
xorps %xmm2, %xmm2
movq -0x60(%rbp), %rax
cmpq %rbx, %r12
je 0x3059f
movss (%rax,%rbx,4), %xmm1
xorps %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jbe 0x30591
ucomiss %xmm2, %xmm1
jb 0x30582
sqrtss %xmm1, %xmm0
jmp 0x30591
movaps %xmm1, %xmm0
callq 0x21c0
xorps %xmm2, %xmm2
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
movss %xmm0, (%rcx,%rbx,4)
incq %rbx
jmp 0x30565
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq -0x88(%rbp), %rcx
movq -0x90(%rbp), %r8
xorl %r9d, %r9d
callq 0x2f4bd
leaq 0x24a63(%rip), %rsi     # 0x55030
xorps %xmm0, %xmm0
movq -0x38(%rbp), %rdi
xorl %edx, %edx
movb $0x1, %al
callq 0x2190
movq -0x50(%rbp), %rax
testl %eax, %eax
movl $0x0, %r12d
cmovgl %eax, %r12d
movl -0x2c(%rbp), %eax
testl %eax, %eax
cmovlel %ebx, %eax
leaq 0x70(%rbp), %rcx
movq 0x38(%rcx), %rcx
movq %rcx, -0x70(%rbp)
incl %eax
movl %eax, -0x2c(%rbp)
pushq $0x1
popq %rbx
cmpl -0x2c(%rbp), %ebx
je 0x306cd
movq %r14, %rdi
movq -0x70(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x50(%rbp), %r8
movss -0x80(%rbp), %xmm0
leaq 0x43edf(%rip), %r9      # 0x74510
callq 0x2f771
xorl %eax, %eax
cmpq %rax, %r12
je 0x30655
movss (%r15,%rax,4), %xmm0
addss (%r14,%rax,4), %xmm0
movss %xmm0, (%r13,%rax,4)
incq %rax
jmp 0x30638
movq -0x88(%rbp), %rdi
movq %r13, %rsi
movq -0xa8(%rbp), %rdx
movq -0x48(%rbp), %rcx
callq 0x47a64
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x60(%rbp)
cvtss2sd %xmm0, %xmm0
movq -0x38(%rbp), %rdi
leaq 0x249a9(%rip), %rsi     # 0x55030
movl %ebx, %edx
movb $0x1, %al
callq 0x2190
movq -0x58(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq %r13, %rcx
movq -0x90(%rbp), %r8
movl %ebx, %r9d
movss -0x60(%rbp), %xmm0
callq 0x2f4bd
movq -0x98(%rbp), %rdi
movq %r13, %rsi
movq -0x48(%rbp), %rdx
callq 0x2f9f4
incl %ebx
jmp 0x30609
leaq 0x2485f(%rip), %rdi     # 0x54f33
leaq -0x1e0(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x38(%rbp), %rdi
callq 0x20c0
leaq 0x24947(%rip), %rdi     # 0x55039
leaq -0x110(%rbp), %rsi
xorl %eax, %eax
callq 0x20f0
movq -0x58(%rbp), %rdi
callq 0x20c0
cmpl $0x0, 0x337b0(%rip)     # 0x63ec0
jne 0x30764
subq $0x40, %rsp
movups 0x43e2b(%rip), %xmm0    # 0x74548
movups %xmm0, 0x30(%rsp)
movups 0x43e0f(%rip), %xmm0    # 0x74538
movups %xmm0, 0x20(%rsp)
movups 0x43df3(%rip), %xmm0    # 0x74528
movups %xmm0, 0x10(%rsp)
movups 0x43dd7(%rip), %xmm0    # 0x74518
movups %xmm0, (%rsp)
callq 0x104a1
addq $0x40, %rsp
movq 0x43e1b(%rip), %rdi     # 0x74570
callq 0x2050
movl $0x1, 0x3375c(%rip)     # 0x63ec0
movq -0xa0(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq