Vector Magic and Naming Conventions for Fortran

  • Thread starter Thread starter ShoxPhysics
  • Start date Start date
  • Tags Tags
    Data Set Velocity
AI Thread Summary
The discussion revolves around calculating velocity in a Fortran program that processes a CSV file. The user seeks confirmation on their approach, which involves using a value from the fourth column (ivar_3) and multiplying it by the difference between the first column's current and previous line values. However, they encounter compilation issues due to undeclared variables, specifically SumVel and MaxVel. Additional feedback highlights the importance of declaring all variables when using "implicit none" and suggests improvements for code readability and structure. A sample code snippet is provided to illustrate better practices in handling vectors and performing calculations in Fortran. The conversation emphasizes the need for proper variable management and the potential for enhancing the program's efficiency through clearer coding techniques.
ShoxPhysics
Messages
5
Reaction score
0
I have just about finished this program, I only need to calculate my velocity. I think that this velocity should be calculated by taking the value from the 4th column of my data set here:https://docs.google.com/file/d/0BzuDEPd26OcheVhiWlZ3STlZU0k/edit?usp=sharing which have called ivar_3 and multiplying it by the difference of the 1st column in the line of the value and the line preceding it. Is this thought process correct? I have done the following (code I am talking about is in blue), can somebody tell me where I might be going wrong because it will not compile?
Code:
program WSUPhys_csv
  implicit none

  integer :: stat, num_lines, ivar1, ivar2, ivar3, &
             MaxVert,MaxSide,MaxForward
  real :: rvar, Sum_ivar1, Sum_ivar2, Sum_ivar3, & 
          Avg_ivar1, Avg_ivar2, Avg_ivar3, Vel, SumVel, MaxVel
  character*80 :: line
  logical :: err
  
  err = .false.

  write(*,*) "Reading file..."

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

  num_lines = 0
  Sum_ivar1 = 0
  Sum_ivar2 = 0
  Sum_ivar3 = 0
  MaxVert=0
  MaxSide=0
  MaxForward=0
  SumVel=0
  MaxVel=0
  do
    read(10,'(A)',end=99, iostat=stat) line 
    if (stat .ne. 0) then
      err = .true.
      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)
    Vel=(ABS(ivar_3))*(rvar-rvar)
    SumVel=SumVel+ ABS(Vel)
    If (ABS(ivar1) > MaxVert) then
      MaxVert=ivar1
    end if
    If (ABS(ivar2) > MaxSide) then
      MaxSide=ivar1
    end if
    If (ABS(ivar3) > MaxForward) then
      MaxForward=ivar1
    end if   
    If (Vel > MaxVel) then
      MaxVel=Vel
  end do
  ! close file
  99 continue
  close (10)

  ! when file processing OK
  if (.not. err) then
    write(*,*) '...done.'
    ! compute averages
    Avg_ivar1 = (Sum_ivar1 / num_lines)
    Avg_ivar2 = (Sum_ivar2 / num_lines)
    Avg_ivar3 = (Sum_ivar3 / num_lines)
    AvgVel = (SumVel / num_lines)
    !

    write(*,*) 'Lines processed: ', num_lines
    write(*,*) 'Average vertical acceleration', Avg_ivar1
    write(*,*) 'Average sideways acceleration', Avg_ivar2
    write(*,*) 'Average forwards acceleration', Avg_ivar3
    write(*,*) 'Maximum vertical acceleration', MaxVert
    write(*,*) 'Maximum sideways acceleration', MaxSide
    write(*,*) 'Maximum forward acceleration ',  MaxForward
    write (*,*) 'Average forward velocity', AvgVel
    write (*,*) 'Maximum velocity', MaxVel
  end if  
end program WSUPhys_csv
 
Last edited:
Technology news on Phys.org
You have an "implicit none" statement which means you have to declare all the variables you use.

You haven't declared SumVel and MaxVel.

There may be more errors, but that's a start.
 
In addition to AlphaZero's remark:

ShoxPhysics said:
Sum_ivar1 = 0
Sum_ivar2 = 0
Sum_ivar3 = 0
MaxVert=0
MaxSide=0
MaxForward=0
Ouch!

Fortran is sort-of a short for Formula Translating and it has that name for a reason - namely for providing means for easy translation of mathematical calculus into a form understandable by machines.

So - what would it be good for if it would not provide means to deal with basic algebraic concepts like vectors and matrices?

Instead of stepping line-by-line through this I've compiled a little snippet from which you may take some concepts for improving readability and structure of your code.

Code:
!----------------------------------------------------------------------------80
!> @file vector_demo.f90
!> @brief illustrating some basic Fortran vector magic, useful practices 
! and proposes a consistent naming convention
!> @author Solkar
!------------------------------------------------------------------------------

!------------------------------------------------------------------------------
!> @function vector_demo
!> @brief entry point, main routine
!------------------------------------------------------------------------------
program vector_demo
    implicit none
    !- parameters ("constants" in C) in mulit-char CAPS,
    integer,                        parameter :: RKIND = 8      ! precision
    !- ... everything else (except matrices) small,
    real(kind=RKIND)                          :: dt             ! time diff
    !- ... vectors with trailing underscore
    real(kind=RKIND), dimension(3)            :: a_, v_         ! accel, velo
    
    a_ = (/-1, -2, -3/) !- Yes. This will convert the integers to the
                        ! (real) vector components
    v_ = 0              !- zeroing all components at once
    dt = 0.42           !- always good to know where the towel is...

    !> plain algebra
    ! \f[ \vec{v} \leftarrow \vec{v} + \Delta t \cdot 
    ! \left(\begin{array}{c} |a_1|\\ |a_2|\\ |a_3| \end{array}\right) \f]

    v_ = v_ + dt * abs(a_) !> this abs() works component-wise(!)

    write(*,*) v_   
end program vector_demo

Have fun!

Solkar
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
6
Views
3K
Back
Top