// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdx, -0xa8(%rbp)
movq %rdi, %rdx
movq -0xa8(%rbp), %rdi
movb %r8b, %al
movq %rdx, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdi, -0x18(%rbp)
andb $0x1, %cl
movb %cl, -0x19(%rbp)
andb $0x1, %al
movb %al, -0x1a(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x5200
movq %rax, %rdi
leaq 0x105a94(%rip), %rsi    # 0x1f6c14
callq 0x5650
movq %rax, -0x28(%rbp)
movsd 0x100ebf(%rip), %xmm0   # 0x1f2050
movsd %xmm0, -0x30(%rbp)
testb $0x1, -0x19(%rbp)
je 0xf11a9
movsd 0x10b28c(%rip), %xmm0   # 0x1fc430
movsd %xmm0, -0x30(%rbp)
movq -0x28(%rbp), %rdi
leaq 0x10b3e2(%rip), %rsi    # 0x1fc596
movb $0x0, %al
callq 0x5750
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x110(%rax), %edx
leaq 0x10b3de(%rip), %rsi    # 0x1fc5ae
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
movb $0x3, %al
callq 0x5750
movq -0xa0(%rbp), %rax
movq -0x28(%rbp), %rdi
movl 0x78(%rax), %edx
movsd 0x18(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd 0x20(%rax), %xmm1
mulsd -0x30(%rbp), %xmm1
movsd 0x28(%rax), %xmm2
mulsd -0x30(%rbp), %xmm2
leaq 0x10b39b(%rip), %rsi    # 0x1fc5ae
movb $0x3, %al
callq 0x5750
movq -0xa0(%rbp), %rax
movq -0x28(%rbp), %rdi
movl 0x7c(%rax), %edx
movsd 0x30(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd 0x38(%rax), %xmm1
mulsd -0x30(%rbp), %xmm1
movsd 0x40(%rax), %xmm2
mulsd -0x30(%rbp), %xmm2
leaq 0x10b361(%rip), %rsi    # 0x1fc5ae
movb $0x3, %al
callq 0x5750
movq -0xa0(%rbp), %rax
movq -0x28(%rbp), %rdi
movl 0x80(%rax), %edx
movsd 0x48(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd 0x50(%rax), %xmm1
mulsd -0x30(%rbp), %xmm1
movsd 0x58(%rax), %xmm2
mulsd -0x30(%rbp), %xmm2
leaq 0x10b324(%rip), %rsi    # 0x1fc5ae
movb $0x3, %al
callq 0x5750
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x110(%rcx), %eax
jge 0xf14e5
movb -0x1a(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0xf142f
movq -0x28(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movq %rax, %rsi
addq $0x40, %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0xb8(%rbp)
callq 0x5220
movq -0xb8(%rbp), %rdi
callq 0x46c40
movl %eax, -0xac(%rbp)
jmp 0xf1306
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movq %rax, %rsi
addq $0x40, %rsi
leaq -0x88(%rbp), %rdi
callq 0x5220
jmp 0xf132f
leaq -0x88(%rbp), %rdi
callq 0x46ae0
movsd %xmm0, -0xc8(%rbp)
jmp 0xf1345
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movsd (%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movsd 0x8(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movq -0xc0(%rbp), %rdi
movl -0xac(%rbp), %edx
movsd -0xc8(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
movsd -0xd0(%rbp), %xmm2
movsd 0x10(%rax), %xmm3
mulsd -0x30(%rbp), %xmm3
leaq 0x10b1f3(%rip), %rsi    # 0x1fc5cd
movb $0x4, %al
callq 0x5750
leaq -0x88(%rbp), %rdi
callq 0x5300
leaq -0x58(%rbp), %rdi
callq 0x5300
jmp 0xf14d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0xf1421
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x5300
leaq -0x58(%rbp), %rdi
callq 0x5300
jmp 0xf165b
movq -0x28(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movsd (%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movsd 0x8(%rax), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x34(%rbp), %rsi
callq 0xfcc0
movq -0xf0(%rbp), %rdi
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
movsd 0x10(%rax), %xmm3
mulsd -0x30(%rbp), %xmm3
leaq 0x10b10c(%rip), %rsi    # 0x1fc5cd
movl $0x1, %edx
movsd 0x100b82(%rip), %xmm0   # 0x1f2050
movb $0x4, %al
callq 0x5750
jmp 0xf14d7
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf1298
movq -0x28(%rbp), %rdi
leaq 0x10b105(%rip), %rsi    # 0x1fc5f5
movb $0x0, %al
callq 0x5750
movl $0x0, -0x8c(%rbp)
movl $0x0, -0x90(%rbp)
movq -0xa0(%rbp), %rcx
movl -0x90(%rbp), %eax
cmpl 0x78(%rcx), %eax
jge 0xf1652
movl $0x0, -0x94(%rbp)
movq -0xa0(%rbp), %rcx
movl -0x94(%rbp), %eax
cmpl 0x7c(%rcx), %eax
jge 0xf163c
movl $0x0, -0x98(%rbp)
movq -0xa0(%rbp), %rcx
movl -0x98(%rbp), %eax
cmpl 0x80(%rcx), %eax
jge 0xf1626
movq -0xa0(%rbp), %rax
movq -0x28(%rbp), %rdi
movq 0x88(%rax), %rax
movslq -0x90(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x94(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x98(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
leaq 0x10b05f(%rip), %rsi    # 0x1fc5fe
movb $0x1, %al
callq 0x5750
movq -0xa0(%rbp), %rcx
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movl -0x98(%rbp), %eax
movl 0x80(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0xf15eb
movq -0x28(%rbp), %rdi
leaq 0x10804b(%rip), %rsi    # 0x1f9625
movb $0x0, %al
callq 0x5750
movl $0x0, -0x8c(%rbp)
cmpl $0x6, -0x8c(%rbp)
jne 0xf1610
movq -0x28(%rbp), %rdi
leaq 0x108026(%rip), %rsi    # 0x1f9625
movb $0x0, %al
callq 0x5750
movl $0x0, -0x8c(%rbp)
jmp 0xf1612
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xf154b
jmp 0xf1628
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xf152b
jmp 0xf163e
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0xf150b
addq $0xf0, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x58f0
nopw %cs:(%rax,%rax)