// File: /workspace/github_cpp_makefile_star2_O3/Gilaverbuch[P]FD_2D_WAVE/Field.cpp
// Function: Field::Propagator()
void Field::Propagator(){
	
	double source, d2x, d2y, A, B, C, D;
	int i, j, k, itteration, pos_s_x, pos_s_y;
	std::cout << "wave propagation!!!" << std::endl;
	system("rm results/*.txt");


	pos_s_x = int(x_source/dx);
	pos_s_y = int(y_source/dy);
	itteration = 0;
	print_to_file(elements_x, elements_y, U, x, y, itteration);


	// propagator

	for (i=0; i<steps; i++){
		source = -2 * (i*dt - source_time_delay) * pow(frequency, 2) * exp(-1 * pow(frequency, 2) * pow((i*dt - source_time_delay), 2));
		

		for (j=1; j<(elements_y-1); j++){
			for (k=1; k<(elements_x-1); k++){

				A = (U[j][k+1] - U[j][k])/dx;
				B = (U[j][k] - U[j][k-1])/dx;
				C = (U[j+1][k] - U[j][k])/dy;
				D = (U[j][k] - U[j-1][k])/dy;

				d2x = ((1/rho[j][k])*A - (1/rho[j][k-1])*B)/(dx);
				d2y = ((1/rho[j][k])*C - (1/rho[j-1][k])*D)/(dy);

				// d2x = (U[j][k-1] - 2*U[j][k] + U[j][k+1])/(pow(dx,2));

				// d2y =  (U[j-1][k] - 2*U[j][k] + U[j+1][k])/(pow(dy,2));

				U_future[j][k] = 2*U[j][k] - U_past[j][k] + pow(vel[j][k],2) * pow(dt,2) * (d2y + d2x);
			}
		}

		U_future[pos_s_y][pos_s_x] = U_future[pos_s_y][pos_s_x] + pow(dt,2) * source;


		for (j=0; j<(elements_y-1); j++){
			U_future[j][0] = 0;
			U_future[j][elements_x-1] = 0;

		} 

		for (k=0; j<(elements_x-1); k++){
			U_future[0][k] = 0;
			U_future[elements_y-1][k] = 0;

		} 
		

		if (i%print_every==0){
			itteration = i/print_every;
			std::cout << "time is " << i*dt << std::endl;

			print_to_file(elements_x, elements_y, U_future, x, y, itteration);
		}

		for (j=0; j<elements_y; j++){
			for (k=0; k<elements_x; k++){

				U_past[j][k] = U[j][k];
				U[j][k] = U_future[j][k];
				U_future[j][k] = 0;
			}
		}

	}


	system("mv wave_signal*.txt results/");	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %rbx
movq 0x355f(%rip), %r14      # 0x6fd8
leaq 0x1762(%rip), %rsi      # 0x51e2
movl $0x13, %edx
movq %r14, %rdi
callq 0x21b0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20d0
leaq 0x173b(%rip), %rdi      # 0x51f6
callq 0x2140
movl 0x84(%rbx), %r14d
movl 0x70(%rbx), %r12d
movl 0x88(%rbx), %r15d
movl 0x74(%rbx), %r13d
movl 0x7c(%rbx), %esi
movl 0x80(%rbx), %edx
movq (%rbx), %rcx
movq 0x48(%rbx), %r8
movq 0x50(%rbx), %r9
movl $0x0, (%rsp)
callq 0x3ecc
cmpl $0x0, 0x78(%rbx)
jle 0x3e97
movl %r15d, %eax
cltd
idivl %r13d
movl %eax, %ecx
movl %r14d, %eax
cltd
idivl %r12d
movslq %ecx, %r12
cltq
movq %rax, -0x48(%rbp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r12, -0x38(%rbp)
xorps %xmm1, %xmm1
cvtsi2sd %r14d, %xmm1
movsd 0x58(%rbx), %xmm0
movsd %xmm1, -0x50(%rbp)
mulsd %xmm1, %xmm0
subsd 0x68(%rbx), %xmm0
movapd %xmm0, %xmm1
mulsd 0x1616(%rip), %xmm1     # 0x5160
movsd %xmm1, -0x58(%rbp)
movsd 0x60(%rbx), %xmm2
movapd %xmm2, %xmm1
xorpd 0x1620(%rip), %xmm1     # 0x5180
movapd %xmm2, -0x90(%rbp)
mulsd %xmm2, %xmm1
mulsd %xmm0, %xmm0
mulsd %xmm1, %xmm0
callq 0x2280
movslq 0x80(%rbx), %rdi
leal -0x1(%rdi), %r8d
movq 0x10(%rbx), %rsi
movslq 0x7c(%rbx), %rdx
movl %r8d, %r9d
cmpq $0x3, %rdi
jl 0x3d31
movl %r8d, -0x2c(%rbp)
movq %rsi, -0x80(%rbp)
movq %rdi, -0x40(%rbp)
movl %r14d, -0x30(%rbp)
leal -0x1(%rdx), %eax
movq (%rbx), %rcx
movq %rcx, -0x78(%rbp)
movq 0x8(%rbx), %rcx
movq %rcx, -0x70(%rbp)
xorps %xmm1, %xmm1
cvtsi2sdl 0x70(%rbx), %xmm1
xorps %xmm2, %xmm2
cvtsi2sdl 0x74(%rbx), %xmm2
movq 0x20(%rbx), %rcx
movq %rcx, -0x68(%rbp)
movq 0x28(%rbx), %rcx
movq %rcx, -0x60(%rbp)
leaq -0x8(,%rax,8), %r15
movl $0x1, %r13d
movsd 0x1576(%rip), %xmm10    # 0x5168
movsd 0x1575(%rip), %xmm11    # 0x5170
movq %r9, %r8
cmpl $0x3, %edx
jl 0x3d0b
movq -0x78(%rbp), %rax
movq -0x8(%rax,%r13,8), %rsi
movq (%rax,%r13,8), %r12
movq 0x8(%rax,%r13,8), %rcx
movq -0x68(%rbp), %rax
movq -0x8(%rax,%r13,8), %r14
movq (%rax,%r13,8), %r9
movq -0x70(%rbp), %rax
movq (%rax,%r13,8), %r11
movq -0x60(%rbp), %rax
movq (%rax,%r13,8), %r10
movq -0x80(%rbp), %rax
movq (%rax,%r13,8), %rax
xorl %edi, %edi
movsd 0x8(%r12,%rdi), %xmm3
movsd 0x10(%r12,%rdi), %xmm4
subsd %xmm3, %xmm4
movapd %xmm3, %xmm6
subsd (%r12,%rdi), %xmm6
divsd %xmm1, %xmm4
divsd %xmm1, %xmm6
movsd 0x8(%rcx,%rdi), %xmm7
subsd %xmm3, %xmm7
divsd %xmm2, %xmm7
movapd %xmm3, %xmm8
subsd 0x8(%rsi,%rdi), %xmm8
divsd %xmm2, %xmm8
movapd %xmm10, %xmm5
divsd 0x8(%r9,%rdi), %xmm5
movapd %xmm11, %xmm9
divsd (%r9,%rdi), %xmm9
mulsd %xmm6, %xmm9
mulsd %xmm5, %xmm4
addsd %xmm9, %xmm4
divsd %xmm1, %xmm4
movapd %xmm11, %xmm6
divsd 0x8(%r14,%rdi), %xmm6
mulsd %xmm8, %xmm6
mulsd %xmm7, %xmm5
addsd %xmm6, %xmm5
divsd %xmm2, %xmm5
addsd %xmm4, %xmm5
addsd %xmm3, %xmm3
subsd 0x8(%r11,%rdi), %xmm3
movsd 0x8(%r10,%rdi), %xmm4
mulsd %xmm4, %xmm4
movsd 0x58(%rbx), %xmm6
mulsd %xmm6, %xmm6
mulsd %xmm4, %xmm6
mulsd %xmm5, %xmm6
addsd %xmm3, %xmm6
movsd %xmm6, 0x8(%rax,%rdi)
addq $0x8, %rdi
cmpq %rdi, %r15
jne 0x3c40
incq %r13
movq %r8, %r9
cmpq %r8, %r13
jne 0x3bfb
movq 0x10(%rbx), %rsi
movq -0x38(%rbp), %r12
xorl %r15d, %r15d
movl -0x30(%rbp), %r14d
movq -0x40(%rbp), %rdi
movl -0x2c(%rbp), %r8d
movapd -0x90(%rbp), %xmm2
mulsd %xmm2, %xmm2
movsd -0x58(%rbp), %xmm1
mulsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movq (%rsi,%r12,8), %rax
movsd 0x58(%rbx), %xmm1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm1
movq -0x48(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
xorl %eax, %eax
cmpl $0x2, %edi
jl 0x3d89
xorl %eax, %eax
movq (%rsi,%rax,8), %rcx
movq %r15, (%rcx)
movq %r15, -0x8(%rcx,%rdx,8)
incq %rax
cmpq %rax, %r9
jne 0x3d72
movl %r8d, %eax
decl %edx
cmpl %edx, %eax
jl 0x3eb1
movl %r14d, %r12d
movl %r14d, %eax
cltd
idivl 0x8c(%rbx)
testl %edx, %edx
jne 0x3e29
movl %eax, %r13d
movl $0x8, %edx
movq 0x3221(%rip), %r14      # 0x6fd8
movq %r14, %rdi
leaq 0x1446(%rip), %rsi      # 0x5207
callq 0x21b0
movsd -0x50(%rbp), %xmm0
mulsd 0x58(%rbx), %xmm0
movq %r14, %rdi
callq 0x2230
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2160
movsbl %al, %esi
movq %r15, %rdi
xorl %r15d, %r15d
callq 0x2040
movq %rax, %rdi
callq 0x20d0
movl 0x7c(%rbx), %esi
movl 0x80(%rbx), %edx
movq 0x10(%rbx), %rcx
movq 0x48(%rbx), %r8
movq 0x50(%rbx), %r9
movl %r13d, (%rsp)
callq 0x3ecc
movl 0x80(%rbx), %edi
testl %edi, %edi
jle 0x3e83
movl 0x7c(%rbx), %eax
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
movq 0x10(%rbx), %rsi
movl %edi, %edi
xorl %r8d, %r8d
testl %eax, %eax
jle 0x3e7b
movq (%rcx,%r8,8), %r9
movq (%rdx,%r8,8), %r10
movq (%rsi,%r8,8), %r11
xorl %r14d, %r14d
movsd (%r9,%r14,8), %xmm0
movsd %xmm0, (%r10,%r14,8)
movsd (%r11,%r14,8), %xmm0
movsd %xmm0, (%r9,%r14,8)
movq $0x0, (%r11,%r14,8)
incq %r14
cmpq %r14, %rax
jne 0x3e53
incq %r8
cmpq %rdi, %r8
jne 0x3e40
movl %r12d, %r14d
incl %r14d
cmpl 0x78(%rbx), %r14d
movq -0x38(%rbp), %r12
jl 0x3b23
leaq 0x1372(%rip), %rdi      # 0x5210
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2140
movq (%rsi), %rax
movq -0x8(%rsi,%rdi,8), %rcx
xorl %edx, %edx
xorl %esi, %esi
movq %rdx, (%rax,%rsi)
movq %rdx, (%rcx,%rsi)
addq $0x8, %rsi
jmp 0x3ebd
nop