---
title: "Minimal Surfaces"
canonical: "/2023/11/12/schwarz-p-surface/"
date: "2023-11-12"
author: "Alok Singh"
tags: []
---


GPT-4 did this in 1 shot.

The prompt is at https://chat.openai.com/share/b2af7f22-2953-4b25-af69-bd16077a6770

Code ran first time, always a rush when that happens.

![](/assets/minimal-surface-3d.png)

> For a periodic minimal hypersurface, one would typically expect the surface to repeat itself in a regular pattern across the space. This periodicity could be along one or more axes, depending on the dimension and the specific geometry of the space.

> To define such a surface mathematically, you would typically use a combination
> of differential geometry and partial differential equations. The key equation
> here is the minimal surface equation, which in its simplest form (for graphs)
> is a nonlinear partial differential equation. For a surface \(S\) given as a
> graph of a function \( u(x_1, x_2, \ldots, x_{n-1}) \), the minimal surface
> equation in \( \mathbb{R}^n \) can be written as:

$$\text{div}\left( \frac{\nabla u}{\sqrt{1 + |\nabla u|^2}} \right) = 0$$

I had it do the Schwarz P Surface, defined by the equation

$$ \cos(x) + \cos(y) + \cos(z) = 0 $$

```
import torch
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from skimage import measure

# Define the range and resolution for the grid
grid_size = 50
x = torch.linspace(-np.pi, np.pi, grid_size)
y = torch.linspace(-np.pi, np.pi, grid_size)
z = torch.linspace(-np.pi, np.pi, grid_size)

# Create a 3D meshgrid
X, Y, Z = torch.meshgrid(x, y, z, indexing='ij')

# Evaluate the Schwarz P surface equation
F = torch.cos(X) + torch.cos(Y) + torch.cos(Z)

# Convert the grid and function values to numpy for processing
F_np = F.numpy()

# Using the marching cubes algorithm to get the surface mesh
verts, faces, _, _ = measure.marching_cubes(F_np, level=0)

# Plotting the surface
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2], cmap='Spectral', lw=1)

# Setting labels and title
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.title('Schwarz P Minimal Surface Visualization')

plt.show()
```
