// File: jamesliu516[P]amrdg2d/main.cpp
// Function: main
int main()
{
    clock_t start, finish; 
    double duration; 

    bool is_stop=false; 
    bool b_in_set=false;

    int nstp=0;
    int lscc=0; 
    int c_amr=1;
    string str123("_");
    double tpr0,tpr1;
    bool btpr0=true, btpr1=true;
    bool bl00=false,bl11=false;
    tpr0=0.08;
    tpr1=0.125;

    if(!(nDOF==3||nDOF==6)) {
        cout<<"We only consider p1 or p2 polynomial.\nSo nDOF=3 or nDOF=6.\nOthers are wrong.\n";
        cout<<"You input nDOF="<<nDOF<<".\n";
        cout<<"The code stop here."<<endl;
        return 0;
    }
    // cout<<"please input a string to add the file name: ";
    // cin>>str123;

    out_para(str123);
    bodygrid=new OctCell[Nx*Ny+1];

    background_grid();

    pointsAtSolid(); 
    shapeTransfer();
    creatgrid(); 

    refineCicleDemain(localRefine); // 1,2,3,4,5�μ���  

    formListForAllGrid();  

    setFlagForAll();
    formListFaceCellAll();   
#if IS_TUBE==1
    set_exFlag4All(NPT_EXT_WALL, ext_wall);
    formExtWallInOutBndry();
#endif
    //  start = clock(); 
    face_vec();
    //   finish = clock();   
    //   duration = static_cast<double>(finish - start) / CLOCKS_PER_SEC;   
    //   cout<<duration<<" seconds-faces."<<endl; 

    initialDataGrid();
//    read4restart();
    initiallevel();
    //  start = clock();     
    point_vec();     
    outputcell_new(0);//output mesh
//    int j8;
  //  cin >>j8;
    //    finish = clock();  
    //   duration = static_cast<double>(finish - start) / CLOCKS_PER_SEC;   
    //   cout<<duration<<" seconds-output mesh modify.\n"<<endl;     

    //   start = clock();         
    //    outputcell(10);
    //    finish = clock();  
    //   duration = static_cast<double>(finish - start) / CLOCKS_PER_SEC;   
    //   cout<<duration<<" seconds-output mesh."<<endl;   
    
#if HAVE_MV!=0
    for(int wy=0; wy<N_BODY;++wy){
        for(int i_t=0;i_t<MVptNum[wy];++i_t){  
            MBsharp_point[wy][i_t]
                =MultiCircle[wy][n_sharp_point[wy][i_t]];

            find_multivalue_cell(MBsharp_point[wy][i_t]);
        }
    }
#endif
          
    computeNormalArea();
    form_inter_cell_set();
    wherePointAll();
    outputcell_sol(0,str123);
    // outputcell_new(0);
    for(int n=0; ; n++) {
#ifndef FIXED_DT
        timestep();
#else
        dt=fixed_dt;
#endif

#ifndef LOCAL_TIME         
        if(timesum+dt > t_print){
            dt=t_print-timesum;
            is_stop=true;
        }

        if(btpr0 && timesum+dt>tpr0){
            dt=tpr0-timesum;
            btpr0=false;
            bl00=true;
            CFL=0.034;
        }

        if(btpr1 && timesum+dt>tpr1){
            dt=tpr1-timesum;
            btpr1=false;
            bl11=true;
        }
#endif    
        //--------------------------------------------------------------
        runge_kutta(n);  
        if(n%20==0){
            cout<<"n: "<<n<<", dt="<<dt<<", time="<<timesum<<
                "  ";
            cal_te_res();
        }
        //---------------------------------------------------------------
#ifndef LOCAL_TIME       
        timesum+=dt;
        if( bl00 ) {			
            if(b_in_set){
                form_inter_cell_set();
                wherePointAll();
                b_in_set=false;
            }
            outputcell_sol(-time(NULL),"_t0p11");
            bl00=false;
        }

        if( bl11 ) {			
            if(b_in_set){
                form_inter_cell_set();
                wherePointAll();
                b_in_set=false;
            }
            outputcell_sol(-time(NULL),"_t0p2");
            bl11=false;
        }

        if(timesum>0.15)value_kxrcf_amr=1.0;
#endif
        if( (n%201 == 0&&n>=4000 )||(n<4000 && n%201==0&&n>0) ) {
//        if( (n%301 == 0 && n>0 ) ) {			
            lscc++;
            if(b_in_set){
                form_inter_cell_set();
                wherePointAll();
                b_in_set=false;
            }
            outputcell_sol(lscc,str123);
        }


     //   if(n==12000) output4restart();
        if(n%200==0 && n>=100 && SwAMR==true){
         //   if(n%1000==0 && n>=800 && SwAMR==true&& c_amr<=6){
    //    if(((n%200==0 && n>300 && n<2000)||(n%300==0&&n>2100) )
      //  if(((n%300==0&&n>200) )
        //    && SwAMR==true){
            SolutionAMR(c_amr);
            b_in_set=true;
            ++c_amr;
        }

        if(n>=max_time_steps&&n%500==0){
            cout<<"input an integer: 0 stop, !=0 continue to compute"<<endl;
            cin>>nstp;
            if(nstp==0) 
                is_stop=true;
        }

        if(is_stop) {
#ifndef LOCAL_TIME          
            cout<<"End time: "<<t_print<<endl;
#endif          
            if(b_in_set){
                form_inter_cell_set();
                wherePointAll();
                b_in_set=false;
            }
            outputcell_sol(-time(NULL));
            break;
        }
    }


    releaseList(); 
    releaseFaceCellList();
    delete [] bodygrid;     
    return 0;
    }

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movl $0x0, 0x1a4(%rsp)
movb $0x0, 0x187(%rsp)
movb $0x0, 0x186(%rsp)
movl $0x0, 0x180(%rsp)
movl $0x0, 0x17c(%rsp)
movl $0x1, 0x178(%rsp)
leaq 0x157(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
callq 0x3450
movq 0xa0(%rsp), %rdx
leaq 0x207ff(%rip), %rsi     # 0x3f2a6
leaq 0x158(%rsp), %rdi
callq 0x6330
jmp 0x1eab6
leaq 0x157(%rsp), %rdi
callq 0x32b0
movb $0x1, 0x12f(%rsp)
movb $0x1, 0x12e(%rsp)
movb $0x0, 0x12d(%rsp)
movb $0x0, 0x12c(%rsp)
movabsq $0x3fb47ae147ae147b, %rax # imm = 0x3FB47AE147AE147B
movq %rax, 0x138(%rsp)
movabsq $0x3fc0000000000000, %rax # imm = 0x3FC0000000000000
movq %rax, 0x130(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x38860
jmp 0x1eb16
movl $0x696a80, %edi         # imm = 0x696A80
callq 0x3030
movq %rax, 0x98(%rsp)
jmp 0x1eb2a
movq 0x98(%rsp), %rax
movq %rax, %rcx
addq $0x696a80, %rcx         # imm = 0x696A80
movq %rcx, 0x88(%rsp)
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x5100
jmp 0x1eb63
movq 0x88(%rsp), %rcx
movq 0x80(%rsp), %rax
addq $0x360, %rax            # imm = 0x360
cmpq %rcx, %rax
movq %rax, 0x90(%rsp)
jne 0x1eb4c
movq 0x98(%rsp), %rcx
leaq 0x2c7db(%rip), %rax     # 0x4b370
movq %rcx, (%rax)
callq 0x4b50
jmp 0x1eb9f
callq 0x15a00
jmp 0x1eba6
callq 0x15da0
jmp 0x1ebad
callq 0x1cc50
jmp 0x1ebb4
xorl %edi, %edi
callq 0x1cfc0
jmp 0x1ebbd
callq 0x1abd0
jmp 0x1ebc4
callq 0x78d0
jmp 0x1ebcb
callq 0x1b190
jmp 0x1ebd2
leaq 0x30737(%rip), %rsi     # 0x4f310
movl $0x138, %edi            # imm = 0x138
callq 0x7c70
jmp 0x1ebe5
callq 0x1ae80
jmp 0x1ebec
callq 0x11bb0
jmp 0x1ebf3
callq 0x35220
jmp 0x1ebfa
callq 0x1b210
jmp 0x1ec01
callq 0x8c40
jmp 0x1ec08
xorl %edi, %edi
callq 0xf640
jmp 0x1ec11
movl $0x0, 0x128(%rsp)
cmpl $0x1, 0x128(%rsp)
jge 0x1ed6a
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
movslq 0x128(%rsp), %rdx
leaq 0x20a55(%rip), %rcx     # 0x3f6a0
cmpl (%rcx,%rdx,4), %eax
jge 0x1ed52
movslq 0x128(%rsp), %r8
imulq $0x1f40, %r8, %rax      # imm = 0x1F40
leaq 0x2c7a6(%rip), %rsi     # 0x4b410
addq %rax, %rsi
movq %r8, %rax
shlq $0x5, %rax
leaq 0x20a35(%rip), %rcx     # 0x3f6b0
addq %rax, %rcx
movslq 0x124(%rsp), %rax
movslq (%rcx,%rax,4), %rdx
shlq $0x4, %rdx
shlq $0x7, %r8
leaq 0x31b47(%rip), %rdi     # 0x507e0
movq %rdi, %rcx
addq %r8, %rcx
shlq $0x4, %rax
movups (%rdx,%rsi), %xmm0
movups %xmm0, (%rax,%rcx)
movslq 0x128(%rsp), %rax
shlq $0x7, %rax
addq %rax, %rdi
movslq 0x124(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rdi
callq 0x3d630
jmp 0x1ecd0
jmp 0x1ecd2
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x1ec35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0x157(%rsp), %rdi
callq 0x32b0
jmp 0x1f49e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x1f491
movq 0x98(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
movl $0x696a80, %esi         # imm = 0x696A80
callq 0x34a0
jmp 0x1f491
jmp 0x1ed54
movl 0x128(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x128(%rsp)
jmp 0x1ec1c
callq 0x2d600
jmp 0x1ed71
callq 0x2d280
jmp 0x1ed78
callq 0x2d520
jmp 0x1ed7f
xorl %edi, %edi
leaq 0x158(%rsp), %rsi
callq 0xfb30
jmp 0x1ed90
movl $0x0, 0x120(%rsp)
callq 0x343d0
jmp 0x1eda2
leaq 0x319ff(%rip), %rax     # 0x507a8
movsd (%rax), %xmm0
leaq 0x319ec(%rip), %rax     # 0x507a0
addsd (%rax), %xmm0
movsd 0x20868(%rip), %xmm1    # 0x3f628
ucomisd %xmm1, %xmm0
jbe 0x1edec
movsd 0x2085a(%rip), %xmm0    # 0x3f628
leaq 0x319d3(%rip), %rax     # 0x507a8
subsd (%rax), %xmm0
leaq 0x319c0(%rip), %rax     # 0x507a0
movsd %xmm0, (%rax)
movb $0x1, 0x187(%rsp)
testb $0x1, 0x12f(%rsp)
je 0x1ee59
leaq 0x319ab(%rip), %rax     # 0x507a8
movsd (%rax), %xmm0
leaq 0x31998(%rip), %rax     # 0x507a0
addsd (%rax), %xmm0
ucomisd 0x138(%rsp), %xmm0
jbe 0x1ee59
movsd 0x138(%rsp), %xmm0
leaq 0x31981(%rip), %rax     # 0x507a8
subsd (%rax), %xmm0
leaq 0x3196e(%rip), %rax     # 0x507a0
movsd %xmm0, (%rax)
movb $0x0, 0x12f(%rsp)
movb $0x1, 0x12d(%rsp)
leaq 0x2c463(%rip), %rax     # 0x4b2b0
movsd 0x207db(%rip), %xmm0    # 0x3f630
movsd %xmm0, (%rax)
testb $0x1, 0x12e(%rsp)
je 0x1eeb3
leaq 0x3193e(%rip), %rax     # 0x507a8
movsd (%rax), %xmm0
leaq 0x3192b(%rip), %rax     # 0x507a0
addsd (%rax), %xmm0
ucomisd 0x130(%rsp), %xmm0
jbe 0x1eeb3
movsd 0x130(%rsp), %xmm0
leaq 0x31914(%rip), %rax     # 0x507a8
subsd (%rax), %xmm0
leaq 0x31901(%rip), %rax     # 0x507a0
movsd %xmm0, (%rax)
movb $0x0, 0x12e(%rsp)
movb $0x1, 0x12c(%rsp)
movl 0x120(%rsp), %edi
callq 0x2ebc0
jmp 0x1eec1
movl 0x120(%rsp), %eax
movl $0x14, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1ef8f
movq 0x2c0f0(%rip), %rdi     # 0x4afd0
leaq 0x20759(%rip), %rsi     # 0x3f640
callq 0x3240
movq %rax, 0x78(%rsp)
jmp 0x1eef3
movq 0x78(%rsp), %rdi
movl 0x120(%rsp), %esi
callq 0x3420
movq %rax, 0x70(%rsp)
jmp 0x1ef0b
movq 0x70(%rsp), %rdi
leaq 0x2072d(%rip), %rsi     # 0x3f644
callq 0x3240
movq %rax, 0x68(%rsp)
jmp 0x1ef23
movq 0x68(%rsp), %rdi
leaq 0x31871(%rip), %rax     # 0x507a0
movsd (%rax), %xmm0
callq 0x3480
movq %rax, 0x60(%rsp)
jmp 0x1ef3f
movq 0x60(%rsp), %rdi
leaq 0x206ff(%rip), %rsi     # 0x3f64a
callq 0x3240
movq %rax, 0x58(%rsp)
jmp 0x1ef57
movq 0x58(%rsp), %rdi
leaq 0x31845(%rip), %rax     # 0x507a8
movsd (%rax), %xmm0
callq 0x3480
movq %rax, 0x50(%rsp)
jmp 0x1ef73
movq 0x50(%rsp), %rdi
leaq 0x20d0e(%rip), %rsi     # 0x3fc8d
callq 0x3240
jmp 0x1ef86
callq 0x3d8b0
jmp 0x1ef8d
jmp 0x1ef8f
leaq 0x3180a(%rip), %rax     # 0x507a0
movsd (%rax), %xmm0
leaq 0x31807(%rip), %rax     # 0x507a8
addsd (%rax), %xmm0
leaq 0x317fc(%rip), %rax     # 0x507a8
movsd %xmm0, (%rax)
testb $0x1, 0x12d(%rsp)
je 0x1f09a
testb $0x1, 0x186(%rsp)
je 0x1efde
callq 0x2d280
jmp 0x1efcf
callq 0x2d520
jmp 0x1efd6
movb $0x0, 0x186(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3230
negl %eax
movl %eax, 0x44(%rsp)
leaq 0xff(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x3450
movq 0x48(%rsp), %rdx
leaq 0x20647(%rip), %rsi     # 0x3f652
leaq 0x100(%rsp), %rdi
callq 0x6330
jmp 0x1f01a
movl 0x44(%rsp), %edi
leaq 0x100(%rsp), %rsi
callq 0xfb30
jmp 0x1f02d
leaq 0x100(%rsp), %rdi
callq 0x34d8
leaq 0xff(%rsp), %rdi
callq 0x32b0
movb $0x0, 0x12d(%rsp)
jmp 0x1f09a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x1f088
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x34d8
leaq 0xff(%rsp), %rdi
callq 0x32b0
jmp 0x1f491
testb $0x1, 0x12c(%rsp)
je 0x1f184
testb $0x1, 0x186(%rsp)
je 0x1f0c8
callq 0x2d280
jmp 0x1f0b9
callq 0x2d520
jmp 0x1f0c0
movb $0x0, 0x186(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3230
negl %eax
movl %eax, 0x34(%rsp)
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x3450
movq 0x38(%rsp), %rdx
leaq 0x20564(%rip), %rsi     # 0x3f659
leaq 0xd8(%rsp), %rdi
callq 0x6330
jmp 0x1f104
movl 0x34(%rsp), %edi
leaq 0xd8(%rsp), %rsi
callq 0xfb30
jmp 0x1f117
leaq 0xd8(%rsp), %rdi
callq 0x34d8
leaq 0xd7(%rsp), %rdi
callq 0x32b0
movb $0x0, 0x12c(%rsp)
jmp 0x1f184
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x1f172
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x34d8
leaq 0xd7(%rsp), %rdi
callq 0x32b0
jmp 0x1f491
leaq 0x3161d(%rip), %rax     # 0x507a8
movsd (%rax), %xmm0
movsd 0x204a1(%rip), %xmm1    # 0x3f638
ucomisd %xmm1, %xmm0
jbe 0x1f1b0
leaq 0x2c0fc(%rip), %rax     # 0x4b2a0
movsd 0x1feac(%rip), %xmm0    # 0x3f058
movsd %xmm0, (%rax)
movl 0x120(%rsp), %eax
movl $0xc9, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1f1d1
cmpl $0xfa0, 0x120(%rsp) # imm = 0xFA0
jge 0x1f1fc
cmpl $0xfa0, 0x120(%rsp) # imm = 0xFA0
jge 0x1f245
movl 0x120(%rsp), %eax
movl $0xc9, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1f245
cmpl $0x0, 0x120(%rsp)
jle 0x1f245
movl 0x17c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x17c(%rsp)
testb $0x1, 0x186(%rsp)
je 0x1f22d
callq 0x2d280
jmp 0x1f21e
callq 0x2d520
jmp 0x1f225
movb $0x0, 0x186(%rsp)
movl 0x17c(%rsp), %edi
leaq 0x158(%rsp), %rsi
callq 0xfb30
jmp 0x1f243
jmp 0x1f245
movl 0x120(%rsp), %eax
movl $0xc8, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1f28a
cmpl $0x64, 0x120(%rsp)
jl 0x1f28a
movl 0x178(%rsp), %edi
callq 0x3d470
jmp 0x1f271
movb $0x1, 0x186(%rsp)
movl 0x178(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x178(%rsp)
cmpl $0x13880, 0x120(%rsp) # imm = 0x13880
jl 0x1f302
movl 0x120(%rsp), %eax
movl $0x1f4, %ecx            # imm = 0x1F4
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1f302
movq 0x2bd1e(%rip), %rdi     # 0x4afd0
leaq 0x203a6(%rip), %rsi     # 0x3f65f
callq 0x3240
movq %rax, 0x28(%rsp)
jmp 0x1f2c5
movq 0x28(%rsp), %rdi
movq 0x2bce7(%rip), %rsi     # 0x4afb8
callq 0x3290
jmp 0x1f2d8
movq 0x2bd01(%rip), %rdi     # 0x4afe0
leaq 0x180(%rsp), %rsi
callq 0x3160
jmp 0x1f2ee
cmpl $0x0, 0x180(%rsp)
jne 0x1f300
movb $0x1, 0x187(%rsp)
jmp 0x1f302
testb $0x1, 0x187(%rsp)
je 0x1f427
movq 0x2bcb9(%rip), %rdi     # 0x4afd0
leaq 0x20373(%rip), %rsi     # 0x3f691
callq 0x3240
movq %rax, 0x20(%rsp)
jmp 0x1f32a
movq 0x20(%rsp), %rdi
movsd 0x202f1(%rip), %xmm0    # 0x3f628
callq 0x3480
movq %rax, 0x18(%rsp)
jmp 0x1f343
movq 0x18(%rsp), %rdi
movq 0x2bc69(%rip), %rsi     # 0x4afb8
callq 0x3290
jmp 0x1f356
testb $0x1, 0x186(%rsp)
je 0x1f376
callq 0x2d280
jmp 0x1f367
callq 0x2d520
jmp 0x1f36e
movb $0x0, 0x186(%rsp)
xorl %eax, %eax
movl %eax, %edi
callq 0x3230
negl %eax
movl %eax, 0xc(%rsp)
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x3450
movq 0x10(%rsp), %rdx
leaq 0x202f9(%rip), %rsi     # 0x3f69c
leaq 0xb0(%rsp), %rdi
callq 0x6330
jmp 0x1f3b2
movl 0xc(%rsp), %edi
leaq 0xb0(%rsp), %rsi
callq 0xfb30
jmp 0x1f3c5
leaq 0xb0(%rsp), %rdi
callq 0x34d8
leaq 0xaf(%rsp), %rdi
callq 0x32b0
jmp 0x1f43f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
jmp 0x1f418
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0xb0(%rsp), %rdi
callq 0x34d8
leaq 0xaf(%rsp), %rdi
callq 0x32b0
jmp 0x1f491
jmp 0x1f429
movl 0x120(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x120(%rsp)
jmp 0x1ed9b
callq 0x1b260
jmp 0x1f446
callq 0x1b2f0
jmp 0x1f44d
leaq 0x2bf1c(%rip), %rax     # 0x4b370
movq (%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x1f46a
movq (%rsp), %rdi
callq 0x3340
movl $0x0, 0x1a4(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x34d8
movl 0x1a4(%rsp), %eax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
leaq 0x158(%rsp), %rdi
callq 0x34d8
movq 0x148(%rsp), %rdi
callq 0x3440
nopl (%rax,%rax)