×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

polymorphic variables?

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.

RE: polymorphic variables?

It sounds a bit like you may be attempting to use a C/C++ logic style for fortran.   I don't think Polymorphic is necessarily what you want.   In any case...  I'm pretty sure you can create simpler code using a different style.

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close