Tuesday, March 11, 2025

The curious engineer is still thriving in me - checking the veracity of the continuity equation in computational fluid dynamics...

 It was early in the morning. Everywhere there was a pin-drop silence. After the regular morning exercise, I turned on my laptop. Today's plan was to verify the continuity equation of computational fluid dynamics. For noncompressible fluid, the continuity equation can be mathematically written as U=0. It's the conservation of mass in fluid simulation. Whatever goes into a volume has to come out - no creation, no destruction, just pure, seamless flow.

The paraFoam is good for visualizing the cavity example of the Tutorial that comes with openFoam. As I am new to using ParaView, i preferred raw data exported to a CSV file.

So I created the following Python script.

from paraview.simple import *

# Load the OpenFOAM case

foamCase = OpenFOAMReader(FileName="cavity.OpenFOAM")

foamCase.MeshRegions = ['internalMesh']

foamCase.CellArrays = ['U'] # Ensure velocity field is loaded


# Apply the new Gradient filter

gradFilter = Gradient(Input=foamCase)

gradFilter.ScalarArray = ['POINTS', 'U'] # Compute gradient of velocity U


# Show the gradient field in ParaView

gradDisplay = Show(gradFilter)

ColorBy(gradDisplay, ('POINTS', 'Gradient'))


# Apply a color map for better visualization

glyph = Glyph(Input=gradFilter, GlyphType='Arrow')

glyph.ScaleFactor = 0.01 # Adjust the size

Show(glyph)

Render()

# Optional: Save visualization as an image

SaveScreenshot("grad_U_visualization.png")

writer = CreateWriter("gradU_data.csv", gradFilter)

writer.UpdatePipeline()

Now the time is to test the veracity of the continuity equation.

So, another Python script as follows.

import pandas as pd
import matplotlib.pyplot as plt

# Load CSV file
df = pd.read_csv("/home/som/OpenFOAM/som-dev/run/cavity/cavity/gradU_data.csv")

# Compute divergence of U (sum of diagonal terms of gradient matrix)
df["divU"] = df["Gradient:0"] + df["Gradient:4"] + df["Gradient:8"]

# Plot divergence
plt.plot(df["Points:0"], df["divU"], marker="o", linestyle="-", label="∇·U")
plt.axhline(0, color='r', linestyle="--", label="Zero Line")
plt.xlabel("X Position")
plt.ylabel("Divergence of Velocity (∇·U)")
plt.title("Verification of Continuity Equation (∇·U)")
plt.legend()
plt.grid()
plt.show()

# Check if max deviation from zero is small
print("Max |∇·U| =", df["divU"].abs().max())



A little explanation for the code 
df["divU"] = df["Gradient:0"] + df["Gradient:4"] + df["Gradient:8"]
This is important because we must take the diagonal values of a 
3X3 matrix to verify the continuity equation.
Everything was looking fine except near the lid. The value was high. 

Max |∇·U| = 100.0.

The culprits were located near the moving lid and the corner regions of the cavity. This made sense—corners were prone to numerical singularities due to discontinuities in the velocity gradient.

Not to my surprise. But i wanted to verify using a histogram image.

So added the following python script.

plt.hist(df["divU"], bins=50)
plt.xlabel("∇·U values")
plt.ylabel("Frequency")
plt.title("Distribution of divergence (∇·U)")
plt.show()

And voila.

Most of the values are almost 0.

The scientist in me became happy to contribute to the learning community.

Happy learning...

Here's the simulation exported from ParaView...



That's it for today.

Reclaiming #WhoWeAre...

Thursday, March 6, 2025

Flipping the learning process of Computer Graphics - from Blender to OpenGL...

 In learning, experimentation matters...

Let’s be honest—OpenGL is cool. When you write a few lines of code and see a triangle pop up on screen, it feels like magic.

But OpenGL is also:

  • Verbose

  • Abstract

  • Unforgiving

  • Missing a lot of concepts critical to modern 3D (like bones, animations, UVs)

So beginners often spend weeks struggling with:

  • Boilerplate setup

  • Matrix math

  • VBOs/VAOs and shaders

  • Texture mapping

  • 3D model loading

All before they see something that even remotely resembles a 3D object.

Flipping the Process: Start With Blender

Blender is a powerful 3D creation suite. Within hours of learning the UI, you can:

  • Model a character or environment

  • Apply textures and materials

  • Animate movement

  • Export to .obj or .gltf

You’re now creating real 3D data, not just rendering a colored cube.

By learning Blender first, you naturally discover:

  • What a mesh is (vertices, edges, faces)

  • What UV unwrapping and texture mapping mean

  • How bones and rigs drive animation

  • How to export models into formats like .obj, .gltf, or .fbx

These concepts make OpenGL easier to understand later, because you've already seen them in action.

Once you've exported a model from Blender, you're in the perfect position to ask:

“How does a program actually draw this model?”

Now when you load a .obj file in OpenGL:

  • You already know what the vertex positions, normals, and UVs are.

  • You understand what the model should look like.

  • You can visually debug your rendering pipeline.

Instead of building triangles from scratch, you're parsing real-world geometry you created yourself.

This gives you:

  • Purpose: you're not just drawing test shapes.

  • Context: you know what the data represents.

  • Validation: you can check if OpenGL renders what you saw in Blender.

My young son Ridit is just doing this. He has taken a less travelled road in learning Computer Graphics. Instead of learning OpenGL first, he mastered 3D skills using Blender. This opens the door of "whats" of computer graphics. Now he is delving into OpenGL... this is opening up the door of "hows".

So his transformation is from a 3D engineer to computer graphics programmer...

This is how my son Ridit started exploring Blender when he was barely 9 years old.


This is his experimentation with OpenGL...

And here is another...


But he mastered three programming languages - namely Java, Python and C++ before doing such experimentation.

Here we go - the path from Blender to OpenGL...

📌 Blender Concept → OpenGL Implementation:

Blender ConceptOpenGL Equivalent

Meshes & Objects            Vertex Buffers (VBO, VAO)

Transformations       glm::translate(), glm::rotate(), glm::scale()

Camera & View        glm::lookAt() (Perspective Projection)

Lights            GLSL Phong Lighting Model

Materials & Textures            Fragment Shader with sampler2D

Learning computer science becomes fun when we do a lot of experimentation with the learning process.

Here's the tech blog of Ridit - a new face in tech sector of Bharat.



Concepts first.... Code later...

Seeing fast results in Blender keeps the learning process exciting...

Reclaiming #WhoWeAre