// File: /workspace/github_cpp_makefile_star2_O3/Luthaf[P]zeopp/grid.cc
// Function: GaussianCube::writeGrid(ATOM_NETWORK*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, bool)
void GaussianCube::writeGrid(ATOM_NETWORK *atmnet, std::string cubefilename, bool angstrom_to_bohr, bool useMassFlag){

 FILE *fp1;
 fp1=fopen(cubefilename.c_str(),"w");

 double toB=1; //scale factor - update it if we want to scale the output
 if(angstrom_to_bohr) toB=1.8903592 ; // scale factor to convert from A to Bohr

 fprintf(fp1,"\nThis is distance grid\n");

 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",atmnet->numAtoms,0.0,0.0,0.0);

 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",na,va.x*toB,va.y*toB,va.z*toB);
 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",nb,vb.x*toB,vb.y*toB,vb.z*toB);
 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",nc,vc.x*toB,vc.y*toB,vc.z*toB);

 for(int i=0;i<atmnet->numAtoms;i++)
   {
   if(useMassFlag == true)
     {
     fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf % 13.6lf\n",lookupAtomicNumber(atmnet->atoms[i].type),lookupMass(atmnet->atoms[i].type),
                                                              atmnet->atoms[i].x*toB, atmnet->atoms[i].y*toB, atmnet->atoms[i].z*toB);
     }else{
     fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf % 13.6lf\n", 1, 1.0,
                                                              atmnet->atoms[i].x*toB, atmnet->atoms[i].y*toB, atmnet->atoms[i].z*toB);
     };
   };

 fprintf(fp1," 1    1\n");

 int count2=0; // for formatting only
 for(int x=0;x<na;x++)
   for(int y=0;y<nb;y++)
     for(int z=0;z<nc;z++)
       {
       fprintf(fp1," % 13.6E ",cube[x][y][z]);

       // count2 is for formatting only (max 6 columns with data)
       count2++;
       if(z==(nc-1)) {fprintf(fp1,"\n");count2=0;};
       if(count2==6) {fprintf(fp1,"\n");count2=0;};
       };

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r13d
movl %ecx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdx), %rdi
leaq 0xca21e(%rip), %rsi     # 0x151c44
callq 0x4510
movq %rax, %r14
testl %r12d, %r12d
jne 0x87a3d
movsd 0xc561d(%rip), %xmm0    # 0x14d058
jmp 0x87a45
movsd 0xcddb3(%rip), %xmm0    # 0x1557f8
movsd %xmm0, (%rsp)
leaq 0xcdeac(%rip), %rdi     # 0x1558fd
movl $0x17, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x4850
movl 0x110(%r15), %edx
leaq 0xcdea4(%rip), %r12     # 0x155915
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x78(%rbx), %edx
movsd 0x18(%rbx), %xmm0
movsd (%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x20(%rbx), %xmm1
mulsd %xmm3, %xmm1
movsd 0x28(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x7c(%rbx), %edx
movsd 0x30(%rbx), %xmm0
movsd (%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x38(%rbx), %xmm1
mulsd %xmm3, %xmm1
movsd 0x40(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x80(%rbx), %edx
movsd 0x48(%rbx), %xmm0
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x50(%rbx), %xmm1
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm3
movsd 0x58(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
cmpl $0x0, 0x110(%r15)
jle 0x87c98
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r13d, 0x14(%rsp)
movq %r15, 0x8(%rsp)
movq 0x2f0(%r15), %rax
testb %r13b, %r13b
je 0x87c3b
leaq 0x48(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x40(%rax,%r12), %rsi
movq 0x48(%rax,%r12), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0xf3ce
movq %r13, %rdi
callq 0x22217
leaq 0x18(%rsp), %r15
movl %eax, %r13d
movq 0x8(%rsp), %rax
movq 0x2f0(%rax), %rax
leaq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x40(%rax,%r12), %rsi
movq 0x48(%rax,%r12), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0xf3ce
movq %r15, %rdi
callq 0x220cf
movq 0x8(%rsp), %rax
movq 0x2f0(%rax), %rax
movsd (%rax,%r12), %xmm1
movsd (%rsp), %xmm4
mulsd %xmm4, %xmm1
movsd 0x8(%rax,%r12), %xmm2
mulsd %xmm4, %xmm2
movsd 0x10(%rax,%r12), %xmm3
mulsd %xmm4, %xmm3
movq %r14, %rdi
leaq 0xcdd47(%rip), %rsi     # 0x155934
movl %r13d, %edx
movb $0x4, %al
callq 0x45f0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x87c13
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
movl 0x14(%rsp), %r13d
movq 0x8(%rsp), %r15
je 0x87c7e
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x87c7e
movsd (%rax,%r12), %xmm1
movsd (%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd 0x8(%rax,%r12), %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rax,%r12), %xmm3
mulsd %xmm0, %xmm3
movq %r14, %rdi
leaq 0xcdcca(%rip), %rsi     # 0x155934
movl $0x1, %edx
movsd 0xc53e1(%rip), %xmm0    # 0x14d058
movb $0x4, %al
callq 0x45f0
incq %rbp
movslq 0x110(%r15), %rax
addq $0x98, %r12
cmpq %rax, %rbp
jl 0x87b3b
leaq 0xcdcbd(%rip), %rdi     # 0x15595c
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x4850
movl 0x78(%rbx), %ecx
testl %ecx, %ecx
jle 0x87d50
movl 0x7c(%rbx), %eax
xorl %r12d, %r12d
xorl %ebp, %ebp
testl %eax, %eax
jle 0x87d41
movl 0x80(%rbx), %ecx
xorl %r13d, %r13d
testl %ecx, %ecx
jle 0x87d33
xorl %r15d, %r15d
movq 0x88(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %r14, %rdi
leaq 0xcdc6e(%rip), %rsi     # 0x155965
movb $0x1, %al
callq 0x45f0
movl 0x80(%rbx), %eax
decl %eax
cmpq %rax, %r15
je 0x87d12
incl %ebp
cmpl $0x6, %ebp
jne 0x87d21
movl $0xa, %edi
movq %r14, %rsi
callq 0x4550
xorl %ebp, %ebp
incq %r15
movslq 0x80(%rbx), %rcx
cmpq %rcx, %r15
jl 0x87cd8
movl 0x7c(%rbx), %eax
incq %r13
movslq %eax, %rdx
cmpq %rdx, %r13
jl 0x87cd1
movl 0x78(%rbx), %ecx
incq %r12
movslq %ecx, %rdx
cmpq %rdx, %r12
jl 0x87cc4
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x87d83
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x87d83
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x87d9f
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780