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!

Read file from comsol

Status
Not open for further replies.

dimb

Technical User
Dec 2, 2010
1
Hello everyone
I started to use FORTRAN 77 week ago
I'm trying to read from a file, I use FORTRAN 77, I can not understand exactly what happens
******************************************************
That is an example of the data file :

Model: config_4.mph
% Version: COMSOL 4.0.0.993
% Date: Oct 2 2010, 23:51
% Dimension: 3
% Nodes: 123220
% Expressions: 4
% Description: Magnetic flux density, x component, Magnetic flux density, y component, Magnetic flux density, z component, Magnetic flux density norm
% x y z mf.Bx (T) mf.By (T) mf.Bz (T) mf.normB (T)
0.7 0.048691700470630664 -0.16502893113726974 -17.48728187721925 -0.1614065348511069 0.10294566053003251 17.488329746772145
0.7 0.042072027653693084 -0.149161178712754 -17.79973537031668 -0.21478827392261124 -0.20897089738410557 17.802257780738678
0.7 0.027414539338143515 -0.16044804853035652 -14.60770546033153 0.20830436409930542 0.001925509990911678 14.609190711038416
0.7149693019789779 0.039708881079374994 -0.1595764600327363 -15.977552204317588 -2.229527239282659 -1.1600150527838873 16.174010049306954
0.7 0.03397038565137395 -0.18081820743294008 -14.835298462101358 0.08954350306353831 0.2839581596700995 14.838285976992458
0.7 0.048691700470630664 -0.16502893113726974 -17.582924467091733 -0.5294189756439849 0.1596965562548931 17.59161789766094
0.7 0.027414539338143515 -0.16044804853035652 -14.719912190769339 0.3319056815995296 -0.05105932487671083 14.7237421649557

*****************************************************
I want to receive the coordinates and cosines of the magnetic field in different arrays
That's what i did:
**********MAGfld**************
integer nmax, u
parameter (nmax=123220, u=20)
real posx(nmax), posy(nmax), posz(nmax)
real mx(nmax), my(nmax), mz(nmax), normb(nmax)

open (u, FILE='XYZ', STATUS='OLD')
read(u,*) n
if (n.GT.nmax) then
write(*,*) 'Error: n = ', n, 'is larger than nmax =', nmax

endif
do 10 i= 1, n
read(u,100) posx(i), posy(i), posz(i), mx(i), my(i), mz(i)
read(u,100) normb(i)
10 enddo
100 format (7(F10.4))
************************************************************

Please help me :(

Thanks in advance
 
Your program is for reads a file in form of
Code:
7
0.7 0.048691700470630664 -0.16502893113726974 -17.48728187721925 -0.1614065348511069 0.10294566053003251 17.488329746772145
0.7 0.042072027653693084 -0.149161178712754 -17.79973537031668 -0.21478827392261124 -0.20897089738410557 17.802257780738678
0.7 0.027414539338143515 -0.16044804853035652 -14.60770546033153 0.20830436409930542 0.001925509990911678 14.609190711038416
0.7149693019789779 0.039708881079374994 -0.1595764600327363 -15.977552204317588 -2.229527239282659 -1.1600150527838873 16.174010049306954
0.7 0.03397038565137395 -0.18081820743294008 -14.835298462101358 0.08954350306353831 0.2839581596700995 14.838285976992458
0.7 0.048691700470630664 -0.16502893113726974 -17.582924467091733 -0.5294189756439849 0.1596965562548931 17.59161789766094
0.7 0.027414539338143515 -0.16044804853035652 -14.719912190769339 0.3319056815995296 -0.05105932487671083 14.7237421649557
i.e. in the 1.line should be the number of data lines.
I corrected a little bit the source
Code:
[COLOR=#2e8b57][b]integer[/b][/color] nmax, u
[COLOR=#2e8b57][b]parameter[/b][/color] (nmax[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]123220[/color], u[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]20[/color])
[COLOR=#2e8b57][b]real[/b][/color] posx(nmax), posy(nmax), posz(nmax), dummy
[COLOR=#2e8b57][b]real[/b][/color] mx(nmax), my(nmax), mz(nmax), normb(nmax)

[COLOR=#804040][b]open[/b][/color] (u, [COLOR=#804040][b]FILE[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'XYZ'[/color], [COLOR=#804040][b]STATUS[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'OLD'[/color])
[COLOR=#804040][b]read[/b][/color](u,[COLOR=#804040][b]*[/b][/color]) n
[COLOR=#804040][b]if[/b][/color] (n[COLOR=#804040][b].GT.[/b][/color]nmax) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error: n = '[/color], n, [COLOR=#ff00ff]'is larger than nmax ='[/color], nmax
[COLOR=#804040][b]endif[/b][/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'n= '[/color], n

[COLOR=#804040][b]do[/b][/color] [COLOR=#ff00ff]10[/color] i[COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
  [COLOR=#804040][b]read[/b][/color](u,[COLOR=#804040][b]*[/b][/color]) posx(i), posy(i), posz(i), mx(i), my(i), mz(i), normb(i)
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]enddo[/b][/color]
[COLOR=#6a5acd]100[/color] [COLOR=#804040][b]format[/b][/color] ([COLOR=#008080]7F10.4[/color])

[COLOR=#0000ff]! display data[/color]
[COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'i= '[/color], i
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) posx(i), posy(i), posz(i), mx(i), my(i), mz(i), normb(i)
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end[/color]
now it compiles and runs
Code:
$ g95 xyz_read.f95 -o xyz_read             

$ xyz_read
 n=  7
 i=  1
 0.7 0.0486917 -0.16502893 -17.487282 -0.16140653 0.10294566 17.488329
 i=  2
 0.7 0.042072028 -0.14916117 -17.799736 -0.21478827 -0.2089709 17.802258
 i=  3
 0.7 0.02741454 -0.16044804 -14.607705 0.20830436 0.00192551 14.609191
 i=  4
 0.7149693 0.039708883 -0.15957646 -15.977552 -2.2295272 -1.1600151 16.17401
 i=  5
 0.7 0.033970386 -0.1808182 -14.835299 0.08954351 0.28395817 14.838286
 i=  6
 0.7 0.0486917 -0.16502893 -17.582924 -0.529419 0.15969655 17.591618
 i=  7
 0.7 0.02741454 -0.16044804 -14.719913 0.3319057 -0.051059324 14.7237425
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top