Fortran Fortran Forum for Programming Help: Resources, Tips, and Support

  • Thread starter Thread starter FUNKER
  • Start date Start date
  • Tags Tags
    Fortran Forum
Click For Summary
The discussion revolves around programming in Fortran, specifically addressing issues related to Fortran 77 and 90/95. A user seeking help with array declarations and common blocks encounters multiple errors in their code. Key points include the importance of correctly declaring arrays and understanding the limitations of common blocks in Fortran. It is emphasized that array dimensions must be defined using integer values, and the use of floating-point numbers for array sizes is incorrect. The conversation also touches on the need for proper syntax, such as starting code lines in the correct column to avoid compilation errors. Users are advised to break down their programming tasks into manageable steps, utilize pseudocode for clarity, and compile code frequently to identify errors early. Additionally, there are discussions about the inability to dynamically allocate memory in Fortran 77, contrasting it with more modern versions that allow for such flexibility. The thread concludes with inquiries about breaking loops in subroutines and plotting functions, indicating ongoing challenges faced by users in mastering Fortran programming.
  • #91
Please Mathmate can you help me out in this, I need to solve a system of linear equations with complex coefficients, so I have used the NAG routine F04CAF.F, pleas see the attached which are the F04CAF.F and my file which is just the NAG file updated abit and does work fine for the given test example in pdf file, but is not giving me the right answer for this simple system
(2+2i)x+4y=1+i
-ix+2y=3
with solutions x=2.5+1.5i and y=0.75+1.25i
 

Attachments

Technology news on Phys.org
  • #92
segmentation fault

hii guys,
can anybody help me with a problem in my fortran programming..
basically i use fortran77...in which i have recently wrtten a large code...
It was running properly earlier for all combinations of cycles and steps...but nw it works only for smaller no. of cycles and steps...for larger ones..it takes the run command,runs for about an hr. or two giving sme output properly but then it suddenly stops with the message-----SEGMENTATION FAULT...and the outfile remains incomplete...
if anybody knws the solution...kindly help me with the same..
 
  • #93
Hello Mohs,
Segmentation fault usually means that you are trying to access an area of memory that does not belong to your program. This usually happens in one of the many cases, most of which relate to arrays out of bounds, directly or indirectly.
An array out-of-bounds can cause the problem directly, or it can overwrite some data or even program in your work area, thus causing the program to go haywire, and eventually it tries to branch outside of its execution area, and then segmentation error results.

Try to execute some data (large number of cycles and steps) that used to run before. If it still runs, there is a good chance you have to recheck your code. If the program does not run old (good) data, the environment has probably changed. So check what has changed since, code, memory, compiler, etc.

Good luck, and let us know how it goes.
 
  • #94
Anglea,

