CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Error in the solver does not make sense (https://www.cfd-online.com/Forums/main/177940-error-solver-does-not-make-sense.html)

pawank September 24, 2016 17:14

Error in the solver does not make sense
 
Maybe this is not the right place to post this question, but I came across this when debugging my solver, and I think it can be related to FD approximations, so posted it here.

I have shared links at the end of this post where you can see the screenshots of my code, the run, etc.

My code is a serial, f90 code, a solver for for compressible N-S equations. I am using Intel compiler. I have a function in the code which calculates derivatives in the z direction using finite difference approximation. I have separate functions, one gives a 4th order approximation, and another gives 2nd order approximation for the kth point in z direction. k goes from 1 to sz(=102 in this case). If k=sz then the derivative is calculated using backward difference, so in the code, the line if k=sz is

if(k==sz) then
ddz = -(-var(i,j,k-2)+4*var(i,j,k-1)-3*var(i,j,k))/(2*dz)

ddz is the name given for derivative and this is 2nd order approximation. Both the 2nd order and 4th order approximation run fine. But or some reason(not important) I copied a line from the 4th order code for same k(i.e. k=sz) and changed the above line to:

if(k==sz) then
ddz = -(-3*var(i,j,k-4)+16*var(i,j,k-3)-36*var(i,j,k-2)+48*var(i,j,k-1)-25*var(i,j,k))/(12*dz)

Just by this change, and everything else in the code remains exactly the same, the code runs but does nothing, that is when I run it, it finishes in less than a second, and does not print anything on the screen like it does before the modification of that line.

point i,j,k-4 exists and thus var(i,j,k-4) has a value. I don't understand why this should not work. And not only this, if i make slight change to the modified line, something like removing the -3 multiplied to the first term(obviously this then becomes wrong expression for approximating the derivative, but i tried out of curiosity) :

if(k==sz) then
ddz = -(var(i,j,k-4)+16*var(i,j,k-3)-36*var(i,j,k-2)+48*var(i,j,k-1)-25*var(i,j,k))/(12*dz)

this code runs. Yes, it will not give correct approximation but still, the code runs, but in case of the prior case, nothing happens.

Note that there is a separate function where everything is 4th order(I uncomment it, and comment the 2nd order function when i want to use 4th order), and ,the 4th order has this same line(line which made the code not do anything) for k=sz , but there there is no issue.

All this does not make sense to me, what am I missing here?

Thanks a lot everyone for your help:)

Links for screenshots:
Original 2nd order function: https://drive.google.com/open?id=0B7...lpjblRJbUQ4ZjA
Changing one line(code does not do anything with this modification): https://drive.google.com/open?id=0B7...Ec5OTZ2ZHc3VkU
Just removing a constant coefficient in the expression makes the code run: https://drive.google.com/open?id=0B7...XVScjlzeXZ1Q1E
Sample run when it works: https://drive.google.com/open?id=0B7...0FhWnRWaTBoQ2M
Screenshot where the code runs but does not do anything at all: https://drive.google.com/open?id=0B7...VVHLVZrYm5aQVU

FMDenaro September 24, 2016 17:43

I have not read in details your post but before to read it into all the details, I suggest to use the compiler with all the debug options (array bounds check, type mysmatch and so on) and without any optimization.
Then perform only the first time step and debug online.

FMDenaro September 24, 2016 17:51

last doubt (maybe stupid), check the Intel Fortran language to see if val() is already an implicit function ... in such a case you cannot use as var(i,j,k)

agd September 24, 2016 18:06

Maybe I am misunderstanding, but for any finite difference approximation the coefficients have to sum to zero to ensure that a uniform field possesses no spurious gradients. It looks like you are violating that. Am I missing something?

pawank September 24, 2016 18:11

Actually It's var(). and if it was indeed some implicit function, it shouldn't have been working for other cases too. And in that case, just multiplying by a constant also shouldn't have made it work.

And the code compiles fine, shows no errors. It also runs fine. But it does not do anything at all. Runs as normal but finishes as if it had encountered a stop statement at the start itself. But i have no stop statements in the code.

An even weird thing is that if i put a write(write statement to print 'Hello' on the screen) or read statement just before the line where the function ddz is first called in the code, the code runs.

pawank September 24, 2016 18:16

Quote:

Originally Posted by agd (Post 619168)
Maybe I am misunderstanding, but for any finite difference approximation the coefficients have to sum to zero to ensure that a uniform field possesses no spurious gradients. It looks like you are violating that. Am I missing something?

You are correct. If you look carefully, there are three Statements. First one is second order, and sum of coefficients is zero. For second which is fourth order, also sum of coefficients are zero, but this line is what creates the problem. the third one in which i change the coefficient of a term of the second statement, which then becomes wrong formulation. But by doing this code runs, obviously the code diverges after a few iterations, but at least it runs.

FMDenaro September 24, 2016 18:37

Just as further idea...have you transferred the code by means of FTP or by other media? I suspect you have some hidden character that usually stems from trasfering of the files.
I got similar problem passing from unix system to others and using different compilers. I fixed that by using a batch file:

#!/bin/csh

foreach a ($*)
tr -d "\r" < $a > _tmp_$a
ls -l $a _tmp_$a
mv _tmp_$a $a
end

FMDenaro September 24, 2016 18:38

Quote:

Originally Posted by pawank (Post 619169)
Actually It's var(). and if it was indeed some implicit function, it shouldn't have been working for other cases too. And in that case, just multiplying by a constant also shouldn't have made it work.

And the code compiles fine, shows no errors. It also runs fine. But it does not do anything at all. Runs as normal but finishes as if it had encountered a stop statement at the start itself. But i have no stop statements in the code.

An even weird thing is that if i put a write(write statement to print 'Hello' on the screen) or read statement just before the line where the function ddz is first called in the code, the code runs.


yes, it is var()

pawank September 24, 2016 18:44

Quote:

Originally Posted by FMDenaro (Post 619171)
Just as further idea...have you transferred the code by means of FTP or by other media? I suspect you have some hidden character that usually stems from trasfering of the files.
I got similar problem passing from unix system to others and using different compilers. I fixed that by using a batch file:

#!/bin/csh

foreach a ($*)
tr -d "\r" < $a > _tmp_$a
ls -l $a _tmp_$a
mv _tmp_$a $a
end


No actually. All work has been done on my lab computer. I sometimes work remotely from my dorm room, but i don't transfer anything.
But thanks anyway :) may be useful in other cases..

sbaffini September 25, 2016 05:04

The way to proceed is always to shrink the problem down to a minimum (not) working example. This is important because you cannot exclude a compiler error (even if it's extremely rare).


All times are GMT -4. The time now is 17:01.