Finding Maximum Acceleration Values in Fortran

  • Context: Fortran 
  • Thread starter Thread starter ShoxPhysics
  • Start date Start date
  • Tags Tags
    Fortran Maximum
Click For Summary

Discussion Overview

The discussion revolves around a Fortran program designed to read acceleration data from a CSV file and calculate maximum acceleration values for three variables (ivar1, ivar2, ivar3) as well as their averages. The scope includes programming, technical implementation, and debugging of the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks help on how to find the maximum values for ivar1, ivar2, and ivar3 in their Fortran code.
  • Another participant questions the effort of the original poster and suggests that they should attempt the problem independently.
  • A later reply indicates that the original code was incorrect and presents an updated version that includes maximum value calculations, but expresses uncertainty about its correctness.
  • One participant points out that ivar1, ivar2, and ivar3 are scalars, not arrays, and thus the use of the 'max()' function is inappropriate.
  • There is a suggestion to initialize maximum variables to zero before entering the loop and to compare the current ivar values to these maximum variables to find the largest value.
  • A participant proposes a separate loop for reading values, but another suggests that the maximum value comparisons should occur within the same loop.
  • One participant expresses gratitude for the guidance received.

Areas of Agreement / Disagreement

Participants express differing views on the approach to finding maximum values, with some suggesting modifications to the original code while others question the original poster's effort. The discussion remains unresolved regarding the best method to implement the maximum value calculations.

Contextual Notes

There are limitations in the original code regarding the handling of scalar versus array variables, and the need for proper initialization of maximum value variables is highlighted. The discussion also reflects uncertainty about the correctness of the proposed solutions.

ShoxPhysics
Messages
5
Reaction score
0
Hello I am currently working on a program that will take the values from a .csv file containing multiple data and analyzing them. The file can be found here: https://docs.google.com/file/d/0BzuDEPd26OcheVhiWlZ3STlZU0k/edit?usp=sharing The data in question starts in row 11 and is in the first 4 columns. In my program I have labeled these: rvar, ivar1, ivar2, ivar3. Rvar is the value corresponding to a time value in microseconds after start of recording whereas ivar1,ivar2,and ivar3 correspond to the value of the acceleration at that point. I have already calculated the average value of each of these columns.

My question is- how do I find the maximum value for ivar1, ivar2 and ivar3 (separately)?

My current code is below:
Code:
program WSUPhys_csv
  implicit none

  integer :: stat, num_lines, ivar1, ivar2, ivar3
  real :: rvar
  character*80 :: line
  
  write(*,*) "Reading file..."

  ! open input file
  open(10,file='DATA-002.csv',status='old',iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99   
  end if

  num_lines = 0
  do
    read(10,'(A)',end=99, iostat=stat) line 
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    ! skip comment/header lines beginning with ";"
    if (adjustl(trim(line(1:1))) .eq. ';') then
      cycle
    end if
    ! read string line into numeric variables
    read(line,*) rvar, ivar1, ivar2, ivar3
    num_lines = num_lines + 1    
    if (num_lines <=  10) then
      write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
    end if
  Sum_ivar1 = Sum_ivar1 + ABS(ivar1) 
  Sum_ivar2 = Sum_ivar2 + ABS(ivar2) 
  Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
  Max
  end do

  ! close file
  99 continue
  Avg_ivar1 = (Sum_ivar1 / num_lines)
  Avg_ivar2 = (Sum_ivar2 / num_lines)
  Avg_ivar3 = (Sum_ivar3 / num_lines)
  close (10)
  !
  write(*,*) '...done.'
  write(*,*) 'Lines processed: ', num_lines
  write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
  write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
  write(*,*) 'Average forward acceleration', Avg_ivar3/1000
end program WSUPhys_csv
 
Technology news on Phys.org
The program is named WSUPhys_csv ...is it because you are the same person as WSUPhys from the Tek-Tips Forum? You going around getting other people to do your work? Or are you a class mate and copied all this code from over there?

Either way, at least give a try, this is easy stuff...you can't simply keep asking for help without showing any effort, whatsoever...o.k., you actually can; but most of us forum people don't like it that way.
 
gsal, I apologize. The incorrect version posted, this version is the updated one with Maximum values but I am unsure if I have coded correctly. New code is in blue
Code:
program WSUPhys_csv
  implicit none

  integer :: stat, num_lines, ivar1, ivar2, ivar3
  real :: rvar
  character*80 :: line
  
  write(*,*) "Reading file..."

  ! open input file
  open(10,file='DATA-002.csv',status='old',iostat=stat)
  if (stat .ne. 0) then
    write(*,*) 'File cannot be opened !'
    go to 99   
  end if

  num_lines = 0
  do
    read(10,'(A)',end=99, iostat=stat) line 
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    ! skip comment/header lines beginning with ";"
    if (adjustl(trim(line(1:1))) .eq. ';') then
      cycle
    end if
    ! read string line into numeric variables
    read(line,*) rvar, ivar1, ivar2, ivar3
    num_lines = num_lines + 1    
    if (num_lines <=  10) then
      write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3
    end if
  Sum_ivar1 = Sum_ivar1 + ABS(ivar1) 
  Sum_ivar2 = Sum_ivar2 + ABS(ivar2) 
  Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
  MaxVert = MAX(ivar1)
  MaxSide = MAX(ivar2)
  MaxForward= MAX(ivar3)
  end do

  ! close file
  99 continue
  Avg_ivar1 = (Sum_ivar1 / num_lines)
  Avg_ivar2 = (Sum_ivar2 / num_lines)
  Avg_ivar3 = (Sum_ivar3 / num_lines)
  close (10)
  !
  write(*,*) '...done.'
  write(*,*) 'Lines processed: ', num_lines
  write(*,*) 'Average vertical acceleration', Avg_ivar1/1000
  write(*,*) 'Average sideways acceleration', Avg_ivar2/1000
  write(*,*) 'Average forwards acceleration', Avg_ivar3/1000
  write(*,*) 'Maximum vertical acceleration', MaxVert
  write(*,*) 'Maximum sideways acceleration', MaxSide
  write(*,*) 'Maximum forward acceleration',  MaxForward
 
end program WSUPhys_csv
 
Last edited by a moderator:
Your ivar1, ivar2, ivar3 variables are NOT arrays, they are scalars and they contain the value just read. So, it does not make sense to apply the 'max()' function to them.

You need initialize your Max* variables to zero before you go into the loop; then, inside the loop, you need to compare the ivar* value just read to the one stored in the corresponding Max* variable and keep the largest of the two.
 
So do I create a separate do loop?

something like this:

Code:
Do i=1,72000
 read(line,*) rvar, ivar1, ivar2, ivar3
 If (ivar1 >> ivar1) then
  MaxVert=ivar1

or am I way off track?
 
Within the same loop

Code:
 If (ivar1 > MaxVert) then
  MaxVert=ivar1
end if
 
Thank you!
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
8K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
9K