将三个功能合并为一个

时间:2016-11-16 11:01:19

标签: function fortran physics

我正在构建一个将轨道元素转换为笛卡尔坐标的程序。

我想创建一个执行此操作的函数,将轨道元素转换为cartisian坐标,但我无法使其工作。这是我第一次使用fortran,所以我还是习惯了它。

program conversion

implicit none
real*8 :: EC, A, IC, OM, W, TA, X, Y, Z
real*8, external :: XCARTISIAN, YCARTISIAN, ZCARTISIAN, r

print *, "Insert Oribtal Elements (EC, A, IC, OM, W, TA): "
read *, EC, A, IC, OM, W, TA, X, Y, Z


X = XCARTISIAN(EC, A, IC, OM, W, TA)
Y = YCARTISIAN(EC, A, IC, OM, W, TA)
Z = ZCARTISIAN(EC, A, IC, OM, W, TA)

print *, "X : ", X, "Y : ", Y, "Z : ", Z

end program conversion



real*8 function XCARTISIAN(EC, A, IC, OM, W, TA)

implicit none
real*8, intent(in) :: EC, A, IC, OM, W, TA
real*8 :: XCARTISIAN, r

r = A(1-EC**2)/(1+EC*COS(TA))
XCARTISIAN = r(COS(OM)*COS(W+TA)-SIN(OM)*SIN(W+TA)*COS(IC))

return

end function XCARTISIAN

real*8 function YCARTISIAN(EC, A, IC, OM, W, TA)

implicit none
real*8, intent(in) :: EC, A, IC, OM, W, TA
real*8 :: YCARTISIAN

r = A(1-EC**2)/(1+EC*COS(TA))
YCARTISIAN = r(SIN(OM)*COS(W+TA)+COS(OM)*SIN(W+TA)*COS(IC))

return

end function YCARTISIAN


real*8 function ZCARTISIAN(EC, A, IC, OM, W, TA)

implicit none
real*8, intent(in) :: EC, A, IC, OM, W, TA
real*8 :: ZCARTISIAN

r = A(1-EC**2)/(1+EC*COS(TA))
ZCARTISIAN = r(SIN(W+F)*SIN(IC)

return

end function ZCARTISIAN

我怎样才能让它工作并制作一个单独的功能,而不是分别为X,Y和Z三个单独的功能?

转换公式如下:

X = r(COS(OM)*COS(W+TA)-SIN(OM)*SIN(W+TA)*COS(IC))
Y = r(SIN(OM)*COS(W+TA)+COS(OM)*SIN(W+TA)*COS(IC))
Z = r(SIN(W+F)*SIN(IC)

其中,

r = A(1-EC**2)/(1+EC*COS(TA))

2 个答案:

答案 0 :(得分:1)

或许将您的功能放入模块中?

program conversion
USE ORBITCANDY
implicit none
TYPE(OE_Typ)   :: OE
TYPE(Cart_Typ) :: Cart
real*8, external :: r ! Add this one above

print *, "Insert Oribtal Elements (EC, A, IC, OM, W, TA): "
read *, OE%EC, OE%A, OE%IC, OE%OM, OE%W, OE%TA

print *, "Insert Oribtal Elements (EC, A, IC, OM, W, TA): "
read *, OE%EC, OE%A, OE%IC, OE%OM, OE%W, OE%TA

XYZ = OE2CART(OE)
print *, "X : ", XYZ%X, "Y : ", XYZ%Y, "Z : ", XYZ%Z

end program conversion

一个用于R,然后是CART2GEODETIC,等等......

_getDataFromServer

iPad中没有编译器,所以我肯定会有拼写错误......

答案 1 :(得分:1)

最直接的做法是制作一个子程序。最好放在一个模块中,但这是可选的(但恕我直言所有功能和子程序应该在某个模块中)。

subroutine XYZCARTISIAN(XCARTISIAN, YCARTISIAN, ZCARTISIAN, EC, A, IC, OM, W, TA)

  implicit none
  real*8 :: XCARTISIAN, YCARTISIAN, ZCARTISIAN
  real*8, intent(in) :: EC, A, IC, OM, W, TA
, r

  r = A(1-EC**2)/(1+EC*COS(TA))

  XCARTISIAN = r*(COS(OM)*COS(W+TA)-SIN(OM)*SIN(W+TA)*COS(IC))
  YCARTISIAN = r*(SIN(OM)*COS(W+TA)+COS(OM)*SIN(W+TA)*COS(IC))
  ZCARTISIAN = r*(SIN(W+F)*SIN(IC)
end subroutine

这也会更快,因为您只计算一次r并且只进行一次子程序调用。

请注意:

  1. real*8完全不标准,不是Fortran。
  2. 使用缩进使代码更具可读性。
  3. 那里不需要return,我们只是提前返回。