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)
-
Example 2 (nested loops, using labels)
WHILE (i <= NumNodes) MainLoop
Loop Control Statements
CONTINUE -
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 …
Create a PCL function to write nodal data to a user-defined file.
-
The function should have a single argument, the filename to be created, i.e., FUNCTION write_nodes_to_file(FileName)
-
The data should be written to the file as a table, i.e.,
Node Id x-coordinate y-coordinate z-coordinate
-
Use virtual arrays
-
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)
-
Use the documentation if you have questions about the arguments to the built-in functions.
-
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?
-
Sample code outline:
-
Declare variables
-
Count nodes in the database
-
Allocate arrays
-
Get node Ids
-
Get node coordinates
-
Open file
-
Write data to the file with a loop
-
Close file
-
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.
Share with your friends: |