Fortran subroutine solving for the velocity and displacement of a spring

In summary, the code that uses a subroutine to calculate the derivative will be quite a bit slower than when you do the calculation inline, as in your first attempt.
  • #1
joseph2015
14
2
I have a simple Fortran code for solving velocity and displacement of spring, the code works fine when writing all together without using a subroutine. but as I am learning Fortran I tried to do it using subroutine, but I keep getting errors
appreciate any help in advance.

Fortran:
program msd
implicit none
real, parameter :: tstop=100.
integer i
real, dimension(2000) :: t,y,v

open (2, file = "test.txt")!v(1)=0.0
!y(1)=1.0
!t(1)=0.0

  do i=1,1000

  call deriv(v,y,t)

  write (2,*) v(i),y(i),t(i)

  end do

close(2)
end program msd

!******************************************************

subroutine deriv(v,y,t)
implicit none
real, parameter :: m=1.,b=0.2,k=1., dt=0.1
real, dimension(2000) :: t,y,v
integer i

v(1)=0.0
y(1)=1.0
t(1)=0.0

    v(i+1) = v(i) +(-(b/m)*v(i)-(k/m)*y(i))*dt
    y(i+1) = y(i) + v(i)*dt
    t(i+1) = t(i)+dt

  v(i)=v(i+1)
  y(i)=y(i+1)
  t(i)=t(i+1)

  end subroutine deriv
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
joseph2015 said:
but I keep getting errors
What are the error messages, exactly? Do they refer to specific lines of code?

[added]

I see an issue which may or may not be related to your error messages.

In the main program, the first pass of your do-loop sets its variable i to 1. Then it calls the subroutine.

The subroutine also has a variable named i, but this is separate and independent of the variable i in the main program. You did not make i an argument of the subroutine, nor did you put it in a 'common' block that is shared by the main program and subroutine.

When you first use i in the subroutine, v(i+1) = ... you have not yet set it to a specific value, therefore it contains a "garbage" value which is effectively random. Therefore v(i) and v(i+i) do not refer to the memory locations that you probably want them to refer to.
 
Last edited:
  • #3
Does your subroutine have the same value of i as your main program?
You may want to pass that as a parameter also.
 
  • #4
Thank you very much jtbell and FactChecker, the issue was not passing i. I really appreciate your help.
 
  • #5
joseph2015 said:
the issue was not passing i.
I don't see how it could work the way you have it, but I will not argue with success.
 
  • #6
Dear FactChecker the following is the corrected version that works with your helps :), Thanks

Fortran:
======================================================================
program msd
implicit none
real, parameter :: tstop=100.
integer i
real, dimension(2000) :: t,y,v

open (2, file = "test.txt")

  do i=1,1000

  call deriv(v,y,t,i)

  write (2,*) t(i),y(i),v(i)

  end do

close(2)
end program msd

!******************************************************
subroutine deriv(v,y,t,i)
implicit none
real, parameter :: m=1.,b=0.2,k=1., dt=0.1
real, dimension(2000) :: t,y,v
integer i

v(1)=0.0
y(1)=1.0
t(1)=0.0

    v(i+1) = v(i) +(-(b/m)*v(i)-(k/m)*y(i))*dt
    y(i+1) = y(i) + v(i)*dt
    t(i+1) = t(i)+dt

  v(i)=v(i+1)
  y(i)=y(i+1)
  t(i)=t(i+1)

  end subroutine deriv
 
Last edited by a moderator:
  • Like
Likes FactChecker
  • #7
joseph2015 said:
the code works fine when writing all together without using a subroutine. but as I am learning Fortran I tried to do it using subroutine
Since you now have both programming styles working, it might be interesting to compare running times for each. I suspect that the code that uses a subroutine to calculate the derivative will be quite a bit slower than when you do the calculation inline, as in your first attempt. The loop in the main program runs 1000 times, and each iteration incurs the overhead of a subroutine call, something that doesn't happen when the derivative calculation is inline in the main program.

As you're new to programming, optimization is less important than just getting something to run. Also, because modern computers have processors that run in the GHz range, there probably won't be much perceptible difference between the two techniques, but it's something to keep in mind for future programs that spend a lot of time on calculations.
 
  • #8
Mark44 said:
The loop in the main program runs 1000 times, and each iteration incurs the overhead of a subroutine call, something that doesn't happen when the derivative calculation is inline in the main program.

As you're new to programming, optimization is less important than just getting something to run. ... but it's something to keep in mind for future programs that spend a lot of time on calculations.
I would vote for the code that is easiest to understand and maintain. In this case, the loop can be moved into the called subroutine. Large, computationally intensive, subroutines may have only a small percentage of subroutine-call overhead. Small, utility subroutines may often be inlined.
 
  • #9
The reason I am looking to understand how to put it in the subroutine, is I am looking to learn how to build larger code in the future and use makefiles to compile.
In terms of how fast i can't tell for now as they both run in a blink
 

1. What is a Fortran subroutine and how does it work?

A Fortran subroutine is a block of code that performs a specific task or calculation. It is called by a main program and can be used to solve complex problems in science and engineering. Subroutines are reusable and can be called multiple times within a program.

2. How does a Fortran subroutine solve for the velocity and displacement of a spring?

A Fortran subroutine can solve for the velocity and displacement of a spring by using mathematical equations that describe the motion of a spring, such as Hooke's law. The subroutine will take in inputs such as the spring constant, mass, and initial conditions, and use these values to calculate the velocity and displacement at different time intervals.

3. What are the advantages of using Fortran for solving this type of problem?

Fortran is a high-level programming language that is specifically designed for scientific and engineering applications. It has built-in functions and data types that make it easier to solve complex mathematical problems, such as those involving the motion of a spring. Fortran also has efficient memory management and is known for its speed and reliability.

4. Can a Fortran subroutine be used to solve for the velocity and displacement of other systems besides a spring?

Yes, a Fortran subroutine can be used to solve for the velocity and displacement of various systems, not just a spring. Its versatility allows it to be used for a wide range of scientific and engineering applications, such as calculating the trajectory of a projectile or modeling the behavior of a circuit.

5. Is it necessary to have a strong background in programming to use a Fortran subroutine for solving this problem?

While having some programming knowledge can be helpful, it is not necessary to have a strong background in programming to use a Fortran subroutine for solving this problem. The subroutine can be called and used within a main program, so as long as the user understands the basic structure and syntax of Fortran, they can successfully implement the subroutine to solve for the velocity and displacement of a spring.

Similar threads

  • Programming and Computer Science
Replies
4
Views
622
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top