// File: /workspace/github_cpp_makefile_star2_O1/IlanPrice[P]Linear-Algebra-Library-in-Cplusplus/Convergence.cpp
// Function: main
int main(int argc, char* argv[])
{
double pi = 3.1415926535897;


// Set up matrix system

int n = 20;
Vector a1 = ones(n-1);
// disp(a1);
Matrix A = 4*eye(n) - diag(a1,1) - diag(a1,-1);
// disp(A);
Matrix B1 = eye(n);
Matrix C1 = kron(B1,A);
// disp(C1);
Matrix B2 = diag(a1,1) + diag(a1,-1);
Matrix C2 = kron(B2,-eye(n));
// disp(C2);
Matrix C = C1+C2;
// disp(C);
//RHS

Vector b(n*n);
double dx = 1.0/(n-1);
Vector xco(n);
Vector yco(n);
for (int i = 2; i<=n; i++)
{
  xco(i) = (i-1)*dx;
  yco(i) = (i-1)*dx;
}

disp(xco);

int fillcount = 1;

for(int i = 1; i<=n; i++)
{
  // std::cout<<i<<"\n";
  for (int j = 1; j<=n; j++)
  {
      b(fillcount) = ((pi*pi)*(xco(j)*xco(j))*(1-xco(j)) + 6*xco(j) - 2)*sin(pi*yco(i));
      // std::cout<<b(fillcount)<<"\n";
      fillcount += 1;
  }
}

b = dx*dx*b;
// disp(b);

// // apply and time methods

//Vector times(7);
////
//std::clock_t LUstart = std::clock();
//Vector x_lu = C/b;
//double lutime = (std::clock() - LUstart) / (double)CLOCKS_PER_SEC;
//std::cout << "Finished in " << lutime << " seconds [CPU Clock] " << std::endl;

// std::clock_t jacobistart = std::clock();
// Vector x_j = jacobi(C,b);
// double jacobitime = (std::clock() - jacobistart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << jacobitime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t GSstart = std::clock();
// Vector x_gs = GS(C,b);
// double GStime = (std::clock() - GSstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << GStime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t SORstart = std::clock();
// Vector x_sor = SOR(C,b,1.7406);
// double SORtime = (std::clock() - SORstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << SORtime << " seconds [CPU Clock] " << std::endl;
//
// std::clock_t CGstart = std::clock();
// Vector x_cg = CG(C,b);
// double CGtime = (std::clock() - CGstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << CGtime << " seconds [CPU Clock] " << std::endl;
//
//
// std::clock_t GMRESstart = std::clock();
// Vector x_gmres = GMRES(C,b);
// double GMREStime = (std::clock() - GMRESstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << GMREStime << " seconds [CPU Clock] " << std::endl;
//
//
// std::clock_t MINRESstart = std::clock();
// Vector x_minres = MINRES(C,b);
// double MINREStime = (std::clock() - MINRESstart) / (double)CLOCKS_PER_SEC;
// std::cout << "Finished in " << MINREStime << " seconds [CPU Clock] " << std::endl;
// //
// // disp(x_gmres);
// times(1) = jacobitime; times(2) = GStime; times(3) = SORtime;
// times(4) = CGtime; times(5) = GMREStime; times(6) = MINREStime;
//times(7) = lutime;
//
//disp(times);

// std::ofstream CPUtimes;
// CPUtimes.open("CPUtimes.txt");
// CPUtimes << std::setprecision(15) << times << "\n";
// CPUtimes.close();



// // Convergence with mesh size


// std::ofstream MMSconvergence;
// MMSconvergence.open("MMSconvergence.txt");
//
// int n=5;
// for (int k = 1; k<=5; k++)
// {
//   Vector a1 = ones(n-1);
//   // disp(a1);
//   Matrix A = 4*eye(n) - diag(a1,1) - diag(a1,-1);
//   // disp(A);
//   Matrix B1 = eye(n);
//   Matrix C1 = kron(B1,A);
//   // disp(C1);
//   Matrix B2 = diag(a1,1) + diag(a1,-1);
//   Matrix C2 = kron(B2,-eye(n));
//   // disp(C2);
//   Matrix C = C1+C2;
//   // disp(C);
//   //RHS
//
//   Vector b(n*n);
//   double dx = 1.0/(n-1);
//   Vector xco(n);
//   Vector yco(n);
//   for (int i = 2; i<=n; i++)
//   {
//     xco(i) = (i-1)*dx;
//     yco(i) = (i-1)*dx;
//   }
//
//   disp(xco);
//
//   int fillcount = 1;
//
//   for(int i = 1; i<=n; i++)
//   {
//     std::cout<<i<<"\n";
//     for (int j = 1; j<=n; j++)
//     {
//         b(fillcount) = ((pi*pi)*(xco(j)*xco(j))*(1-xco(j)) + 6*xco(j) - 2)*sin(pi*yco(i));
//         std::cout<<b(fillcount)<<"\n";
//         fillcount += 1;
//     }
//   }
//
//   b = dx*dx*b;
//   Vector x = C/b;
//   MMSconvergence << std::setprecision(15) << x << "\n";
//   n +=5;
//   std::cout<< n << "\n";
// }
// MMSconvergence.close();


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
leaq 0x68(%rsp), %rdi
movl $0x13, %esi
callq 0x378d
leaq 0x38(%rsp), %rdi
movl $0x14, %esi
callq 0x6cf2
leaq 0x48(%rsp), %rdi
movsd 0x9c50(%rip), %xmm0     # 0xc008
leaq 0x38(%rsp), %rsi
callq 0x4e71
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x6db0
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x4dca
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6db0
leaq 0xc0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rsp, %rdx
callq 0x4dca
movq %rsp, %rdi
callq 0x4016
leaq 0x58(%rsp), %rdi
callq 0x4016
leaq 0x18(%rsp), %rdi
callq 0x4016
leaq 0x48(%rsp), %rdi
callq 0x4016
leaq 0x38(%rsp), %rdi
callq 0x4016
leaq 0x58(%rsp), %rdi
movl $0x14, %esi
callq 0x6cf2
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x69e5
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x6db0
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6db0
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %rsp, %rdx
callq 0x4d23
movq %rsp, %rdi
callq 0x4016
leaq 0x18(%rsp), %rdi
callq 0x4016
leaq 0x28(%rsp), %rdi
movl $0x14, %esi
callq 0x6cf2
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x677a
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %rsp, %rdx
callq 0x69e5
movq %rsp, %rdi
callq 0x4016
leaq 0x28(%rsp), %rdi
callq 0x4016
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x4d23
leaq 0x28(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
callq 0x28c8
leaq 0x78(%rsp), %rdi
movl $0x14, %esi
callq 0x28c8
leaq 0x88(%rsp), %rdi
movl $0x14, %esi
callq 0x28c8
movl $0x2, %ebx
leaq 0x78(%rsp), %r14
leaq 0x88(%rsp), %r15
leal -0x1(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x9ab9(%rip), %xmm0     # 0xc010
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x3354
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r15, %rdi
movl %ebx, %esi
callq 0x3354
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %ebx
cmpl $0x15, %ebx
jne 0x2545
leaq 0x78(%rsp), %rdi
callq 0x33e3
movl $0x1, %ebx
leaq 0x78(%rsp), %r14
leaq 0x28(%rsp), %r12
movl $0x1, %r15d
movl $0x1, %r13d
movq %r14, %rdi
movl %r13d, %esi
callq 0x3354
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3354
movsd 0x10(%rsp), %xmm0
mulsd (%rax), %xmm0
mulsd 0x9a35(%rip), %xmm0     # 0xc018
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3354
movsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3354
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x3354
leal (%r15,%r13), %ebp
decl %ebp
movsd (%rax), %xmm0
mulsd 0x99e6(%rip), %xmm0     # 0xc020
callq 0x2050
movsd %xmm0, 0x98(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x3354
movsd 0x99ce(%rip), %xmm0     # 0xc028
subsd 0xa8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
mulsd 0x99b2(%rip), %xmm0     # 0xc030
addsd %xmm1, %xmm0
addsd 0x99ae(%rip), %xmm0     # 0xc038
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %r13d
cmpl $0x15, %r13d
jne 0x25b1
incl %ebx
addl %r13d, %r15d
decl %r15d
cmpl $0x15, %ebx
jne 0x25ab
movabsq $0x3f66b1490aa31a3c, %rax # imm = 0x3F66B1490AA31A3C
leaq 0xd0(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x2dde
leaq 0x28(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x303c
leaq 0xb0(%rsp), %rdi
callq 0x2970
leaq 0x88(%rsp), %rdi
callq 0x2970
leaq 0x78(%rsp), %rdi
callq 0x2970
leaq 0x28(%rsp), %rdi
callq 0x2970
movq %rsp, %rdi
callq 0x4016
leaq 0x18(%rsp), %rdi
callq 0x4016
leaq 0x38(%rsp), %rdi
callq 0x4016
leaq 0x48(%rsp), %rdi
callq 0x4016
leaq 0x58(%rsp), %rdi
callq 0x4016
leaq 0xc0(%rsp), %rdi
callq 0x4016
leaq 0x68(%rsp), %rdi
callq 0x2970
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x2970
jmp 0x2858
jmp 0x2855
jmp 0x2855
movq %rax, %rbx
jmp 0x2865
movq %rax, %rbx
jmp 0x286f
movq %rax, %rbx
jmp 0x2879
movq %rax, %rbx
jmp 0x2881
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4016
jmp 0x27c6
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x2886
movq %rax, %rbx
jmp 0x288b
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4016
jmp 0x27e8
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x2890
movq %rax, %rbx
jmp 0x2895
movq %rax, %rbx
jmp 0x289f
movq %rax, %rbx
jmp 0x28a9
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4016
jmp 0x281a
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x4016
jmp 0x2829
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x4016
jmp 0x2838
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4016
jmp 0x2847
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x28b1
movq %rax, %rbx
jmp 0x28b6
jmp 0x2855
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2970
leaq 0x78(%rsp), %rdi
callq 0x2970
leaq 0x28(%rsp), %rdi
callq 0x2970
movq %rsp, %rdi
callq 0x4016
leaq 0x18(%rsp), %rdi
callq 0x4016
leaq 0x38(%rsp), %rdi
callq 0x4016
leaq 0x48(%rsp), %rdi
callq 0x4016
leaq 0x58(%rsp), %rdi
callq 0x4016
leaq 0xc0(%rsp), %rdi
callq 0x4016
leaq 0x68(%rsp), %rdi
callq 0x2970
movq %rbx, %rdi
callq 0x21c0