// File: /workspace/github_cpp_makefile_star2_O3/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 0x4d0d
leaq 0x38(%rsp), %rax
movsd %xmm0, (%rax)
leaq 0xb0(%r14), %r15
movsd 0x3ea9(%rip), %xmm1     # 0x71d8
leaq 0x1c(%rsp), %r12
ucomisd (%r13), %xmm1
ja 0x35a3
movq %r14, %rdi
callq 0x2ea6
movq %r14, %rdi
callq 0x2f24
movq (%r14), %rdx
movq 0x48(%r14), %rsi
movq 0x58(%r14), %rcx
movsd 0xb0(%r14), %xmm0
movq %r14, %rdi
callq 0x30b4
movq %r14, %rdi
callq 0x31c6
movl $0x0, 0x18(%rsp)
incl 0x18(%rsp)
movslq 0x20(%r14), %rax
movq 0x30(%r14), %rdx
movq 0x68(%r14), %rcx
testq %rax, %rax
jle 0x33b7
movq 0x60(%r14), %rsi
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, (%rsi,%rdi,8)
mulsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rcx,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x3397
movq 0x40(%r14), %rsi
movsd 0xb0(%r14), %xmm0
addsd 0x3e14(%rip), %xmm0     # 0x71e0
movq %r14, %rdi
callq 0x30b4
movslq 0x20(%r14), %rax
testq %rax, %rax
jle 0x340a
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 0x33eb
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 0x463b
addq $0x10, %rsp
movq 0x40(%r14), %rsi
movq 0x28(%r14), %rdi
movq 0x38(%r14), %rdx
movq %rbp, %rcx
callq 0x44b0
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 0x50ae
addq $0x20, %rsp
cmpl $0x1, %eax
je 0x34e0
cmpl $0x2, %eax
je 0x35e5
movsd 0x40(%rsp), %xmm0
ucomisd 0xa8(%r14), %xmm0
ja 0x337c
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 0x4d2d
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 0x4f2e
cmpl $0x1, %eax
jne 0x34f9
movsd 0x3cf0(%rip), %xmm0     # 0x71d8
ucomisd (%r13), %xmm0
jbe 0x3340
jmp 0x35a3
movsd (%r13), %xmm2
movsd 0x10(%rsp), %xmm0
addsd %xmm2, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x65, 0xc(%rsp)
jl 0x3542
movsd 0x3cca(%rip), %xmm1     # 0x71e8
ucomisd %xmm2, %xmm1
jbe 0x3542
movl $0x0, 0xc(%rsp)
movapd %xmm2, %xmm1
mulsd 0x3cb0(%rip), %xmm1     # 0x71e8
addsd %xmm2, %xmm1
movsd %xmm1, (%r13)
movslq (%rbp), %rax
testq %rax, %rax
jle 0x357a
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 0x3558
movsd 0x10(%rsp), %xmm0
movsd %xmm2, 0x20(%rsp)
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movsd 0x3c46(%rip), %xmm1     # 0x71d8
jae 0x3334
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax)
jmp 0x35e5
movq 0x7a2e(%rip), %rbx      # 0xafd8
leaq 0x3c67(%rip), %rsi      # 0x7218
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