report
report
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
The ReadPipeData function reads pipe size data from a file in the specified path and stores
ReadPipeData function reads pipe size data from a file in the specified path and stores the
Inputs:
file_path (string type) - This is the argument passed to the ReadFluidData and ReadPipeData
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
ReadPipeData function:
On success: Returns a vector of type std::vector<PipeSize>, containing all pipe size data read
On failure (such as a failed file opening) : Return an empty std::vector<PipeSize> vector with
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.
Calculation
Here's a breakdown of the formulas used in the `calculate_process.cpp` file and what each
formula does within the algorithm:
Calculates the volume flow rate, which is the volume of fluid that can be filled in a given
where:
Determines the ideal diameter of the pipe required to achieve the desired flow rate
where:
This is important for further analysis such as calculating the Reynolds number.
where:
predict the flow patterns in a fluid, such as whether the flow is laminar, transitional, or turbulent.
where:
ν 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).
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
:
- 'Windows.h' : contains all the declarations and definitions of the Windows API, and is the
program.
2. Macro definition
- 'UNICODE' : Undefine UNICODE, which is usually used to control whether a program uses a
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.
4. WinMain function
This is the entry point for Windows programs and is responsible for the initialization of the
- Here, the program defines a window class, registers the class, then creates a window, and
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'
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.
- In the processing of the 'WM_CREATE' message, the program creates GUI components
- 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.
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.
- In the processing of 'WM_PAINT' messages, the program uses GDI functions to draw GUI
- 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
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.
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.
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.