Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Type To Function Causes Data Corruption

Status
Not open for further replies.

Travis214

Programmer
May 19, 2009
9
CA
I have defined two simple types in a module in Fortran 95. The types are defined as:
TYPE :: SPINVECTOR
DOUBLE PRECISION, dimension(0:3,1:3,1:3,1:3) :: x, y, z
END TYPE SPINVECTOR
TYPE :: IVECTOR
integer :: x, y, z
END TYPE IVECTOR

I am then using these types to pass information from the main program to a function. The function uses the data from the SPINVECTOR type to assign values to a variable of type IVECTOR which is needed by main.
Data passed using the SPINVECTOR type is fine but if I use the IVECTOR type I get different results from my calculations that are done in the function. Both the main program and the function have use module statements.
So, is there some sort of data corruption that can happen when passing types as parameters and, if so, why am I only getting strange results when I use the second one (if only spinvector is used I get the same results as if I passed the function three separate variables for the x, y, and z component for the IVECTOR).
 
UPDATE:
I went back and removed all of the uses of IVECTOR (and not SPINVECTOR) and I have the same problem. Maybe the first run where the results did not change was a fluke.
 
I don't see any problems with your datatypes. If you have a problem with it, I guess that you have an error elsewhere in your program code.
As you posted nothing, here is an example:
vectors.f90
Code:
[COLOR=#a020f0]module[/color] my_vectors
  [COLOR=#2e8b57][b]type[/b][/color] spinvector
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] ([COLOR=#ff00ff]0[/color]:[COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]1[/color]:[COLOR=#ff00ff]3[/color]) :: x, y, z
  [COLOR=#2e8b57][b]end type[/b][/color] spinvector

  [COLOR=#2e8b57][b]type[/b][/color] ivector
    [COLOR=#2e8b57][b]integer[/b][/color] :: x, y, z
  [COLOR=#2e8b57][b]end type[/b][/color] ivector

[COLOR=#a020f0]contains[/color]
  [COLOR=#a020f0]function[/color] sv2iv(sv, i, j, k, l) [COLOR=#a020f0]result[/color](iv)
    [COLOR=#0000ff]! spinvector to ivector[/color]
    [COLOR=#0000ff]! converst the (i, j, k, l)-th component of sv into iv [/color]
    [COLOR=#2e8b57][b]type[/b][/color](spinvector), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: sv
    [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: i, j, k, l
    [COLOR=#2e8b57][b]type[/b][/color](ivector) :: iv

    [COLOR=#0000ff]! return value[/color]
    iv%x [COLOR=#804040][b]=[/b][/color] sv%x(i, j, k, l)
    iv%y [COLOR=#804040][b]=[/b][/color] sv%y(i, j, k, l)
    iv%z [COLOR=#804040][b]=[/b][/color] sv%z(i, j, k, l)
  [COLOR=#a020f0]end function[/color] sv2iv

[COLOR=#a020f0]end module[/color] my_vectors

[COLOR=#a020f0]program[/color] vectors
  [COLOR=#a020f0]use[/color] my_vectors

  [COLOR=#2e8b57][b]type[/b][/color](spinvector) ::my_sv
  [COLOR=#2e8b57][b]type[/b][/color](ivector) ::my_iv

  my_sv%x([COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1.41[/color]
  my_sv%y([COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2.71[/color]
  my_sv%z([COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color]) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3.14[/color]

  my_iv [COLOR=#804040][b]=[/b][/color] sv2iv(my_sv,[COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color])

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_iv%x = "[/color], my_iv%x
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_iv%y = "[/color], my_iv%y
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_iv%z = "[/color], my_iv%z
[COLOR=#a020f0]end program[/color] vectors
Output:
Code:
$ g95 vectors.f90 -o vectors

$ vectors
 my_iv%x =  1
 my_iv%y =  2
 my_iv%z =  3

But, I'm not sure if you want such datatypes: The spinvector is a structure which consists of 3 vectors of the same dimensions and the ivector is a structure consisting of 3 numbers.

 
Thanks for your help. That is very similar to what I am trying to do. I have been over the code several times and I can't see anything else wrong, but I guess I will keep looking.
 
I have played around with my code a lot and found a spot where I had changed how a calculation was done to increase efficiency which included a negative sign that was not in the original. The new equation still gives valid results, but they are different. I reverted the calculation to its original form and regained my original results.
The new equation assigns one variable the negative of another. So after reverting the calculation type I created a dummy variable and assigned it the negative of the other value. The dummy variable is never used except in the assignment but my results come out different. If I remove the negative sign and just do a straight assignment this does not happen. Also trying a multiply by -1.0D+00 causes the same results as using a negative. What is going on?

P.S. I am working on making the code short enough to upload without making you have to read through all of the code, not all of which is actually used for my current calculation (but is not the cause of the error).
 
I have found my problem. After hours of playing around with the code someone suggested that my problem may be from an array out of bounds problem. After compliling with -fbounds-check (wish I had heard of this before now) it turned out that that was the case. So the negation was probably changing the value when the array indices went out of bounds.
Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top