polymorphic variables?
polymorphic variables?
(OP)
I'm struggling with a f95 code problem that really needs polymorphic pointers/variables (i.e. a single array consisting of multiple data types), but I understand these are not to be supported until fortran 2000 (?)... Any help much appreciated:
I have defined an overall 'particle' type, and within this type need to define the shape of the particle in question. The shapes however are defined as a series of different types (e.g. sphere, ellipsoid, polygon, etc.) all which have different numbers of parameters within them. What I essentially need to do is given two 'particle' type variables perform a set of calculations that depend on which shape they are.
IDEALLY.... Use a polymorphic variable to imbed the shape information within the 'particle' type, and then overload an operator to perform the correct set of calculations from the shape type passed to it.
PRACTICALLY.... At the moment have referenced an integer 'shape_ID' within the 'particle' type and then use a series of IF statements to based on the shape_ID to sort which calculations are to be performed.
IF (shape_ID particle A = 1 AND shape_ID particle B = 1)
Do some calcs, calling external shape type info for A & B
ELSE IF(shape_ID particle A = 2 AND shape_ID particle B = 1)
Do some different calcs, calling external shape type info
ELSE IF... etc.
Messy, and slow. Also the actual shape parameters need to be stored in separate arrays for each type, which need to be referenced in.
Not been using fortran 95 for long, and suspect there must be a faster, or at least more elegant solution to the one I have already. Any words of wisdom?
Cheers.
I have defined an overall 'particle' type, and within this type need to define the shape of the particle in question. The shapes however are defined as a series of different types (e.g. sphere, ellipsoid, polygon, etc.) all which have different numbers of parameters within them. What I essentially need to do is given two 'particle' type variables perform a set of calculations that depend on which shape they are.
IDEALLY.... Use a polymorphic variable to imbed the shape information within the 'particle' type, and then overload an operator to perform the correct set of calculations from the shape type passed to it.
PRACTICALLY.... At the moment have referenced an integer 'shape_ID' within the 'particle' type and then use a series of IF statements to based on the shape_ID to sort which calculations are to be performed.
IF (shape_ID particle A = 1 AND shape_ID particle B = 1)
Do some calcs, calling external shape type info for A & B
ELSE IF(shape_ID particle A = 2 AND shape_ID particle B = 1)
Do some different calcs, calling external shape type info
ELSE IF... etc.
Messy, and slow. Also the actual shape parameters need to be stored in separate arrays for each type, which need to be referenced in.
Not been using fortran 95 for long, and suspect there must be a faster, or at least more elegant solution to the one I have already. Any words of wisdom?
Cheers.
RE: polymorphic variables?
Consider developing a Type for each of your different particles. This makes since to me since each of your particles has a different set of attributes associated with it. You could also create a parent Type which can point to each of these seperate particle types as needed ( though.. I think doing such just complicates things ).
Lets say you have defined the following particle types:
TYPE PartA
Integer :: i
Character :: name
END TYPE PartA
TYPE PartB
Real :: x
END TYPE PartB
TYPE PartC
Logical :: yn
END TYPE PartC
You can now do the following:
Create a module which contains the "different" routines for operating on various combinations of particles. Then.. create a generic name which can be used for calling these routines. In this example, "Call Calculate ( x, y )" can be used to call your calculation routines. The correct generic routine will be called based on the types of X and Y in your CALLing line. You've essentially overloaded the Calculate subroutine.
Module Particle_Routines
Interface Calculate
Module Procedure PartAB, PartBC, ...
End Interface Calculate
Contains
Subroutine PartAB ( Part1, Part2 )
TYPE(PartA) :: Part1
TYPE(PartB) :: Part2
... Do Something Here ...
End Subroutine PartAB
Subroutine PartBC ( Part1, Part2 )
TYPE(PartB) :: Part1
TYPE(PartC) :: Part2
... Do Something Here ...
End Subroutine PartAB
End Module Particle_Routines
Dan
www.dtware.com