Skip to content

Commit

Permalink
Several compatibility updates to the two-scale-heat-conduction tutori…
Browse files Browse the repository at this point in the history
…al (#460)

* Make Nutils micro simulation compatible with Nutils v8.5 and the latest Micro Manager

* Make run scripts consistent

* initialize micro-data and accept sim_id in constructor

* Do not pass initial data which is not relevant for adaptivity

* Formatting

---------

Co-authored-by: Mathis Kelm <[email protected]>
  • Loading branch information
IshaanDesai and mathiskelm authored Mar 20, 2024
1 parent ac3ff00 commit eb3daf5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
35 changes: 30 additions & 5 deletions two-scale-heat-conduction/micro-dumux/appl/micro_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class MicroSimulation {
using SolutionVector = Dumux::GetPropType<CellProblemTypeTag, Dumux::Properties::SolutionVector>;

public:
MicroSimulation();
void initialize();
MicroSimulation(int simulationID);
py::dict initialize();

// solve takes python dict for macro_write data, dt, and returns python dict for macro_read data
py::dict solve(py::dict macro_write_data, double dt);
Expand Down Expand Up @@ -104,7 +104,7 @@ class MicroSimulation {
};

// Constructor
MicroSimulation::MicroSimulation()
MicroSimulation::MicroSimulation(int simulationID)
{
using namespace Dumux;

Expand Down Expand Up @@ -180,9 +180,34 @@ MicroSimulation::MicroSimulation()
_timeLoop->start();
};

// Initialize
void MicroSimulation::initialize()
// Initialize micro-data to be used in initial adaptivity
py::dict MicroSimulation::initialize()
{
//update Phi in the cell problem
_cpProblem->spatialParams().updatePhi(_phi);

// solve the cell problems
_cpLinearPDESolver->solve(_psi);

// calculate porosity
_porosity = _acProblem->calculatePorosity(_phi);

//compute the psi derivatives (required for conductivity tensor)
_cpProblem->computePsiDerivatives(*_cpProblem, *_cpAssembler, *_cpGridVariables, _psi);

//calculate the conductivity tensor
_k_00 = _cpProblem->calculateConductivityTensorComponent(0, 0);
_k_11 = _cpProblem->calculateConductivityTensorComponent(1, 1);

// create python dict for micro_write_data
py::dict micro_write_data;

// add micro_scalar_data and micro_vector_data to micro_write_data
micro_write_data["k_00"] = _k_00;
micro_write_data["k_11"] = _k_11;
micro_write_data["porosity"] = _porosity;

return micro_write_data;
}

// Solve
Expand Down
18 changes: 15 additions & 3 deletions two-scale-heat-conduction/micro-nutils/micro.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class MicroSimulation:

def __init__(self):
def __init__(self, sim_id):
"""
Constructor of MicroSimulation class.
"""
Expand Down Expand Up @@ -43,6 +43,7 @@ def __init__(self):
self._initial_condition_is_set = False
self._k_nm1 = None # Average effective conductivity of last time step

def initialize(self):
# Define initial namespace
self._ns = function.Namespace()
self._ns.x = self._geom
Expand Down Expand Up @@ -85,6 +86,17 @@ def __init__(self):
self._solphi = solphi # Save solution of phi
self._psi_nm1 = psi # Average porosity value of last time step

# Solve the heat cell problem
solu = self._solve_heat_cell_problem(self._topo, solphi)
k = self._get_eff_conductivity(self._topo, solu, solphi)

output_data = dict()
output_data["k_00"] = k[0][0]
output_data["k_11"] = k[1][1]
output_data["porosity"] = psi

return output_data

def _reinitialize_namespace(self, topo):
self._ns = None # Clear old namespace
self._ns = function.Namespace()
Expand Down Expand Up @@ -112,7 +124,7 @@ def _reinitialize_namespace(self, topo):

@staticmethod
def _analytical_phasefield(x, y, r, lam):
return 1. / (1. + function.exp(-4. / lam * (function.sqrt(x ** 2 + y ** 2) - r + 0.001)))
return 1. / (1. + np.exp(-4. / lam * (np.sqrt(x ** 2 + y ** 2) - r + 0.001)))

@staticmethod
def _get_analytical_phasefield(topo, ns, degree_phi, lam, r):
Expand Down Expand Up @@ -263,7 +275,7 @@ def solve(self, macro_data, dt):


def main():
micro_problem = MicroSimulation()
micro_problem = MicroSimulation(0)
dt = 1e-3
micro_problem.initialize()
concentrations = [0.5, 0.4]
Expand Down
6 changes: 3 additions & 3 deletions two-scale-heat-conduction/micro-nutils/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ pip install -r requirements.txt
# Check if no input argument was provided
if [ -z "$*" ] ; then
echo "No input argument provided. Micro Manager is launched in serial"
python3 run-micro-problems.py
python3 run_micro_manager.py
fi

while getopts ":sp" opt; do
case ${opt} in
s)
python3 run-micro-problems.py
python3 run_micro_manager.py
;;
p)
mpiexec -n "$2" python3 run-micro-problems.py
mpiexec -n "$2" python3 run_micro_manager.py
;;
*)
usage
Expand Down

0 comments on commit eb3daf5

Please sign in to comment.