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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Velocity from a data set-Fortran

Status
Not open for further replies.

WSUPhys

Technical User
Feb 6, 2013
20
US
I have cross posted this to a few other forums, in order to increase my chances of getting an answer. 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"][/URL] 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
  [COLOR="Navy"]SumVel=0
  MaxVel=0[/COLOR]
  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)
    [COLOR="Navy"]Vel=(ABS(ivar_3))*(rvar-rvar)
    SumVel=SumVel+ ABS(Vel)[/COLOR]
    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   
    [COLOR="Navy"]If (Vel > MaxVel) then
      MaxVel=Vel[/COLOR]
  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)
    [COLOR="Navy"]AvgVel = (SumVel / num_lines)[/COLOR]
    !

    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
    [COLOR="Navy"]write (*,*) 'Average forward velocity', AvgVel
    write (*,*) 'Maximum velocity', MaxVel[/COLOR]
  end if  
end program WSUPhys_csv
 
okay, I have rewritten this program but I am still having trouble with getting the velocity. I know that the v=a*t (general form), which for my program should mean Velocity=ivar3*(rvar-(the rvar of the previous row)), how do I get the rvar of the previous row into my equation?
 
WSUPhys said:
I think that this velocity should be calculated by taking the value from the 4th column of my data set here ... which have called ivar3 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?
Nobody hereknows what about is your data. You must know it. This is the forum about programming not Physics.

But I doubt what you wrote about the velocity above.
If your 4.th column (i.e. iva3) represents path and 1.st column (i.e. rvar) represents time, then the velocity should be the difference
v[sub]n+1[/sub] = (ivar3[sub]n+1[/sub] - ivar3[sub]n[/sub])/(rvar[sub]n+1[/sub] - rvar[sub]n[/sub])



 
When ivar3 is acceleration, then what you wrote could be true.
 
how do I get the rvar of the previous row into my equation?
Before reading current value of rvar, store the old value into rvar_old
 
.... and you should consider what unit your data are in ....
.... and maybe you should set the sum vector of ax, ay, and az to define speed ....

How are we expected to know what is the meaning of your data and what you want to evaluate out of them? Speed in only one projection? Or vectorized?
You should have some knowledge or documentation of what was measured in there.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
rvar is a variable representing a specific time; ivar1,ivar2,and ivar3 are variables representing the acceleration of the body that the accelerometer was attached to. I know from physics, that in general, v=a*t where t is my change in time. So, I want to take the value of my acceleration and multiply it by my change in time, which is equal to rvar of the line of the acceleration minus the previous rvar.

so Mikrom what I believe you are saying is to do something like this:

set
Code:
rvar_old=0
then
Code:
If (rvar > rvar_old) 
                then Vel=(ABS(ivar3))*(rvar-(rvar_old))
              end if
Am I on the right track here, or am I forgetting something?
 
When you set it as described above, you will have constantly rvar_old=0

You need to set it everytime before reading new values - like this
Code:
    ...
    ! save previous value of rvar
    rvar_old = rvar
    ! read string line into numeric variables
    read(line,*) rvar, ivar1, ivar2, ivar3
    ...
 
something like this?:
Code:
program WSUPhys_csv
  implicit none

  integer :: stat,cmd_rc,num_lines, ivar1, ivar2, ivar3
  integer :: MaxVert,MaxSide,MaxForward
  real :: rvar, Sum_ivar1, Sum_ivar2, Sum_ivar3
  real :: Avg_ivar1, Avg_ivar2, Avg_ivar3,Vel, SumVel, MaxVel
  real :: AVGVel,rvar_old,timedif
  ! command string - using sed:
  ! delete lines 1-10 and replace "," with " "
  character(*), parameter :: cmd_string = &
     'sed -e "1,11d; s/,/ /g" DATA-002.CSV > DATA-002.txt'

  
  write(*,*) "Preprocessing file..."
  cmd_rc = system (cmd_string)
  ! if command not succesfull then stop the program
  if ( cmd_rc .ne. 0 ) then 
    stop 'system: Error - command failed !!!'
  else
    write(*,*) '...done.'
  end if

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

  ! open input file
  open(10,file='DATA-002.txt',status='old',iostat=stat)
  if (stat .ne. 0) then
    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
    ! save previous value of rvar
    rvar_old = rvar
    read(10,*,end=99, iostat=stat) rvar, ivar1, ivar2, ivar3, timedif
    if (stat .ne. 0) then
      write(*,*) 'Error reading data !'
      go to 99
    end if
    num_lines = num_lines + 1
    if (num_lines <=  10) then
      write(*,*) num_lines, ':', rvar, ivar1, ivar2, ivar3, timedif
    end if
    Sum_ivar1 = Sum_ivar1 + ABS(ivar1) 
    Sum_ivar2 = Sum_ivar2 + ABS(ivar2) 
    Sum_ivar3 = Sum_ivar3 + ABS(ivar3)
    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 (rvar > rvar_old) then 
      Vel=(ABS(ivar3))*(rvar-rvar_old)
    end if  
    if (Vel>MaxVel) then
      MaxVel=Vel
    end if
  end do

  ! close file
  99 continue
  close (10)
  ! 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(*,*) '...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/1000
  write(*,*) 'Maximum sideways acceleration', MaxSide/1000
  write(*,*) 'Maximum forward acceleration ',  MaxForward/1000
  write(*,*) 'velocity', AvgVel
  write(*,*) 'Maximum velocity',MaxVel/100
  
end program WSUPhys_csv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top