0% found this document useful (0 votes)
5 views15 pages

report

Uploaded by

barbaraes906214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

report

Uploaded by

barbaraes906214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Pipe sizing optimization

Requirements analysis
The purpose of this experiment is to optimize the selection of pipe size to meet
the requirements of maximum flow and maximum loading rate by collecting
customer information and according to certain calculation logic.
In the experiment, multi-threaded programming and mutex are deplyed to
handle concurrent data access, and the network delay is simulated to simulate the
real environment.

Specification
In the data.cpp file, it contains three main functions, which are as follows:

 The ReadFluidData function reads fluid data from a file with a specified path and stores the

data in a vector of type FluidType.

 The ReadPipeData function reads pipe size data from a file in the specified path and stores

the data in a vector of type PipeSize.

 ReadPipeData function reads pipe size data from a file in the specified path and stores the

data in a vector of type PipeSize

Inputs:

file_path (string type) - This is the argument passed to the ReadFluidData and ReadPipeData

functions, specifying the path to the file to read data from.

customer (object of type CustomerOrder) - This is the argument passed to the

WriteCustomerToFile function and contains information about the customer order to be written

to the file.
file_path (string type) - This is also the argument passed to the WriteCustomerToFile function,

specifying the path to the file to write customer order information to.

Outputs:

 ReadFluidData function:

On success: Returns a vector of type std::vector<FluidType>, containing all the fluid data

read from the file.

On failure (such as a failed file opening) : Return an empty std::vector<FluidType> vector

with an error message printed in standard error output.

 ReadPipeData function:

On success: Returns a vector of type std::vector<PipeSize>, containing all pipe size data read

from the file.

On failure (such as a failed file opening) : Return an empty std::vector<PipeSize> vector with

an error message printed in standard error output.

 WriteCustomerToFile function:

Success: No direct output, but customer order information will be appended to the specified

file.

Failure (such as failure to open a file) : Print an error message in standard error output.

Figure 1 Customer ID samples

Calculation

Here's a breakdown of the formulas used in the `calculate_process.cpp` file and what each
formula does within the algorithm:

1. Volume Flow Rate Calculation

Calculates the volume flow rate, which is the volume of fluid that can be filled in a given

time. This is a fundamental parameter used in the subsequent calculations.

where:

Q is the volume flow rate (cubic feet per second).

V is the maximum tank capacity (in cubic feet).

t is the maximum fill time (in seconds).

2. Ideal Pipe Diameter Calculation

Determines the ideal diameter of the pipe required to achieve the desired flow rate

with a given flow velocity.

where:

Dideal is the ideal pipe diameter in inches.

𝑄𝑄Q is the volume flow rate.

𝑣v is the flow rate (constant in this code, with a value of 9.0)

3. Actual Flow Velocity Calculation

This is important for further analysis such as calculating the Reynolds number.

where:

vactual is the actual flow rate.

D is the nominal diameter (in inches) of the pipe.

4. Reynolds Number Calculation


Purpose: Determines the Reynolds number, which is a dimensionless quantity that helps

predict the flow patterns in a fluid, such as whether the flow is laminar, transitional, or turbulent.

where:

Re is the Reynolds number.

vactual is the actual flow rate.

D is the nominal diameter (in feet) of the pipe.

ν is the coefficient of kinematic viscosity (also known as dynamic viscosity in square feet per

second) of a fluid, given in code as kinematic viscosity divided by density (i.e. kinematic viscosity).

Reynolds number is used to determine the type of fluid flow:

If Re≤2000, the flow type is Laminar.

If2000<𝑅𝑒Or less4000If 2000<Re≤4000, the flow type is Transitional.

If𝑅𝑒>4000If Re>4000, the flow type is Turbulent.

Software architecture
File ect326.cpp is a C++ application framework based on the Windows API for creating a

graphical user interface (GUI) program. The core function of the program is to allow the user to

select the fluid type, enter the relevant parameters, and calculate the best pipe size, and then

write this information to the file. Here is a detailed explanation of the code framework
:

Figure 2 Flowchart of the program

1. Included header file

- 'Windows.h' : contains all the declarations and definitions of the Windows API, and is the

basis for creating Windows programs.

- 'iostream' : standard input/output stream, used to perform input/output operations in a

program.

- 'thread' : Used in multithreaded programming to allow a program to perform multiple

tasks at the same time.

2. Macro definition

- 'UNICODE' : Undefine UNICODE, which is usually used to control whether a program uses a

wide character set.

- 'FLUID_DATA_DIR', 'PIPE_DATA_DIR', 'CUSTOMER_INFO_DIR' : defines the file path where


fluid data, pipe size data, and customer information are stored.

3. Global variables

- 'fluid_type_list' : Stores the fluid type information read from the fluid data file.

- 'pipe_size_list' : Stores pipe size information read from the pipe size data file.

- 'selected_fluid' : stores the fluid type selected by the user.

- 'result' : Stores the result of the pipeline selection.

- result_valid: A Boolean value indicating whether the result is valid.

4. WinMain function

This is the entry point for Windows programs and is responsible for the initialization of the

program and the start of the message loop.

- Here, the program defines a window class, registers the class, then creates a window, and

enters the message loop.

5. WndProc function

- This is the window procedure function, which is the core of processing messages in

Windows programs.

