NewtonGrid is a 3D gravitational field simulator written in C++ that computes and visualizes vector fields produced by point masses on a discrete domain. The project adopts a scientific-computing approach to solve Newton’s Universal Law of Gravitation on regular Cartesian grids, with export support for formats compatible with ParaView.
- Flexible 3D Cartesian grid: customizable domains (dimensions, spacing, and origin).
- Multiple configuration modes: masses defined manually, symmetrically, randomly, or via file.
- Realistic physics: implementation of the Universal Law of Gravitation with softening for numerical stability.
- VTK export: generates
.vti(VTK Image Data) files for professional 3D visualization. - Interactive interface: terminal menu with step-by-step input and data validation.
- Cross-platform: Linux, macOS and Windows (ANSI/VT colors).
Gravitational field produced by point masses — magnitude visualization.
Another view of the field, used for visual validation.
src/
├── NewtonGrid.cpp # Main program with interactive interface
├── data/
│ ├── Grid.hpp # 3D grid representation
│ └── FieldData.hpp # Storage for vector fields
├── physics/
│ └── GravitationalField.hpp # Gravitational field computation
├── io/
│ └── VTIWriter.hpp # VTK Image Data (.vti) exporter
└── output.vti # File generated after execution
Represents a regular 3D Cartesian domain.
Characteristics:
- Always 3D (2D supported via
nz = 1). - Contiguous memory with F-contiguous ordering (x fastest).
- Immutable interface after construction.
- Efficient conversions between discrete indices, linear index, and physical coordinates.
- Compatible with
VTK ImageData.
Main methods:
dimensions(),spacing(),origin()linear_index(i, j, k)discrete_indices(idx)physical_to_discrete(x, y, z)discrete_to_physical(i, j, k)for_each_point(func)
Stores a 3D vector field defined on a Grid.
Characteristics:
- Separate component storage (
gx,gy,gz). - Bulk operations (zeroing, filling, applying functions).
- Derived quantity calculations (magnitude, L2 norm).
- Conversion to Array-of-Structures (AOS) for export.
Main methods:
allocate(grid, zero_init)get_vector(idx),set_vector(idx, vec)magnitude(idx)max_magnitude(),min_positive_magnitude(),norm_l2()to_aos()
Computes Newtonian gravitational fields generated by point masses.
Characteristics:
- Implements the Universal Law of Gravitation with softening.
- Supports multiple masses.
- Read thread-safe.
- Prepared for optimizations (SIMD, OpenMP).
Main methods:
add_mass(x, y, z, value)compute(field)compute_optimized(field)compute_magnitude_only(field)compute_potential(field)
Exports data to the VTK ImageData (.vti) format.
Characteristics:
- XML files compatible with ParaView.
- Exports both the vector field and its magnitude.
- Configurable precision (default: 15 digits).
- ASCII format (human-readable).
Main methods:
write(filename, grid, field)set_precision(precision)enable_magnitude(enabled)enable_vector_field(enabled)
- C++17 compiler (g++ 7+, clang 5+, MSVC 2019+)
- Terminal with ANSI color support
- ParaView (optional, for visualization)
# g++
g++ -std=c++17 -O2 -I. NewtonGrid.cpp -o NewtonGrid -pthread
# clang
clang++ -std=c++17 -O2 -I. NewtonGrid.cpp -o NewtonGrid -pthread# MinGW
g++ -std=c++17 -O2 -I. NewtonGrid.cpp -o NewtonGrid.exe
# MSVC (Developer Command Prompt)
cl /EHsc /std:c++17 /O2 NewtonGrid.cpp
Program screen showing the interactive menu.
./NewtonGridThe program guides the user through an interactive menu:
- Choose the grid (preset or custom)
- Set
Gand softening - Configure the masses
- Compute the field
- Export to
output.vti
echo -e "2\n1.0\n0.001\n1\n2\n1.0 0.0 0.0 5.0\n-1.0 0.0 0.0 5.0" | ./NewtonGrid
Graphical editor used to inspect and tweak the VTI output.
- Install ParaView
- Open
output.vti - Apply filters such as Glyph, Contour, or Slice
- Adjust scale, colors and magnitude as needed
# x y z mass
0.0 0.0 0.0 1.0
1.0 0.0 0.0 0.5
-1.0 0.0 0.0 0.5
- Binary system
./NewtonGrid
# Choice: Medium Grid (50x50x50)
# G = 1.0, softening = 0.001
# Masses: Symmetric, 2 masses, separation = 3.0, mass = 5.0- Stellar cluster
./NewtonGrid
# Grid: Large (100x100x100)
# Masses: Random, 10 masses, minimum distance = 1.0, varying masses- Single well (point mass)
./NewtonGrid
# Grid: Custom (nx=64, ny=64, nz=1, dx=dy=0.1, dz=1.0)
# Masses: Manual, position (0,0,0), mass = 10.0
Representative image of the generated output (vectors and magnitudes).
The program produces:
- Grid statistics: dimensions, spacing, origin, total points
- Mass list: positions and values of all masses
- Field statistics: maximum/minimum magnitude, L2 norm
output.vtifile: VTK-formatted data for visualization- Validation examples: field values at selected points for verification
Where:
G: gravitational constant\vec{r}: displacement vector\varepsilon: softening parameter