|
|
2 |
| Structural Analysis Programming Course (1) |
|
|
This is the second installment, and this time we will cover key points that are useful when dealing with large amounts of data in structural analysis programming
This explanation covers general characteristics of programming that involves mathematical formulas. Furthermore, it describes how to add in-plane analysis functions to applications
We will also introduce how to use the development kit, the "FRAME(In-Plane) SDK."
|
|
Handle large amounts of data efficiently! |
|
In structural analysis programming, the amount of data being handled often becomes enormous, so it is sometimes better to manage it collectively. When there are a large number of grid points, instead of having separate arrays for "grid point numbers," "X coordinates," and "Y coordinates," it is easier to manage if you combine the grid point number, X coordinate, and Y coordinate information into a single array of grid point information. If you handle data individually, there is a risk of errors such as retrieving different data, for example, getting the "grid point number array" as 10 while getting the "X coordinate array" as 11. Therefore, it is generally better to manage the data in a consolidated manner.
The data that is organized in this way is called a record (or structure), and it is defined using the `type` clause. Furthermore, each variable within the record is referred to as a field.
TNODE = packed record
NodeNo : integer;
XPoint : double;
Point : double;
end; |
To define an array of records, you can define it in the same way as a regular array, using the following syntax.
| NodeData : array[1..100] of TNODE; |
Specify the name of the record defined in the array type section. Use the dot (".") to access data.
NodeData.NodeNo := 1;
NodeData.XPoint := 0.0;
NodeData.YPoint := 0.0 |
|
Attempt to allocate memory during program execution |
|
In structural analysis programming, the number of grid points and components required for calculations can vary significantly depending on the type of structure being analyzed, and in some cases, the amount of data involved can be extremely large. Here, we will explain how to allocate memory on a computer, which is important in such situations. There are 2 main methods for allocating memory: static allocation and dynamic allocation.
Static Allocation: This refers to a method of allocating memory when you know in advance the exact amount of data that will be used
Dynamic Allocation: This refers to a method of allocating memory only for the amount of data required during program execution
In terms of programming difficulty, static allocation is generally considered an easier method. Another approach involves pre-defining the maximum number of grid points and components based on anticipated needs, fixing these values, and then performing calculations; however, this method may consume memory unnecessarily for grid points and components that are not actually required.
While recording media has become cheaper and large-capacity memory is now more readily available, ensuring that you have only the necessary amount of memory remains a fundamental principle in programming.
Furthermore, it often happens in practice that the maximum value is not determined until the data creation stage.
Let's take a closer look at each of these methods below
■ Methods for Static Security
This corresponds to the array declaration that we have been explaining so far. Similar to the example on the previous page, if you statically allocate an array of records containing grid point information, it would look like this:
In this example description, an array of 100 grid point information entries is defined using array indices from 1 to 100.
■ Methods for Dynamic Allocation
The number of data elements used is determined during program execution and is not fixed at the time of declaration in the programming process. However, a declaration is necessary to utilize the data.
In this case, the declaration would be as follows in the previously mentioned grid point information declaration. This corresponds to the array declarations that have been explained so far. Similar to the example on the previous page, if you statically allocate an array of records for grid point information, it would look like this:
First, we declare an array without specifying its size. Then, once the number of elements is determined, we allocate memory for that many elements in the array
| SetLength( NodeData, 100 ); |
So far, we have created an array of 100 grid point information elements. Note that unlike static allocation, the indices of the array range from 0 to 99. Another way to dynamically allocate memory is to use pointers. A pointer is a variable that refers to a memory address. During program execution, both the program's code and data are allocated in memory, and a pointer is a variable that points to a specific location (memory address) within that memory space. To dynamically allocate memory using pointers, you need to define the type of the pointer in the `type` section.
TNODE = packed record
NodeNo : integer;
XPoint : double;
YPoint : double;
end;
PNODE = ^TNODE; |
To define a pointer of a record type, you must include a caret symbol "ˆ" in the definition of the record type. This PNODE will then become a pointer to the record type. To allocate memory using this pointer, write the following
var
List : TList;
pNodeData : PNODE;
begin
List := TList.Create; // Creates a list object
New(pNodeData); // Creates node data
pNodeData->NodeNo = 1; // Assigning the grid point number
pNodeData->XPoint = 0.0; // Assigning the X-coordinate of the grid point
pNodeData->YPoint = 0.0; // Assigns the Y-coordinate of the grid point
List.Add(pNodeData); // Add to the list
New( pNodeData );
pNodeData^.NodeNo := 2;
pNodeData^.XPoint := 5.0;
pNodeData^.YPoint := 0.0;
List.Add( pNodeData );
for i:=0 to 1 do
begin
Dispose(List[i]); // Release the grid point data
end; // Release the list
List.Free;
end; |
When you define a pointer variable and allocate memory using the New procedure, an area of memory equal to the size of the record is reserved. Then, by adding a caret "^" to the pointer variable, you can specify the fields of the record and perform read/write operations on their values. The memory allocated with New must be released by the programmer themselves, which requires calling the Dispose procedure.
|
What should one be careful of when performing calculations? |
|
■ Pay attention to the units used in calculations
This is not limited to structural analysis, but when it comes to programming that involves calculations, it's a good idea to develop the habit of paying close attention to units. Even if you write a function to process an equation, if the data passed to that equation has inconsistent units, the results can be completely wrong. Also, in cases where calculations are performed using specific units, it is necessary to pass values that have been converted to those units before performing the calculation. In this case, the flow would be to convert the calculated value to the unit you want to use and then use it.
■ Avoid calculation errors caused by zero values
For programs that perform calculations, the value "zero" can be a very problematic element. In expressions involving division, dividing by zero results in either an error or a non-numeric value (NaN). For calculation formulas that do not require zero, it is best to predefine values to prevent accidental input of zero, which would reduce the risk of such errors occurring.
■ Do not compare real numbers in the same way as integers
When creating a computational program that handles real numbers, it may not function correctly if treated in the same way as integers. To verify this, we will place 2 buttons on the form: one labeled "Integer Check" and the other labeled "Real Number Check," and test them accordingly.
First, we will perform calculations using integers. The following program produces the expected results when used with integers.
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
sum : integer;
begin
sum := 0;
for i := 1 to 10 do
sum := sum + 1;
if sum=10 then
ShowMessage('Correct')
else
ShowMessage('Incorrect');
end; |
When executed, the sum of adding 0.1 10 times is supposed to be stored in the variable "sum," and the if statement should evaluate to True; however, an incorrect message is actually displayed. This occurs because the value 0.1 cannot be perfectly represented in a computer system that uses binary numbers, and calculations are performed using an approximate value of 0.1, leading to this result. Even when dealing with decimal numbers used in everyday life, certain values like one-third cannot be expressed precisely; for example, it can only be represented as "0.333" by truncating the digits after a certain point. It is clear that even if you add this truncated value 3 times, the result will be 0.999, not 1.
Now, how can we make the above program run? There is a method where we consider it "okay" if the absolute value of the difference between "sum" and 1.0 is small enough to be considered equal.
if Abs(sum-1.0)<0.00001 then
ShowMessage('Correct')
else
ShowMessage('Incorrect');
end; |
Besides this, there's also a method that involves performing calculations as integers and then adjusting the decimal places at the end. However, I will omit the explanation of that method here.
■ Types of Calculation Errors
In computer calculations, errors can occur in the results not only in the cases mentioned earlier but also in other situations. The following provides an overview of the types of calculation errors that can arise from computer programs.
Rounding error
If the number of digits in a calculation result is too large to be represented, it needs to be rounded to a representable value. The difference between this rounded value and the original value is called "rounding error."
Information blackout
In computer calculations, the number of significant digits is limited, so when adding or subtracting large and small values, the smaller value may not be reflected. This phenomenon is called "loss of information."
Truncation error
When subtracting 2 values that are almost the same, the number of significant digits decreases, a phenomenon known as "loss of precision." For example, if we set the number of significant digits to 5 and subtract 0.12344 from 0.12345, the result is 0.00001, which has only 1 significant digit. This means that the missing digits are automatically filled with zeros, leading to an error.
Interruption error
When using a computer to obtain calculation results, and employing a method of repeatedly performing calculations to approach the solution, it is necessary to stop the process at some point because the calculations cannot be continued indefinitely. The error that arises between the calculated value obtained by stopping the computation and the correct answer is called "truncation error."
|
Development of solver utilization for structural analysis |
|
■ Utilizing SDKs within the FRAME interface
This section introduces how to use the FRAME in-plane SDK, a development kit that allows you to add in-plane analysis functions to your applications. We will explain everything from file input to calculation and output results using sample source code included on the accompanying DVD-ROM. First, we will describe key points that should be understood before actually using the samples.
The FRAME in-plane SDK is a development kit (SDK: Software Development Kit) that provides the calculation engine as a library from the FRAME in-plane analysis software (manufactured by FORUM8), which is a general-purpose planar structural analysis software with in-plane load analysis functions. This allows readers to add in-plane analysis functionality to new or existing applications using this library
■ To call a function in a DLL
To call functions within a DLL from an application, you need to access the "entry point" of that function. This refers to the location (point) where program execution begins, and each externally exported function in a DLL has its own entry point.
There are 2 methods for importing functions from a DLL: static linking and dynamic linking. Static linking involves loading the DLL into memory at the start of program execution, while dynamic linking allows the DLL to be loaded into memory at any time.
Dynamic linking allows you to conserve memory because the DLL is only loaded into memory when its functions are used. However, it requires several steps before importing the library. Therefore, if a function is frequently used within a program, it's better to use static linking, which completes the process in one go and results in faster access.
■ What is a pointer?
A pointer is a variable that stores information about the memory address where a variable's value is located. In other words, it indicates the starting address of the memory area where the variable's value is stored; it does not contain the data of the variable itself.
When retrieving the value at an address pointed to by a pointer, you need to write code that extracts the value from the pointer. Let's look at a simple example of how to work with pointers in Delphi.
var
a : Integer;
b : Integer;
p: Integer; // "PInteger" is also acceptable
begin
a := 200; // Assign the value 200 to the variable a.
p := @a; // Assign the address of 'a' to 'p'
b := p*; // Assign the data pointed to by pointer p to variable b.
end; |
The above is an example of assigning the address of an integer variable 'a' to an integer pointer variable 'p', and then assigning the actual data at the address pointed to by the pointer 'p' to a variable 'b'. However, as shown in line 4, you can define a type as a pointer type by adding "^" (caret) before the type. In line 7, the address of variable 'a' is assigned to the pointer 'p', but by placing "@" before the variable name, you can obtain the address where that variable is stored.
Finally, on line 8, the actual data is retrieved from the address pointed to by pointer p and assigned to variable b. When accessing data through a pointer, you can refer to the data by adding "^" after the pointer variable. You can also write values to the memory location accessed by the pointer. In that case, it would be written as "p^:=400;", but this will simultaneously change the value of variable a, which points to the same address as the pointer variable p. Variable b only receives the data part of the pointer variable p, and since the addresses are different, it is not affected and remains at 200 [see diagram below].
In this way, pointers become 1 method of accessing data.
I mentioned earlier that the term "pointer" is not often heard in Delphi because Delphi handles pointer-related operations internally, allowing developers to create applications without having to explicitly deal with pointers. For example, in the above example, when a variable named 'a' is declared, memory space and its address are automatically allocated.
Therefore, while knowledge of pointers is not strictly essential for learning Delphi, it's true that using pointers offers several advantages.
First, since pointers only store addresses, they can essentially store the address of any type of variable. Also, because the address pointed to by a pointer is valid within the same process, when passing parameters to external routines, using a pointer allows the external routine to directly read and write values in memory. Therefore, even when passing complex structures with deep hierarchies or internal pointers, only 1 pointer needs to be specified as a parameter.
The examples above illustrate the use of pointers at the variable level, but in structural analysis programming, pointers are often used to pass data within structures. The functions included in the development kit "FRAME In-Plane SDK," which will be introduced later, also use these pointers for data transfer, so it is essential to master them.
if SaveDialog1.Execute then
begin
if FileExists( SaveDialog1.FileName ) then
if MessageDlg('File aleady exists. Save now?', mtWarning,
[mbYes, mbNo], 0, mbYes) = mrYes then
begin
Memo1.Lines.SaveToFile( SaveDialog1.FileName );
end;
end; |
The SaveDialog, like the OpenDialog, allows you to set the Filter property.
When you register the above description, the file types displayed in the dialog will be limited to text format. Additionally, if you register the extension you want to use for auto-completion (in this case, "txt") in the DefaultExt property, it will automatically suggest that extension.
Similarly, we will implement features such as "Save As," "Print," "Application Settings," and the "Format" menu. For more details, please refer to the book "Programming Introduction for Civil Engineering and Architecture Engineers," which I mentioned at the beginning; if you are interested, please take a look.
The next online seminar will be "Structural Analysis Programming Course (2)". We plan to explain how to use sample programs when utilizing solvers in structural analysis, leveraging the FRAME in-plane SDK. We look forward to your participation.
Announcement of paid seminars
| Introduction to Programming for Engineers Seminar CPD Accredited |
| • Date and time |
April 15, 2011 (Friday), 9:30 AM to 5:00 PM |
| • Course fees |
Price for 1 person: 15,750 yen (including tax) |
| • This is the main venue |
FORUM8 Tokyo headquarters, GT Tower seminar room
※TV conferencing systemThe event will be held simultaneously in Tokyo, Osaka, Nagoya, and Fukuoka |
|
|
| (Featured in the Up&Coming January issue) |
 |
|







>> Product comprehensive catalog

>> Premium membership services
>> Financial support
| Recently released products |
|
|
|