// File: snesic[P]stochastic_thin_films/film.cpp
// Function: film::simulation(double, double&, long*)
int film::simulation(double t, double& t0, long int *inic)
{   int i;
    int no_newton_it, no_consecutive_neg, no_good_solutions;
    double jdt,dt_old;
    double maxq, vol_0;
    no_good_solutions=0;
    jdt = t0;
    dt_old = dt;
    
    vol_0 = calculate_volume(L, h, dx);
    
    
    do
    {
        // Simulation begins
    ponovo:
        
        if(dt<1e-15) {cout<< "Wrong parameters" << endl; goto kraj;}
        
        set_h2_3();
        
        create_noise(inic);
        create_funcional_st(r_fix, h, h3,theta);
        create_funcional_add_vdw_noise();
        
        //--------Implicit iteration process - Newton-Raphson method----------------
        
        no_newton_it=0;
        do {
            no_newton_it++;
            set_y2_3();
            
            create_funcional_st(r, y, y3,theta-1);
            
            for(i=0;i<L;i++)
            {   r[i]=r_fix[i]-r[i];
                q[i]=0;
            }
            
            calculate_matrix_A(a,L, y, y2, y3, theta, dx, dt);
            elimination(a,r,q,L); // Gauss elimination to calculate q from Aq=r;
            
            switch (check_newton_convergence(L, h, y, q, dt, jdt, no_good_solutions, no_consecutive_neg, no_newton_it, maxq))
            {
                case 0: break;
                case 1: goto ponovo;
                        break;
                case 2: goto kraj;
                        break;
            }
            
        } while(maxq>tol);
        
        no_consecutive_neg=0;
        
        //---End of Newton-Raphson iterative method-->
        //----Checking errors (vol conservation, second time derivative small enough)
        
        check_volume(L, vol_0, h, y, dx, jdt);
        if (check_second_time_derivative(L, h, y, old_sol, dt, dt_old, no_good_solutions) == 1 ) goto ponovo;
        
        //------------The solution is good enough - write in file-------------------------
        
        jdt=jdt+dt;
        dt_old=dt;
        
        if (no_good_solutions>100 && dt<1e-3 )   //Everything works fine => the time step increases
        {
            no_good_solutions=0;
            dt=dt+0.001*dt;
        }

        for(i=0;i<L;i++) { old_sol[i]=h[i]; h[i]=y[i]; }   //---New solution -> initial condition
        
    } while (jdt<=t);   //----------------- End of the simulation - writing data -----------------//
    
     t0=jdt;
    
kraj:
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x8(%rbp), %rdx
movq %rdx, -0x58(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x10(%rdx), %xmm0
movsd %xmm0, -0x40(%rbp)
movq %rdx, %rdi
addq $0x20, %rdi
movq (%rdx), %rsi
addq $0x8, %rdx
callq 0x8380
movsd %xmm0, -0x50(%rbp)
jmp 0x4efc
movq -0x58(%rbp), %rax
movsd 0x5258(%rip), %xmm0     # 0xa160
ucomisd 0x10(%rax), %xmm0
jbe 0x4f36
movq 0xa0c2(%rip), %rdi      # 0xefd8
leaq 0x525b(%rip), %rsi      # 0xa178
callq 0x3270
movq %rax, %rdi
movq 0xa09c(%rip), %rsi      # 0xefc8
callq 0x3290
jmp 0x5225
movq -0x58(%rbp), %rdi
callq 0x4720
movq -0x58(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x4850
movq -0x58(%rbp), %rdi
movq 0x48(%rdi), %rsi
movq (%rdi), %rdx
movq 0x58(%rdi), %rcx
movsd 0xb0(%rdi), %xmm0
callq 0x4a90
movq -0x58(%rbp), %rdi
callq 0x4ce0
movl $0x0, -0x28(%rbp)
movq -0x58(%rbp), %rdi
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
callq 0x47c0
movq -0x58(%rbp), %rdi
movq 0x40(%rdi), %rsi
movq 0x30(%rdi), %rdx
movq 0x68(%rdi), %rcx
movsd 0xb0(%rdi), %xmm0
movsd 0x505e(%rip), %xmm1     # 0xa008
subsd %xmm1, %xmm0
callq 0x4a90
movl $0x0, -0x24(%rbp)
movq -0x58(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x20(%rcx), %eax
jge 0x500c
movq -0x58(%rbp), %rax
movq 0x48(%rax), %rcx
movslq -0x24(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x40(%rax), %rcx
movslq -0x24(%rbp), %rdx
subsd (%rcx,%rdx,8), %xmm0
movq 0x40(%rax), %rcx
movslq -0x24(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x38(%rax), %rax
movslq -0x24(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4fba
movq -0x58(%rbp), %r11
movq %r11, %rsi
addq $0x20, %rsi
movq %rsi, -0x70(%rbp)
movq 0x28(%r11), %rdi
movq 0x30(%r11), %rdx
movq 0x60(%r11), %rcx
movq 0x68(%r11), %r8
movq %r11, %r9
addq $0xb0, %r9
movq %r11, %r10
addq $0x8, %r10
addq $0x10, %r11
movq %r11, -0x68(%rbp)
movq %rsp, %rax
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x7430
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rcx
movq 0x40(%rax), %rsi
movq 0x28(%rax), %rdi
movq 0x38(%rax), %rdx
callq 0x6fa0
movq -0x58(%rbp), %rax
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %r8
movq (%rax), %rsi
movq 0x30(%rax), %rdx
movq 0x38(%rax), %rcx
movq %rsp, %rax
leaq -0x48(%rbp), %r9
movq %r9, 0x18(%rax)
leaq -0x28(%rbp), %r9
movq %r9, 0x10(%rax)
leaq -0x2c(%rbp), %r9
movq %r9, 0x8(%rax)
leaq -0x30(%rbp), %r9
movq %r9, (%rax)
leaq -0x38(%rbp), %r9
callq 0x8850
movl %eax, %ecx
movl %ecx, -0x5c(%rbp)
testl %eax, %eax
je 0x50cd
jmp 0x50b9
movl -0x5c(%rbp), %eax
subl $0x1, %eax
je 0x50cf
jmp 0x50c3
movl -0x5c(%rbp), %eax
subl $0x2, %eax
je 0x50d4
jmp 0x50d9
jmp 0x50d9
jmp 0x4efc
jmp 0x5225
jmp 0x50db
movq -0x58(%rbp), %rax
movsd -0x48(%rbp), %xmm0
ucomisd 0xa8(%rax), %xmm0
ja 0x4f78
movq -0x58(%rbp), %r8
movl $0x0, -0x2c(%rbp)
movq %r8, %rdi
addq $0x20, %rdi
movq (%r8), %rdx
movq 0x30(%r8), %rcx
addq $0x8, %r8
leaq -0x50(%rbp), %rsi
leaq -0x38(%rbp), %r9
callq 0x83e0
movq -0x58(%rbp), %r8
movq %r8, %rdi
addq $0x20, %rdi
movq (%r8), %rsi
movq 0x30(%r8), %rdx
movq 0x78(%r8), %rcx
addq $0x10, %r8
movsd -0x40(%rbp), %xmm0
leaq -0x30(%rbp), %r9
callq 0x85c0
cmpl $0x1, %eax
jne 0x514e
jmp 0x4efc
movq -0x58(%rbp), %rax
movsd -0x38(%rbp), %xmm0
addsd 0x10(%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd 0x10(%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
cmpl $0x64, -0x30(%rbp)
jle 0x51ae
movq -0x58(%rbp), %rax
movsd 0x4feb(%rip), %xmm0     # 0xa168
ucomisd 0x10(%rax), %xmm0
jbe 0x51ae
movq -0x58(%rbp), %rax
movl $0x0, -0x30(%rbp)
movsd 0x10(%rax), %xmm1
movsd 0x10(%rax), %xmm0
movsd 0x4fc7(%rip), %xmm2     # 0xa168
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rax)
movl $0x0, -0x24(%rbp)
movq -0x58(%rbp), %rcx
movl -0x24(%rbp), %eax
cmpl 0x20(%rcx), %eax
jge 0x5202
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movslq -0x24(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq 0x78(%rax), %rcx
movslq -0x24(%rbp), %rdx
movsd %xmm0, (%rcx,%rdx,8)
movq 0x30(%rax), %rcx
movslq -0x24(%rbp), %rdx
movsd (%rcx,%rdx,8), %xmm0
movq (%rax), %rax
movslq -0x24(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x51b5
jmp 0x5204
movsd -0x38(%rbp), %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x4efa
movsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
movsd %xmm0, (%rax)
xorl %eax, %eax
addq $0x90, %rsp
popq %rbp
retq