- It performs different operations depending on the type of message received (such as'

WM_PAINT ', 'WM_CREATE', 'WM_COMMAND', etc.).

6. ConcurrentCalculateAndWrite function

This function uses C++ lambda expressions and 'std::thread' to create a new thread.

- In the new thread, it calls the 'Calculate' function to calculate the optimal pipe size, and

then writes the customer order information to the file using the 'WriteCustomerToFile' function.

7. GUI component creation

- In the processing of the 'WM_CREATE' message, the program creates GUI components

such as radio buttons, edit boxes, and buttons.

- Radio buttons allow the user to select different fluid types.

- The edit box allows the user to enter the maximum tank capacity and the maximum tank
fill time.

- Buttons allow the user to confirm selections and inputs, triggering calculations and writes.

Figure 3 GUI screenshot

8. Message processing

- In the processing of 'WM_COMMAND' messages, the program responds to the user's input

and actions.

- When the user clicks the Confirm button, the program reads the input in the edit box,

selects the fluid type, performs the calculation, and writes the result to a file.

9. Draw the GUI

- In the processing of 'WM_PAINT' messages, the program uses GDI functions to draw GUI

elements such as text and buttons.

10. Program exit

- In the handling of the 'WM_CLOSE' message, the program ends the message loop and exits

by sending a 'PostQuitMessage'.

This framework provides a basic Windows application structure that can be further

extended and refined to meet specific application needs.


Code Solution
Debug Log
The following is a consolidated list of possibilities and solutions for debugging
problems that may arise in the 'calculate_process.cpp' file:

Well, I'll provide a more detailed textual description of each debugging possibility,
including the potential causes, implications, and how to address these issues through
code and design.

1. Divide by zero error


 Cause: When calculating the volume flow rate, if 'max_fill_time' is zero, it will
result in a case of dividing by zero, which is mathematically undefined and
usually results in a run-time error in the program.
 Impact: The program may terminate abnormally or produce unpredictable
results.
 Solutions:In the 'CalculateVolumeFlowRate' function, first check if 'max_fill_time'
is zero. If zero, an error code or a specific value such as' -1 'or' NaN '(not a
number) is returned and an error log is recorded.At the user interface level, add
input validation to prevent users from entering zero or negative numbers as'
max_fill_time '.

2. List of invalid pipe sizes


 Reason: If pipe_size_list is empty or not loaded correctly, the FindActualPipe
function will not be able to find the appropriate pipe size because there is no size
to choose from.
 Impact: There is no suitable pipe size to choose from, resulting in the inability to
complete the calculation and analysis of fluid flow.
 Solutions :Make sure that pipe_size_list is loaded correctly and has at least one
valid pipe size before calling FindActualPipe. Implements an initialization check.
If 'pipe_size_list' is empty, the program should stop execution and prompt the
user to load pipe size data.

3. Error in calculation of ideal pipe diameter


 Reason: In the 'FindActualPipe' function, the formula for calculating the ideal
pipe diameter may not produce the expected result due to the accuracy of the
input data or calculation errors.
 Impact: May result in the selection of an inappropriate pipe size, affecting the
efficiency or safety of fluid flow.
 Solutions :Review and test the formula for the ideal pipe diameter to ensure that
it produces reasonable results across all expected input ranges. Performs a
logical check to ensure that the calculated ideal pipe diameter is within the
optional size range provided by 'pipe_size_list'.

4. Floating point accuracy problem


 Reason: The representation of floating-point numbers in the computer may have
precision errors, and comparing two floating-point numbers with the equal sign
'==' may result in incorrect results.
 Impact: May result in logical errors, such as when looking for the pipe with the
closest diameter to the ideal.
 Solutions:Use a small error value (epsilon) to check if two floating-point
numbers are "close enough".When comparing floating-point numbers, check
that the absolute value of their difference is less than epsilon.

5. Multi-threading security
 Reason: in ` ConcurrentCalculateAndWrite ` function, if multiple threads access
or modify the Shared resources at the same time, and without the proper
synchronization mechanism, race conditions may occur.
 Impact: May cause data inconsistencies or program status errors.
 Solutions :Use mutexes to protect access to shared resources, ensuring that only
one thread can execute critical code segments at a time.Consider using condition
variables or other synchronization primitives to coordinate operations between
threads.

6. Resource leakage
 Cause: In a multithreaded environment, memory leaks may occur if dynamically
allocated memory or other resources are not released correctly.
 Impact: Long running programs may consume more and more memory,
eventually causing performance degradation or program crashes.
 Solutions :Use smart Pointers such as' std::unique_ptr 'or' std::shared_ptr 'to
automatically manage dynamically allocated memory.For other types of
resources, such as file handles or network connections, make sure to use
appropriate functions to close or free them when they are no longer needed.

7. User input validation


 Cause: If the data entered by the user (such as' max_tank_volume 'and'
max_fill_time ') is not validated, it may contain illegal values or do not conform
to the format expected by the program.
 Impact: Illegal or illogical input values may result in calculation errors or program
logic confusion.
 Solutions:Add input validation steps to the program logic to ensure that all input
data is valid and reasonable before being used.For numeric input, check that it is
within a reasonable range, such as a non-negative number, a specific minimum
or maximum value.For string input, check its length, format, or whether it
contains illegal characters.
With these detailed debugging possibilities and solutions, problems in the program
can be more fully identified and fixed, thus improving the stability and reliability of
the program.

Conclusion
Through this experiment, a pipe size optimization program is implemented, and
the problem of concurrent data access is successfully dealt with.The efficiency and
performance of a fluid transfer system can be improved by the appropriate choice of
pipe size.Based on the actual flow rate and Reynolds number, the pipe size can be
adjusted to accommodate different types of fluid flow.The calculation logic in this
experiment needs to be further optimized and improved to consider more practical
situations and factors.Future work could include consideration of the characteristics
of different fluid types, more accurate flow rate calculation methods, and more
complex pipe network modelingType, etc. Through continuous optimization, the
requirements of fluid transmission system can be better met, and the efficiency and
reliability of the system can be improved.

You might also like