Msc. Patran pcl handbook In a Nutshell 7


Exercise 5: Reading Files



Download 1.08 Mb.
Page7/25
Date05.08.2017
Size1.08 Mb.
#26706
1   2   3   4   5   6   7   8   9   10   ...   25

Exercise 5: Reading Files


    Write a function to read the file created in Exercise 4.

  1. The function should accept a single argument, i.e., the filename to be read.

    FUNCTION ReadFile(FileName)

  1. Use the data in the file to create nodes with the following built-in function from the PCL Reference Manual:

    fem_create_nodes_1(RefCIDList, AnalysisCIDList, GeomFlag, @

    NodeIDList, XYZList, NodesCreatedList)

  1. Use the following built-in functions:

text_open(FileName, Options, idum, idum, FileId)

text_close(FileId, Options)

    text_read(FileId, Format, Ints, Reals, Chars)

  1. Sample code outline:

  1. Declare variables

  2. Open the file

  3. Read the file within a loop

  4. Create new nodes as the file is read within the loop

  5. Close the file

Extra credit: What if a node ID to be created already exists in the database?

Extra credit: What if the file to be read doesn’t exist? What function can be used to determine if a file exists?

Extra credit: Searching the documentation reveals another function that can be used to create nodes, i.e.,
db_create_nodes(num_nodes, rcids, acids, xyz, group_id, node_ids, node_exists). Why might you use this function versus fem_create_nodes_1(…)?

Conditional Control Statements

IF Statement


  • Syntax


    IF (logical_expression) THEN


    statements …
    ELSE IF (logical_expression) THEN


    statements …
    ELSE


    statements …
    END IF


  • Example


    IF (MyKeyWord == “CBAR” || MyKeyWord == “CBEAM”) THEN


    statements …
    ELSE IF (MyKeyWord == “CTRIA” || MyKeyWord == “CQUAD”) THEN


    statements …
    ELSE IF (MyKeyWord == “GRID”) THEN


    statements …
    ELSE


    statements …
    END IF


Conditional Control Statements

SWITCH Statement


This is often used in place of an IF statement when many comparisons will be made.



  • Syntax



    SWITCH (expression) [label]

    CASE (expression1, expression2, …)

    statements …

    CASE (expression3, expression 4, …)

    statements …

    DEFAULT

    statements …
    END SWITCH


  • Example


    Note that this SWITCH statement is equivalent to the IF statement on the previous page.




    SWITCH (EntityType)

    CASE (“CBAR”, “CBEAM”)

    statements …

    CASE (“CTRIA”, “CQUAD”)

    statements …

    CASE (“GRID”)

    statements …

    DEFAULT

    statements …
    END SWITCH

Structure of a PCL Function


  • Example 1 (subroutine or procedure example)

FUNCTION calling_function()

REAL Var1, Var2

Var1 = 5.0

still_another_function(Var1, Var2)

ui_write(“Var2 = “//STR_FROM_REAL(Var2))

END FUNCTION /* calling_subroutine */


Input argument (NumIn)






FUNCTION still_another_function(NumIn, NumOut)

R
Output argument (NumOut)
EAL NumIn, NumOut


NumOut = NumIn + 3.14

ui_write(“NumIn = “//STR_FROM_REAL(NumIn))

ui_write(“NumOut = “//STR_FROM_REAL(NumOut))

END FUNCTION /* still_another_function */


Calling statement




calling_function()

$
Sample output
#NumIn = 5.0


$#NumOut = 8.14

$#Var2 = 8.14

Structure of a PCL Function



  • Example 2 (function or command example)

FUNCTION calling_subroutine2()


Variable declaration




REAL MyVal

R
Variable declaration

and initialization


EAL a(3) = [1.0, 1.0, 2.0]


REAL b(3)

b(1) = 2.0

b(2) = 2.0

b(3) = 1.0

MyVal = DotProduct(a, b)

ui_write(“Dot product = “//STR_FROM_REAL(MyVal))

END FUNCTION /* calling_subroutine2 */


All variables must be declared, including function arguments. Note the use of dummy parentheses to indicate that the argument is an array. The array dimensions within the function DotProduct is inherited (3 in this case) from the array dimensions within the calling function (calling_subroutine2 in this case).


FUNCTION DotProduct(a, b)



REAL a(), b()

REAL val

val = a(1)*b(1) + a(2)*b(2) + a(3)*b(3)

RETURN val

END FUNCTION /* DotProduct */


Calling statement




calling_subroutine2()


Sample output

$#Dot product = 6.0



Structure of a PCL Function

  • Example 3. In this example, the PCL function acts both as a procedure and a command. The result of the calculation is returned as an argument for the function. The return value is used to indicate if the function calculation was successful or not. This methodology is very common in PCL functions written by the MSC.Software development staff.

FUNCTION calling_subroutine3()

INTEGER status

REAL NumIn, NumOut

status = MySqrt(NumIn, NumOut)

IF (status == 0) THEN

ui_write(“The square root of “//STR_FROM_REAL(NumIn)//” is “//@

STR_FROM_REAL(NumOut))

ELSE

ui_write(STR_FROM_REAL(NumIn)//” does not have a square root!”)

END IF

RETURN status

END FUNCTION /* calling_subroutine3 */

FUNCTION MySqrt(NumIn, NumOut)

REAL NumIn, NumOut

IF (NumIn <= 0.0) THEN RETURN –1

NumOut = MTH_SQRT(NumIn)

RETURN 0

END FUNCTION /* MySqrt */


Download 1.08 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   10   ...   25




The database is protected by copyright ©ininet.org 2024
send message

    Main page