Fortran Simple Harmonic Oscillator Problem

In summary, the conversation discusses a program being written in Fortran 90 to plot the positions of three particles connected by two springs. The code is provided and the issue being faced is that the positions of the particles overlap at certain points. Possible reasons for this are discussed, such as the values of the masses, spring and damping constants, and viscous drag. It is also mentioned that the program does not take into account the physical characteristics of springs, such as minimum and maximum lengths, which could affect the behavior being modeled.
  • #1
Dylicious
5
0
Hello fellow computer physics nerds,

I'm trying to write a program to plot the positions of the three particles connected by two springs (one dimensional) in Fortran 90. I have a main program block and a module that calls a PGPLOT.

My problem is that the positions of the second and third particle overlap at some points as my code stands and I don't know why! :cry:

Here is my code please help meeeeeeeeeeeee

program springparticles
use ploty
implicit none

! Variable Declaration
real :: position_p1, position_p2, position_p3, velocity_p1, velocity_p2, velocity_p3, mass_p1, mass_p2, mass_p3, force_p1, force_p2, force_p3, dx, r
real, parameter :: spring_constant=0.1 , damping_constant=0.01 , viscous_drag=0.1
integer :: n
real, dimension(1000)::ap, bp, cp, tp
real :: t,dt
write (*,*)'Please enter the mass of the first particle: '
read *, mass_p1
write (*,*)'Second Particle: '
read *, mass_p2
write (*,*)'Third Particle: '
read *, mass_p3

position_p1=1.0
position_p2=2.0
position_p3=3.0

t= 0
dt = 0.1

dx = 0

force_p1=0
force_p2=0
force_p3=0

velocity_p1=0
velocity_p2=0
velocity_p3=0

print *,'Particle One |||||| Particle Two |||||| Particle Three'

do n = 1,1000

t = t +dt
dx = dx + 0.01


! force_p1 = force_p1-viscous_drag*velocity_p1
! force_p2 = force_p2-viscous_drag*velocity_p2

r= 0.5

force_p1 = spring_constant*(position_p2-position_p1-r) - damping_constant*(velocity_p1-velocity_p2)

force_p2 = - spring_constant*(position_p2-position_p1-r) - damping_constant*(velocity_p1-velocity_p2) + spring_constant*(position_p3-position_p2-r) - damping_constant*(velocity_p2-velocity_p3)

force_p3 = -spring_constant*(position_p3-position_p2-r) - damping_constant*(velocity_p2-velocity_p3)



velocity_p1 = velocity_p1+force_p1/mass_p1*dt
velocity_p2 = velocity_p2+force_p2/mass_p1*dt
velocity_p3 = velocity_p3+force_p3/mass_p3*dt

position_p1 = position_p1+velocity_p1*dt
position_p2 = position_p2+velocity_p2*dt
position_p3 = position_p3+velocity_p3*dt


! velocity_p1 = velocity_p1+dx/dt
! velocity_p2 = velocity_p2+force_p2/mass_p1p2*dt



print *,position_p1,'|| ',position_p2,'|| ',position_p3

tp(n) = t
ap(n)= position_p1
bp(n)= position_p2
cp(n)= position_p3

end do



call plotfunction(tp, ap, bp, cp)
end program springparticles


module ploty
implicit none
contains
!-----------------------------------------------------------------------
! This module uses pgplot to plot the positions of the particles
!-----------------------------------------------------------------------
subroutine plotfunction(t, a, b, c)
implicit none
integer, parameter :: d = 100
real :: x(100), y(100)
real, intent(in) :: t(:), a(:), b(:), c(:)
integer :: pgopen

! Open a plot window
IF (PGOPEN('/XWINDOW') .LE. 0) STOP


! Set-up plot axes
call PGENV(minval(t),maxval(t),min(minval(a),minval(b)),max(maxval(c),maxval(b)),0,0)
call PGLAB('Time', 'Distance from Equilibrium', 'Positions of Particles')


! Change plot colour to colour 6 ()
! Compute the function at the points
! x(d) = t(:)
! y(d) = a(:)

! write(6,*) t(::10)
! write(6,*) a(::10)
! Plot the curve
call PGSCI(6)
call PGLINE(size(a),t,a)
call PGSCI(4)
call PGLINE(size(b),t,b)
call PGSCI(7)
call PGLINE(size(c),t,c)

! Pause and then close plot window
call PGCLOS
end subroutine plotfunction
!-----------------------------------------------------------------------
end module ploty
 
Technology news on Phys.org
  • #2
Dylicious said:
My problem is that the positions of the second and third particle overlap at some points as my code stands and I don't know why!
Is there some reason that you think they shouldn't overlap? I don't know what values you're putting in for the masses, and with some values for these, plus the values of the spring and damping constants and viscous drag, it might be that two or more of the particles collide from time to time.
 
  • #3
I just figured that conceptually the spring would compress to a point and never allow the particles to move past one another, mathematically I see how its possible but doesn't that just mean I'm modelling the physical situation inaccurately?
 
  • #4
Dylicious said:
I just figured that conceptually the spring would compress to a point and never allow the particles to move past one another, mathematically I see how its possible but doesn't that just mean I'm modelling the physical situation inaccurately?
I don't recall seeing anything that sets a minimum length for the springs. Your program doesn't understand the physical characteristics of springs such as minimum length when it is compressed, or maximum length at which the spring deforms or breaks when it is stretched. If you want your program to model that behavior, you need to have code that does this.
 
  • #5




Dear fellow physics enthusiast,

I can see that you have put a lot of effort into your code and it seems like you have a good understanding of the concepts involved in a simple harmonic oscillator problem. However, it is difficult for me to pinpoint the exact issue without running the code myself. I would suggest checking your equations and making sure you are using the correct variables and constants. Also, try debugging your code by printing out the values of each variable at each time step to see if they make sense. Additionally, you may want to consider using a debugger tool to track down the error in your code.

I hope this helps and best of luck with your project! Keep up the good work!
 

What is a Simple Harmonic Oscillator?

A Simple Harmonic Oscillator is a type of system that experiences a restoring force proportional to its displacement from equilibrium. This results in a periodic motion with a constant frequency and amplitude.

What is the Fortran programming language?

Fortran is a high-level programming language commonly used in scientific and numerical computing. It is known for its efficient handling of mathematical operations and is often used in applications such as weather forecasting, computational physics, and engineering simulations.

How do I solve the Simple Harmonic Oscillator problem using Fortran?

To solve the Simple Harmonic Oscillator problem using Fortran, you will need to first define the equation of motion for the oscillator, which is typically a second-order differential equation. Then, you can use numerical integration methods, such as the Runge-Kutta method, to solve the equation and obtain the position and velocity of the oscillator at different time steps.

What are some common challenges when solving the Simple Harmonic Oscillator problem in Fortran?

Some common challenges when solving the Simple Harmonic Oscillator problem in Fortran include choosing the appropriate integration method, determining the appropriate time step, and handling boundary conditions. It is also important to ensure the accuracy and stability of the solution by checking for convergence and avoiding round-off errors.

How can I use the solution of the Simple Harmonic Oscillator problem in Fortran for my research or experiments?

The solution of the Simple Harmonic Oscillator problem in Fortran can be used to model a wide range of physical systems, such as pendulums, springs, and molecular vibrations. It can also be used to study the behavior of oscillating systems under different conditions, such as varying the initial conditions or changing the parameters of the system. This can provide valuable insights for research and experiments in various fields, including physics, engineering, and chemistry.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Quantum Physics
Replies
9
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Replies
17
Views
2K
  • Advanced Physics Homework Help
Replies
4
Views
1K
Back
Top