// File: /workspace/github_c_makefile_star2_O3/mohuangrui[P]ArtraCFD/numerical_test.c
// Function: ComputeSolutionFunctional
void ComputeSolutionFunctional(const Time *time, Space *space, const Model *model)
{
    FILE *fp = Fopen("solution_functional.csv", "a");
    if (0 == time->stepC) { /* initialization step */
        fprintf(fp, "# time, kinetic energy, enstrophy \n");
    }
    const Partition *const part = &(space->part);
    Node *const node = space->node;
    Real *restrict U = NULL; /* numerical solution */
    int idx = 0; /* linear array index math variable */
    const RealVec d = {part->d[X], part->d[Y], part->d[Z]};
    const int h[DIMS][DIMS] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; /* direction indicator */
    Real rho = model->refRho; /* density */
    RealVec V = {0.0}; /* velocity */
    RealVec W = {0.0}; /* vorticity */
    Real Vs[DIMS][TTN] = {{0.0}}; /* velocity stencil */
    Real dV[DIMS][DIMS] = {{0.0}}; /* velocity gradient */
    Real Ek = 0.0; /* kinetic energy */
    Real Ee = 0.0; /* enstrophy */
    int N = 0; /* number of nodes */
    for (int k = part->ns[PIN][Z][MIN]; k < part->ns[PIN][Z][MAX]; ++k) {
        for (int j = part->ns[PIN][Y][MIN]; j < part->ns[PIN][Y][MAX]; ++j) {
            for (int i = part->ns[PIN][X][MIN]; i < part->ns[PIN][X][MAX]; ++i) {
                for (int s = 0; s < DIMS; ++s) {
                    for (int n = -TCN; n <= TCN; ++n) {
                        idx = IndexNode(k + n * h[s][Z], j + n * h[s][Y], i + n * h[s][X], part->n[Y], part->n[X]);
                        U = node[idx].U[TO];
                        Vs[X][TCN+n] = U[1] / U[0];
                        Vs[Y][TCN+n] = U[2] / U[0];
                        Vs[Z][TCN+n] = U[3] / U[0];
                    }
                    dV[X][s] = (-Vs[X][TCN+2] + 8.0 * Vs[X][TCN+1] - 8.0 * Vs[X][TCN-1] + Vs[X][TCN-2]) / (12.0 * d[s]);
                    dV[Y][s] = (-Vs[Y][TCN+2] + 8.0 * Vs[Y][TCN+1] - 8.0 * Vs[Y][TCN-1] + Vs[Y][TCN-2]) / (12.0 * d[s]);
                    dV[Z][s] = (-Vs[Z][TCN+2] + 8.0 * Vs[Z][TCN+1] - 8.0 * Vs[Z][TCN-1] + Vs[Z][TCN-2]) / (12.0 * d[s]);
                }
                idx = IndexNode(k, j, i, part->n[Y], part->n[X]);
                U = node[idx].U[TO];
                rho = U[0];
                V[X] = U[1] / U[0];
                V[Y] = U[2] / U[0];
                V[Z] = U[3] / U[0];
                W[X] = dV[Z][Y] - dV[Y][Z];
                W[Y] = dV[X][Z] - dV[Z][X];
                W[Z] = dV[Y][X] - dV[X][Y];
                Ek = Ek + rho * Dot(V,V);
                Ee = Ee + rho * Dot(W,W);
                ++N;
            }
        }
    }
    Ek = 0.5 * Ek / N;
    Ee = 0.5 * Ee / N;
    fprintf(fp, "%.6g, %.6g, %.6g\n", time->now, Ek, Ee);
    fclose(fp);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xa695(%rip), %rdi      # 0x1e415
leaq 0x8b20(%rip), %rsi      # 0x1c8a7
callq 0x9026
movq %rax, 0x18(%rsp)
movq %rbx, 0x70(%rsp)
cmpl $0x0, 0x8(%rbx)
jne 0x13db7
leaq 0xa68a(%rip), %rdi      # 0x1e42d
movl $0x23, %esi
movl $0x1, %edx
movq 0x18(%rsp), %rcx
callq 0x2200
movq (%r14), %rbx
movups 0x58(%r14), %xmm0
movaps %xmm0, 0x1c0(%rsp)
movsd 0x68(%r14), %xmm0
movsd %xmm0, 0x1d0(%rsp)
xorpd %xmm2, %xmm2
movapd %xmm2, 0x90(%rsp)
movapd %xmm2, 0xa0(%rsp)
movl $0x1, %eax
movl %eax, 0x90(%rsp)
movl %eax, 0xa0(%rsp)
movl %eax, 0xb0(%rsp)
xorl %esi, %esi
movq %rsi, 0x60(%rsp)
movapd %xmm2, 0x50(%rsp)
movq %rsi, 0xd0(%rsp)
movapd %xmm2, 0xc0(%rsp)
movq %rsi, 0x150(%rsp)
movapd %xmm2, 0x140(%rsp)
movapd %xmm2, 0x130(%rsp)
movapd %xmm2, 0x120(%rsp)
movapd %xmm2, 0x110(%rsp)
movapd %xmm2, 0x100(%rsp)
movapd %xmm2, 0xf0(%rsp)
movapd %xmm2, 0xe0(%rsp)
movq %rsi, 0x1a0(%rsp)
movapd %xmm2, 0x190(%rsp)
movapd %xmm2, 0x180(%rsp)
movapd %xmm2, 0x170(%rsp)
movapd %xmm2, 0x160(%rsp)
movslq 0xa0(%r14), %rax
movl 0xa4(%r14), %ecx
xorpd %xmm0, %xmm0
movq %rax, 0x10(%rsp)
cmpl %ecx, %eax
jge 0x14231
movl 0x9c(%r14), %eax
movq %r14, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq 0x28(%rsp), %r15
movq 0x20(%rsp), %r13
movl 0x98(%r14), %edx
movl %edx, 0x8(%rsp)
cmpl %eax, %edx
jge 0x14209
movl 0x94(%r14), %ecx
movl 0x90(%r14), %edx
movl %edx, 0xc(%rsp)
cmpl %ecx, %edx
jge 0x141f0
movq %rsi, 0x78(%rsp)
movapd %xmm2, 0x1b0(%rsp)
xorl %eax, %eax
movq %rax, 0x80(%rsp)
leaq (%rax,%rax,2), %rax
movl 0x98(%rsp,%rax,4), %ecx
movl 0x90(%rsp,%rax,4), %edx
movl 0x94(%rsp,%rax,4), %eax
movq %rax, 0x88(%rsp)
addl %eax, %eax
movl 0x8(%rsp), %r14d
subl %eax, %r14d
movq %rdx, 0x30(%rsp)
movl 0xc(%rsp), %ebx
subl %edx, %ebx
subl %edx, %ebx
movq %rcx, 0x40(%rsp)
leal (%rcx,%rcx), %eax
movq 0x10(%rsp), %rcx
movl %ecx, %r12d
subl %eax, %r12d
xorl %ebp, %ebp
movl 0x34(%r15), %r8d
movl 0x38(%r15), %ecx
movl %r12d, %edi
movl %r14d, %esi
movl %ebx, %edx
callq 0x7d53
cltq
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%rcx,%rax,8), %rax
movsd 0x10(%r13,%rax), %xmm0
movsd 0x18(%r13,%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0xe0(%rsp,%rbp,8)
movsd 0x20(%r13,%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x108(%rsp,%rbp,8)
movsd 0x28(%r13,%rax), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x130(%rsp,%rbp,8)
addl 0x88(%rsp), %r14d
addl 0x30(%rsp), %ebx
incq %rbp
addl 0x40(%rsp), %r12d
cmpq $0x5, %rbp
jne 0x13f63
movsd 0xf8(%rsp), %xmm0
movsd 0xa3bd(%rip), %xmm3     # 0x1e3b0
mulsd %xmm3, %xmm0
subsd 0x100(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm1
movsd 0xa3a7(%rip), %xmm4     # 0x1e3b8
mulsd %xmm4, %xmm1
addsd %xmm0, %xmm1
addsd 0xe0(%rsp), %xmm1
movsd 0x120(%rsp), %xmm0
mulsd %xmm3, %xmm0
subsd 0x128(%rsp), %xmm0
movsd 0x110(%rsp), %xmm2
mulsd %xmm4, %xmm2
addsd %xmm0, %xmm2
movq 0x80(%rsp), %rax
movsd 0x1c0(%rsp,%rax,8), %xmm0
mulsd 0xa35e(%rip), %xmm0     # 0x1e3c0
divsd %xmm0, %xmm1
addsd 0x108(%rsp), %xmm2
movsd %xmm1, 0x160(%rsp,%rax,8)
divsd %xmm0, %xmm2
movsd %xmm2, 0x178(%rsp,%rax,8)
movsd 0x148(%rsp), %xmm1
mulsd %xmm3, %xmm1
subsd 0x150(%rsp), %xmm1
movsd 0x138(%rsp), %xmm2
mulsd %xmm4, %xmm2
addsd %xmm1, %xmm2
addsd 0x130(%rsp), %xmm2
divsd %xmm0, %xmm2
movsd %xmm2, 0x190(%rsp,%rax,8)
incq %rax
cmpq $0x3, %rax
jne 0x13f0e
movq 0x28(%rsp), %r14
movl 0x34(%r14), %r8d
movl 0x38(%r14), %ecx
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %esi
movl 0xc(%rsp), %ebp
movl %ebp, %edx
callq 0x7d53
cltq
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%rcx,%rax,8), %rax
movq 0x20(%rsp), %rbx
movsd 0x10(%rbx,%rax), %xmm1
movapd %xmm1, 0x40(%rsp)
movsd 0x18(%rbx,%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x20(%rbx,%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x28(%rbx,%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movupd 0x198(%rsp), %xmm0
movupd 0x188(%rsp), %xmm1
movhpd 0x170(%rsp), %xmm0      # xmm0 = xmm0[0],mem[0]
subpd %xmm1, %xmm0
movapd %xmm0, 0xc0(%rsp)
movsd 0x178(%rsp), %xmm0
subsd 0x168(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
callq 0x7e36
movapd %xmm0, 0x30(%rsp)
leaq 0xc0(%rsp), %rdi
movq %rdi, %rsi
callq 0x7e36
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0x30(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
movapd %xmm2, %xmm0
movapd 0x1b0(%rsp), %xmm2
addpd %xmm0, %xmm2
movq 0x78(%rsp), %rsi
incl %esi
incl %ebp
movl 0x94(%r14), %ecx
movl %ebp, 0xc(%rsp)
cmpl %ecx, %ebp
jl 0x13efe
movl 0x9c(%r14), %eax
movl 0x8(%rsp), %edx
incl %edx
movl %edx, 0x8(%rsp)
cmpl %eax, %edx
jl 0x13eeb
movl 0xa4(%r14), %ecx
movq 0x10(%rsp), %rdi
incq %rdi
movslq %ecx, %rdx
movq %rdi, 0x10(%rsp)
cmpq %rdx, %rdi
jl 0x13ed1
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
mulpd 0x83df(%rip), %xmm2     # 0x1c610
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
unpckhpd %xmm2, %xmm2            # xmm2 = xmm2[1,1]
divsd %xmm0, %xmm2
movq 0x70(%rsp), %rax
movsd 0x48(%rax), %xmm0
leaq 0xa1b1(%rip), %rsi      # 0x1e403
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movb $0x3, %al
callq 0x2160
movq %rbx, %rdi
callq 0x2080
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop