// File: snesic[P]stochastic_thin_films/newton-raphson_tools.cpp
// Function: check_newton_convergence(int const&, double*, double*, double*, double&, double const&, int&, int&, int const&, double&)
int check_newton_convergence(const int& L, double *h, double *y, double *q, double& dt, const double& jdt, int& no_good_solutions, int& no_consecutive_neg, const int& no_newton_it, double& mq)
{   double maxq=0;
    int i;
    
    for(i=0;i<L;i++)
    {   if(maxq<fabs(q[i])) maxq=fabs(q[i]);
        
        if(y[i]+q[i]<0)
            {
            cout<< h[i] << "  at " << jdt << " , on the position " <<  i   << "  is negative!" << endl;
            no_consecutive_neg++;
            if(no_consecutive_neg>10) {cout << "Wrong parameters -> Simulation breaks!" << endl; return 2;}
            return 1;
            }
        else
        y[i]=y[i]+q[i];
    }
    
    if(no_newton_it>10)
        {   no_good_solutions=0;
            dt=dt-pow(10,-fabs(round(log10(dt)))-2);
            return 1;
        }
    
    mq=maxq;
    
return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x89fa
movsd -0x40(%rbp), %xmm1
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movaps 0x3329(%rip), %xmm2     # 0xbbe0
pand %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x88de
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movaps 0x330b(%rip), %xmm1     # 0xbbe0
pand %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x20(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x89c3
movq -0x18(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x66bf(%rip), %rdi      # 0xefd8
callq 0x34b0
movq %rax, %rdi
leaq 0x3377(%rip), %rsi      # 0xbc9f
callq 0x3270
movq %rax, %rdi
movq -0x38(%rbp), %rax
movsd (%rax), %xmm0
callq 0x34b0
movq %rax, %rdi
leaq 0x335e(%rip), %rsi      # 0xbca5
callq 0x3270
movq %rax, %rdi
movl -0x44(%rbp), %esi
callq 0x3400
movq %rax, %rdi
leaq 0x3358(%rip), %rsi      # 0xbcb9
callq 0x3270
movq %rax, %rdi
movq 0x6658(%rip), %rsi      # 0xefc8
callq 0x3290
movq 0x18(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
movq 0x18(%rbp), %rax
cmpl $0xa, (%rax)
jle 0x89b7
movq 0x6648(%rip), %rdi      # 0xefd8
leaq 0x3331(%rip), %rsi      # 0xbcc8
callq 0x3270
movq %rax, %rdi
movq 0x6622(%rip), %rsi      # 0xefc8
callq 0x3290
movl $0x2, -0x4(%rbp)
jmp 0x8a95
movl $0x1, -0x4(%rbp)
jmp 0x8a95
movq -0x20(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x89ec
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x888f
movq 0x20(%rbp), %rax
cmpl $0xa, (%rax)
jle 0x8a81
movq 0x10(%rbp), %rax
movl $0x0, (%rax)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x50(%rbp)
movq -0x30(%rbp), %rax
movsd (%rax), %xmm0
callq 0x3110
callq 0x30d0
movaps 0x31ad(%rip), %xmm1     # 0xbbe0
pand %xmm1, %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd 0x16da(%rip), %xmm1     # 0xa130
subsd %xmm1, %xmm0
movl $0xa, %edi
callq 0x8aa0
movaps %xmm0, %xmm1
movsd -0x50(%rbp), %xmm0
subsd %xmm1, %xmm0
movq -0x30(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0x8a95
movsd -0x40(%rbp), %xmm0
movq 0x28(%rbp), %rax
movsd %xmm0, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop