Test documentation: Lid driven cavity using dye injection
This repository contains the computational framework and tools for multiphase fluid dynamics simulations based on the Basilisk C framework. The codebase specializes in the simulation and analysis of lid driven cavity flow with dye injection.
Documentation
The full documentation for this project is available at comphy-lab.org/documentationWeb/. This website contains detailed explanations of the code, examples, and usage instructions.
Overview
The codebase implements a classical lid driven cavity flow problem with dye injection using the Basilisk CFD library with custom implementations for specific dye injection including.
Repository Structure
- basilisk/src/ - Core Basilisk CFD library (reference only, do not modify)
- simulationCases/ -
Test cases for simulation
- LidDrivenCavity-Newtonian-dyeInjection.c - Lid-driven cavity with dye injection
- src-local/
- Custom header files extending Basilisk functionality
- dye-injection.h - Dye injection for flow visualization
- postProcess/ -
Project-specific post-processing tools
- 2-LidDrivenCavity-Newtonian-dyeInjection.py - Visualization script for post-processing
- getData-LidDriven.c - Data extraction utility
src-local/ Directory
The src-local/
directory contains custom header files that extend the
base Basilisk functionality:
- dye-injection.h:
Implements a circular tracer (dye) injection for flow
visualization with:
- Configurable injection time and position
- Circular dye region with adjustable radius
- Integration with Basilisk’s tracer module for advection
postProcess/ Directory
The postProcess/
directory provides tools for analyzing and visualizing
simulation results:
- 2-LidDrivenCavity-Newtonian-dyeInjection.py:
Python script for generating visualizations that:
- Processes simulation timesteps in parallel
- Creates two-panel plots showing dye concentration and velocity magnitude
- Adds streamlines to visualize flow patterns
- Uses ‘getData-LidDriven’ utility to extract field data
- getData-LidDriven.c: C utility that extracts and processes numerical data from simulation output files
simulationCases/ Directory
The simulationCases/
directory contains simulation configurations:
- LidDrivenCavity-Newtonian-dyeInjection.c:
Main simulation case that implements:
- Classical lid-driven cavity flow for a Newtonian fluid
- Configurable Reynolds number (default: Re = 1)
- Dye injection in the upper center of the cavity at t=0.05
- Adaptive grid with level 8 refinement
- Proper boundary conditions for the moving lid and no-slip walls
- Regular snapshot generation for visualization
Getting Started
Prerequisites
- Basilisk C installed (Installation instructions)
- C compiler (gcc recommended)
- Make build system
- Scientific visualization tools (optional: ParaView, Gnuplot, etc.)
Installation
Clone this repository:
git clone https://github.com/username/comphy-lab.git cd comphy-lab
Make sure Basilisk is properly installed and the BASILISK environment variable is set.
Compile a test case to verify the setup:
cd simulationCases make test_name.tst
Usage
Compiling Simulation Files
Basic compilation:
qcc -autolink file.c -o executable -lm
Compilation with custom headers:
qcc -I$PWD/src-local -autolink file.c -o executable -lm
Running Simulations
Run a compiled executable:
./executable
Execute a specific test case:
cd simulationCases && make test_name.tst
Post-Processing
The postProcess/
directory contains
tools for post-processing simulation results:
- Visualization Generation: Automated
generation of two-panel plots showing:
- Dye concentration with coolwarm colormap
- Velocity magnitude with viridis colormap
- Streamlines to visualize flow patterns
- Data Extraction: Utility to extract numerical data from simulation output files
- Parallel Processing: Multi-core processing of simulation timesteps for efficient visualization
- LaTeX Integration: Formatted plots with mathematical notation using LaTeX
Code Style Guidelines
- Indentation: 2 spaces (no tabs)
- Line length: 80 characters maximum
- Documentation: Use markdown in
comments starting with
/**
. Do not use*
in comments. - Spacing: Space after commas, spaces around operators (+, -)
- Files: Core functionality in
.h
headers, tests in.c
files - Naming: Snake_case for variables, camelCase for functions
- Error handling: Return values with stderr messages
Example
Here’s the actual lid-driven cavity flow with dye injection implementation:
#include "navier-stokes/centered.h"
#include "dye-injection.h"
// Constants
#define LEVEL 8 // Grid refinement level
#define MAXDT (1e-4) // Maximum timestep
// Global variables
int imax = 1e5; // Maximum iterations
double tmax = 1.0; // Maximum simulation time
double tsnap = 0.01; // Time interval between snapshots
double end = 2.0; // End time for simulation
// Scalar field for convergence check
[]; // Previous x-velocity
scalar unconst face vector muv[] = {1.0, 1.0}; // Face-centered viscosity field
/**
## Boundary Conditions
*/
// Top moving wall (lid)
.t[top] = dirichlet(1);
u// Other no-slip boundaries
.t[bottom] = dirichlet(0);
u.t[left] = dirichlet(0);
u.t[right] = dirichlet(0);
u
/**
## Initialization
*/
(t = 0) {
event init // Set constant viscosity for Newtonian fluid
= muv;
mu
// Initialize velocity field
() {
foreach.x[] = 0;
u.y[] = 0;
u[] = 0;
un}
(file = "start");
dump }
/**
## Snapshot Generation
Save snapshots at regular intervals for flow visualization
*/
(t=0.; t += tsnap; t < tmax+tsnap) {
event writingFiles char filename[100];
(filename, "intermediate/snapshot-%5.4f", t);
sprintf(file=filename);
dump}
int main() {
// Initialize grid and parameters
(1<<LEVEL);
init_grid= 1.0;
L0 (-0.5, -0.5);
origin= MAXDT;
DT = 1e-5;
TOLERANCE = 0.25;
CFL
// Store current velocity for convergence check
() {
foreach[] = u.x[];
un}
// dye injection parameters
= 0.05; // Inject the dye after flow is established
tInjection = 0.00; // X position (center of cavity)
xInjection = 0.40; // Y position (upper center of cavity)
yInjection
// Create a folder for simulation snapshots
char comm[80];
(comm, "mkdir -p intermediate");
sprintf (comm);
system
// Run simulation
();
run}
Contributing
- Fork the repository
- Create your feature branch
(
git checkout -b feature/amazing-feature
) - Commit your changes
(
git commit -m 'Add some amazing feature'
) - Push to the branch
(
git push origin feature/amazing-feature
) - Open a Pull Request
Reporting Issues and Feature Requests
We use GitHub Issues to track bugs, feature requests, and example requests for this repository. When creating an issue, please select the appropriate template to help us address your needs efficiently.
Issue Templates
Bug Report:
- For problems with installation, compilation, or running code. Please include:
- Detailed description of the issue
- Your environment (OS, compiler version)
- Steps to reproduce
- Expected vs. actual behavior
- Error messages and logs
- Code snippets or files that demonstrate the issue
Feature/Topic Request:
Report here - For requesting specific topics or functionality - New examples or tutorials - Additional functionality in the codebase - Improvements to existing materials
Example Request:
Report here - For requesting specific examples that demonstrate: - Particular Basilisk features - Solutions to common problems - Implementation of specific physics or numerical methods
General Question:
Report here - For any other questions
How to Create an Issue
- Go to the “Issues” tab in the GitHub repository
- Click the “New Issue” button
- Select the appropriate template from the options
- Fill in the required information according to the template
- Add relevant labels if available
- Submit the issue
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Acknowledgments
- S. Popinet & collaborators, Basilisk C, http://basilisk.fr (Last accessed: June 2025) (2013-2025)