fortran training - large-scale simulations with fortran 95 an object-based approach

Upload: william-kilpatrick

Post on 08-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    1/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    2/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    3/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    4/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    5/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    6/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    7/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    8/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    9/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    10/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    11/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    12/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    13/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    14/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    15/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    16/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    17/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    18/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    19/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    20/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    21/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    22/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    23/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    24/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    25/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    26/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    27/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    28/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    29/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    30/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    31/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    32/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    33/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    34/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    35/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    36/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    37/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    38/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    39/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    40/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    41/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    42/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    43/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    44/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    45/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    46/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    47/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    48/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    49/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    50/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    51/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    52/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    53/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    54/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    55/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    56/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    57/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    58/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    59/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    60/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    61/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    62/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    63/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    64/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    65/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    66/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    67/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    68/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    69/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    70/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    71/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    72/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    73/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    74/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    75/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    76/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    77/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    78/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    79/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    80/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    81/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    82/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    83/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    84/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    85/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    86/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    87/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    88/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    89/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    90/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    91/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    92/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    93/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    94/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    95/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    96/196

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    97/196

    interface chargemodule procedure atom_charge

    end interface charge

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    98/196

    9 8 o f 1 0 6

    end interface charge

    interface set_mass

    module procedure set_atom_massend interface set_mass

    interface set_chargemodule procedure set_atom_charge

    end interface set_charge

    interface as_stringmodule procedure atom_as_string

    end interface as_string

    contains

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    99/196

    9 9 o f 1 0 6

    contains

    subroutine atom_new (a, name)

    ...... the rest as it was before...

    ...end module atoms

    M od ule m olecu les i s v ery s im ila r.

    module molecules

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    100/196

    1 0 0 o f

    module moleculesuse precisionimplicit noneprivate

    type moleculeprivate

    real(adequate) massinteger chargeinteger number_of_elements, number_of_atoms

    end type molecule

    public molecule, mass, charge, set_mass, &set_charge, new, as_string

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    101/196

    1 0 1 o f

    interface newmodule procedure molecule_new

    end interface new

    interface massmodule procedure molecule_mass

    end interface mass...

    T here a r e p ro s and cons t o u s ing th i sp r iva t e com po nen t s ty le .

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    102/196

    1 0 2 o f

    Pro: better locality. For example,

    i f you ha ve a tom (m , c ) a l l ove r the c od e , an d

    y o u g e t a n e w c o m p o n e n t , th o s e r e f e re n c e sm us t be a l l cha ng ed . You ha ve m oref lex ib i l ity i f there a re o n e o r m o reco ns t ruc to r s t ha t you w r ite , pos s ib ly

    us ing ke yw ord o r op t ion a l a rgu m en ts .

    O O p rog ram m ers h av e lea rne d tha t if a c l ien tcan R E A D a co m po ne n t d i r ec t ly the y canW R IT E i t, po ss ib ly c rea t ing an ob jec t tha t

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    103/196

    1 0 3 o f

    W R IT E i t, po ss ib ly c rea t ing an ob jec t tha tn o lo n g e r h a s a v a lid v a lu e .

    In f ac t , a .b o n the l e f t side o f an a s s ign m en top e ra to r i s a r ed f lag to an exp e r ience d O Op ro g ra m m e r.

    C o n : ta k e s m o re w o r k .

    Besides, writing a%b = c is nice and clear and I never make

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    104/196

    1 0 4 o f

    gmistakes. And I can do the generic functions whether or notthe components are private. Anyway, I dont trust thecompiler to do those accessors efficiently.

    H o m e w o r k

    Write a program with a user-defined type point that has x,h d f d

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    105/196

    1 0 5 o f

    y, z components. Write another user-defined typequadrilateral that has a component vertices that is anarray of 4 points. (For simplicity, make everything public).

    Try creating and manipulating some quadrilaterals andarrays of quadrilaterals.

    If q is an array of quadrilaterals,q(1)%vertices(1)%x

    is the xth coordinate of the first vertex of the firstquadrilateral in q.

    But can you do q(1)%vertices%x, q(1)%vertices(2:3)%x,q(2:4)%vertices%x?

    Just how cool is this?

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    106/196

    1 0 6 o f

    Just how cool is this?

    And what about

    integer:: iv(3) = (/ 1, 2, 4 /)

    real:: integer x(20)

    What about:x(iv)?

    q(1)%vertices(iv)?

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    107/196

    1 0 7 o f

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    108/196

    1 0 8 o f

    Lesson 3: Types and objects

    Paul F. Dubois

    X-Division

    [email protected]

    In ou r f ir s t tw o l e ssons , we ha veconcen tr a t ed on a r r ay s , m odu le s , andder ived types .

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    109/196

    1 0 9 o f

    It is the combination of types and modules that allows us towork in a style I will call object-based.

    The homework should have helped you get a feel forcreating and working with derived types.

    M in i -le s son #3 : G ive ea ch c om p on en t in ade r ive d typ e a d efau lt in i t ia l va lue .

    type particle

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    110/196

    11 0 o f 1 5 1

    type particletype(point):: location = point(0.0, 0.0, 0.0)type(point):: momentum = point(0.0, 0.0, 0.0)real(adequate):: mass = 1.0real(adequate):: charge = 0.0

    end type particle

    The default initial value can be over-ridden by an explicitone.You must use the entity-oriented form of declaration with

    the two colons.

    W e w ish to im plem ent an

    ab strac t data type, inw hich no illeg al o bjects

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    111/196

    111 o f 151

    yp ,w hich no illeg al o bjectsof this type can ever be

    made .Initializing each instance properly is an important part of this.

    M od u les an d de r ived types ea ch ha ve som easp ec t o f c lasses .

    Modules can contain both data and functions, and do

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    112/196

    11 2 o f 1 5 1

    Modules can contain both data and functions, and doinformation hiding. But there is only one instance of thedata in a module.

    Derived types have a constructor that can be used as acookie cutter to produce instances, but they do not containmethods.

    Derived types do have the possibility of hiding all or part of their data but you lose some conveniences if you do so.

    => The secret is to combine the two.

    M in i -le s son #4 : U se a m odu le t o m ode ls ing le tons .

    A singleton is a class which has exactly one instance.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    113/196

    11 3 o f 1 5 1

    g y

    So, if you have some data which should exist exactly once

    in your application, package it into a module, together withthe functions whose exclusive purpose is to manipulate thatdata.

    Then think out what you want the public interface to look like.

    T h e g en er ic in te r face ca pa b ili ty is m u chm ore p ow er fu l than w e have s een so f a r.

    We used it to define a single name that could be applied to

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    114/196

    11 4 o f 1 5 1

    g ppobjects of varied types. You can also use it to:

    d e f in e o p e r a to r s s u c h a s + a n d * ; ad d ne w op e ra to r s su ch as .do t ., .i n teg ra te . ,

    o r . inv e r se . , o f y ou r ow n ch oo s ing ;

    t ake ov e r the a s s ign m en t ope ra to r ;

    ev en ex ten d the m ean ing o f in t r ins i c n am ess u c h a s s q rt

    H e re is a m o d u le im p le m e n t in g a 3 - D p o in tob jec t .

    module points

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    115/196

    11 5 o f 1 5 1

    puse precisionprivatepublic dot, sqrt, normpublic operator(+), operator(-), operator(*), operator(/)

    type,public:: pointreal(adequate):: x = 0.0real(adequate):: y = 0.0real(adequate):: z = 0.0

    end type point

    Weve defined a new type named point. Weve givenevery component a default value so there wont be anyuninitialized points.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    116/196

    11 6 o f 1 5 1

    Note the alternate syntax used for giving type(point) the

    attribute public.

    N ow w e de fine t he ba s ic a r ithm e ticope ra to r s to im p lem en t vec to r a r it hm e ticon po in t s .

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    117/196

    11 7 o f 1 5 1

    We supply three versions of each so that we can dopoint+point, point+real, real+point, for convenient

    operations such as translation and scaling.

    interface operator(+)module procedure point_add, point_add_s, point_s_add

    end interface

    interface operator(-)module procedure point_subtract, point_subtract_s, &

    point_s_subtract, point_negateend interface

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    118/196

    11 8 o f 1 5 1

    end interface

    interface operator(*)module procedure point_multiply, point_multiply_s, &point_s_multiply

    end interface

    interface operator(/)module procedure point_divide, point_divide_s, &

    point_s_divideend interface

    M od u le p rocedu re s d o the r ea l w ork .

    function point_add (a, b) result(r)type(point), intent(in):: a, b

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    119/196

    11 9 o f 1 5 1

    type(point):: rr%x = a%x + b%xr%y = a%y + b%yr%z = a%z + b%z

    end function point_add

    function point_add_s (a, b) result(r)type(point), intent(in):: areal(adequate), intent(in):: b

    type(point):: r

    r%x = a%x + br%y = a%y + br%z = a%z + b

    end function point add s

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    120/196

    1 2 0 o f

    end function point_add_s

    function point_s_add (a, b) result(r)type(point), intent(in):: breal(adequate), intent(in):: atype(point):: r

    r%x = a + b%xr%y = a + b%yr%z = a + b%z

    end function point_s_addThese are all in the procedure part of the module points.

    T h e com p i le r ch oo ses an ap prop r ia t efun c tion ba sed on s ign a tu re .

    type(point) x, y, z

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    121/196

    1 2 1 o f

    z = x + y !actually does z = point_add(x, y)

    z = x + 1.0 !actually does z = point_add_s(x, 1.0)

    z = 1.0 + x !actually does z = point_s_add(1.0, x)

    W e a lso de f ine a gen e r ic no rm func t ion ,a b ina ry o pe ra to r fo r do t p rod uc t , and ane lem en t -w ise squ a re roo t.

    Users can write norm(v) v dot w and sqrt(v) And yes

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    122/196

    1 2 2 o f

    Users can write norm(v), v .dot. w, and sqrt(v). And yes,sqrt(2.0) still works!

    interface normmodule procedure point_norm

    end interface

    interface operator(.dot.)module procedure point_dot

    end interface

    interface sqrtmodule procedure point_sqrt

    end interface

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    123/196

    1 2 3 o f

    W e lis ted in the in t e r face fo r each o pera to rthe fu n c t ion s tha t th e co m p ile r co u ld u se to

    d o t h e jo b .

    The functions were all in the module itself so all we had to

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    124/196

    1 2 4 o f

    The functions were all in the module itself so all we had todo was list them as module procedures.

    We could also use functions defined elsewhere by puttinginterface specifications for them into the interface blocks.

    In the contains section of the module we put the actualworker routines. These are all private to the module.

    I t is a l so h e lp fu l to a dd un ary m inu s to thesub t rac t in t e r face .

    function point_negate (a) result(r)type(point) intent(in):: a

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    125/196

    1 2 5 o f

    type(point), intent(in):: atype(point):: r

    r%x = -a%xr%y = -a%yr%z = -a%z

    end function point_negate

    N o w w e d o t h e n o r m , .d o t ., a n d s q r t.

    function point_norm(self) result(r)type(point),intent(in):: self

    l( d )

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    126/196

    1 2 6 o f

    real(adequate) rr = sqrt(self%x**2 + self%y**2 + self%z**2)

    end function point_norm

    function point_dot(a, b) result(r)type(point), intent(in):: a, breal(adequate):: rreal(precise):: s

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    127/196

    1 2 7 o f

    s = real(a%x, precise) * b%x + &real(a%y, precise) * b%y + &real(a%z, precise) * b%z

    r = real(s, adequate)end function point_dot

    function point_sqrt(a) result(r)type(point), intent(in):: atype(point):: r

    r%x = sqrt(a%x)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    128/196

    1 2 8 o f

    r%y = sqrt(a%y)r%z = sqrt(a%z)

    end function point_sqrtend module points

    L et s te s t -d r ive m od u le po in t s .

    type(point), parameter:: origin = point(0.0, 0.0, 0.0)type(point), parameter:: e1 = point(1.0, 0.0, 0.0)type(point) parameter:: e2 = point(0 0 1 0 0 0)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    129/196

    1 2 9 o f

    type(point), parameter:: e2 = point(0.0, 1.0, 0.0)type(point), parameter:: e3 = point(0.0, 0.0, 1.0)type(point) x1, x2

    x1 = 3.0 * e1 + 4.0 * e2 + 5.0 * e3x2 = -4.0 * x1 - (2.0_adequate * e2) + e3write(11, *) "x1 ", x1write(11, *) "x2 ", x2write(11, *) "norm(x1)", norm(x1)write(11, *) "sqrt(x1)", sqrt(x1)

    write(11, *) "dot product", x1 .dot. x2

    N o te th a t k in d c o n v e r s io n w a s p e r fo r m e d ,bu t typ e conve r s ion w on t be .

    x1 = 3.0 * e1 + 4.0 * e2 + 5.0 * e3 !ok x1 = 3.0 * e1 + 4.0 * e2 + 5 * e3 !doesnt compile

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    130/196

    1 3 0 o f

    x1 3.0 e1 4.0 e2 5 e3 !doesn t compile

    Naturally, you could define two more handlers so thateach operation would work with integers.

    Su pp ose w e w an t to r ep re sen t po in t s on t heun it c ir c le , an d to b e ab le to a sk fo r the ir x ,

    y, an d th e ta coo rd ina tes .However, we always want 0.0

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    131/196

    1 3 1 o f

    We also want to be able to add and subtract angles fromsuch points, and to create the points from real numbersrepresenting the angle.

    H ere is how w e a r e go ing to u se en t it ie s o f type ucp o in t.

    type(point) atype(ucpoint) r1, r2

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    132/196

    1 3 2 o f

    yp ( p ) ,

    r1 = pi / 4.0_adequater2 = -r1 + pi / 12.0_adequatewrite (11, *) "r1 ", x (r1), y (r1), theta (r1)write (11, *) "r2 ", x (r2), y (r2), theta (r2)write (11, *) "r3 ", theta (as_ucpoint (-0.3 * pi))a = point(x(r1), y(r1), 0.0)write (11, *) "a ", a

    module ucpoints !points on the unit circleuse precisionprivate

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    133/196

    1 3 3 o f

    privatepublic ucpoint, as_ucpointpublic operator(+), operator(-), assignment(=)public x, y, theta

    type ucpointprivatereal(adequate):: theta = 0.0

    end type ucpoint

    interface assignment(=)module procedure ucpoint_set

    end interface

    i t f t (+)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    134/196

    1 3 4 o f

    interface operator(+)module procedure ucpoint_add_s, ucpoint_s_add

    end interface

    interface operator(-)

    module procedure ucpoint_subtract_s, &ucpoint_s_subtract, ucpoint_negateend interface

    N o w in t h e p r o ce d u r e p a r t of th e m o d u lew e de f ine t he se op e ra to r s.

    containsfunction as_ucpoint(r) result(p)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    135/196

    1 3 5 o f

    real(adequate), intent(in):: r

    type(ucpoint) pp = r

    end function as_ucpoint

    function x(p)type(ucpoint), intent(in):: preal(adequate) x

    x = cos(p%theta)end function x

    function y(p)type(ucpoint), intent(in):: preal(adequate) y

    y = sin(p%theta)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    136/196

    1 3 6 o f

    y = sin(p%theta)end function y

    function theta(p)type(ucpoint), intent(in):: p

    real(adequate) rtheta = p%thetaend function theta

    To de f ine the a s s ign m en t o pe ra to r w esupp ly a su b rou t ine .

    You can have multiple assignment handlers with differentsignatures for the second argument.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    137/196

    1 3 7 o f

    subroutine ucpoint_set (p, r)

    type(ucpoint), intent(out):: preal(adequate), intent(in):: rp%theta = modulo(r, 2.0_adequate * pi)

    end subroutine ucpoint_set

    Note that the intent of p must be out or inout. The intent of rmust be in.

    The statement p = r will be handled by a call ucpoint_set(p,r).

    B y u s ing ou r ow n ass ign m en t op e ra to r, w ecan insu re tha t the ta i s a lw ays in the

    des ir ed r ang e .function ucpoint_add_s (a, b) result(p)

    ( i ) i (i )

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    138/196

    1 3 8 o f

    type(ucpoint), intent(in):: a

    real(adequate), intent(in):: btype(ucpoint):: p

    p = a%theta + b

    end function ucpoint_add_s

    Note that the assignment is really ucpoint_set (p, a%theta +b).

    Thus, ucpo int_set m akes

    sure on ly legal ucpointscan be created.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    139/196

    1 3 9 o f

    If each point is born correct, and each (public) operationkeeps it correct, it is always correct.

    F or t ran 95 s d e r ived types com pa reun favo rab ly to C ++ s .

    Com parison o f derived types in F95and C++

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    140/196

    1 4 0 o f

    N etEffec tfo r F 9 0v s .C + +

    A rea o f conce rn

    M in u s T h e re a re n o p ro v is io n sfor dest ructors .

    M in u s T h e re is o n ly th e o n eofficial con struc tor.

    Com parison o f derived types in F95and C++

    N etEffec tfo r F 9 0 A rea o f conce rn

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    141/196

    1 4 1 o f

    S lig h t P l u s T h e re is n o a u to m a tic s ig -na tu re m a tch ing o rim pl ic i t conve rs ion s .

    H u g eM i n u s N o inher i tanc e in F or tran9 5

    H u g eM i n u s

    N o pa ram e te r ized typesin F9 5

    H u ge P lu s Typ e sa fe ty

    o 9 0v s .C + +

    ea o co ce

    Com parison o f derived types in F95and C++

    N etEffec tfo r F 9 0 A rea o f conce rn

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    142/196

    1 4 2 o f

    H u g e P l us In te ra c tio n o f a rra y fa c ilityw ith der ived types .

    H u g e P lu s E a s e o f le a rn in g to u se

    de r ived typ e s correct ly.

    v s .C + +

    T h e job o f a des t ruc to r is to re lease an yasse t s acq u i red b y an ob jec t d u r ing i ts

    l i fe t ime.module curves !points on the unit circle

    use precision

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    143/196

    1 4 3 o f

    use precision

    privatepublic curvepublic length, segment, create, destroypublic get_x, get_y

    type curveprivatereal(adequate), pointer:: x(:) => Null()real(adequate), pointer:: y(:) => Null()

    end type curve

    interface createmodule procedure curve_create

    end interface create

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    144/196

    1 4 4 o f

    interface destroymodule procedure curve_destroy

    end interface destroy

    interface lengthmodule procedure curve_length

    end interface length

    D efine a s s ign m en t o f one cu rv e to an o the rso tha t it m ak es a cop y.

    interface assignment(=)module procedure curve_set_curve

    end interface

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    145/196

    1 4 5 o f

    end interface

    T he con s truc to r an d de s t ruc to r andas s ign m en t ope ra to r w ork t oge the r to

    av o id m em ory leak s .subroutine curve_create(c, x, y)

    real(adequate), intent(in):: x(:), y(:)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    146/196

    1 4 6 o f

    real(adequate), intent(in):: x(:), y(:)

    type(curve), intent(inout):: cif(size(x) /= size(y)) thenstop "curve assignment: incorrect sizes"

    endif

    call curve_destroy(c)allocate(c%x(size(x)))allocate(c%y(size(y)))c%x = xc%y = y

    end subroutine curve_create

    subroutine curve_set_curve(p, q)type(curve), intent(inout):: ptype(curve), intent(in):: q

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    147/196

    1 4 7 o f

    call curve_create (p, q%x, q%y)end subroutine curve_set_curve

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    148/196

    M em ory in t eg r it y t hen dep end s on ap r o g r a m m e r c o n v e n t io n to d e stro y e a c h

    it em tha t i s c rea ted .subroutine try_curves

    use precision

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    149/196

    1 4 9 o f

    p

    use curvesimplicit nonetype(curve) c1, c2, c3real(adequate) xx(4), yy(4)

    xx = (/ 1., 2., 3., 4. /)yy = xx**2 + 1call create(c1, xx, yy)call create(c2, xx, yy / 2.)write(11, *) "y(c2) ", y(c2)

    ... some coding which may or may not use c3....call destroy(c1)

    call destroy(c2)call destroy(c3)

    end subroutine try_curves

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    150/196

    1 5 0 o f

    H o m e w o r k

    A tic-tac-toe board consists of a 3 by 3 matrix. Each entrycan either be empty, an X, or an O. Create a tic-tac-toeboard object which is sufficiently rich to support writing atic-tac-toe game. In the game, users can undo moves right

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    151/196

    1 5 1 o f

    g g , g

    back to the start. The board class should refuse to makeillegal moves and return an error flag if so requested.

    There is no right answer.

    How robust you make the input procedure is up to you.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    152/196

    1 5 2 o f

    Lesson 4: New features

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    153/196

    1 5 3 o f

    Lesson 4: New features

    Paul F. Dubois

    X-Division

    [email protected]

    To com plete ou r u pg rad e t o F95 , th i sw eek w e ll cove r som e o f t he new f ea tu re s

    o f the lang uag e .Scaling considerations

    More about working with arrays

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    154/196

    1 5 4 o f

    More about working with arrays

    Looping and control statements

    More intrinsic functions

    Whats still missing?

    Caution: todays example is strongly F95, not just F90.Youll have to comment out quite a few things to get it to

    compile F90.

    T h e en ve lope , p l ease ...

    (The contest was won by George Zimmerman).

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    155/196

    1 5 5 o f

    T he m od u le is the k ey to ha pp ines s , bu tthe re a re p rob lem s fo r l a rge p rogr am s .

    The module is what remembers information between files.So clearly the compiler must remember some informationabout each module it encounters.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    156/196

    1 5 6 o f

    Typically, this is done by creating a module_name.mod file.(Not universal, not standard).

    When the compiler sees a use statement, it looks for the

    .mod file.

    M od u le use m us t fo rm an acyc l ic d ir ectedg r a p h .

    Depending on the situation, you may decide to separate datafrom functions.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    157/196

    1 5 7 o f

    module a_data...end module a_datamodule b_data...end module b_datamodule a

    use a_data

    use b_datacontains

    functions that manipulate a_databut sometimes need b_data

    end module a_data

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    158/196

    1 5 8 o f

    Similarly for b.

    M in i -le s son #5 : O ne m od u le , on e f ile

    Often the module contains one type and implements anobject.

    You can put module statements in a file with other programunits but dont

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    159/196

    1 5 9 o f

    units, but don t.

    F 95 sup p or t s go - to - le s s loop s .

    x = (/ (i, i=1, n) /)y = sqrt(x**2/3.0)

    The old way to find a maximum...t = x(1)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    160/196

    1 6 0 o f

    t = x(1)k = 1do i = 2, n

    if(x(i) > t) then

    t = x(i)k = iendif

    enddo

    New way #1...t = x(1)

    k = 1do i = 2, n

    if(x(i)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    161/196

    1 6 1 o f

    ( )k = i

    enddo

    New way #2...

    t = maxval(x)k = maxloc(x, 1)

    T h e w o r d e x it n o w m e a n s to le a v e a lo o p ,no t the p rogra m !

    t = 12.0k = 0do i = 1, n

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    162/196

    1 6 2 o f

    if(x(i) == t) thenk = iexit

    endif

    enddo

    T h e do w hi le loop is o f ten used fo r it e ra tiveprocesses .

    k = 0do while(k /= 0)

    if(x(i) == t) k = i

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    163/196

    1 6 3 o f

    end do

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    164/196

    write(11, *) "Sum of y elements within x range is ", t

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    165/196

    1 6 5 o f

    A labe l c an b e u sed t o con t ro l behav io r innes ted loo ps .

    outer: do i=1, ninner: do j = 1, m

    ...

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    166/196

    1 6 6 o f

    if (...) exit outerenddoenddo

    However, loops of any sort are not fun to write.

    F95 to the rescue!

    M ask ed a s s ign m ent e lim ina tes the ne ed fo rm a n y lo o p s .

    where (w1

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    167/196

    1 6 7 o f

    z = 0.0end wherewrite(11, *) "Same thing, without a loop ", sum(z)

    T he w he re con s truc t a lso ha s a on e -s ta tem en t fo rm .

    logical cond(size(y))...z = 0.0

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    168/196

    1 6 8 o f

    cond = w1

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    169/196

    1 6 9 o f

    enddo

    Now we face the same inequity with the array syntax:x = sqrt(y) !ok

    x(1) = f(y(1))x = vector_version_of_f(y)

    Generic interfaces would work so that you could just writef(y) but you would have to write a separate function for

    each rank of array.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    170/196

    1 7 0 o f

    I f f is dec la red e lem en ta l , f(x ) w ill w orkon an y a r r ay.

    elemental function f(x)real(adequate), intent(in):: xreal(adequate):: f

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    171/196

    1 7 1 o f

    f = sqrt(x / 3.0)end function f

    where(cond)

    z = f(x)elsewhere

    z = 0.0endwhere

    write(11, *) "Same thing, using elemental function ",sum(z)

    x = x - 4.0 ! make some values negativew1 = 0.0cond = w1

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    172/196

    1 7 2 o f

    where(cond)z = f(x)

    elsewherez = 0.0

    endwherewrite(11, *) "f(x) not evaluated at bad points ", sum(z)

    L oo p s an d fun c t ion s d isgu i se in fo rm a t ionyou m ay ha ve abo u t p a r a l le lism .

    Two new facilities are provided.

    You can d ec l a re a f un c t ion p u re t o ind i ca tei t ha s no s ide e ffec ts .

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    173/196

    1 7 3 o f

    T he fo r a ll con s truc t exp re s se s p a r a lle l loo psco m p ac t ly an d c lear ly.

    T h e fo ra ll s ta t em en t a lso com es in tw of lavors .

    integer c(6)

    forall(i=1:6) c(i) = 3 - i

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    174/196

    1 7 4 o f

    forall(i=1:6)c(i) = 3 - i

    end forall

    M u lt ip le d im en s ion s rep lace n es ted loo pcons t ruc t s .

    integer, parameter:: n= 5, m = 6integer a(n, m), b(n, m)

    forall(i=1:n, j=1:m) a(i,j) = 10 * i + j

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    175/196

    1 7 5 o f

    forall(i=1:n, j=1:m)a(i, j) = 10 * i + j

    b(i, j) = x(i) - y(j)end forall

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    176/196

    N am elis t is no w s tand a rd i zed .

    You declare a namelist group to contain a group of variablesthat you wish to read or write as a group.

    namelist /ab/ a, b

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    177/196

    1 7 7 o f

    T he ab is c a l led t he g roup nam e .

    I t c an b e u se d fo r inp u t o r ou tpu t. I t ran read w h a t i t w ro te .

    W h at i t w ro te m ig h t b e u g ly.

    You use t he N M L keyw ord in a r ead o rw r it e s t a tem en t in s t ead o f a fo rm at .

    forall(i=1:n, j=1:m)a(i,j) = i + jb(i,j) = i - j

    end foralli ( b)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    178/196

    1 7 8 o f

    write(11, NML=ab)

    Here is what it wrote.&ABA = 11, 21, 31, 41, 12, 22, 32, 42, 13,

    23, 33, 43, 14, 24, 34, 44, 15, 25, 35,45,

    B = 2*0, 2*1, 2*0, 2*1, 0, 3*1, 0, 3*1, 0, 3*1/

    You can ca ll f unc t ion s u s ing keyw ords .

    subroutine go(x, y, z)

    call go(1, 2, 3)

    call go(1, z=4, y=2)

    call go(x=1 z=2 y=3)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    179/196

    1 7 9 o f

    call go(x=1, z=2, y=3)

    To do this, subroutine go must have an explicit interface.

    T h ere a re som e in te res ting a r ray -o r i en tedin t r ins ic fu n c t ion s .

    spread is used to replicate parts of arrays to make largerdimensional versions.

    a = 10 * spread(iotan dim=2 ncopies=m) + &

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    180/196

    1 8 0 o f

    a = 10 spread(iotan, dim=2, ncopies=m) + &spread(iotam, dim=1, ncopies=n)

    is the same as:

    forall(i=1:m, j=1:n) a(i,j) = 10 * i + j

    We could have done our outer product with spread.

    H e re a r e so m e o t h e r a r r a y fu n c tio n s y o um igh t w an t to exp lo re .

    csh i ft -- c i rcu la r sh i f t

    eo sh i ft -- end -o ff sh i ft

    m atm u l -- m a trix m u ltip ly

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    181/196

    1 8 1 o f

    m a tm u l -- m a trix m u ltip ly d o t _ p r o d u c t

    co un t - - cou n t ing true e l em en t s in log ica le x p r e s s i o n s

    m erge ( true_ sou rce , f a lse_ sou rce , m ask )

    p ack (ar ray, m ask ) - - co m p ress

    T h e resh ap e fun c tion is in c red ib lyp o w e r f u l .

    Use it for reshaping and reordering subscripts.

    reshape(source, shape, pad, order)

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    182/196

    1 8 2 o f

    reshape( iota(6), (/ 2, 3 /)) is the matrix1 3 52 4 6

    reshape( iota(6), (/ 2, 4 /), (/ 0, 0 /), (/ 2, 1 /))1 2 3 45 6 0 0

    R e sh a p e c a n b e u s ed t o m a k e a n a r r a y o n e -d i m e n s i o n a l

    real(adequate) a(n,m), z(n*m)z = reshape(a, (/ size(a) /))

    You can also do it with transfer(a,a). The transfer function isextremely powerful non portable and fun

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    183/196

    1 8 3 o f

    extremely powerful, non-portable, and fun.

    I shouldnt tell you about this... -- Jeanne Martin

    I deny ever telling you about it.

    M os t o f the inqu iry o pe ra to r s on a r ray s a reab l e to t ake an ex t ra a rgum en t ind ica t ing ad i m e n s i o n .

    shape(a) 4 5

    Sum up columnssum(a,1) 14 18 22 26 30

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    184/196

    1 8 4 o f

    sum(a,1) 14 18 22 26 30

    Sum up rows

    sum(a,2) 20 25 30 35

    Sum up arraysum(a) 110

    A lso , m os t o f the sca la r in t r ins ic s a ree lem en ta l , so yo u can u se them on a r ray s .

    sq r t , ex p , cos , . ..

    d im (x , y ) - - x - y bu t f loo red a t ze ro

    r and om n um be r (z )

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    185/196

    1 8 5 o f

    _ ( )

    So w hy d idn t I wa n t to le a rn F 90 b e fo re?

    Stup id i ty

    N o c o m p i lers

    N o p erfo rm an ce

    but more seriously

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    186/196

    1 8 6 o f

    but more seriously

    N o inh er i tan ce

    N o p o lym o rph ism / d y n am ic b ind ing N o ga rbag e co l lec t ion

    N o ex cep t ion han d l ing

    N o ge ner i c type s

    N o c o m m an d l ine a rgu m en ts

    N o s igna l h and l ing

    N o s t r ing s

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    187/196

    1 8 7 o f

    T h e s t r in g fac ilit ies in F 95 a re s t illm a s siv e ly a n n o y in g .

    character(len=32):: s = hellocharacter(len=32):: t = worldcharacter(len=32):: greetinggreeting = s // ' ' // t !Not...sets greeting to hellogreeting = trim(s) // ' '

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    188/196

    1 8 8 o f

    g g ( )greeting(len(trim(s)) + 1: ) = t !Just shoot me...

    You cant even write a function that returns a variably sizedstring such as a file name with no trailing blanks, I thought.

    (I was wrong, as we shall see.)

    H e y ! I d e a ! I c a n w r ite m y o w n s tr in g ty p e !

    type stringcharacter(len=1), pointer:: storage(1)integer capacity

    integer lengthend type string

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    189/196

    1 8 9 o f

    end type string

    and go ahead and make a string object out of it.

    Hey! Wait! If this is such a great idea, why didnt ISO do it?

    A ctu a l ly, ISO d id d o i t.

    They made a separate standard for it and never mention itout loud.

    I had to ask Jeanne Martin for it, and she didnt even have acopy.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    190/196

    1 9 0 o f

    MODULE ISO_VARYING_STRING

    ! Written by J.L.Schonfelder! Incorporating suggestions by C.Tanasescu, C.Weber, J.Wagener and W.Walter,! and corrections due to L.Moss, M.Cohen, P.Griffiths, B.T.Smith! and many other members of the committee ISO/IEC JTC1/SC22/WG5

    ! Version produced (??-Jul-94)

    !-----------------------------------------------------------------------------!! This module defines the interface and one possible implementation for a !! dynamic length character string facility in Fortran 90. The Fortran 90 !! language is defined by the standard ISO/IEC 1539 : 1991. !! The publicly accessible interface defined by this module is conformant !

    ! with the auxilliary standard, ISO/IEC 1539-2 : 1994. !! The detailed implementation may be considered as an informal definition of !! the required semantics, and may also be used as a guide to the production !! of a portable implementation. !! N.B. Although every care has been taken to produce valid Fortran code in !

    ! construction of this module no guarantee is given or implied that this !! code will work correctly without error on any specific processor, nor !! is this implementation intended to be in any way optimal either in use !! of storage or CPU cycles. !!-----------------------------------------------------------------------------!

    Gosh, how reassuring...

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    191/196

    1 9 1 o f

    Indeed , va ry ing s t ring s eem s to b es o m e th i n g lik e w h a t I im a g in e d .

    TYPE VARYING_STRINGPRIVATECHARACTER,DIMENSION(:),POINTER :: chars

    ENDTYPE VARYING_STRING...

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    192/196

    1 9 2 o f

    PUBLIC :: VARYING_STRING,VAR_STR,CHAR,LEN,GET,PUT,PUT_LINE,INSERT,REPLACE,SPLIT,REMOVE,REPEAT,EXTRACT,INDEX,SCAN,VERIFY,LLT,LLE,LGE,LGT,ASSIGNMENT(=),OPERATOR(//),OPERATOR(==),OPERATOR(/=),OPERATOR(),LEN_TRIM,TRIM,IACHAR,ICHAR,

    ADJUSTL,ADJUSTR

    L et s t ake it fo r a t es t d r ive .

    subroutine try_string! try the ISO varying_string

    use iso_varying_stringimplicit nonetype(varying_string) s, t, us = "hello"

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    193/196

    1 9 3 o f

    t = "world"u = s // var_str(" ") // t

    write(11, *) char(u)end subroutine try_string

    ...and it nicely writes out hello world.

    T h e s tr in g tha t is p r in ted tu rned ou t no t toh a v e a n y o b n o x i o u s p a d d in g o n t h e e n d .

    Indeed, the source for the char() operator teaches us how:

    FUNCTION s_to_c(string)

    type(VARYING_STRING),INTENT(IN) :: stringCHARACTER(LEN=SIZE(string%chars)) :: s to c

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    194/196

    1 9 4 o f

    CHARACTER(LEN SIZE(string%chars)) :: s_to_c! returns the characters of string as an automatically! sized characterINTEGER :: lclc=SIZE(string%chars)DO i=1,lc

    s_to_c(i:i) = string%chars(i)

    ENDDOENDFUNCTION s_to_c

    Remember how we couldnt do real(kind(x)) for anargument x?That doesnt apply here as it is simply a matter of how much

    automatic storage is needed.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    195/196

    1 9 5 o f

    H o w y o u g o n n a k ee p e m , d o w n o n th efa rm. . .

    Now that you have looked over, and seen the promised land,Im sure youll get there.

    I hope you have enjoyed your upgrade experience.

    There is no support number.

  • 8/7/2019 FORTRAN TRAINING - Large-scale simulations with Fortran 95 An object-based approach

    196/196

    1 9 6 o f

    -- Paul