// File: /workspace/github_c_makefile_star2_O2/mohuangrui[P]ArtraCFD/solid_dynamics.c
// Function: IntegrateSurfaceForce
void IntegrateSurfaceForce(Space *space, const Model *model)
{
    const Partition *const part = &(space->part);
    const Node *const node = space->node;
    Geometry *const geo = &(space->geo);
    const IntVec nMin = {part->ns[PIN][X][MIN], part->ns[PIN][Y][MIN], part->ns[PIN][Z][MIN]};
    const IntVec nMax = {part->ns[PIN][X][MAX], part->ns[PIN][Y][MAX], part->ns[PIN][Z][MAX]};
    const RealVec sMin = {part->domain[X][MIN], part->domain[Y][MIN], part->domain[Z][MIN]};
    const RealVec d = {part->d[X], part->d[Y], part->d[Z]};
    const RealVec dd = {part->dd[X], part->dd[Y], part->dd[Z]};
    const IntVec ng = {part->ng[X], part->ng[Y], part->ng[Z]};
    const Real zero = 0.0;
    const Real percent = FLT_EPSILON * FLT_EPSILON;
    Polyhedron *poly = NULL;
    int idx = 0; /* linear array index math variable */
    int box[DIMS][LIMIT] = {{0}}; /* bounding box in node space */
    int lidN = 0; /* count total number of interfacial nodes */
    int gstN = 0; /* count total number of ghost nodes */
    RealVec pG = {zero}; /* ghost point */
    RealVec pO = {zero}; /* boundary point */
    RealVec pI = {zero}; /* image point */
    RealVec N = {zero}; /* normal */
    Real Uo[DIMUo] = {zero};
    RealVec V = {zero}; /* velocity vector */
    RealVec r = {zero}; /* position vector */
    RealVec Fp = {zero}; /* pressure force */
    RealVec Fv = {zero}; /* viscous force */
    RealVec Fs = {zero}; /* surface force */
    RealVec Tt = {zero}; /* torque */
    RealVec fvar = {zero}; /* force offset, mean, variance */
    Real Vn = zero; /* velocity projection */
    Real mu = zero; /* viscosity */
    Real ds = zero; /* infinitesimal area for integration */
    for (int n = 0; n < geo->totN; ++n) {
        poly = geo->poly + n;
        if (0 < poly->state) { /* surface force negligible */
            continue;
        }
        /* reset some non accumulative information to zero */
        memset(poly->Fp, 0, DIMS * sizeof(*poly->Fp));
        memset(poly->Fv, 0, DIMS * sizeof(*poly->Fv));
        memset(poly->Tt, 0, DIMS * sizeof(*poly->Tt));
        memset(fvar, 0, DIMS * sizeof(*fvar));
        lidN = 0;
        gstN = 0;
        /* determine search range according to bounding box of polyhedron and valid node space */
        for (int s = 0; s < DIMS; ++s) {
            box[s][MIN] = ConfineSpace(MapNode(poly->box[s][MIN], sMin[s], dd[s], ng[s]), nMin[s], nMax[s]);
            box[s][MAX] = ConfineSpace(MapNode(poly->box[s][MAX], sMin[s], dd[s], ng[s]), nMin[s], nMax[s]) + 1;
        }
        for (int k = box[Z][MIN]; k < box[Z][MAX]; ++k) {
            for (int j = box[Y][MIN]; j < box[Y][MAX]; ++j) {
                for (int i = box[X][MIN]; i < box[X][MAX]; ++i) {
                    idx = IndexNode(k, j, i, part->n[Y], part->n[X]);
                    if ((2 == node[idx].lid) && (n + 1 == node[idx].did)) {
                        ++lidN; /* an interfacial node of current geometry */
                    }
                    if ((2 != node[idx].gst) || (n + 1 != node[idx].did)) {
                        continue;
                    }
                    ++gstN; /* a ghost node of current geometry */
                    /* surface force exerted by fluid (pressure + shear force) */
                    pG[X] = MapPoint(i, sMin[X], d[X], ng[X]);
                    pG[Y] = MapPoint(j, sMin[Y], d[Y], ng[Y]);
                    pG[Z] = MapPoint(k, sMin[Z], d[Z], ng[Z]);
                    ComputeGeometricData(pG, node[idx].fid, poly, pO, pI, N);
                    r[X] = pO[X] - poly->O[X];
                    r[Y] = pO[Y] - poly->O[Y];
                    r[Z] = pO[Z] - poly->O[Z];
                    MapPrimitive(model->gamma, model->gasR, node[idx].U[TO], Uo);
                    Fp[X] = Uo[4] * N[X];
                    Fp[Y] = Uo[4] * N[Y];
                    Fp[Z] = Uo[4] * N[Z];
                    if (1 == gstN) {
                        fvar[0] = Uo[4];
                    }
                    fvar[1] = fvar[1] + Uo[4] - fvar[0];
                    fvar[2] = fvar[2] + (Uo[4] - fvar[0]) * (Uo[4] - fvar[0]);
                    if ((zero < model->refMu) && (zero < poly->cf)) {
                        mu = model->refMu * Viscosity(Uo[5] * model->refT);
                        Cross(poly->W[TO], r, V);
                        V[X] = Uo[1] - (poly->V[TO][X] + V[X]);
                        V[Y] = Uo[2] - (poly->V[TO][Y] + V[Y]);
                        V[Z] = Uo[3] - (poly->V[TO][Z] + V[Z]);
                        Vn = Dot(V, N);
                        Fv[X] = mu * (V[X] - Vn * N[X]) / Dist(pG, pO);
                        Fv[Y] = mu * (V[Y] - Vn * N[Y]) / Dist(pG, pO);
                        Fv[Z] = mu * (V[Z] - Vn * N[Z]) / Dist(pG, pO);
                    } else {
                        memset(Fv, 0, DIMS * sizeof(*Fv));
                    }
                    Fs[X] = Fp[X] + Fv[X];
                    Fs[Y] = Fp[Y] + Fv[Y];
                    Fs[Z] = Fp[Z] + Fv[Z];
                    Cross(r, Fs, Tt);
                    /* integration sum */
                    for (int s = 0; s < DIMS; ++s) {
                        poly->Fp[s] = poly->Fp[s] + Fp[s];
                        poly->Fv[s] = poly->Fv[s] + Fv[s];
                        poly->Tt[s] = poly->Tt[s] + Tt[s];
                    }
                }
            }
        }
        /* calibrate the sum of discrete forces into integration */
        if ((0 == lidN) || (0 == gstN)) { /* no surface force exerted */
            continue;
        }
        ds = poly->area / lidN;
        fvar[2] = (fvar[2] - fvar[1] * fvar[1] / gstN) / gstN; /* variance */
        fvar[1] = fvar[1] / gstN + fvar[0]; /* mean */
        if (percent * fvar[1] * fvar[1] > fvar[2]) { /* recover equilibrium state and ignore integration error */
            ds = zero;
        }
        for (int s = 0; s < DIMS; ++s) {
            poly->Fp[s] = -poly->Fp[s] * ds;
            poly->Fv[s] = -poly->Fv[s] * ds;
            poly->Tt[s] = -poly->Tt[s] * ds;
        }
    }
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %rsi, 0x30(%rsp)
movq %rdi, %r14
movq (%rdi), %rax
movq %rax, 0x1c8(%rsp)
movl 0x90(%rdi), %eax
movl %eax, 0x114(%rsp)
movl 0x98(%rdi), %eax
movl %eax, 0x118(%rsp)
movl 0xa0(%rdi), %eax
movl %eax, 0x11c(%rsp)
movl 0x94(%rdi), %eax
movl %eax, 0x108(%rsp)
movl 0x9c(%rdi), %eax
movl %eax, 0x10c(%rsp)
movl 0xa4(%rdi), %eax
movl %eax, 0x110(%rsp)
movsd 0x3f0(%rdi), %xmm0
movsd %xmm0, 0x1c0(%rsp)
movsd %xmm0, 0x200(%rsp)
movsd 0x400(%rdi), %xmm0
movsd %xmm0, 0x1b8(%rsp)
movsd %xmm0, 0x208(%rsp)
movsd 0x410(%rdi), %xmm0
movsd %xmm0, 0x1b0(%rsp)
movsd %xmm0, 0x210(%rsp)
movsd 0x58(%rdi), %xmm0
movsd %xmm0, 0x1a8(%rsp)
movsd 0x60(%rdi), %xmm0
movsd %xmm0, 0x1a0(%rsp)
movsd 0x68(%rdi), %xmm0
movsd %xmm0, 0x198(%rsp)
movups 0x70(%rdi), %xmm0
movaps %xmm0, 0x2d0(%rsp)
movsd 0x80(%rdi), %xmm0
movsd %xmm0, 0x2e0(%rsp)
movl 0x40(%rdi), %eax
movl %eax, 0x5c(%rsp)
movl %eax, 0xfc(%rsp)
movl 0x44(%rdi), %eax
movl %eax, 0x58(%rsp)
movl %eax, 0x100(%rsp)
movl 0x48(%rdi), %eax
movl %eax, 0x54(%rsp)
movl %eax, 0x104(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
leaq 0xe0(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x170(%rsp), %rax
andq $0x0, 0x10(%rax)
leaq 0x2f0(%rsp), %rcx
andq $0x0, 0x10(%rcx)
movapd %xmm0, (%rax)
movapd %xmm0, (%rcx)
leaq 0xc0(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x220(%rsp), %rax
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
leaq 0x60(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x150(%rsp), %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, (%rax)
andq $0x0, 0x130(%rsp)
leaq 0x1e0(%rsp), %rax
andq $0x0, 0x10(%rax)
movapd %xmm0, 0x120(%rsp)
movapd %xmm0, (%rax)
leaq 0x290(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq $0x0, 0x20(%rsp)
movslq 0x8(%r14), %rax
movq 0x20(%rsp), %rdx
cmpq %rax, %rdx
jge 0x16a3b
movq 0x18(%r14), %rax
imulq $0x238, %rdx, %rcx      # imm = 0x238
cmpl $0x0, 0xc(%rax,%rcx)
jle 0x162aa
incq %rdx
movq %rdx, 0x20(%rsp)
jmp 0x1627c
leaq (%rax,%rcx), %r13
xorpd %xmm0, %xmm0
movupd %xmm0, 0x188(%r13)
movupd %xmm0, 0x178(%r13)
andq $0x0, 0x198(%r13)
movupd %xmm0, 0x168(%r13)
movupd %xmm0, 0x158(%r13)
addq %rcx, %rax
addq $0x1d8, %rax            # imm = 0x1D8
movq %rax, 0x10(%rsp)
xorl %ebx, %ebx
cmpq $0xc, %rbx
je 0x1638f
movq 0x10(%rsp), %rbp
movsd -0x8(%rbp,%rbx,4), %xmm0
movsd 0x200(%rsp,%rbx,2), %xmm1
movsd %xmm1, 0x38(%rsp)
movsd 0x2d0(%rsp,%rbx,2), %xmm2
movsd %xmm2, 0x18(%rsp)
movl 0xfc(%rsp,%rbx), %edi
movl %edi, 0xc(%rsp)
callq 0x761c
movl 0x114(%rsp,%rbx), %r12d
movl 0x108(%rsp,%rbx), %r15d
movl %eax, %edi
movl %r12d, %esi
movl %r15d, %edx
callq 0x7633
movl %eax, 0x90(%rsp,%rbx,2)
movsd (%rbp,%rbx,4), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movl 0xc(%rsp), %edi
callq 0x761c
movl %eax, %edi
movl %r12d, %esi
movl %r15d, %edx
callq 0x7633
incl %eax
movl %eax, 0x94(%rsp,%rbx,2)
addq $0x4, %rbx
jmp 0x162ee
movl 0xa0(%rsp), %edi
movl 0xa4(%rsp), %eax
movl %eax, 0x48(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x50(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x4c(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x38(%rsp)
incq 0x20(%rsp)
leaq 0xb0(%r13), %rax
movq %rax, 0x188(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
movl $0x0, 0x10(%rsp)
movsd %xmm0, 0x28(%rsp)
movapd %xmm0, 0x140(%rsp)
cmpl 0x48(%rsp), %edi
jge 0x16954
movl 0x44(%rsp), %r15d
cmpl 0x50(%rsp), %r15d
jge 0x1694d
movl 0x4c(%rsp), %r12d
cmpl 0x38(%rsp), %r12d
jge 0x16945
movl 0x34(%r14), %r8d
movl 0x38(%r14), %ecx
movl %edi, %ebp
movl %r15d, %esi
movl %r12d, %edx
callq 0x75ec
cltq
imulq $0x88, %rax, %rax
movq 0x1c8(%rsp), %rcx
leaq (%rcx,%rax), %rbx
cmpl $0x2, 0x8(%rcx,%rax)
jne 0x16475
movl (%rbx), %eax
xorl %ecx, %ecx
cmpq %rax, 0x20(%rsp)
sete %cl
addl %ecx, 0xc(%rsp)
cmpl $0x2, 0xc(%rbx)
jne 0x1693b
movl (%rbx), %eax
cmpq %rax, 0x20(%rsp)
jne 0x1693b
movl %r12d, %edi
movsd 0x1c0(%rsp), %xmm0
movsd 0x1a8(%rsp), %xmm1
movl 0x5c(%rsp), %esi
callq 0x7651
movsd %xmm0, 0xe0(%rsp)
movl %r15d, %edi
movsd 0x1b8(%rsp), %xmm0
movsd 0x1a0(%rsp), %xmm1
movl 0x58(%rsp), %esi
callq 0x7651
movsd %xmm0, 0xe8(%rsp)
movl %ebp, %edi
movsd 0x1b0(%rsp), %xmm0
movsd 0x198(%rsp), %xmm1
movl 0x54(%rsp), %esi
callq 0x7651
movsd %xmm0, 0xf0(%rsp)
movl 0x4(%rbx), %esi
leaq 0xe0(%rsp), %rdi
movq %r13, %rdx
leaq 0x170(%rsp), %rcx
leaq 0x2f0(%rsp), %r8
leaq 0xc0(%rsp), %r9
callq 0xad8e
movsd 0x170(%rsp), %xmm0
subsd 0x20(%r13), %xmm0
movsd 0x178(%rsp), %xmm1
movsd %xmm0, 0x150(%rsp)
subsd 0x28(%r13), %xmm1
movsd %xmm1, 0x158(%rsp)
movsd 0x180(%rsp), %xmm0
subsd 0x30(%r13), %xmm0
movsd %xmm0, 0x160(%rsp)
movq 0x30(%rsp), %rax
movsd 0x40(%rax), %xmm0
movsd 0x48(%rax), %xmm1
addq $0x10, %rbx
movq %rbx, %rdi
leaq 0x220(%rsp), %rsi
callq 0x74af
movsd 0x240(%rsp), %xmm3
movapd %xmm3, %xmm2
unpcklpd %xmm3, %xmm2            # xmm2 = xmm2[0],xmm3[0]
mulpd 0xc0(%rsp), %xmm2
movapd %xmm2, 0x2b0(%rsp)
movsd 0xd0(%rsp), %xmm4
mulsd %xmm3, %xmm4
movsd %xmm4, 0x2c0(%rsp)
movapd %xmm3, %xmm1
cmpl $0x0, 0x10(%rsp)
je 0x165e0
movsd 0x18(%rsp), %xmm1
movq 0x30(%rsp), %rax
movsd 0x38(%rax), %xmm5
ucomisd 0x1a1e(%rip), %xmm5     # 0x18010
movsd %xmm1, 0x18(%rsp)
jbe 0x1684c
movsd 0x1b8(%r13), %xmm0
ucomisd 0x1a01(%rip), %xmm0     # 0x18010
jbe 0x1684c
movsd 0x248(%rsp), %xmm0
movq 0x30(%rsp), %rax
mulsd 0x70(%rax), %xmm0
movapd %xmm2, 0x280(%rsp)
movapd %xmm3, 0x270(%rsp)
movsd %xmm4, 0x190(%rsp)
movapd %xmm5, 0x80(%rsp)
callq 0x7478
movapd 0x80(%rsp), %xmm1
mulsd %xmm0, %xmm1
movapd %xmm1, 0x80(%rsp)
movq 0x188(%rsp), %rdi
leaq 0x150(%rsp), %rsi
leaq 0x60(%rsp), %rbx
movq %rbx, %rdx
callq 0x775d
movsd 0x80(%r13), %xmm0
addsd 0x60(%rsp), %xmm0
movsd 0x228(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x230(%rsp), %xmm0
movsd %xmm1, 0x60(%rsp)
movsd 0x88(%r13), %xmm1
addsd 0x68(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x238(%rsp), %xmm0
movsd 0x90(%r13), %xmm1
addsd 0x70(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq %rbx, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x76cc
movapd %xmm0, 0x1d0(%rsp)
movaps 0x60(%rsp), %xmm0
movaps %xmm0, 0xb0(%rsp)
movaps 0xc0(%rsp), %xmm0
movaps %xmm0, 0x250(%rsp)
leaq 0xe0(%rsp), %rdi
leaq 0x170(%rsp), %rbx
movq %rbx, %rsi
callq 0x7741
movaps %xmm0, 0x260(%rsp)
movapd 0x1d0(%rsp), %xmm0
movapd 0x250(%rsp), %xmm1
movhpd 0xc8(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd %xmm0, %xmm1
movapd 0xb0(%rsp), %xmm0
movhpd 0x68(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
subpd %xmm1, %xmm0
movapd 0x80(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
mulpd %xmm0, %xmm1
movapd %xmm1, 0xb0(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x7741
movapd 0x260(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
movapd 0xb0(%rsp), %xmm0
divpd %xmm1, %xmm0
movapd %xmm0, 0xb0(%rsp)
movapd %xmm0, 0x120(%rsp)
movsd 0x70(%rsp), %xmm1
movapd 0x1d0(%rsp), %xmm0
mulsd 0xd0(%rsp), %xmm0
subsd %xmm0, %xmm1
mulsd 0x80(%rsp), %xmm1
movsd %xmm1, 0x80(%rsp)
leaq 0xe0(%rsp), %rdi
movq %rbx, %rsi
callq 0x7741
movsd 0x80(%rsp), %xmm6
movapd 0xb0(%rsp), %xmm5
movsd 0x190(%rsp), %xmm4
movapd 0x270(%rsp), %xmm3
movapd 0x280(%rsp), %xmm2
movsd 0x18(%rsp), %xmm1
divsd %xmm0, %xmm6
movsd %xmm6, 0x130(%rsp)
jmp 0x1686a
andq $0x0, 0x130(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x120(%rsp)
xorpd %xmm5, %xmm5
xorpd %xmm6, %xmm6
incl 0x10(%rsp)
movsd 0x28(%rsp), %xmm0
addsd %xmm3, %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
subsd %xmm1, %xmm3
mulsd %xmm3, %xmm3
movapd 0x140(%rsp), %xmm0
addsd %xmm3, %xmm0
movapd %xmm0, 0x140(%rsp)
addpd %xmm5, %xmm2
movapd %xmm2, 0x1e0(%rsp)
addsd %xmm6, %xmm4
movsd %xmm4, 0x1f0(%rsp)
leaq 0x150(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
leaq 0x290(%rsp), %rdx
callq 0x775d
xorl %eax, %eax
cmpq $0x3, %rax
je 0x1693b
movsd 0x158(%r13,%rax,8), %xmm0
movsd 0x170(%r13,%rax,8), %xmm1
addsd 0x2b0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x158(%r13,%rax,8)
addsd 0x120(%rsp,%rax,8), %xmm1
movsd %xmm1, 0x170(%r13,%rax,8)
movsd 0x188(%r13,%rax,8), %xmm0
addsd 0x290(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x188(%r13,%rax,8)
incq %rax
jmp 0x168d9
movl %ebp, %edi
incl %r12d
jmp 0x16429
incl %r15d
jmp 0x16419
incl %edi
jmp 0x1640a
cmpl $0x0, 0xc(%rsp)
je 0x1627c
cmpl $0x0, 0x10(%rsp)
je 0x1627c
movsd 0x1c0(%r13), %xmm0
cvtsi2sdl 0xc(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd 0x28(%rsp), %xmm3
movapd %xmm3, %xmm1
cvtsi2sdl 0x10(%rsp), %xmm2
mulsd %xmm3, %xmm1
divsd %xmm2, %xmm1
movapd 0x140(%rsp), %xmm4
subsd %xmm1, %xmm4
divsd %xmm2, %xmm4
divsd %xmm2, %xmm3
movsd 0x18(%rsp), %xmm2
addsd %xmm3, %xmm2
movapd %xmm2, %xmm1
mulsd 0x8450(%rip), %xmm1     # 0x1ee10
mulsd %xmm2, %xmm1
cmpltsd %xmm1, %xmm4
andnpd %xmm0, %xmm4
pushq $-0x18
popq %rax
testq %rax, %rax
je 0x1627c
movsd 0x170(%r13,%rax), %xmm0
movsd 0x188(%r13,%rax), %xmm1
movsd 0x1a0(%r13,%rax), %xmm2
movapd 0x4b11(%rip), %xmm3     # 0x1b510
xorpd %xmm3, %xmm0
mulsd %xmm4, %xmm0
movsd %xmm0, 0x170(%r13,%rax)
xorpd %xmm3, %xmm1
mulsd %xmm4, %xmm1
movsd %xmm1, 0x188(%r13,%rax)
xorpd %xmm3, %xmm2
mulsd %xmm4, %xmm2
movsd %xmm2, 0x1a0(%r13,%rax)
addq $0x8, %rax
jmp 0x169d0
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)