简体   繁体   English

遍历Fortran中定义的数组元素

[英]Looping over defined array elements in Fortran

Hello I am currently trying to find out the best way of identifying how many items have been added to an array in Fortran. 您好,我目前正在尝试找出识别在Fortran中已添加到数组中的项的最佳方法。 To better explain the question I have simplified the system I am working with to act as an example. 为了更好地解释这个问题,我简化了我正在使用的系统作为示例。

Let's say I have a box with 11 atoms bouncing around in it. 假设我有一个盒子,里面有11个原子弹跳。 At each timeframe I identify all atoms that are within a certain distance of my favourite atom, atom_α, and add them to an array named array_C, (of length 10). 在每个时间范围内,我都会识别出在我最喜欢的原子atom_α一定距离内的所有原子,并将它们添加到名为array_C(长度为10)的数组中。 Now I would then like to iterate over array_C and perform calculations on the atoms in it. 现在,我想遍历array_C并对其中的原子执行计算。 In python this would be no big deal as the list is long as the number of elements in it. 在python中,这没什么大不了的,因为列表只要其中的元素数量即可。 In fortran the list/array is defined by you, and is as long as you say it is (in this case 10). 在fortran中,列表/数组由您定义,并且只要您说的是就可以(在本例中为10)。 As the other atoms could be far or close to atom_α the array, array_C, could contain 0, 10 or any number of atoms between. 由于其他原子可能接近或接近atom_α,因此数组array_C可以包含0、10或之间的任意数量的原子。 So if I was to loop over array_C I may loop over an “empty” cell or a cell that had not been overwritten from the last step causing all sorts of problems. 因此,如果我要遍历array_C,则可能会遍历“空”单元或上一步未覆盖的单元,从而导致各种问题。 How would I deal with this issue or is it simply a case of keeping track of the number of atoms I added to the array and then doing a do loop over it using that value? 我将如何处理这个问题?或者只是跟踪我添加到数组中的原子数,然后使用该值对其执行do循环的情况?

In reality there are around 4000 atoms and each atom is a “Class” like object each containing, amongst other things, their own “array_C” listing which other atoms are close to it. 实际上,大约有4000个原子,每个原子都是一个“类”,类似于对象,每个对象除其他外还包含自己的“ array_C”,列出了与之接近的其他原子。

Thank you for your time. 感谢您的时间。

You should declare an array to have length greater than what you expect to need. 您应该声明一个数组,该数组的长度大于您期望的长度。 When adding elements to that array, keep track of the number of elements you have added (called numElements in the example). 将元素添加到该数组时,请跟踪已添加的元素数(在示例中称为numElements )。 Then you can loop from 1 to numElements - elements of the array greater than that number are not used in that timestep. 然后,您可以从1循环到numElements在该时间步中不使用大于该数字的数组元素。 For example: 例如:

integer, parameter :: maxElements = 10000
integer :: i, numElements
real :: curData, myData(maxElements)

numElements = 0

! Adding elements to list
do         ! Over all atoms
   if      ! Atom is close
      ! Include atom in list myData
      ! and increment numElements

      numElements = numElements + 1

      if (numElements > maxElements) then
         ! Error
      endif

      myData(numElements) = ...
   endif
enddo

! Now loop over list
do i=1,numElements
   curData = myData(i)

   ! Calculations with curData
enddo

Does that make sense? 那有意义吗? I agree with vlad that the question is difficult to understand so please give us more specifics if you need more answers. 我同意弗拉德的观点,这个问题很难理解,因此如果您需要更多答案,请给我们提供更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM