// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm0, 0x30(%rsp)
movq %rdi, %r14
leaq 0xc(%rsp), %rax
andl $0x0, (%rax)
movq %rsi, 0x28(%rsp)
movsd (%rsi), %xmm0
leaq 0x10(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0x10(%rdi), %r13
movsd 0x10(%rdi), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0x20(%rdi), %rbp
movq (%rdi), %rsi
leaq 0x8(%rdi), %rbx
movq %rbp, %rdi
movq %rbx, %rdx
callq 0x4654
leaq 0x38(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0xb0(%r14), %r15
leaq 0x1c(%rsp), %r12
movsd 0x3099(%rip), %xmm0     # 0x61a8
ucomisd (%r13), %xmm0
ja 0x333e
movq %r14, %rdi
callq 0x2ca4
movq %r14, %rdi
callq 0x2d1e
movq (%r14), %rdx
movq 0x48(%r14), %rsi
movq 0x58(%r14), %rcx
movsd 0xb0(%r14), %xmm0
movq %r14, %rdi
callq 0x2ea6
movq %r14, %rdi
callq 0x2fae
andl $0x0, 0x18(%rsp)
incl 0x18(%rsp)
movq %r14, %rdi
callq 0x2ce8
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rsi
movq 0x68(%r14), %rcx
movsd 0xb0(%r14), %xmm0
addsd 0x3033(%rip), %xmm0     # 0x61b0
movq %r14, %rdi
callq 0x2ea6
movl 0x20(%r14), %eax
movq 0x48(%r14), %rcx
movq 0x40(%r14), %rdx
movq 0x38(%r14), %rsi
testl %eax, %eax
movl $0x0, %edi
cmovlel %edi, %eax
xorl %edi, %edi
cmpq %rdi, %rax
je 0x31bf
movsd (%rcx,%rdi,8), %xmm0
subsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
andq $0x0, (%rsi,%rdi,8)
incq %rdi
jmp 0x31a1
movq 0x28(%r14), %rdi
movq 0x30(%r14), %rdx
movq 0x60(%r14), %rcx
movq 0x68(%r14), %r8
movq %rbp, %rsi
movq %r15, %r9
pushq %r13
pushq %rbx
callq 0x3f84
popq %rax
popq %rcx
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdi
movq 0x38(%r14), %rdx
movq %rbp, %rcx
callq 0x3e04
movq (%r14), %rsi
movq 0x30(%r14), %rdx
movq 0x38(%r14), %rcx
movq %rbp, %rdi
movq %r13, %r8
leaq 0x10(%rsp), %r9
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x20(%rsp), %rax
pushq %rax
pushq %r12
leaq 0x24(%rsp), %rax
pushq %rax
callq 0x4930
addq $0x20, %rsp
cmpl $0x1, %eax
je 0x3107
cmpl $0x2, %eax
je 0x3359
movsd 0x40(%rsp), %xmm0
ucomisd 0xa8(%r14), %xmm0
ja 0x3154
andl $0x0, 0x1c(%rsp)
movq (%r14), %rdx
movq 0x30(%r14), %rcx
movq %rbp, %rdi
leaq 0x38(%rsp), %rsi
movq %rbx, %r8
leaq 0x10(%rsp), %r9
callq 0x4675
movq (%r14), %rsi
movq 0x30(%r14), %rdx
movq 0x78(%r14), %rcx
movq %rbp, %rdi
movq %r13, %r8
movsd 0x20(%rsp), %xmm0
leaq 0xc(%rsp), %r9
callq 0x47ee
cmpl $0x1, %eax
je 0x3107
movsd (%r13), %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x65, 0xc(%rsp)
jl 0x32de
movsd 0x2efb(%rip), %xmm0     # 0x61b8
ucomisd %xmm1, %xmm0
jbe 0x32de
andl $0x0, 0xc(%rsp)
movapd %xmm1, %xmm0
mulsd 0x2ee4(%rip), %xmm0     # 0x61b8
addsd %xmm1, %xmm0
movsd %xmm0, (%r13)
movl 0x20(%r14), %eax
movq (%r14), %rcx
movq 0x78(%r14), %rdx
testl %eax, %eax
movl $0x0, %esi
cmovlel %esi, %eax
movq 0x30(%r14), %rsi
xorl %edi, %edi
cmpq %rdi, %rax
je 0x3317
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, (%rdx,%rdi,8)
movsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
jmp 0x32f9
movsd %xmm1, 0x20(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x3107
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax)
jmp 0x3359
movq 0x6c93(%rip), %rdi      # 0x9fd8
leaq 0x2e9c(%rip), %rsi      # 0x61e8
callq 0x21b0
movq %rax, %rdi
callq 0x20a0
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq