Skip to content

Instantly share code, notes, and snippets.

View finsberg's full-sized avatar
🐛
Squashing bugs

Henrik Finsberg finsberg

🐛
Squashing bugs
View GitHub Profile
@finsberg
finsberg / create_mesh.py
Last active August 15, 2025 13:34
Gradient precision - dolfinx_adjoint vs dolfin_adjoint
# Run in dolfinx-nightly container
from pathlib import Path
from mpi4py import MPI
import dolfinx
import gmsh
def generate_mesh(
path: Path = Path("annulus.msh"),
@finsberg
finsberg / log.py
Last active June 9, 2025 19:08
Logging to file with MPI
import logging
from mpi4py import MPI
mode2mpi_mode = {
"w": MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_EXCL,
"a": MPI.MODE_WRONLY | MPI.MODE_CREATE | MPI.MODE_APPEND,
}
from dataclasses import dataclass
import shutil
from mpi4py import MPI
from petsc4py import PETSc
import dolfinx.fem.petsc
from dolfinx_external_operator import (
FEMExternalOperator,
evaluate_external_operators,
evaluate_operands,
@finsberg
finsberg / Dockerfile_fp32
Created March 8, 2024 12:45
Running nonlinear hyperelasticity with double and single precision in dolfinx based on https://jsdokken.com/dolfinx-tutorial/chapter2/hyperelasticity.html
from ghcr.io/fenics/test-env:current-mpich
ENV PETSC_ARCH=linux-gnu-real32-32
ARG UFL_BRANCH=main
ARG FFCX_BRANCH=main
ARG BUILD_MODE=Release
RUN python3 -m pip install pybind11
RUN git clone --branch=main --single-branch https://github.com/FEniCS/dolfinx.git
@finsberg
finsberg / fenics-dev-ex3.md
Last active April 4, 2024 09:02
Installing FEniCS development version on ex3

Installing FEniCS development version on ex3

Here we will explain the steps needed to install fenics development version on ex3 (the HPC cluster at Simula).

Note that the recipe here is rather general and could probalby be adopted to most HPC systems. Note that at the time of writing there is an open PR to add FEniCS devlopment version, and this has to merged (or you need to use my fork) to make it work.

First we need a working C and Fortran compiler before installing anything. To get this I used

module purge
@finsberg
finsberg / convergence_test.json
Last active February 14, 2025 21:12
An updated gist description
{
"588e6dc941ef2f6e85f77fa64e1303665906c037": {
"dx0.2_dt0.025": {
"import_time": 6.809999604229233e-07,
"timestamp": "2025-02-14T17:45:30.415965",
"simcardems_version": "2024.0.0",
"dt": 0.025,
"dx": 0.2,
"sha": "588e6dc941ef2f6e85f77fa64e1303665906c037",
"coupling_type": "fully_coupled_ORdmm_Land",
@finsberg
finsberg / fenics_mechancs_demo.py
Created January 28, 2023 17:06
Simple demo of solving a cardiac mechanics problem in FEniCS
import dolfin
import ufl
def subplus(x):
r"""
Ramp function
.. math::
\max\{x,0\}
"""
@finsberg
finsberg / in1910_info_cmd.md
Created August 11, 2022 08:03
Commands for listing students and info about students in 1910

Commands for listing info about students and the course

First log in to an ifi machine, i.e

ssh [email protected]

Here you can list the teachers using the command

lsng gin1910
# Change prefix to what ever you want - I usually install everything in a python virtual environment
export PREFIX=$HOME/.venv
# Activate the virtual environment
source $PREFIX/bin/activate
export HDF5_MPI="ON"
export CC=mpicc
wget https://hdf-wordpress-1.s3.amazonaws.com/wp-content/uploads/manual/HDF5/HDF5_1_12_2/source/hdf5-1.12.2.tar
tar -xvf hdf5-1.12.2.tar && cd hdf5-1.12.2/ && ./configure --enable-parallel --prefix=$PREFIX && make install && cd ..
@finsberg
finsberg / fenics_ode_pde_independent.py
Last active April 6, 2022 12:10
Example of solving and ODE and a PDE independently at the same time using FEniCS
import matplotlib.pyplot as plt
import dolfin
from pulse import Constant
def boundary(x):
return x[0] < dolfin.DOLFIN_EPS or x[0] > 1.0 - dolfin.DOLFIN_EPS
def rhs(x, y):