// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x13
popq %rbp
movl %ebp, %esi
callq 0x23df
leaq 0x38(%rsp), %rdi
pushq $0x14
popq %rsi
callq 0x50c7
leaq 0x48(%rsp), %rdi
movsd 0x7ca2(%rip), %xmm0     # 0x9008
leaq 0x38(%rsp), %rsi
callq 0x36ec
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x518b
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x3642
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
pushq $-0x1
popq %rdx
callq 0x518b
leaq 0xc0(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rsp, %rdx
callq 0x3642
movq %rsp, %rdi
callq 0x2a78
leaq 0x58(%rsp), %rdi
callq 0x2a78
leaq 0x18(%rsp), %rdi
callq 0x2a78
leaq 0x48(%rsp), %rdi
callq 0x2a78
leaq 0x38(%rsp), %rdi
callq 0x2a78
leaq 0x58(%rsp), %rdi
pushq $0x14
popq %rsi
callq 0x50c7
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0xc0(%rsp), %rdx
callq 0x4e0c
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x518b
movq %rsp, %rdi
leaq 0x68(%rsp), %rsi
pushq $-0x1
popq %rdx
callq 0x518b
leaq 0x38(%rsp), %rdi
leaq 0x18(%rsp), %rsi
movq %rsp, %rdx
callq 0x3598
movq %rsp, %rdi
callq 0x2a78
leaq 0x18(%rsp), %rdi
callq 0x2a78
leaq 0x28(%rsp), %rdi
pushq $0x14
popq %rsi
callq 0x50c7
movq %rsp, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4bac
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %rsp, %rdx
callq 0x4e0c
movq %rsp, %rdi
callq 0x2a78
leaq 0x28(%rsp), %rdi
callq 0x2a78
movq %rsp, %rdi
leaq 0x48(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x3598
leaq 0x28(%rsp), %rdi
movl $0x190, %esi            # imm = 0x190
callq 0x186c
leaq 0x78(%rsp), %rdi
pushq $0x14
popq %rsi
callq 0x186c
leaq 0x88(%rsp), %rdi
pushq $0x14
popq %rsi
callq 0x186c
pushq $0x2
popq %r14
leaq 0x78(%rsp), %r15
leaq 0x88(%rsp), %r12
subl $0x1, %ebp
jb 0x152c
leal -0x1(%r14), %eax
cvtsi2sd %eax, %xmm0
mulsd 0x7b19(%rip), %xmm0     # 0x9010
movsd %xmm0, 0x10(%rsp)
movq %r15, %rdi
movl %r14d, %esi
callq 0x2030
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r12, %rdi
movl %r14d, %esi
callq 0x2030
movsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %r14d
jmp 0x14e2
leaq 0x78(%rsp), %rdi
callq 0x20a5
xorl %r15d, %r15d
leaq 0x78(%rsp), %r14
leaq 0x28(%rsp), %r12
pushq $0x1
popq %rbp
cmpl $0x15, %ebp
je 0x1656
pushq $0x1
popq %rax
movl %eax, %r13d
cmpl $0x15, %r13d
je 0x164b
movq %r14, %rdi
movl %r13d, %esi
callq 0x2030
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x2030
movsd 0x10(%rsp), %xmm0
mulsd (%rax), %xmm0
mulsd 0x7a87(%rip), %xmm0     # 0x9018
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x2030
movsd (%rax), %xmm0
movsd %xmm0, 0xa8(%rsp)
movq %r14, %rdi
movl %r13d, %esi
callq 0x2030
movsd (%rax), %xmm0
movsd %xmm0, 0xa0(%rsp)
leaq 0x88(%rsp), %rdi
movl %ebp, %esi
callq 0x2030
leal (%r15,%r13), %ebx
movsd (%rax), %xmm0
mulsd 0x7a3a(%rip), %xmm0     # 0x9020
callq 0x1050
movsd %xmm0, 0x98(%rsp)
movq %r12, %rdi
movl %ebx, %esi
callq 0x2030
movsd 0x7a22(%rip), %xmm0     # 0x9028
subsd 0xa8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
mulsd 0x7a06(%rip), %xmm0     # 0x9030
addsd %xmm1, %xmm0
addsd 0x7a02(%rip), %xmm0     # 0x9038
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rax)
incl %r13d
jmp 0x1555
incl %ebp
addl $0x14, %r15d
jmp 0x1546
movabsq $0x3f66b1490aa31a3c, %rax # imm = 0x3F66B1490AA31A3C
leaq 0xd0(%rsp), %rsi
movq %rax, (%rsi)
leaq 0xb0(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0x1c1c
leaq 0x28(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1da0
leaq 0xb0(%rsp), %rdi
callq 0x1908
leaq 0x88(%rsp), %rdi
callq 0x1908
leaq 0x78(%rsp), %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x1908
movq %rsp, %rdi
callq 0x2a78
leaq 0x18(%rsp), %rdi
callq 0x2a78
leaq 0x38(%rsp), %rdi
callq 0x2a78
leaq 0x48(%rsp), %rdi
callq 0x2a78
leaq 0x58(%rsp), %rdi
callq 0x2a78
leaq 0xc0(%rsp), %rdi
callq 0x2a78
leaq 0x68(%rsp), %rdi
callq 0x1908
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 0x1908
jmp 0x17f9
jmp 0x17f6
jmp 0x17f6
movq %rax, %rbx
jmp 0x1806
movq %rax, %rbx
jmp 0x1810
movq %rax, %rbx
jmp 0x181a
movq %rax, %rbx
jmp 0x1822
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2a78
jmp 0x1767
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x1827
movq %rax, %rbx
jmp 0x182c
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2a78
jmp 0x1789
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
jmp 0x1831
movq %rax, %rbx
jmp 0x1836
movq %rax, %rbx
jmp 0x1840
movq %rax, %rbx
jmp 0x184a
movq %rax, %rbx
movq %rsp, %rdi
callq 0x2a78
jmp 0x17bb
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2a78
jmp 0x17ca
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x2a78
jmp 0x17d9
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2a78
jmp 0x17e8
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x1852
movq %rax, %rbx
jmp 0x1857
jmp 0x17f6
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x1908
leaq 0x78(%rsp), %rdi
callq 0x1908
leaq 0x28(%rsp), %rdi
callq 0x1908
movq %rsp, %rdi
callq 0x2a78
leaq 0x18(%rsp), %rdi
callq 0x2a78
leaq 0x38(%rsp), %rdi
callq 0x2a78
leaq 0x48(%rsp), %rdi
callq 0x2a78
leaq 0x58(%rsp), %rdi
callq 0x2a78
leaq 0xc0(%rsp), %rdi
callq 0x2a78
leaq 0x68(%rsp), %rdi
callq 0x1908
movq %rbx, %rdi
callq 0x1180
nopl (%rax)