Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] something very peculiar

  1. Oct 21, 2014 #1

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Hello guys,

    As, you probably know by now, I've been trying to run this piece of code for a while now. I finally got it to run and then I realized something very weird is happening!

    So in my code, which simulates neutrino evolution, I have made 3 conditional statements in the evolution stage of the code, depending on a parameter that I defined in a module called params.

    In my module called params there is a line:
    Code (Text):
    integer, parameter::spinco=1
    spinco should be a parameter defining if I want to run the simulation with or without a "spin-coherence term" in the Hamiltonian. spinco=0 means no spin-coherence, spinco=1 means majorana type spin-coherence, and spinco=2 means dirac type spin-coherence. (It's not important that you understand what this means, but just that spinco governs what kind of Hamiltonian I'm using).

    In my main code there are lines like:

    Code (Text):
     if(spinco.eq.0) then
    XXX
    else if(spinco.eq.1) then
    XXX
    else if(spinco.eq.2) then
    XXX
    endif
    Now, this part of the code is VERY IMPORTANT because this is the part of the code that actually evolves the wave function (the rest of the code is finding the Hamiltonian lol). Here's the weird part. I FORGOT to put the parameter spinco in my params.F90 file (meaning, I did not define it at all, there was no integer,parameter:: spinco line in the params file), and tried to run the code (only realizing I forgot this afterwards). The code ran! I didn't get any errors like "undefined variable 'spinco'" or anything like that! It went through several hundred loops before I stopped it, and the output looked normal! I am so perplexed by this. How can this code run if I didn't declare the parameter spinco?

    This question is very important to me because the first "proof of concept" test I am running is to show that my code gives the same output as an unmodified set of code that has none of this "spin coherence" stuff in it if I set spinco=1 and set all the "majorana terms" in the Hamiltonian to 0. If I set all the "majorana terms" to 0, the problem SHOULD reduce to the spinco=0 case and I SHOULD get the same result as the old un-modified code. This should prove that my architecture is valid, and that I can proceed to the next phase.

    But since WITHOUT DEFINING spinco AT ALL, the code ran AND seemed to give me the "correct" output, I am now not certain if my modifications to the code is actually doing anything at all...:(

    Anyone have any insights into this?
     
  2. jcsd
  3. Oct 21, 2014 #2
    Are you using Implicit None?

    If not FORTRAN will implicitly assume that Spinco is a real.
     
  4. Oct 21, 2014 #3
    what he said...

    ...this is one of the first things you learn about Fortran...variables do not need to be declared and if so, variables with names starting with the letters I through N are implicitly INTEGER, otherwise they are REAL.

    Oh, and hopefully, variables that were not declared will take on zero value...but I am not sure if this is guaranteed, it may depend on the compiler or compiler flags.
     
  5. Oct 21, 2014 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    The Fortran PARAMETER statement is designed so that the programmer can specify a named constant value, something that is not supposed to change during the running of the program, like REAL PARAMETER PI :: 3.1415926.

    Since you want to use the parameter SPINCO to select a certain type of calculation, you should make it a variable instead of a PARAMETER, which would allow you to change its value depending on other factors or make it an input.
     
  6. Oct 21, 2014 #5

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    spinco will never be changed, and should not be changed, during a single run, so I think keeping it as a parameter is correct.

    Also, I think my modifications did do something since now running the code I am getting no evolution of the wave function lol!.
     
  7. Oct 21, 2014 #6

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    I see, yes, the code is not written with implicit none. So I'm guessing spinco will be given some memory space and assigned some value based on whatever junk is in that space?

    I have never been formally taught fortran, I sort of just picked it up as I go.
     
  8. Oct 21, 2014 #7

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    If the SPINCO parameter is never to be changed, then why include all the conditional statements which depend on the value of SPINCO? This is where novice programmers create needless complexity in their code.
     
  9. Oct 21, 2014 #8

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Spinco might be needed to be changed for different runs of the code. I might run the code and try to simulate majorana neutrinos and choose spinco=1, and then run the code again to simulate dirac neutrinos and choose spinco=2. But within 1 iteration of the program, spinco should never change, because I either simulate majorana, or dirac, I can't simulate both.

    Different conditions on spinco requires different evolution of the wave function; hence, why all the conditional statements.

    This way I can change one parameter in params and run a wholly different simulation, instead of having to go back and rewrite the code every time.
     
  10. Oct 22, 2014 #9

    jtbell

    User Avatar

    Staff: Mentor

    Values specified in a PARAMETER statement are fixed at compilation time. Are you planning to re-compile the program every time you change the value of SPINCO? If not, then you should not make SPINCO a PARAMETER, but rather declare it as a normal variable.
     
  11. Oct 22, 2014 #10

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    I believe using makefile I can recompile just the necessary pieces of the code?

    But right now I have indeed been recompiling at each run. Each run might take (when all is said and done) between 10 and 100 hours. Compiling takes <20 seconds. So it's not really a problem. I would rather keep it as a parameter to make sure I don't make a mistake and reassign a value to it during some other parts of the code.
     
  12. Oct 22, 2014 #11

    Mark44

    Staff: Mentor

    Even though compilation time is very small in comparison to run time, it makes sense to me (and the others in this thread) to make it a variable. At the beginning of your main program you could present a simple interface to ask the user for the spinco value, and then take whatever action is appropriate after that. There should be no reason to change its value anywhere else in your code.

    It seems silly to me to have to recompile code just because a single number changes, regardless of how little time the compiler takes. Furthermore, if at some point down the line you have the executable handy and you don't remember where all the source code is, a recompile might be a real inconvenience.
     
  13. Oct 22, 2014 #12

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    If this were the overall philosophy of the code that I'm working on, then yeah, I might have gone this way with the coding, but really there are already a ton of other "parameters" that could theoretically be changed at every run-time including all the # of bins of energy and angles, and number of flavors you want to simulate, etc. All of those were written as parameters just like I have written spinco. Many of those need to be parameters because they must be determined at compile time since they give the sizes of arrays. And I can not be certain that in future edits of the code spinco will not be used in a similar way. Right now it is only used in conditional statements; but, I could certainly see some instances where I might use spinco as an integer in an array declaration or some such. I have not written the code to find the Hamiltonian matrix yet. There is no interface while running this code, you create the executable, you run the executable in the terminal, or on the supercomputer, and you wait for the code to finish so you can look at the output.
     
  14. Oct 22, 2014 #13

    Mark44

    Staff: Mentor

    Which is reasonable justification for making them parameters.
    So even though you haven't written the code to find the Hamiltonian yet, it sounds like that's something you will be doing down the line. I'm not talking about anything fancy for the interface - probably two lines of code, a WRITE or PRINT statement for the prompt on the terminal, and a READ statement for the input.

    You've asked for advice from this group, and between us all we have many, many decades of writing code in literally dozens of programming languages. If we are unanimous in advising for or against something, you should really think hard about going against that advice.
     
  15. Oct 22, 2014 #14

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    \

    Ok, when I finish writing the code, if at that time spinco remains inside conditionals only, and does not find itself into any array size declarations, I'll remove the parameter tag from it and write a few lines to take input and determine it from the terminal. Thanks for the advice.
     
  16. Oct 22, 2014 #15

    DrClaude

    User Avatar

    Staff: Mentor

    Didn't I tell you before?
    :D
     
  17. Oct 22, 2014 #16
    I'm not sure what your code is doing, but it probably depends on your compiler and what compiler flags you are using. The assigned value might be based on whatever was previously in memory, or your compiler might automatically assign it a value. I don't know.

    Use IMPLICIT NONE!

    This is a huge deal. The rest of the discussion is noise at this point. With out implicit none you should not believe any of the results your code is generating!!!!!! NONE!

    Implicit delcartions can break your code in too many ways.
     
  18. Oct 22, 2014 #17

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

  19. Oct 22, 2014 #18

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Yeah, I might go on and try to get that implemented. The code has the declaration "implicit real(kind=reel8) (a-h,o-z)" instead, I'm not sure what's up with that.
     
  20. Oct 22, 2014 #19

    Mark44

    Staff: Mentor

    I seem to remember that you asked about this before in another thread. This statement means that all variables whose names begin with a through h and o through z are 8-byte reals.

    DrClaude had this to say about these implicit statements in this thread - https://www.physicsforums.com/threads/fortran-help-figuring-out-zheev.769647/:
     
  21. Oct 22, 2014 #20

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    Ah ok. In that case, I would have to replace it, and then declare all the letters used in all the loops in this program. I'll...get around to that...hopefully.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] something very peculiar
  1. Linking Fortran (Replies: 5)

Loading...