April 5, 2023
Mohamed Aly Sayed

ChatGPT and CFD

What the hell "is" ChatGPT?

ChatGPT is a large-scale language model based on the Generative Pre-trained Transformer (GPT) architecture developed by OpenAI. It uses deep learning techniques to generate natural language text that is similar in style and coherence to human-generated text. The GPT architecture is based on a deep neural network that consists of multiple layers of attention-based transformers*, which enable the model to capture complex relationships and patterns in natural language data. The model is pre-trained on a large corpus of text data, such as web pages or books, to learn patterns in the data and develop a general understanding of natural language.

*Attention-based transformers are a type of neural network architecture that have been shown to be highly effective for processing and generating natural language. These models use a mechanism called attention to focus on the most relevant parts of an input sequence when generating an output sequence.

The attention mechanism in transformers involves computing a set of attention weights that determine the relative importance of each input token to the output at each time step. This allows the model to selectively attend to specific parts of the input sequence and generate more accurate and coherent outputs.

In the case of ChatGPT, the model is based on the GPT architecture, which uses a stack of these attention-based transformers to process and generate natural language text. The input to the model is a sequence of text tokens, which are fed into the model one at a time. At each time step, the model uses attention to focus on the most relevant parts of the input sequence based on the current state of the model, and generates a probability distribution over the possible next tokens in the sequence.

The model is trained on a large corpus of text data using a self-supervised learning approach, which involves predicting the next token in a sequence given the previous tokens. This allows the model to learn patterns and relationships in natural language data and develop a general understanding of language.

Once trained, ChatGPT can be fine-tuned on specific tasks, such as language translation, text generation, or even code debugging by providing it with task-specific data and adjusting its parameters. The model can generate text in response to input prompts, such as questions or statements, by using the patterns and relationships it learned during pre-training and fine-tuning.

How big are we talking?

The size and complexity of ChatGPT varies depending on the version of the model. As of September 2021, the largest version of ChatGPT, GPT-3, has 175 billion parameters, making it one of the largest and most powerful language models available. However, smaller versions of the model with fewer parameters, such as GPT-2, are also available for use.

ChatGPT does not necessarily improve on a daily basis, but rather with each new release or update from OpenAI. The model is trained on large amounts of data and requires significant computational resources to train and fine-tune, which limits how frequently the model can be updated. However, new releases and updates can bring improvements in the model's performance and capabilities, such as increased accuracy or the ability to perform new tasks.


Can I use ChatGPT in CFD?

Well you can use ChatGPT for your interest in pretty much any project. The big catch is: What do you expect from the tool? As you all may know CFD is a sophisticated topic that needs an extensive experience and a far-more detail in modeling that what ChatGPT is capable of (only at the moment). However, you can still use it for generating simple CFD solvers for 2D/3D canonical flows. Once you increase the complexity of the problem you have to be very careful with what you feed the model (which requires the level of experience it takes you to write the code yourself). I'm not saying the that the tool is useless in that sense; I'm saying that in order to use it you have to have a base-level of knowledge to be a good referee for what the tool outputs to you.In other words, to get the tool to increase your efficiency but not to follow the output text blindly.

For instance, I asked ChatGPT to write a python script for a laminar 2D lid-driven cavity flow using Finite Difference Method (FDM). The first time it gave me a very rudimentary script that didn't work, I kept refining my input (by being very specific about the code structure, problem setup and the way to write the solution result). Here's the result:

Square Cavity Lid-driven Flow

Visualization of output vtk files in Paraview [code snippet can be found here]:

Scaled velocity vectors overlayed on top of velocity magnitude contours

Be careful! As I mentioned above, you should NOT trust a code that come from ChatGPT straight away. The generated code must be traced line by line and must have gone through V&B before use. This is just an illustrative example to show you how far (so far) the tool can go.


Visual Chat-GPT in CFD

As you may have heard, Visual-ChatGPT is a recent extension of the ChatGPT architecture that is specifically designed to generate natural language text in response to visual inputs, such as images or videos. The model is trained on large datasets of image-caption pairs, where each image is associated with one or more natural language captions that describe the content of the image.

Visual-ChatGPT has shown so far very impressive results in generating natural language descriptions of images, answering questions about visual scenes, and even generating new images from textual descriptions. These capabilities could potentially be useful in optimizing meshes in CFD simulations, by allowing the user for example to describe the geometry using natural language text and the result would be: generating high-quality meshes based on those descriptions 🤯 This would literally be a game changer in CFD.

For example, you could provide a description of the desired mesh characteristics, such as "a high-quality mesh with uniform cell sizes and low skewness, focused on the area around sharp edges and growing a minimum number of 15 prism layers at the wall proximity," and Visual-ChatGPT could generate a corresponding mesh that meets those requirements. This could help streamline the mesh generation process and make it more accessible to non-experts, while also improving the accuracy and efficiency of CFD simulations. Another feature could be to provide snapshots of the mesh and asking for quality enhancement in some regions. Or feeding the model with a mesh picture let's say 2D mesh and ask ChatGPT to generate a customizable script for this mesh.

Again, I cannot stress enough on the fact that this is a language model. So bear in mind that generating high-quality meshes requires not only a description of the desired mesh characteristics but also an understanding of the physical geometry and flow field being simulated, which may require an existing expertise in mesh generation beforehand.

Recommendations on how to use chatGPT

  1. Remember that this is an AI language model! what does that mean? it means that as the tool is highly is capable of generating optimized text-based outputs, it is still prone to "error".
  2. Keep in mind that ChatGPT works in a block-strucctured way, which means you have to build the block step-by-step to get what you need. If you start by a very detailed question in a highly technical question, it may give you a very generic answer (here I mean the most generic it may get). Instead, give an idea where you come from and where you're heading (introductory context --> well-defined problem --> useful answer).
  3. You have to be fully aware that ChatGPT is not meant to be a google-like search engine. Apart from the single/multi output that each tool gives you, ChatGPT os based on its training data and the input it receives. That said, ChatGPT won't give you the efficiency you came for until you feed it with the right input - you need to be specific (remember what we said about attention-based transformers). Let's say you need to know a rough estimation of budget for a project you want to take. Instead of typing something like "what could be roughly the budget of a graphic design project for a chocolate factory", you need to be more elaborate and feed the model what "parameters" it needs to consider to calculate the budget for your project. You may write something like "I'm a graphic designed with XX years of experience and I'm about to take on a project to design a logo for a chocolate factory. The logo is to be delivered with an animated version which may need a complex tool like Adobe packages. The design might need a high-spec graphics card for rendering. Could you give me a rough estimation of how much this project should cost (given that the design may require several iterations till the final logo is agreed upon)".
  4. To maximize the benefit of ChatGPT (as in point 2), you need to spend sometime to understand your case first. It's kind of pointless when ChatGPT is used in any field by copy-paste approach.
  5. You have to be aware that, apart from probable errors in text/ coding ChatGPT might have, it might also give you incomplete answers (especially when your input is insufficient) - because simply it doesn't have enough data to know what you want to know.