// File: /workspace/github_cpp_makefile_star2_O3/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 0x3769
leaq 0x38(%rsp), %rdi
movl $0x14, %esi
callq 0x6d21
leaq 0x48(%rsp), %rdi
movsd 0x9c60(%rip), %xmm0     # 0xc008
leaq 0x38(%rsp), %rsi
callq 0x4e7a
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x6ddf
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x4dd5
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6ddf
leaq 0xc0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rsp, %rdx
callq 0x4dd5
movq %rsp, %rdi
callq 0x3fe2
leaq 0x58(%rsp), %rdi
callq 0x3fe2
leaq 0x18(%rsp), %rdi
callq 0x3fe2
leaq 0x48(%rsp), %rdi
callq 0x3fe2
leaq 0x38(%rsp), %rdi
callq 0x3fe2
leaq 0x58(%rsp), %rdi
movl $0x14, %esi
callq 0x6d21
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x6a14
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
movl $0x1, %edx
callq 0x6ddf
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6ddf
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %rsp, %rdx
callq 0x4d30
movq %rsp, %rdi
callq 0x3fe2
leaq 0x18(%rsp), %rdi
callq 0x3fe2
leaq 0x28(%rsp), %rdi
movl $0x14, %esi
callq 0x6d21
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x67af
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %rsp, %rdx
callq 0x6a14
movq %rsp, %rdi
callq 0x3fe2
leaq 0x28(%rsp), %rdi
callq 0x3fe2
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x4d30
leaq 0x28(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
callq 0x28b8
leaq 0x78(%rsp), %rdi
movl $0x14, %esi
callq 0x28b8
leaq 0x88(%rsp), %rdi
movl $0x14, %esi
callq 0x28b8
movl $0x2, %ebx
leaq 0x78(%rsp), %r14
leaq 0x88(%rsp), %r15
leal -0x1(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x9ac9(%rip), %xmm0     # 0xc010
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %ebx, %esi
callq 0x3330
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r15, %rdi
movl %ebx, %esi
callq 0x3330
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %ebx
cmpl $0x15, %ebx
jne 0x2535
leaq 0x78(%rsp), %rdi
callq 0x33bf
movl $0x1, %ebx
leaq 0x78(%rsp), %r14
leaq 0x28(%rsp), %r12
movl $0x1, %r15d
movl $0x1, %r13d
movq %r14, %rdi
movl %r13d, %esi
callq 0x3330
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3330
movsd 0x10(%rsp), %xmm0
mulsd (%rax), %xmm0
mulsd 0x9a45(%rip), %xmm0     # 0xc018
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3330
movsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x3330
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
movl %ebx, %esi
callq 0x3330
leal (%r15,%r13), %ebp
decl %ebp
movsd (%rax), %xmm0
mulsd 0x99f6(%rip), %xmm0     # 0xc020
callq 0x2050
movsd %xmm0, 0x98(%rsp)
movq %r12, %rdi
movl %ebp, %esi
callq 0x3330
movsd 0x99de(%rip), %xmm0     # 0xc028
subsd 0xa8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
mulsd 0x99c2(%rip), %xmm0     # 0xc030
addsd %xmm1, %xmm0
addsd 0x99be(%rip), %xmm0     # 0xc038
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %r13d
cmpl $0x15, %r13d
jne 0x25a1
incl %ebx
addl %r13d, %r15d
decl %r15d
cmpl $0x15, %ebx
jne 0x259b
movabsq $0x3f66b1490aa31a3c, %rax # imm = 0x3F66B1490AA31A3C
leaq 0xd0(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x2da1
leaq 0x28(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x300a
leaq 0xb0(%rsp), %rdi
callq 0x294e
leaq 0x88(%rsp), %rdi
callq 0x294e
leaq 0x78(%rsp), %rdi
callq 0x294e
leaq 0x28(%rsp), %rdi
callq 0x294e
movq %rsp, %rdi
callq 0x3fe2
leaq 0x18(%rsp), %rdi
callq 0x3fe2
leaq 0x38(%rsp), %rdi
callq 0x3fe2
leaq 0x48(%rsp), %rdi
callq 0x3fe2
leaq 0x58(%rsp), %rdi
callq 0x3fe2
leaq 0xc0(%rsp), %rdi
callq 0x3fe2
leaq 0x68(%rsp), %rdi
callq 0x294e
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 0x294e
jmp 0x2848
jmp 0x2845
jmp 0x2845
movq %rax, %rbx
jmp 0x2855
movq %rax, %rbx
jmp 0x285f
movq %rax, %rbx
jmp 0x2869
movq %rax, %rbx
jmp 0x2871
movq %rax, %rbx
movq %rsp, %rdi
callq 0x3fe2
jmp 0x27b6
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x2876
movq %rax, %rbx
jmp 0x287b
movq %rax, %rbx
movq %rsp, %rdi
callq 0x3fe2
jmp 0x27d8
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x2880
movq %rax, %rbx
jmp 0x2885
movq %rax, %rbx
jmp 0x288f
movq %rax, %rbx
jmp 0x2899
movq %rax, %rbx
movq %rsp, %rdi
callq 0x3fe2
jmp 0x280a
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3fe2
jmp 0x2819
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3fe2
jmp 0x2828
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x3fe2
jmp 0x2837
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x28a1
movq %rax, %rbx
jmp 0x28a6
jmp 0x2845
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x294e
leaq 0x78(%rsp), %rdi
callq 0x294e
leaq 0x28(%rsp), %rdi
callq 0x294e
movq %rsp, %rdi
callq 0x3fe2
leaq 0x18(%rsp), %rdi
callq 0x3fe2
leaq 0x38(%rsp), %rdi
callq 0x3fe2
leaq 0x48(%rsp), %rdi
callq 0x3fe2
leaq 0x58(%rsp), %rdi
callq 0x3fe2
leaq 0xc0(%rsp), %rdi
callq 0x3fe2
leaq 0x68(%rsp), %rdi
callq 0x294e
movq %rbx, %rdi
callq 0x21b0