Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several compatibility updates to the two-scale-heat-conduction tutorial #460

Merged
merged 7 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions two-scale-heat-conduction/micro-dumux/appl/micro_sim.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to adapt the pybind wrapper to the constructor taking an argument simulationID, i.e. in the wrapper's constructor and setstate functions.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not straightforward and has now been documented in an issue in the Micro Manager repository: precice/micro-manager#87

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