FROM ubuntu:focal

# Set timezone
RUN apt-get update --fix-missing && \
    DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata

# OpenFOAM 

## Minimal prerequisites for OpenFOAM: system OpenMPI and scotch
RUN apt update && apt-get install --fix-missing -y gcc-10 g++-10 \
    libopenmpi-dev openmpi-bin flex bison \
    git wget make libscotch-dev zlib1g-dev unzip \
    apt-transport-https ca-certificates

## Default gcc/g++ to gcc-10/g++-10
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10

## Clone OpenFOAM and checkout OpenFOAM-v2112
RUN mkdir /opt/OpenFOAM
WORKDIR "/opt/OpenFOAM"
RUN git clone https://develop.openfoam.com/Development/openfoam.git && \
    mv openfoam OpenFOAM-v2112 && cd OpenFOAM-v2112 && git checkout OpenFOAM-v2112 
    
## Building OpenFOAM
WORKDIR "/opt/OpenFOAM/OpenFOAM-v2112"

### Use bash 
SHELL ["/bin/bash", "-c"] 

### Enable dynamicCode as root 
COPY dynamicCode.C.v2112 src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCode.C 

### Change C++ standard from c++11 to c++2a
RUN sed -i 's/c++11/c++2a/' wmake/rules/General/Gcc/c++

### Compile OpenFOAM with system scotch and ptscotch libraries 
COPY scotch.ubuntu-focal etc/config.sh/scotch 
RUN export WM_NCOMPPROCS=42 && \
    export SCOTCH_INC_DIR=/usr/include/scotch && \
    export PTSCOTCH_INC_DIR=$SCOTCH_INC_DIR && \
    export SCOTCH_LIB_DIR=/usr/lib/x86_64-linux-gnu && \
    export PTSCOTCH_LIB_DIR=$SCOTCH_LIB_DIR && \
    source ./etc/bashrc && ./Allwmake -l

### Compile and install OpenFOAM submodules: required to use cfMesh
RUN git submodule init && git submodule status && git submodule update
WORKDIR "/opt/OpenFOAM/OpenFOAM-v2112/modules"
RUN export WM_NCOMPPROCS=42 && \
    source ../etc/bashrc && \
    ./Allwmake

# Project dependencies

## Install project dependencies
RUN apt-get update --fix-missing && apt-get install -y cmake gmsh \
    texlive-full pandoc vim curl

## Visualization and data processing
RUN apt-get update --fix-missing && apt-get install -y python3.9-full 

## Default to python3.9 and pip3.9
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10 

## Get pip3.9 on Ubuntu 20.04
RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
    python3.9 get-pip.py && which pip && pip --version 

## Python3.9 packages 
RUN pip install matplotlib numpy pandas notebook \ 
    nbconvert PyFoam jupyter-client==6.1.12

RUN apt-get -y install libeigen3-dev

