// File: jamesliu516[P]amrdg2d/tvbm_limiter4all.cpp
// Function: pos_limiter(bodygridclass*)
void pos_limiter(OctCell *pc)
{
    double dx,dy,xc,yc;
    double dof[4][6]={0.0};
    double cgsx[3];
    double cgsy[3];
    
    double cgslbx[4]={0.0};
    double cgslby[4]={0.0};

    double gslbwei[4]={0.0};
    double gslbpt[4]={0.0};
    double qmin=0.0, rtp,tht1,tht2, pre0;
    double u, v, gma1;
    double pre1;
    double ush[4],ujb[4];

    dx=pc->dx;
    dy=pc->dy;
    xc=pc->xc1;
    yc=pc->yc1;

    for(int i=0;i<4;i++) 
        for(int j=0; j< nDOF; j++) dof[i][j]=pc->dof[i][j];


    if(NLBpt==3){
        for(int i=0; i<3;++i){
            gslbwei[i]=weiLB3[i];
            gslbpt[i]=gsLB3pt[i];
        }
    }
    else{ 
        for(int i=0; i<4;++i){
            gslbwei[i]=weiLB4[i];
            gslbpt[i]=gsLB4pt[i];
        }
    }

    for(int i=0; i<3;++i){
        cgsx[i]=xc+0.5*dx*gspt[i];
        cgsy[i]=yc+0.5*dy*gspt[i];
    }

    for(int i=0; i<NLBpt;++i){
        cgslbx[i]=xc+0.5*dx*gslbpt[i];
        cgslby[i]=yc+0.5*dy*gslbpt[i];
    }

//limit density
    if(dof[0][0]<epsPos){
    //    dof[0][0]=epsPos;
        for(int mm=1;mm<nDOF;++mm)
            dof[0][mm]=0.0;
    }
    else{
        qmin=1.0e20;
        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                rtp=n_approx_sol(cgslbx[j],cgsy[i],
                    xc,yc,dx,dy, dof[0],nDOF);
                if(rtp<qmin) qmin=rtp;
            }
        }

        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                rtp=n_approx_sol(cgsx[i],cgslby[j],
                    xc,yc,dx,dy, dof[0],nDOF);
                if(rtp<qmin) qmin=rtp;
            }
        }

        tht1=min2(fabs((dof[0][0]-epsPos)/(dof[0][0]-qmin)), 1.0);

        for(int i=1;i<nDOF;++i)
            dof[0][i]*=tht1;
    }


    gma1=GAMMA-1.0;

    u=dof[1][0]/dof[0][0];
    v=dof[2][0]/dof[0][0];

    pre0=gma1*(dof[3][0]-0.5*dof[0][0]*(u*u+v*v));

    if(pre0<epsPos){
        for(int i=0;i<4;++i){
            for(int j=1;j<nDOF;j++){
                dof[i][j]=0.0;
            }
        }
    }
    else{
        tht2=1.0e20;
        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                for(int a=0;a<4;++a){
                    ush[a]=n_approx_sol(cgsx[i],cgslby[j],
                        xc,yc,dx,dy, dof[a],nDOF);
                }
                shbl2jbbl(ush,ujb);
                pre1=ujb[3];
                if(pre1>epsPos){
                    rtp=1.0;
                }
                else{
                    rtp=(pre0-epsPos)/(pre0-pre1);
                }

                if(rtp<tht2) tht2=rtp;
            }
        }


        for(int i=0;i<3;++i){
            for(int j=0;j<NLBpt;++j){
                for(int a=0;a<4;++a){
                    ush[a]=n_approx_sol(cgslbx[j],cgsy[i],
                        xc,yc,dx,dy, dof[a],nDOF);
                }
                shbl2jbbl(ush,ujb);
                pre1=ujb[3];
                if(pre1>epsPos){
                    rtp=1.0;
                }
                else{
                    rtp=(pre0-epsPos)/(pre0-pre1);
                }

                if(rtp<tht2) tht2=rtp;
            }
        }

        for(int j=0;j<4;++j)
            for(int i=1;i<nDOF;++i)
                dof[j][i]*=tht2;
    }

    for(int i=0;i<4;i++) 
        for(int j=1; j< nDOF; j++) pc->dof[i][j]= dof[i][j];
}