You will find many different versions of diff on XP (if that's your operating system) by googling "diff xp". I do not use any of those because I have a unix simulator from years back.

Also, I will look at the files and get back to you shortly.
 
  • #95
Anglea,
I tried to compile the code you send me, but am missing a module X04DBF.
Irrespective of that, could you confirm if your code gives you the following answers:
x=(11i-3)/10,y=-(3i-19)/20
This is what I got solving the equations manually, and have checked by back-substitution.
Could you double check on the problem or the answers?
 
  • #96
mathmate said:
Anglea,

You will find many different versions of diff on XP (if that's your operating system) by googling "diff xp". I do not use any of those because I have a unix simulator from years back.

thanks aloooooot...I am using unix, so what is the command to check weather I have it on not? regarding to the linear system ...yes I've got the same as you've got manually...did you solve them by eliminating the variables, cos that is what I used to get my solutions...
 
  • #97
Diff is directly available on any unix system. If you have the command-line screen where you can type in commands, typing in "man diff" will give you the command syntax.
Basically, use the cd command to go to the directory where the two source files reside, then type
diff file1.for file2.for
it will display the diffrerences, if any. Man diff will show you how to interprete the results.

with solutions x=2.5+1.5i and y=0.75+1.25i
Are these solutions from the program or are these your hand-calculated values?
I did mine by elimination, as you thought.

Perhaps you may want to check the input data to see if something was amiss. Also, you could try another set of equations just to double check.
 
  • #98
hii mathmate,
thanks for ur reply..i have checked all the runs..and the runs of the earlier types are also not working properly nw..actually i use the services of my institute so there is little way for me to check the changes of memory,compiler etc...the dimensions for the arrays are the same as i was using earlier...though i shud tell u here that i use 3D and 4D arrays...when i remove the one of the 3d arrays the it works smetimes but then it is compulsory for me to include all the 3D and 4D arrays...what shud i do in such a case...
pleasez help...
 
  • #99
In general, 3D and 4D arrays do not cause problems if you use them always as such. Sometimes programmers define a 3D array but access it as a simple array (for efficiency). That's when the size and access method is important. If you don't do that, it would not be a problem. On the other hand, this is the kind of problem that will fail a small sized problem, which seems to be your case.
Then, you can check the function/subroutine parameters. If the parameters in the calling program do not correspond in size, type and function as the subprogram definition, Fortran will likely let it go and give you useless results.
So has the program ever worked before? If so, get that working version and make a comparison (using diff or otherwise) to see what changes you have made, and take it from there.
If the program has never worked (properly) before, then you need to do tracing of the values of the variables at all stages of the calculations, either by a utility supplied with your compiler, or simply making print statements all over the place. For a simple problem, it should not be too troublesome to do so.

So there, give it a try and tell me what you get.
 
  • #100
hii mathmate..
actually my program has worked before properly for all combinations of cycles and steps...and i have nt made any changes since..but nw it has stopped workng properly...yesterday i tried making the initial values of one of the 3D arrays '1' instead of zero...then it worked for 2 cycles ,100steps for which it wasnt working...but when i made it 3 cycles 100 steps...it again strated giving the same problem..i just can't figure out why it is so...
 
  • #101
You may want to check program logic.
Trace back from where problem occurs by printing out values.
Again, find the exact set of data that used to work before, and see if it stops working now.
Print out all values for a simple problem and see where the logic starts to deviate.
Short of seeing the code or running the code, the above are the few options available.
 
  • #102
hii mathmate,
thanks for ur help..my problem has got solved..actually the logic of the program was gettng stuck after reaching a point where the program didnt knw how to proceed..so it was giving seg.fault and crashing..for smaller steps & cycles it was not gettng stuck due to certian conditions...i finally found itr and corrected it..thanks for ur replies..
 
  • #103
Glad that everything works out. Iterative problems are quite tricky and you seem to have managed to provide code for all the foreseeable cases.
 
  • #104
can anyone help me out in ...how to read a specific column id data file in FORTRAN 77, let say I have a data file of 5 columns , I need to read the third column...
 
  • #105
Are the columns in fixed format or free format, for example,
fixed format:
123 2345 2456
482 2421 5820
Free format
1 2024 2520
24556 24821 23

If it is in fixed format, and the third column of data falls betwen the 11th to 15th characters, then

Code:
      READ(9,999)ID
  999 FORMAT(10X,I5)
would do the job.

If it is in free format, try
Code:
      READ(5,*)I,J,K
      PRINT *,K
where I,J are just dummy variables to skip the unnecessary data.
 
  • #106
Thanks Mathmate for your help, can I put the body of subroutine in separate file, and in the main program just write

include 'subroutine_name.f'
the same of including the numerical recipes...
 
  • #107
Yes, but you have to make sure the subroutine parameters allow you to return the arrays that you have just filled.
 
  • #108
Hi MATHmate, I found the angle between two adjacent side in rectangle and then divide by \pi, as I need to know wheather the angles are rational or irrational multiple of \pi, I've got this number 0.397583618, how do I know WEATHER rational or irrational?
 
  • #109
It doesn't look like rational to me.
It is almost impossible to know from a single number because of rounding errors.
Would you have an idea of the three sides?
 
  • #110
let say for polygon, the length of the five sides are 2.82842712, 2.82842712, 2, 2.82842712, 4.47213595, and the angles are after diving by pi 0.5,0.75, 0.75, 0.397583618, 0.852416382

I want to to know if the last two ones are rational or irrational, is there any role to distinguish between the two types, I think it easier if we can force the code to produce the number as numerator and denumertor, but how can we do that...
 
Last edited:
  • #111
The last number (4.47213595) appears to be 2sqrt(5), while the other ones (2.82842712) appear to be 2sqrt(2).
 
  • #112
mathmate said:
The last number (4.47213595) appears to be 2sqrt(5), while the other ones (2.82842712) appear to be 2sqrt(2).

Thanks mathmate, i meant the these two numbers 0.397583618 , 0.602416382 , are they rational or irrational? can one say that ...( if the length of the side of the triangle are irrational number, then the angle will be irrational multiple of \pi?)
 
  • #113
I do not think one implies the other.
Try a 30-60-90 triangle.
The side lengths are 1,2 and sqrt(3), the last side being irrational. Yet the angles are rational fractions (1/6, 1/3, 1/2) of π.
The converse is not valid either. For example, a triangle with rational sides such as 1,2,3 will have most probably all three angles irrational multiples of π.
 
  • #114
small question on fomatting in f77, I typed the line below to write the data in a file, but the data being separated in two lines in the data file, the first 6 numbers in th first line of the data file then the last two numbers in the second line of the data file, how can we write them in one line

999 FORMAT(2x,I2,5x,F9.6,5x,F9.6,5x,F9.6,5x
& ,F9.6,5x, F9.6,5x, F9.6,5x, F9.6)
 
  • #115
Try
999 FORMAT(2x,I2,5x,F9.6,5x,F9.6,5x,F9.6,5x,F9.6,5x, F9.6 / F9.6,5x, F9.6)
or
999 FORMAT(I4,F14.6,F14.6,F14.6,F14.6,F14.6 /F18.6,F14.6)
 
  • #116
Does anyone know of an algorithm to solve coupled first order differential equations?

Thanks,
 
  • #117
how can I integrate a function(f(x)) on polygonal area numerically on each point we can start with double summation and multiply by dx, dy, but how can we end the summation? the double summation seems to be difficult to implemented as a code...
 
  • #118
Are you looking for simply the area, or do you put weights throughout the area?

If it is a matter of simply the area, you do not need to integrate. There are formulae for calculating the area of a non-self intersecting polygon from the known vertices. See for example:
http://en.wikipedia.org/wiki/Polygon
under Area and centroid.

If the polygon is guaranteed convex, and you need to put weights throughout the surface of the polygon as a function of x and y, I suggest you write code for a triangle (which is simpler), and subdivide the polygon into a number of triangles all radiating from a given source vertex. If the polygon has only one concavity, subdivision into triangles will still work if the source vertex is at the concave point.
 
  • #119
mathmate said:
Are you looking for simply the area, or do you put weights throughout the area?

If it is a matter of simply the area, you do not need to integrate. There are formulae for calculating the area of a non-self intersecting polygon from the known vertices. See for example:
http://en.wikipedia.org/wiki/Polygon
under Area and centroid.

If the polygon is guaranteed convex, and you need to put weights throughout the surface of the polygon as a function of x and y, I suggest you write code for a triangle (which is simpler), and subdivide the polygon into a number of triangles all radiating from a given source vertex. If the polygon has only one concavity, subdivision into triangles will still work if the source vertex is at the concave point.
nooooooo not the area, I have some function with given value at points in the polygon with regular grids, what I need is how to code the numerical integral over the polygon, for example if I have a rectangle I will integrate by doing summation over the region by increasing x till the length and increase y till the width of the rectangle, but for ploygon how can I do this with double summation
 
  • #120
If there are no libraries available for this purpose, you will have to write your own.

If the polygon is convex, I would subdivide the polygon into triangles and write a double integration function for a triangle with the given vertices, together the given weight function.

For integration over the area of the triangle, you will need to split the integration into two regions through the line that passes through the middle vertex (along the x-axis). This is where there is a discontinuity in the integration.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
6
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
38K
  • · Replies 11 ·
Replies
11
Views
6K