Msc. Patran pcl handbook In a Nutshell 7



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

Loop Control Statements

For Loop


  • Syntax

    FOR (
    var=numeric_expr TO numeric_expr [BY numeric_expr]) [label]
    statements …
    END FOR


  • E
    Square braces [ ], denotes optional
    xample

    AvgTemp = 0.0
    FOR (i = 1 TO NumNodes)
    AvgTemp += NodalTemp(i)
    END FOR
    AvgTemp = AvgTemp/NumNodes

While Loop


  • Syntax

    WHILE (
    logical_expression) [label]
    statements …
    END WHILE


  • Example

    AvgTemp = 0.0
    i = 1
    WHILE (i <= NumNodes)
    AvgTemp += NodalTemp(i)
    i += 1
    END WHILE
    AvgTemp = AvgTemp/NumNodes


Loop Control Statements

Repeat Loop


This is similar to a WHILE Loop except that it will always be executed at least once. Only use REPEAT loops if you want to ALWAYS execute the loop at least once, otherwise us a WHILE loop.

  • Syntax


    REPEAT [label]
    statements …
    UNTIL (logical_expression)


  • Example


    AvgTemp = 0.0
    i = 1
    REPEAT
    AvgTemp += NodalTemp(i)
    i += 1
    UNTIL (i > NumNodes)
    AvgTemp = AvgTemp/NumNodes


Loop Control Statements

BREAK


The BREAK statement is used to exit a loop prior to its normal termination. It can be used in any of the loop statements.

  • S
    If status is not equal to zero, then the end of the file has been reached. Execution then breaks out of the WHILE loop to the statements immediately following the loop.
    yntax

    BREAK [
    label]

  • Example 1(reading a text file)

    WHILE (TRUE)

    status = text_read_string(…)

    IF (status != 0) THEN BREAK
    END WHILE
    statements



  • Example 2 (nested loops, using labels)

    WHILE (i <= NumNodes) MainLoop




    WHILE (j <= 100) AnotherLoop

    WHILE (k <= 200) InnerLoop

    IF (status != 0) THEN BREAK MainLoop

    IF (status != 0) THEN BREAK AnotherLoop

    IF (status != 0) THEN BREAK

    IF (status != 0) THEN BREAK InnerLoop

    END WHILE

    statements …

    END WHILE

    statements …

    END WHILE


    statements


Loop Control Statements

CONTINUE

The CONTINUE statement is used to skip to the end of the loop

  • Syntax

    CONTINUE [
    label]

  • Example 1 (reading a text file)

    INTEGER file_id, length

    STRING read_str[80]

    /* read grid information */

    WHILE (text_read_string(file_id, read_str, length) == 0)

    IF (str_index(read_str, “GRID”) == 0) THEN CONTINUE

    statements …
    END WHILE


  • Example 2 (nested loop, using labels)


    WHILE (i <= NumNodes) MainLoop


    REPEAT InnerLoop

    CONTINUE MainLoop

    BREAK InnerLoop

    CONTINUE

    CONTINUE InnerLoop

    UNTIL (j > 1000)

    statements …

    END WHILE

    statements …

Exercise 4: Writing Files


    Create a PCL function to write nodal data to a user-defined file.

  1. The function should have a single argument, the filename to be created, i.e., FUNCTION write_nodes_to_file(FileName)

  2. The data should be written to the file as a table, i.e.,

Node Id x-coordinate y-coordinate z-coordinate

  1. Use virtual arrays

  2. Use the following built-in functions:

db_count_nodes(NumNodes)

db_get_node_ids(NumNodes, NodeIds)

db_get_nodes(NumNodes, NodeIds, rcids, acids, NodeXYZ)

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

text_close(FileId, Options)

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

text_write_string(FileId, OutString)

  1. Use the documentation if you have questions about the arguments to the built-in functions.

  2. Think about your choice for the format argument in the text_write(…) function. Will your file be comma or space delimited? Will it be fixed or free format?

  3. Sample code outline:

  1. Declare variables

  2. Count nodes in the database

  3. Allocate arrays

  4. Get node Ids

  5. Get node coordinates

  6. Open file

  7. Write data to the file with a loop

  8. Close file

  9. Be sure to include a message that the file output is complete

Writing Files

Extra credit: Include a header line at the top of the file that includes the filename and the total number of nodes written to the file.




Download 1.08 Mb.

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




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

    Main page