// File: /workspace/github_cpp_makefile_star2_O1/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
movl $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 0x4cbf
leaq 0x38(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0xb0(%r14), %r15
movsd 0x3e2d(%rip), %xmm1     # 0x7188
leaq 0x1c(%rsp), %r12
ucomisd (%r13), %xmm1
ja 0x35d7
movq %r14, %rdi
callq 0x2ec8
movq %r14, %rdi
callq 0x2f4c
movq (%r14), %rdx
movq 0x48(%r14), %rsi
movq 0x58(%r14), %rcx
movsd 0xb0(%r14), %xmm0
movq %r14, %rdi
callq 0x30e2
movq %r14, %rdi
callq 0x31f2
movl $0x0, 0x18(%rsp)
incl 0x18(%rsp)
movslq (%rbp), %rax
testq %rax, %rax
jle 0x33e3
movq 0x30(%r14), %rcx
movq 0x60(%r14), %rdx
movq 0x68(%r14), %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rdx,%rdi,8)
mulsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x33c3
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rsi
movq 0x68(%r14), %rcx
movsd 0xb0(%r14), %xmm0
addsd 0x3d90(%rip), %xmm0     # 0x7190
movq %r14, %rdi
callq 0x30e2
movslq 0x20(%r14), %rax
testq %rax, %rax
jle 0x343e
movq 0x48(%r14), %rcx
movq 0x38(%r14), %rdx
movq 0x40(%r14), %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
subsd (%rsi,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
movq $0x0, (%rdx,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x341f
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 0x45e4
addq $0x10, %rsp
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdi
movq 0x38(%r14), %rdx
movq %rbp, %rcx
callq 0x444c
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 0x5060
addq $0x20, %rsp
cmpl $0x1, %eax
je 0x3514
cmpl $0x2, %eax
je 0x3619
movsd 0x40(%rsp), %xmm0
ucomisd 0xa8(%r14), %xmm0
ja 0x33a8
movl $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 0x4cdf
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 0x4ee0
cmpl $0x1, %eax
jne 0x352d
movsd 0x3c6c(%rip), %xmm0     # 0x7188
ucomisd (%r13), %xmm0
jbe 0x336c
jmp 0x35d7
movsd (%r13), %xmm1
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x65, 0xc(%rsp)
jl 0x3576
movsd 0x3c46(%rip), %xmm0     # 0x7198
ucomisd %xmm1, %xmm0
jbe 0x3576
movl $0x0, 0xc(%rsp)
movapd %xmm1, %xmm0
mulsd 0x3c2c(%rip), %xmm0     # 0x7198
addsd %xmm1, %xmm0
movsd %xmm0, (%r13)
movsd %xmm1, 0x20(%rsp)
movslq (%rbp), %rax
testq %rax, %rax
jle 0x35ae
movq (%r14), %rcx
movq 0x30(%r14), %rdx
movq 0x78(%r14), %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
movsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x3592
movsd 0x10(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x3bc2(%rip), %xmm1     # 0x7188
jae 0x3360
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax)
jmp 0x3619
movq 0x79fa(%rip), %rbx      # 0xafd8
leaq 0x3bfb(%rip), %rsi      # 0x71e0
movl $0x10, %edx
movq %rbx, %rdi
callq 0x2200
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21c0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2110
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq