compute center-of-mass of a set of particles
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(particle_type), | intent(in), | dimension(:) | :: | particles |
pure function compute_center_of_mass(particles) result(center_of_mass)
class(particle_type), dimension(:), intent(in) :: particles
integer :: i
real(dp) :: mass
real(dp) :: total_mass
real(dp), dimension(3) :: coordinate
real(dp), dimension(3) :: center_of_mass
total_mass = 0.0_dp
center_of_mass = 0.0_dp
do i = 1, size(particles)
mass = particles(i)%mass
coordinate = particles(i)%coordinate
total_mass = total_mass + mass
center_of_mass = center_of_mass + mass * coordinate
end do
center_of_mass = center_of_mass / total_mass
end function compute_center_of_mass