// Assembly:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x2a0(%rsp)
leaq 0x1c0(%rsp), %rdi
xorl %esi, %esi
movl $0xc0, %edx
callq 0x3150
leaq 0x160(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3150
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3150
leaq 0x120(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3150
leaq 0x100(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x3150
xorps %xmm0, %xmm0
movsd %xmm0, 0xf8(%rsp)
movq 0x2a0(%rsp), %rax
movsd 0x288(%rax), %xmm0
movsd %xmm0, 0x298(%rsp)
movq 0x2a0(%rsp), %rax
movsd 0x290(%rax), %xmm0
movsd %xmm0, 0x290(%rsp)
movq 0x2a0(%rsp), %rax
movsd 0x278(%rax), %xmm0
movsd %xmm0, 0x288(%rsp)
movq 0x2a0(%rsp), %rax
movsd 0x280(%rax), %xmm0
movsd %xmm0, 0x280(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x4, 0x6c(%rsp)
jge 0x3745d
movl $0x0, 0x68(%rsp)
cmpl $0x6, 0x68(%rsp)
jge 0x3744e
movq 0x2a0(%rsp), %rax
addq $0x30, %rax
movslq 0x6c(%rsp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x68(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movslq 0x6c(%rsp), %rcx
leaq 0x1c0(%rsp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x68(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x373fa
jmp 0x37450
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x373eb
movl $0x0, 0x64(%rsp)
cmpl $0x3, 0x64(%rsp)
jge 0x374b7
movslq 0x64(%rsp), %rcx
leaq 0x84f8(%rip), %rax      # 0x3f970
movsd (%rax,%rcx,8), %xmm0
movslq 0x64(%rsp), %rax
movsd %xmm0, 0x120(%rsp,%rax,8)
movslq 0x64(%rsp), %rcx
leaq 0x84f9(%rip), %rax      # 0x3f990
movsd (%rax,%rcx,8), %xmm0
movslq 0x64(%rsp), %rax
movsd %xmm0, 0x100(%rsp,%rax,8)
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x37465
movl $0x0, 0x60(%rsp)
cmpl $0x3, 0x60(%rsp)
jge 0x3755c
movsd 0x288(%rsp), %xmm1
movsd 0x7c65(%rip), %xmm0     # 0x3f140
mulsd 0x298(%rsp), %xmm0
movslq 0x60(%rsp), %rcx
leaq 0x84c0(%rip), %rax      # 0x3f9b0
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq 0x60(%rsp), %rax
movsd %xmm0, 0x1a0(%rsp,%rax,8)
movsd 0x280(%rsp), %xmm1
movsd 0x7c24(%rip), %xmm0     # 0x3f140
mulsd 0x290(%rsp), %xmm0
movslq 0x60(%rsp), %rcx
leaq 0x847f(%rip), %rax      # 0x3f9b0
movsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq 0x60(%rsp), %rax
movsd %xmm0, 0x180(%rsp,%rax,8)
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x374bf
movl $0x0, 0x5c(%rsp)
cmpl $0x3, 0x5c(%rsp)
jge 0x375fb
movsd 0x288(%rsp), %xmm1
movsd 0x7bc0(%rip), %xmm0     # 0x3f140
mulsd 0x298(%rsp), %xmm0
movslq 0x5c(%rsp), %rax
movsd 0x100(%rsp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq 0x5c(%rsp), %rax
movsd %xmm0, 0x160(%rsp,%rax,8)
movsd 0x280(%rsp), %xmm1
movsd 0x7b82(%rip), %xmm0     # 0x3f140
mulsd 0x290(%rsp), %xmm0
movslq 0x5c(%rsp), %rax
movsd 0x100(%rsp,%rax,8), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movslq 0x5c(%rsp), %rax
movsd %xmm0, 0x140(%rsp,%rax,8)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x37564
movsd 0x1c0(%rsp), %xmm1
leaq 0x13c9d(%rip), %rax     # 0x4b2a8
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37647
movl $0x1, 0x58(%rsp)
cmpl $0x6, 0x58(%rsp)
jge 0x37642
movslq 0x58(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x1c0(%rsp,%rax,8)
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x3761d
jmp 0x378d3
movsd 0x8319(%rip), %xmm0     # 0x3f968
movsd %xmm0, 0xf8(%rsp)
movl $0x0, 0x54(%rsp)
cmpl $0x3, 0x54(%rsp)
jge 0x37727
movl $0x0, 0x50(%rsp)
cmpl $0x3, 0x50(%rsp)
jge 0x37715
movslq 0x50(%rsp), %rax
movsd 0x160(%rsp,%rax,8), %xmm0
movslq 0x54(%rsp), %rax
movsd 0x180(%rsp,%rax,8), %xmm1
movsd 0x288(%rsp), %xmm2
movsd 0x280(%rsp), %xmm3
movsd 0x298(%rsp), %xmm4
movsd 0x290(%rsp), %xmm5
leaq 0x1c0(%rsp), %rdi
movl $0x6, %esi
callq 0x1f600
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm1
movsd 0xf8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37703
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xf8(%rsp)
jmp 0x37705
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x37673
jmp 0x37717
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x37660
movl $0x0, 0x4c(%rsp)
cmpl $0x3, 0x4c(%rsp)
jge 0x377f6
movl $0x0, 0x48(%rsp)
cmpl $0x3, 0x48(%rsp)
jge 0x377e4
movslq 0x4c(%rsp), %rax
movsd 0x1a0(%rsp,%rax,8), %xmm0
movslq 0x48(%rsp), %rax
movsd 0x140(%rsp,%rax,8), %xmm1
movsd 0x288(%rsp), %xmm2
movsd 0x280(%rsp), %xmm3
movsd 0x298(%rsp), %xmm4
movsd 0x290(%rsp), %xmm5
leaq 0x1c0(%rsp), %rdi
movl $0x6, %esi
callq 0x1f600
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm1
movsd 0xf8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x377d2
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xf8(%rsp)
jmp 0x377d4
movl 0x48(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x48(%rsp)
jmp 0x37742
jmp 0x377e6
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x3772f
movsd 0x1c0(%rsp), %xmm1
leaq 0x13aa2(%rip), %rax     # 0x4b2a8
subsd (%rax), %xmm1
movsd 0x1c0(%rsp), %xmm0
subsd 0xf8(%rsp), %xmm0
divsd %xmm0, %xmm1
movaps 0x79d9(%rip), %xmm0     # 0x3f200
pand %xmm0, %xmm1
movsd 0x7825(%rip), %xmm0     # 0x3f058
ucomisd %xmm1, %xmm0
jbe 0x37876
movsd 0x1c0(%rsp), %xmm0
leaq 0x13a5f(%rip), %rax     # 0x4b2a8
subsd (%rax), %xmm0
movsd 0x1c0(%rsp), %xmm1
subsd 0xf8(%rsp), %xmm1
divsd %xmm1, %xmm0
movaps 0x7996(%rip), %xmm1     # 0x3f200
pand %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
jmp 0x37886
movsd 0x77da(%rip), %xmm0     # 0x3f058
movsd %xmm0, 0x8(%rsp)
jmp 0x37886
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movl $0x1, 0x44(%rsp)
cmpl $0x6, 0x44(%rsp)
jge 0x378d1
movsd 0xe8(%rsp), %xmm0
movslq 0x44(%rsp), %rax
mulsd 0x1c0(%rsp,%rax,8), %xmm0
movsd %xmm0, 0x1c0(%rsp,%rax,8)
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x3789d
jmp 0x378d3
movsd 0x790d(%rip), %xmm0     # 0x3f1e8
movsd %xmm0, 0xc0(%rsp)
movsd 0x1f0(%rsp), %xmm0
divsd 0x1c0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x220(%rsp), %xmm0
divsd 0x1c0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc0(%rsp), %xmm0
movsd 0x250(%rsp), %xmm2
movsd 0x780c(%rip), %xmm1     # 0x3f140
mulsd 0x1c0(%rsp), %xmm1
movsd 0xd0(%rsp), %xmm3
movsd 0xd0(%rsp), %xmm5
movsd 0xc8(%rsp), %xmm4
mulsd 0xc8(%rsp), %xmm4
mulsd %xmm5, %xmm3
addsd %xmm4, %xmm3
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0xd8(%rsp), %xmm1
leaq 0x13903(%rip), %rax     # 0x4b2a8
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37a0f
movl $0x0, 0x40(%rsp)
cmpl $0x4, 0x40(%rsp)
jge 0x37a0a
movl $0x1, 0x3c(%rsp)
cmpl $0x6, 0x3c(%rsp)
jge 0x379fb
movslq 0x40(%rsp), %rcx
leaq 0x1c0(%rsp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x3c(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x379c6
jmp 0x379fd
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x379b7
jmp 0x37d80
movsd 0x7f51(%rip), %xmm0     # 0x3f968
movsd %xmm0, 0xe0(%rsp)
movl $0x0, 0x38(%rsp)
cmpl $0x3, 0x38(%rsp)
jge 0x37b9c
movl $0x0, 0x34(%rsp)
cmpl $0x3, 0x34(%rsp)
jge 0x37b8a
movl $0x0, 0x30(%rsp)
cmpl $0x4, 0x30(%rsp)
jge 0x37ace
movslq 0x38(%rsp), %rax
movsd 0x1a0(%rsp,%rax,8), %xmm0
movslq 0x34(%rsp), %rax
movsd 0x140(%rsp,%rax,8), %xmm1
movsd 0x288(%rsp), %xmm2
movsd 0x280(%rsp), %xmm3
movsd 0x298(%rsp), %xmm4
movsd 0x290(%rsp), %xmm5
movslq 0x30(%rsp), %rax
leaq 0x1c0(%rsp), %rdi
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x6, %esi
callq 0x1f600
movslq 0x30(%rsp), %rax
movsd %xmm0, 0x90(%rsp,%rax,8)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x37a4e
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2e130
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x137a6(%rip), %rax     # 0x4b2a8
ucomisd (%rax), %xmm0
jbe 0x37b1b
movsd 0x7548(%rip), %xmm0     # 0x3f058
movsd %xmm0, 0xf0(%rsp)
jmp 0x37b4e
movsd 0xd8(%rsp), %xmm0
leaq 0x1377d(%rip), %rax     # 0x4b2a8
subsd (%rax), %xmm0
movsd 0xd8(%rsp), %xmm1
subsd 0xb8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37b78
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x37b7a
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x37a3b
jmp 0x37b8c
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x37a28
movl $0x0, 0x2c(%rsp)
cmpl $0x3, 0x2c(%rsp)
jge 0x37d18
movl $0x0, 0x28(%rsp)
cmpl $0x3, 0x28(%rsp)
jge 0x37d06
movl $0x0, 0x24(%rsp)
cmpl $0x4, 0x24(%rsp)
jge 0x37c4a
movslq 0x28(%rsp), %rax
movsd 0x160(%rsp,%rax,8), %xmm0
movslq 0x2c(%rsp), %rax
movsd 0x180(%rsp,%rax,8), %xmm1
movsd 0x288(%rsp), %xmm2
movsd 0x280(%rsp), %xmm3
movsd 0x298(%rsp), %xmm4
movsd 0x290(%rsp), %xmm5
movslq 0x24(%rsp), %rax
leaq 0x1c0(%rsp), %rdi
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x6, %esi
callq 0x1f600
movslq 0x24(%rsp), %rax
movsd %xmm0, 0x90(%rsp,%rax,8)
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x37bca
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2e130
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xb8(%rsp), %xmm0
leaq 0x1362a(%rip), %rax     # 0x4b2a8
ucomisd (%rax), %xmm0
jbe 0x37c97
movsd 0x73cc(%rip), %xmm0     # 0x3f058
movsd %xmm0, 0xf0(%rsp)
jmp 0x37cca
movsd 0xd8(%rsp), %xmm0
leaq 0x13601(%rip), %rax     # 0x4b2a8
subsd (%rax), %xmm0
movsd 0xd8(%rsp), %xmm1
subsd 0xb8(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x37cf4
movsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
jmp 0x37cf6
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x37bb7
jmp 0x37d08
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x37ba4
movl $0x0, 0x20(%rsp)
cmpl $0x4, 0x20(%rsp)
jge 0x37d7e
movl $0x1, 0x1c(%rsp)
cmpl $0x6, 0x1c(%rsp)
jge 0x37d6f
movsd 0xe0(%rsp), %xmm0
movslq 0x20(%rsp), %rcx
leaq 0x1c0(%rsp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x1c(%rsp), %rcx
mulsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x37d2f
jmp 0x37d71
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x37d20
jmp 0x37d80
movl $0x0, 0x18(%rsp)
cmpl $0x4, 0x18(%rsp)
jge 0x37dfa
movl $0x1, 0x14(%rsp)
cmpl $0x6, 0x14(%rsp)
jge 0x37deb
movslq 0x18(%rsp), %rcx
leaq 0x1c0(%rsp), %rax
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x2a0(%rsp), %rax
addq $0x30, %rax
movslq 0x18(%rsp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movslq 0x14(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x14(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x37d97
jmp 0x37ded
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x37d88
addq $0x2a8, %rsp            # imm = 0x2A8
retq
nopw %cs:(%rax,%rax)