A fluid simulation method and system based on Taichi and Pytorch
By combining Taichi and PyTorch in a fluid simulation approach, a fluid dataset is generated and a model is trained, which solves the problem of insufficient accuracy in fluid simulation and achieves high-precision fluid simulation results.
Patent Information
- Application Number
- CN202510015142.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-06
- Publication Date
- 2026-02-06
- Estimated Expiration
- 2045-01-06
AI Technical Summary
Existing deep learning-based fluid simulation methods lack a complete framework for dataset generation, model building, and training, resulting in insufficient accuracy in fluid simulation.
The Taichi framework is used to build a fluid simulation algorithm to generate a fluid dataset. A multilayer perceptron model is built using PyTorch. The dataset is transformed using NumPy and Torch, the fluid simulation model is trained, and the fluid animation is rendered using the Blender engine.
A comprehensive framework was constructed, encompassing fluid dataset generation, fluid simulation model building and training, and fluid animation generation. This framework improved the accuracy of fluid simulation and ensured the rationality of fluid particle motion and rendering precision.
Smart Images

Figure CN119903747B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of fluid simulation, and particularly relates to a fluid simulation method and system based on Taichi and Pytorch. BACKGROUND
[0002] Fluid simulation refers to simulating the actual flow of fluid. Fluid simulation plays an important role in product design optimization, process control optimization, environmental protection and disaster response, aerospace, automotive engineering, hydroelectric power generation, meteorology and other fields. Although fluid simulation methods based on deep learning have developed rapidly, there is no perfect framework including data set generation, model building and training, and fluid animation generation, so that the precision of current fluid simulation is not satisfactory.
[0003] Therefore, how to provide a fluid simulation method and system based on Taichi and Pytorch to improve the precision of fluid simulation has become a technical problem to be solved. SUMMARY
[0004] The technical problem to be solved by the present application is to provide a fluid simulation method and system based on Taichi and Pytorch to improve the precision of fluid simulation.
[0005] In a first aspect, the present application provides a fluid simulation method based on Taichi and Pytorch, comprising the following steps:
[0006] Step S1, a fluid simulation algorithm based on a Taichi framework is used to generate a fluid data set comprising a plurality of fluid motion trajectories;
[0007] Step S2, a fluid simulation model composed of multiple layers of perception is built through Pytorch;
[0008] Step S3, each fluid motion trajectory in the fluid data set is converted into a corresponding Torch tensor to obtain a tensor data set, and the fluid simulation model is trained through the tensor data set;
[0009] Step S4, the initial state of the fluid is obtained, and the initial state of the fluid is input into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence;
[0010] Step S5, the fluid particle position sequence is converted into a fluid model sequence, and the fluid model sequence is rendered into a fluid animation to complete the fluid simulation.
[0011] Further, in step S1, the fluid simulation algorithm is a position-based fluid simulation algorithm.
[0012] The fluid motion trajectory at least comprises a fluid position, a fluid position correction value, a fluid particle radius and a fluid particle quantity; the fluid position correction value is calculated based on a density conservation law;
[0013] In the step S2, the fluid simulation model is used to constrain and update the fluid position.
[0014] Further, the step S3 is specifically:
[0015] Each fluid motion trajectory in the fluid dataset is read as Numpy data by Numpy, and each Numpy data is converted into a corresponding Torch tensor to obtain a tensor dataset; the tensor dataset is divided into a training set, a verification set and a test set based on a preset proportion.
[0016] The fluid simulation model is trained through the training set until a preset convergence condition is met, the trained fluid simulation model is verified through the verification set, the fluid simulation model that passes the verification is tested through the test set, and the model parameters of the fluid simulation model that passes the test are saved.
[0017] Further, the step S4 is specifically:
[0018] An initial state of the fluid including at least a fluid position, a fluid particle radius and a fluid particle quantity is obtained, the initial state of the fluid is converted into a Torch tensor, and the trained fluid simulation model is input for forward simulation to obtain a fluid particle position sequence.
[0019] Further, the step S5 is specifically:
[0020] Through the Blender engine, the fluid particle position sequence is converted into a fluid model sequence carrying vertex information, vertex normal information and face index by using the Stop Motion Obj plug-in, and the fluid model sequence is rendered into a fluid animation to complete fluid simulation.
[0021] In a second aspect, the present application provides a fluid simulation system based on Taichi and Pytorch, comprising the following modules:
[0022] A fluid dataset construction module is used to generate a fluid dataset including a plurality of fluid motion trajectories based on a fluid simulation algorithm constructed based on a Taichi framework.
[0023] A fluid simulation model building module is used to build a fluid simulation model composed of a plurality of layers of perception by Pytorch.
[0024] a fluid simulation model training module, configured to convert each fluid motion trajectory in the fluid dataset into a corresponding Torch tensor to obtain a tensor dataset, and train a fluid simulation model based on the tensor dataset;
[0025] a forward simulation module, configured to obtain a fluid initial state, and input the fluid initial state into the trained fluid simulation model to perform forward simulation and obtain a fluid particle position sequence;
[0026] a fluid animation generation module, configured to convert the fluid particle position sequence into a fluid model sequence, and render the fluid model sequence into a fluid animation to complete fluid simulation.
[0027] Further, in the fluid dataset construction module, the fluid simulation algorithm is a position-based fluid simulation algorithm.
[0028] The fluid motion trajectory at least includes fluid position, fluid position correction value, fluid particle radius, and fluid particle quantity; and the fluid position correction value is calculated based on the density conservation law.
[0029] In the fluid simulation model construction module, the fluid simulation model is configured to constrain and update fluid position.
[0030] Further, the fluid simulation model training module is specifically configured to:
[0031] read each fluid motion trajectory in the fluid dataset as Numpy data through Numpy, convert each Numpy data into a corresponding Torch tensor to obtain a tensor dataset, and divide the tensor dataset into a training set, a validation set, and a test set based on a preset proportion;
[0032] train the fluid simulation model based on the training set until a preset convergence condition is met, verify the trained fluid simulation model based on the validation set, test the fluid simulation model that passes the verification based on the test set, and save the model parameters of the fluid simulation model that passes the test.
[0033] Further, the forward simulation module is specifically configured to:
[0034] obtain a fluid initial state including at least fluid position, fluid particle radius, and fluid particle quantity, convert the fluid initial state into a Torch tensor to input the trained fluid simulation model to perform forward simulation and obtain a fluid particle position sequence.
[0035] Further, the fluid animation generation module is specifically configured to:
[0036] The fluid particle position sequence is converted into a fluid model sequence carrying vertex information, vertex normal information and face index by using the Stop Motion Obj plug-in of the Blender engine, and the fluid model sequence is rendered into a fluid animation to complete the fluid simulation.
[0037] The present application has the advantages of:
[0038] The fluid simulation algorithm constructed by the Taichi framework generates a fluid dataset including a plurality of fluid motion trajectories; then a fluid simulation model composed of multiple layers of perception is built by Pytorch, each fluid motion trajectory in the fluid dataset is converted into a corresponding Torch tensor to obtain a tensor dataset, and the fluid simulation model is trained through the tensor dataset; then the initial state of the fluid is obtained and input into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence; finally, the fluid particle position sequence is converted into a fluid model sequence, and the fluid model sequence is rendered into a fluid animation to complete the fluid simulation; that is, a perfect framework is constructed from fluid dataset generation, fluid simulation model building and training, and fluid animation generation, and since the Taichi framework has high performance and flexibility, it is compatible with Python and can be well combined with deep learning, the fluid simulation model is used to constrain and update the fluid position to ensure the rationality of the fluid particle motion, the fluid model sequence carries vertex information, vertex normal information and face index to ensure the accuracy of rendering, and finally the fluid simulation accuracy is greatly improved. BRIEF DESCRIPTION OF DRAWINGS
[0039] The present application will be further described below with reference to the accompanying drawings and embodiments.
[0040] Fig. 1 is a flowchart of a fluid simulation method based on Taichi and Pytorch according to the present application.
[0041] Fig. 2 is a structural schematic diagram of a fluid simulation system based on Taichi and Pytorch according to the present application.
[0042] Fig. 3 is a schematic diagram of a fluid animation according to the present application. DETAILED DESCRIPTION
[0043] The technical solutions in the embodiments of the present application have the following general idea: a perfect framework is constructed for generating a fluid data set, building and training a fluid simulation model, and generating a fluid animation, and because the Taichi framework has high performance and flexibility, it is compatible with Python and can be well combined with deep learning, the fluid simulation model is used to constrain and update the fluid position to ensure the rationality of the fluid particle motion, and the fluid model sequence carries vertex information, vertex normal information, and face index to ensure the accuracy of rendering, so as to improve the fluid simulation accuracy.
[0044] Please refer to Figs. 1 to 3 A preferred embodiment of the fluid simulation method based on Taichi and Pytorch provided by the present application comprises the following steps:
[0045] Step S1: A fluid simulation algorithm based on the Taichi framework is used to generate a fluid data set comprising a plurality of fluid motion trajectories.
[0046] The Taichi framework is suitable for fields such as physical simulation and machine learning, and has high performance and flexibility, and is compatible with Python and can be well combined with deep learning; the fluid motion trajectory is in txt format.
[0047] Step S2: A fluid simulation model composed of a plurality of multilayer perceptrons (MLP) is built by using Pytorch.
[0048] Step S3: Each fluid motion trajectory in the fluid data set is converted into a corresponding Torch tensor to obtain a tensor data set, and the fluid simulation model is trained by using the tensor data set.
[0049] Step S4: The initial state of the fluid is obtained, and the initial state of the fluid is input into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence.
[0050] Step S5: The fluid particle position sequence is converted into a fluid model sequence, and the fluid model sequence is rendered into a fluid animation to complete the fluid simulation.
[0051] In step S1, the fluid simulation algorithm is a position-based fluid simulation algorithm (PBF).
[0052] The fluid motion trajectory at least comprises a fluid position, a fluid position correction value, a fluid particle radius, and a fluid particle quantity; and the fluid position correction value is calculated based on the law of conservation of density.
[0053] In step S2, the fluid simulation model is used to constrain and update the fluid position.
[0054] The step S3 is specifically:
[0055] Each fluid motion trajectory in the fluid dataset is read as Numpy data by Numpy, and each Numpy data is converted into a corresponding Torch tensor to obtain a tensor dataset, and the tensor dataset is divided into a training set, a validation set and a test set based on a preset ratio.
[0056] Since the fluid simulation model (neural network) cannot directly identify the text file (fluid dataset), Numpy is needed to read the text file and convert it into Numpy data (digital fluid position sequence), and Torch is used to convert the Numpy data into a Torch tensor for the fluid simulation model to identify. Numpy data is a three-dimensional variable, where each item is the position information of all fluid particles in each frame, for example ((0.2, 0.5, 0.3), (0.4, 0.7, 0.8)) means the position of the first fluid particle x = 0.2, y = 0.5, z = 0.3, and the position of the second fluid particle x = 0.4, y = 0.7, z = 0.8.
[0057] The fluid simulation model is trained through the training set until the preset convergence condition is met, and the trained fluid simulation model is verified through the validation set, the fluid simulation model that passes the verification is tested through the test set, and the model parameters of the fluid simulation model that passes the test are saved as a pth file.
[0058] The step S4 is specifically:
[0059] The initial state of the fluid including at least fluid position, fluid particle radius and fluid particle number is obtained, the initial state of the fluid is converted into a Torch tensor, and the trained fluid simulation model is input into the forward simulation to obtain a fluid particle position sequence.
[0060] The step S5 is specifically:
[0061] Through the Blender engine, the fluid particle position sequence is converted into a fluid model sequence carrying vertex information, vertex normal information and face index using the Stop Motion Obj plug-in, and the fluid model sequence is rendered into a fluid animation (video file) to complete the fluid simulation.
[0062] Blender is widely used in animation, modeling, rendering and other fields, and Stop Motion OBJ is a plug-in designed for Blender, which allows a series of OBJ format mesh files to be imported as animation frames.
[0063] The preferred embodiment of the fluid simulation system based on Taichi and Pytorch comprises the following modules:
[0064] A fluid dataset construction module is configured to generate a fluid dataset comprising a plurality of fluid motion trajectories based on a fluid simulation algorithm constructed based on a Taichi framework.
[0065] The Taichi framework is suitable for physical simulation and machine learning and has high performance and flexibility.
[0066] A fluid simulation model construction module is configured to construct a fluid simulation model comprising a plurality of multilayer perceptrons (MLPs) by using Pytorch.
[0067] A fluid simulation model training module is configured to convert each fluid motion trajectory in the fluid dataset into a corresponding Torch tensor to obtain a tensor dataset, and train the fluid simulation model based on the tensor dataset.
[0068] A forward simulation module is configured to obtain an initial state of a fluid, input the initial state of the fluid into the trained fluid simulation model to perform forward simulation, and obtain a sequence of fluid particle positions.
[0069] A fluid animation generation module is configured to convert the sequence of fluid particle positions into a sequence of fluid models, render the sequence of fluid models into a fluid animation, and complete fluid simulation.
[0070] In the fluid dataset construction module, the fluid simulation algorithm is a position-based fluid simulation algorithm (PBF).
[0071] The fluid motion trajectory comprises at least a fluid position, a fluid position correction value, a fluid particle radius, and a fluid particle quantity.
[0072] In the fluid simulation model construction module, the fluid simulation model is configured to constrain and update the fluid position.
[0073] The fluid simulation model training module is specifically configured to:
[0074] Each fluid motion trajectory in the fluid dataset is read as Numpy data by using Numpy, each Numpy data is converted into a corresponding Torch tensor to obtain a tensor dataset, and the tensor dataset is divided into a training set, a validation set, and a test set based on a preset ratio.
[0075] Since the fluid simulation model (neural network) cannot directly identify the text file (fluid dataset), it is necessary to use Numpy to read the text file, convert it into Numpy data (digital fluid position sequence), and use Torch to convert the Numpy data into a Torch tensor for the fluid simulation model to identify. The Numpy data is a three-dimensional variable, where each item is the position information of all fluid particles in each frame, for example ((0.2, 0.5, 0.3), (0.4, 0.7, 0.8)) means the first fluid particle position x = 0.2, y = 0.5, z = 0.3, and the second fluid particle position x = 0.4, y = 0.7, z = 0.8.
[0076] The fluid simulation model is trained by the training set until the preset convergence condition is met, and then the trained fluid simulation model is verified by the verification set, the fluid simulation model that passes the verification is tested by the test set, and the model parameters of the fluid simulation model that passes the test are saved as a pth file.
[0077] The forward simulation module is specifically used for:
[0078] Obtaining the initial state of the fluid including at least the fluid position, the fluid particle radius and the number of fluid particles, converting the initial state of the fluid into a Torch tensor, inputting the trained fluid simulation model for forward simulation, and obtaining the fluid particle position sequence.
[0079] The fluid animation generation module is specifically used for:
[0080] Using the Stop Motion Obj plug-in to convert the fluid particle position sequence into a fluid model sequence carrying vertex information, vertex normal information and face index through the Blender engine, and rendering the fluid model sequence into a fluid animation (video file) to complete the fluid simulation.
[0081] Blender is widely used in the fields of animation, modeling, rendering, etc., and Stop Motion OBJ is a plug-in designed for Blender, which allows a series of OBJ format mesh files to be imported as animation frames.
[0082] In summary, the advantages of the present application are:
[0083] The fluid simulation algorithm built by the Taichi framework generates a fluid dataset including a plurality of fluid motion trajectories; then a fluid simulation model composed of a plurality of layers of perception is built by Pytorch, each fluid motion trajectory in the fluid dataset is converted into a corresponding Torch tensor to obtain a tensor dataset, and the fluid simulation model is trained through the tensor dataset; then the initial state of the fluid is obtained, the initial state of the fluid is input into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence; finally, the fluid particle position sequence is converted into a fluid model sequence, and the fluid model sequence is rendered into a fluid animation to complete the fluid simulation; that is, a perfect framework is built from fluid dataset generation, fluid simulation model building and training, and fluid animation generation, and because the Taichi framework has high performance and flexibility, it is compatible with Python and can be well combined with deep learning, the fluid simulation model is used to constrain and update the fluid position to ensure the rationality of the fluid particle motion, the fluid model sequence carries vertex information, vertex normal information and face index to ensure the accuracy of rendering, and finally the fluid simulation accuracy is greatly improved.
[0084] Although the specific embodiments of the present application are described above, those skilled in the art should understand that the specific embodiments described are only illustrative, and are not intended to limit the scope of the present application, and equivalent modifications and changes made by those skilled in the art in accordance with the spirit of the present application should be covered within the scope of the claims of the present application.
Claims
1. A Taichi and Pytorch based fluid simulation method, characterized in that: The method comprises the following steps: Step S1, generating a fluid data set comprising a plurality of fluid motion trajectories based on a fluid simulation algorithm constructed based on a Taichi framework; Step S2, building a fluid simulation model comprising a plurality of layers of perception through Pytorch; Step S3, converting each fluid motion trajectory in the fluid data set into a corresponding Torch tensor to obtain a tensor data set, and training the fluid simulation model through the tensor data set; Step S4, obtaining a fluid initial state, inputting the fluid initial state into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence; Step S5, converting the fluid particle position sequence into a fluid model sequence, and rendering the fluid model sequence into a fluid animation to complete fluid simulation.
2. The Taichi and Pytorch-based fluid simulation method of claim 1, wherein: In the step S1, the fluid simulation algorithm is a position-based fluid simulation algorithm; The fluid motion trajectory at least comprises a fluid position, a fluid position correction value, a fluid particle radius, and a fluid particle quantity; the fluid position correction value is calculated based on the law of conservation of density; In the step S2, the fluid simulation model is used to constrain and update the fluid position.
3. The Taichi and Pytorch-based fluid simulation method of claim 1, wherein: The step S3 specifically comprises: reading each fluid motion trajectory in the fluid data set into Numpy data through Numpy, converting each Numpy data into a corresponding Torch tensor to obtain a tensor data set, and dividing the tensor data set into a training set, a validation set, and a test set based on a preset ratio; training the fluid simulation model through the training set until a preset convergence condition is met, verifying the trained fluid simulation model through the validation set, testing the fluid simulation model that passes the verification through the test set, and saving the model parameters of the fluid simulation model that passes the test.
4. The Taichi and Pytorch-based fluid simulation method of claim 1, wherein: The step S4 specifically comprises: obtaining a fluid initial state comprising at least a fluid position, a fluid particle radius, and a fluid particle quantity, converting the fluid initial state into a Torch tensor to input into the trained fluid simulation model for forward simulation to obtain a fluid particle position sequence.
5. The Taichi and Pytorch-based fluid simulation method of claim 1, wherein: The step S5 specifically comprises: using a Stop Motion Obj plug-in to convert the fluid particle position sequence into a fluid model sequence carrying vertex information, vertex normal information, and face index through a Blender engine, and rendering the fluid model sequence into a fluid animation to complete fluid simulation.
6. A Taichi and Pytorch based fluid simulation system, characterized in that: The method comprises the following modules: a fluid data set construction module, configured to generate a fluid data set comprising a plurality of fluid motion trajectories based on a fluid simulation algorithm constructed based on a Taichi framework; a fluid simulation model building module, configured to build a fluid simulation model comprising a plurality of layers of perception through Pytorch; a fluid simulation model training module, configured to convert each fluid motion trajectory in the fluid data set into a corresponding Torch tensor to obtain a tensor data set, and train the fluid simulation model through the tensor data set; a fluid simulation model training module, configured to convert each fluid motion trajectory in the fluid data set into a corresponding Torch tensor to obtain a tensor data set, and train the fluid simulation model through the tensor data set; The forward simulation module is configured to obtain a fluid initial state, input the fluid initial state into the trained fluid simulation model for forward simulation, and obtain a fluid particle position sequence; The fluid animation generation module is configured to convert the fluid particle position sequence into a fluid model sequence, render the fluid model sequence into a fluid animation, and complete fluid simulation.
7. The Taichi and Pytorch based fluid simulation system of claim 6, wherein: In the fluid dataset construction module, the fluid simulation algorithm is a position-based fluid simulation algorithm. The fluid motion trajectory includes at least a fluid position, a fluid position correction value, a fluid particle radius, and a fluid particle quantity; and the fluid position correction value is calculated based on the law of conservation of density. In the fluid simulation model construction module, the fluid simulation model is configured to constrain and update the fluid position.
8. The Taichi and Pytorch based fluid simulation system of claim 6, wherein: The fluid simulation model training module is specifically configured to: read each fluid motion trajectory in the fluid dataset as Numpy data through Numpy, convert each Numpy data into a corresponding Torch tensor, obtain a tensor dataset, divide the tensor dataset into a training set, a validation set, and a test set based on a preset proportion, train a fluid simulation model through the training set until a preset convergence condition is met, verify the trained fluid simulation model through the validation set, test the fluid simulation model that passes the verification through the test set, and save the model parameters of the fluid simulation model that passes the test. The forward simulation module is specifically configured to:
9. The Taichi and Pytorch based fluid simulation system of claim 6, wherein: obtain a fluid initial state including at least a fluid position, a fluid particle radius, and a fluid particle quantity, convert the fluid initial state into a Torch tensor, input the fluid initial state into the trained fluid simulation model for forward simulation, and obtain a fluid particle position sequence. The fluid animation generation module is specifically configured to:
10. The Taichi and Pytorch based fluid simulation system of claim 6, wherein: use the Stop Motion Obj plug-in to convert the fluid particle position sequence into a fluid model sequence carrying vertex information, vertex normal information, and face index through the Blender engine, render the fluid model sequence into a fluid animation, and complete fluid simulation.
Citation Information
Patent Citations
Data-driven coal supercritical water hydrogen production reactor multiphase flow prediction method
CN115186575A
Circuit board device thermal analysis method and device based on target detection
CN118070748A