ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE_old/src/mdtools/libmdCode/vector_class.F90
(Generate patch)

Comparing trunk/OOPSE_old/src/mdtools/libmdCode/vector_class.F90 (file contents):
Revision 314 by gezelter, Tue Mar 11 19:01:19 2003 UTC vs.
Revision 346 by chuckv, Fri Mar 14 19:51:11 2003 UTC

# Line 1 | Line 1
1 + ! vector_class.F90
2 + !! Module Vector_class
3 + !! Fortran 95 Vector class module. Similar to java.util vector class.
4 + !!
5 + !! The Vector class implements a growable array of objects. Like an array,
6 + !! it contains components that can be accessed using an integer index.
7 + !! However, the size of a Vector can grow as needed to accommodate
8 + !! adding and removing items after the Vector has been created.
9 + !! Each vector tries to optimize storage management by maintaining a
10 + !! capacity and a capacityIncrement. The capacity is always at least as
11 + !! large as the vector size;
12 + !! it is usually larger because as components are added to the vector,
13 + !! the vector's storage increases in chunks the size of capacityIncrement.
14 + !! An application can increase the capacity of a vector before inserting a
15 + !! large number of components; this reduces the amount of incremental
16 + !! reallocation.
17 + !!
18 + !!
19 + !! @author J. Daniel Gezelter
20 + !! @author Charles F. Vardeman II
21 + !! @author Matthew Meineke
22 + !! @version $Id: vector_class.F90,v 1.8 2003-03-14 19:51:11 chuckv Exp $, $Date: 2003-03-14 19:51:11 $, $Name: not supported by cvs2svn $, $Revision: 1.8 $
23 +
24   module Vector_class
25    
26    implicit NONE
# Line 18 | Line 41 | module Vector_class
41    integer, parameter :: integer_data_type = 2
42    integer, parameter :: real_data_type = 3
43  
44 + !!
45    type, public :: Vector
46       PRIVATE
47       integer :: initialCapacity = 10
# Line 28 | Line 52 | module Vector_class
52       integer :: PropertyIncrement = 0
53       integer :: propertyCount = 0
54      
55 <     integer, pointer :: ElementData(:)
56 <     character(len=100), pointer :: PropertyDescriptions(:)
57 <     integer, pointer :: PropertyDataType(:)
58 <     real(kind = 8), pointer :: realElementProperties(:,:)
59 <     integer, pointer :: integerElementProperties(:,:)
60 <     logical, pointer :: logicalElementProperties(:,:)
55 >     integer, pointer :: ElementData(:) => null()
56 >     character(len=100), pointer :: PropertyDescriptions(:) => null()
57 >     integer, pointer :: PropertyDataType(:) => null()
58 >     real(kind = 8), pointer :: realElementProperties(:,:) => null()
59 >     integer, pointer :: integerElementProperties(:,:) => null()
60 >     logical, pointer :: logicalElementProperties(:,:) => null()
61    end type Vector
62  
63 + !! Initialize vector
64    interface initialize
65       module procedure initialize_0i
66       module procedure initialize_1i
# Line 59 | Line 84 | module Vector_class
84    interface getMatchingElementList
85       module procedure getMatchingElementList1i
86       module procedure getMatchingElementList2i
87 +     module procedure getMatchingElementList1l
88 +     module procedure getMatchingElementList2l
89    end interface
90  
91    interface getFirstMatchingElement
92       module procedure getFirstMatchingElement1i
93       module procedure getFirstMatchingElement2i
94 +     module procedure getFirstMatchingElement1l
95 +     module procedure getFirstMatchingElement2l
96    end interface
97   contains
98  
# Line 154 | Line 183 | contains
183      return
184    end function getFirstMatchingElement2i
185  
186 +  function getFirstMatchingElement1l(this, MatchName, MatchValue) result (id)
187 +    type(Vector), pointer :: this
188 +    character(len=*), intent(in) :: MatchName
189 +    logical, intent(in) :: MatchValue
190 +    integer :: id
191 +    integer :: i, j
192 +    
193 +    id = 0
194 +
195 +    do i = 1, this%propertyCount
196 +       if (this%PropertyDescriptions(i) == MatchName) then
197 +          do j = 1, this%elementCount
198 +             if (this%logicalElementProperties(j, i) .eqv. MatchValue) then
199 +                id = j
200 +                return
201 +             endif
202 +          enddo
203 +       endif
204 +    enddo
205 +    return
206 +  end function getFirstMatchingElement1l
207 +
208 +  function getFirstMatchingElement2l(this, MatchName1, MatchValue1, &
209 +       MatchName2, MatchValue2) result (id)
210 +    type(Vector), pointer :: this
211 +    character(len=*), intent(in) :: MatchName1, MatchName2
212 +    logical, intent(in) :: MatchValue1, MatchValue2
213 +    integer :: id
214 +    integer :: i, j, MatchID1, MatchID2
215 +    logical :: found1 = .false.
216 +    logical :: found2 = .false.
217 +
218 +    id = 0
219 +    ! first figure out which properties we are using to do the match:
220 +
221 +    do i = 1, this%propertyCount
222 +       if (this%PropertyDescriptions(i) == MatchName1) then
223 +          MatchID1 = i
224 +          found1 = .true.
225 +       endif
226 +       if (this%PropertyDescriptions(i) == MatchName2) then
227 +          MatchID2 = i
228 +          found2 = .true.
229 +       endif
230 +
231 +       if (found1.and.found2) then
232 +          do j = 1, this%elementCount
233 +             if ((this%logicalElementProperties(j, MatchID1).eqv.MatchValue1) &
234 +                  .and. &
235 +                  (this%logicalElementProperties(j, MatchID2).eqv.MatchValue2)) &
236 +                  then
237 +                id = j
238 +                return
239 +             endif
240 +          enddo
241 +       endif
242 +    end do
243 +    
244 +    return
245 +  end function getFirstMatchingElement2l
246  
247    subroutine getMatchingElementList1i(this, MatchName, MatchValue, &
248         nMatches, MatchList)
# Line 161 | Line 250 | contains
250      character(len=*), intent(in) :: MatchName
251      integer, intent(in) :: MatchValue
252      integer, intent(out) :: nMatches
253 <    integer, pointer :: MatchList
253 >    integer, pointer :: MatchList(:)
254      integer :: i
255  
256      ! first figure out which property we are using to do the match:
257  
258      do i = 1, this%propertyCount
259         if (this%PropertyDescriptions(i) == MatchName) then
260 <          call getMatches1(this, i, MatchValue, MatchList)
260 >          call getAllMatches1i(this, i, MatchValue, nMatches, MatchList)
261            return
262         endif
263      enddo
# Line 206 | Line 295 | contains
295      enddo
296      return
297    end subroutine getMatchingElementList2i
298 +
299 +  subroutine getMatchingElementList1l(this, MatchName, MatchValue, &
300 +       nMatches, MatchList)
301 +    type(Vector), pointer :: this
302 +    character(len=*), intent(in) :: MatchName
303 +    logical, intent(in) :: MatchValue
304 +    integer, intent(out) :: nMatches
305 +    integer, pointer :: MatchList(:)
306 +    integer :: i
307 +
308 +    ! first figure out which property we are using to do the match:
309 +
310 +    do i = 1, this%propertyCount
311 +       if (this%PropertyDescriptions(i) == MatchName) then
312 +          call getAllMatches1l(this, i, MatchValue, nMatches, MatchList)
313 +          return
314 +       endif
315 +    enddo
316 +    return
317 +  end subroutine getMatchingElementList1l
318 +
319 +  subroutine getMatchingElementList2l(this, MatchName1, MatchValue1, &
320 +       MatchName2, MatchValue2, nMatches, MatchList)
321 +    type(Vector), pointer :: this
322 +    character(len=*), intent(in) :: MatchName1, MatchName2
323 +    logical, intent(in)  :: MatchValue1, MatchValue2
324 +    integer, intent(out)  :: nMatches
325 +    integer, pointer :: MatchList(:)
326 +    integer :: i, MatchID1, MatchID2
327 +    logical :: found1 = .false.
328 +    logical :: found2 = .false.
329      
330 +    ! first figure out which properties we are using to do the match:
331 +    
332 +    do i = 1, this%propertyCount
333 +       if (this%PropertyDescriptions(i) == MatchName1) then
334 +          MatchID1 = i
335 +          found1 = .true.
336 +       endif
337 +       if (this%PropertyDescriptions(i) == MatchName2) then
338 +          MatchID2 = i
339 +          found2 = .true.
340 +       endif
341 +      
342 +       if (found1.and.found2) then
343 +          call getAllMatches2l(this, MatchID1, MatchValue1, &
344 +               MatchID2, MatchValue2, nMatches, MatchList)
345 +          return
346 +       endif
347 +    enddo
348 +    return
349 +  end subroutine getMatchingElementList2l
350 +    
351    subroutine getAllMatches1i(this, MatchID, MatchValue, nMatches, MatchList)
352      type(Vector), pointer :: this
353      integer, intent(in) :: MatchID
# Line 250 | Line 391 | contains
391         endif
392      enddo
393    end subroutine getAllMatches2i
394 +
395 +  subroutine getAllMatches1l(this, MatchID, MatchValue, nMatches, MatchList)
396 +    type(Vector), pointer :: this
397 +    integer, intent(in) :: MatchID
398 +    logical, intent(in) :: MatchValue
399 +    integer, pointer :: MatchList(:)
400 +    integer, intent(out) :: nMatches
401 +    integer :: error, i
402 +    
403 +    allocate(MatchList(this%elementCount), stat=error)
404 +    if(error .ne. 0) write(*,*) 'Could not allocate MatchList!'
405 +    
406 +    nMatches = 0
407 +    
408 +    do i = 1, this%elementCount
409 +       if (this%logicalElementProperties(i, MatchID).eqv.MatchValue) then
410 +          nMatches = nMatches + 1
411 +          MatchList(nMatches) = i
412 +       endif
413 +    enddo
414 +  end subroutine getAllMatches1l
415 +
416 +  subroutine getAllMatches2l(this, MatchID1, MatchValue1, &
417 +       MatchID2, MatchValue2, nMatches, MatchList)
418 +    type(Vector), pointer :: this
419 +    integer, intent(in) :: MatchID1, MatchID2
420 +    logical, intent(in) :: MatchValue1, MatchValue2
421 +    integer, pointer :: MatchList(:)
422 +    integer, intent(out) :: nMatches
423 +    integer :: error, i
424      
425 +    allocate(MatchList(this%elementCount), stat=error)
426 +    if(error .ne. 0) write(*,*) 'Could not allocate MatchList!'
427 +    
428 +    nMatches = 0
429 +    
430 +    do i = 1, this%elementCount
431 +       if ((this%logicalElementProperties(i, MatchID1).eqv.MatchValue1) .and. &
432 +            (this%logicalElementProperties(i, MatchID2).eqv.MatchValue2)) then
433 +          nMatches = nMatches + 1
434 +          MatchList(nMatches) = i
435 +       endif
436 +    enddo
437 +  end subroutine getAllMatches2l
438 +  
439      
440    subroutine getElementPropertyReal(this, id, PropName, pv)
441      type(Vector), pointer :: this
442      integer :: id, whichprop
443      character(len=*) :: PropName
444 <    real(kind=8) :: pv
444 >    real( kind = 8 ) :: pv
445      
446      whichprop = getPropertyIndex(this, PropName)
447      if (whichprop .eq. 0 ) then
448 <       write(*,*) 'unknown property!'
448 >       write(*,*) 'unknown property: ', PropName
449         pv = 0.0
450      else
451         if (this%PropertyDataType(whichprop) .ne. real_data_type) then
452 <          write(*,*) 'wrong data type for this property!'
452 >          write(*,*) 'Property: ', PropName, " is not real data type."
453            pv = 0.0
454         else
455            pv = this%realElementProperties(id, whichprop)
# Line 280 | Line 465 | contains
465      
466      whichprop = getPropertyIndex(this, PropName)
467      if (whichprop .eq. 0 ) then
468 <       write(*,*) 'unknown property!'
468 >       write(*,*) 'unknown property! ', PropName
469         pv = 0
470      else
471         if (this%PropertyDataType(whichprop) .ne. integer_data_type) then
472 <          write(*,*) 'wrong data type for this property!'
472 >          write(*,*) 'Property! ', PropName, " is not integer data type."
473            pv = 0
474         else
475            pv = this%integerElementProperties(id, whichprop)
# Line 300 | Line 485 | contains
485      
486      whichprop = getPropertyIndex(this, PropName)
487      if (whichprop .eq. 0 ) then
488 <       write(*,*) 'unknown property!'
488 >       write(*,*) 'unknown property! ', PropName
489         pv = .false.
490      else
491         if (this%PropertyDataType(whichprop) .ne. logical_data_type) then
492 <          write(*,*) 'wrong data type for this property!'
492 >          write(*,*) 'Property! ', PropName, " is not logical data type."
493            pv = .false.
494         else
495            pv = this%logicalElementProperties(id, whichprop)
# Line 316 | Line 501 | contains
501      type(Vector), pointer :: this
502      integer :: id, i
503      character(len=*) :: PropName
504 <    
504 >
505      do i = 1, this%propertyCount
506         if (this%PropertyDescriptions(i) == PropName) then
507            id = i
# Line 331 | Line 516 | contains
516      integer, intent(in) :: minCapacity, minPropCap
517      integer :: oldCapacity, oldPropCap
518      integer :: newCapacity, newPropCap
519 <    logical :: resizeFlag = .false.
519 >    logical :: resizeFlag
520  
521 <    oldCapacity = size(this%ElementData)
337 <    oldPropCap = size(this%PropertyDescriptions)
521 >    resizeFlag = .false.
522  
523 + !  first time: allocate a new vector with default size
524 +
525 +    if (.not. associated(this)) then
526 +       this => initialize()
527 +    endif
528 +
529 +    oldCapacity = size(this%ElementData)
530 +    oldPropCap  = size(this%PropertyDescriptions)
531 +    
532      if (minCapacity > oldCapacity) then
533         if (this%capacityIncrement .gt. 0) then
534            newCapacity = oldCapacity + this%capacityIncrement
# Line 346 | Line 539 | contains
539            newCapacity = minCapacity
540         endif
541         resizeFlag = .true.
542 +    else
543 +       newCapacity = oldCapacity
544      endif
545 <
545 >    
546 > !!! newCapacity is not set.....
547      if (minPropCap > oldPropCap) then
548         if (this%PropertyIncrement .gt. 0) then
549            newPropCap = oldPropCap + this%PropertyIncrement
# Line 358 | Line 554 | contains
554            newPropCap = minPropCap
555         endif
556         resizeFlag = .true.
557 +    else
558 +       newPropCap = oldPropCap
559      endif
560 <      
560 >    
561      if (resizeFlag) then
562 <       that = initialize(newCapacity, newPropCap, &
562 >       that => initialize(newCapacity, newPropCap, &
563              this%capacityIncrement, this%PropertyIncrement)      
564         call copyAllData(this, that)
565         deallocate(this)
# Line 378 | Line 576 | contains
576         v2%elementData(i) = v1%elementData(i)
577         do j = 1, v1%propertyCount
578  
579 <          if (v1%PropertyDataType(j) .eq. integer_data_type)  &
579 >          if (v1%PropertyDataType(j) .eq. integer_data_type) &
580                 v2%integerElementProperties(i,j) = &
581                 v1%integerElementProperties(i,j)
582  
# Line 405 | Line 603 | contains
603    function addElement(this) result (id)
604      type(Vector), pointer :: this
605      integer :: id
606 <    call ensureCapacityHelper(this, this%elementCount + 1, this%PropertyCount)
607 <    this%elementCount = this%elementCount + 1
608 <    this%elementData = this%elementCount
609 <    id = this%elementCount
606 >    integer :: error
607 >
608 >    if (.not. associated(this)) then
609 >       call ensureCapacityHelper(this,1,0)
610 >    else
611 >       call ensureCapacityHelper(this, this%elementCount + 1, this%PropertyCount)
612 >    end if
613 >
614 >       this%elementCount = this%elementCount + 1
615 >       this%elementData = this%elementCount
616 >       id = this%elementCount
617 >
618 >
619    end function addElement
620  
621    recursive subroutine setElementPropertyReal(this, id, PropName, PropValue)
622      type(Vector), pointer :: this
623      integer :: id, i
624      character(len=*), intent(in) :: PropName
625 <    real( kind=8 ), intent(in) :: PropValue
626 <    logical :: foundit = .false.
625 >    real( kind = 8 ), intent(in) :: PropValue
626 >    logical :: foundit
627 >    
628 >    foundit = .false.
629 >
630      ! first make sure that the PropName isn't in the list of known properties:
631 +
632      do i = 1, this%propertyCount
633         if (PropName == this%PropertyDescriptions(i)) then
634            foundit = .true.
635            this%realElementProperties(id,i) = PropValue
636         endif
637      enddo
638 <    
638 >
639      if (.not.foundit) then
640 <       call addPropertyToVector(this, PropName, real_data_type)
641 <       call setElementPropertyReal(this, id, PropName, PropValue)
640 >         call addPropertyToVector(this, PropName, real_data_type)
641 >         call setElementPropertyReal(this, id, PropName, PropValue)
642      endif
643    end subroutine setElementPropertyReal
644  
# Line 436 | Line 647 | contains
647      integer :: id, i
648      character(len=*), intent(in) :: PropName
649      integer, intent(in) :: PropValue
650 <    logical :: foundit = .false.
650 >    logical :: foundit
651 >
652 >    foundit = .false.
653      ! first make sure that the PropName isn't in the list of known properties:
654      do i = 1, this%propertyCount
655         if (PropName == this%PropertyDescriptions(i)) then
# Line 456 | Line 669 | contains
669      integer :: id, i
670      character(len=*), intent(in) :: PropName
671      logical, intent(in) :: PropValue
672 <    logical :: foundit = .false.
672 >    logical :: foundit
673 >
674 >    foundit = .false.
675      ! first make sure that the PropName isn't in the list of known properties:
676      do i = 1, this%propertyCount
677         if (PropName == this%PropertyDescriptions(i)) then
# Line 475 | Line 690 | contains
690      type(Vector), pointer :: this
691      character(len=*), intent(in) :: PropName
692      integer data_type
693 <    call ensureCapacityHelper(this, this%elementCount, this%propertyCount + 1)
694 <    this%propertyCount = this%propertyCount + 1
695 <    this%PropertyDescriptions(this%propertyCount) = PropName
696 <    this%PropertyDataType(this%propertyCount) = data_type
697 <  end subroutine addPropertyToVector
693 >    
694 >     call ensureCapacityHelper(this, this%elementCount, this%propertyCount + 1)
695 >     this%propertyCount = this%propertyCount + 1
696 >     this%PropertyDescriptions(this%propertyCount) = PropName
697 >     this%PropertyDataType(this%propertyCount) = data_type
698 >   end subroutine addPropertyToVector
699    
700    function initialize_0i() result(this)
701      type(Vector), pointer :: this
702 <    nullify(this)
487 <    this = initialize_2i(10, 5)
702 >    this => initialize_2i(10, 5)
703    end function initialize_0i
704  
705    function initialize_1i(nprop) result(this)
706      integer, intent(in) :: nprop
707      type(Vector), pointer :: this
708 <    nullify(this)
494 <    this = initialize_2i(10, nprop)
708 >    this => initialize_2i(10, nprop)
709    end function initialize_1i
710  
711    function initialize_2i(cap, nprop) result(this)
712      integer, intent(in) :: cap, nprop
713      type(Vector), pointer :: this
714 <    nullify(this)
501 <    this = initialize_4i(cap, nprop, 0, 0)
714 >    this => initialize_4i(cap, nprop, 0, 0)
715    end function initialize_2i
716  
717    function initialize_3i(cap, nprop, capinc) result(this)
718      integer, intent(in) :: cap, nprop, capinc
719      type(Vector), pointer :: this
720 <    nullify(this)
508 <    this = initialize_4i(cap, nprop, capinc, 0)
720 >    this => initialize_4i(cap, nprop, capinc, 0)
721    end function initialize_3i
722  
723    function initialize_4i(cap, nprop, capinc, propinc) result(this)
724      integer, intent(in) :: cap, nprop, capinc, propinc
725      integer :: error
726      type(Vector), pointer :: this
727 +
728      nullify(this)
729 <    if (cap .lt. 0) then
729 >
730 >     if (cap < 0) then
731         write(*,*) 'Bogus Capacity:', cap
732 <       stop
732 >       return
733      endif
734 <    if (nprop .lt. 0) then
734 >    if (nprop < 0) then
735         write(*,*) 'Bogus Number of Properties:', nprop
736 <       stop
736 >       return
737      endif
738 +    
739 +    allocate(this,stat=error)
740 +    if ( error /= 0 ) then
741 +       write(*,*) 'Could not allocate Vector!'
742 +       return
743 +    end if
744  
525    allocate(this, stat=error)
526    if(error .ne. 0) write(*,*) 'Could not allocate Vector!'
527
745      this%initialCapacity = cap
746      this%initialProperties = nprop
747      this%capacityIncrement = capinc
748      this%propertyIncrement = propinc
749 <
749 >
750      allocate(this%elementData(this%initialCapacity), stat=error)
751 <    if(error .ne. 0) write(*,*) 'Could not allocate elementData!'
752 <    
751 >    if(error /= 0) write(*,*) 'Could not allocate elementData!'
752 >
753 >
754      allocate(this%PropertyDescriptions(this%initialProperties), &
755           stat=error)
756 <    if(error .ne. 0) write(*,*) 'Could not allocate PropertyDescriptions!'
756 >    if(error /=  0) write(*,*) 'Could not allocate PropertyDescriptions!'
757  
758 +    allocate(this%PropertyDataType(this%initialProperties), &
759 +         stat=error)
760 +    if(error /=  0) write(*,*) 'Could not allocate PropertyDataType!'
761 +    
762      allocate(this%integerElementProperties(this%initialCapacity, &
763           this%initialProperties), stat=error)
764 <    if(error .ne. 0) write(*,*) 'Could not allocate integerElementProperties!'
764 >    if(error /= 0) write(*,*) 'Could not allocate integerElementProperties!'
765  
766      allocate(this%realElementProperties(this%initialCapacity, &
767           this%initialProperties), stat=error)
768 <    if(error .ne. 0) write(*,*) 'Could not allocate realElementProperties!'  
768 >    if(error /= 0) write(*,*) 'Could not allocate realElementProperties!'  
769  
770      allocate(this%logicalElementProperties(this%initialCapacity, &
771           this%initialProperties), stat=error)
772      if(error .ne. 0) write(*,*) 'Could not allocate logicalElementProperties!'
773 +
774    end function initialize_4i
775      
776 <
777 <    
555 <
556 <
557 <
776 >  
777 >  
778   end module Vector_class

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines