Skip to content

Commit

Permalink
init any core restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Sep 3, 2024
1 parent 6fb6a68 commit b7147a9
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 9 deletions.
23 changes: 16 additions & 7 deletions src/core/data/electromag/electromag.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "initializer/data_provider.hpp"
#include "core/def.hpp"

#include "electromag_initializer.hpp"

namespace PHARE
{
Expand All @@ -17,34 +18,36 @@ namespace core
template<typename VecFieldT>
class Electromag
{
using This = Electromag<VecFieldT>;

public:
static constexpr std::size_t dimension = VecFieldT::dimension;
using vecfield_type = VecFieldT;
auto static constexpr dimension = VecFieldT::dimension;

explicit Electromag(std::string name)
: E{name + "_E", HybridQuantity::Vector::E}
, B{name + "_B", HybridQuantity::Vector::B}
, Binit_{}
{
}


explicit Electromag(initializer::PHAREDict const& dict)
: E{dict["name"].template to<std::string>() + "_"
+ dict["electric"]["name"].template to<std::string>(),
HybridQuantity::Vector::E}
, B{dict["name"].template to<std::string>() + "_"
+ dict["magnetic"]["name"].template to<std::string>(),
HybridQuantity::Vector::B}
, Binit_{dict["magnetic"]["initializer"]}
, dict_{dict["magnetic"]["initializer"]}
{
}

using vecfield_type = VecFieldT;


template<typename GridLayout>
void initialize(GridLayout const& layout)
{
Binit_.initialize(B, layout);
ElectromagInitializerFactory<This, GridLayout>::create(dict_)->init(*this, layout);
// dict = initializer::PHAREDict{}; // clear ?
}


Expand Down Expand Up @@ -78,8 +81,14 @@ namespace core
VecFieldT B;

private:
VecFieldInitializer<dimension> Binit_;
initializer::PHAREDict dict_{};
};


} // namespace core
} // namespace PHARE




#endif
54 changes: 54 additions & 0 deletions src/core/data/electromag/electromag_initializer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef _PHARE_CORE_DATA_ELECTROMAG_ELECTROMAG_INITIALIZER_HPP_
#define _PHARE_CORE_DATA_ELECTROMAG_ELECTROMAG_INITIALIZER_HPP_

#include "initializer/data_provider.hpp"

#include <array>


namespace PHARE::core
{

template<typename Electromag_t, typename GridLayout>
class ElectromagInitializer
{
public:
virtual void init(Electromag_t& /*em*/, GridLayout const& /*layout*/) const { /*noop*/ }
virtual ~ElectromagInitializer() {}
};

template<typename Electromag_t, typename GridLayout>
class ElectromagUserFuncInitializer : public ElectromagInitializer<Electromag_t, GridLayout>
{
public:
ElectromagUserFuncInitializer(initializer::PHAREDict const& dict)
: Binit_{dict["magnetic"]["initializer"]}
{
}
virtual ~ElectromagUserFuncInitializer() {}

void init(Electromag_t& em, GridLayout const& layout) const override
{
Binit_.initialize(em.B, layout);
}

VecFieldInitializer<Electromag_t::dimension> Binit_;
};


template<typename Electromag_t, typename GridLayout>
class ElectromagInitializerFactory
{
public:
NO_DISCARD static std::unique_ptr<ElectromagInitializer<Electromag_t, GridLayout>>
create(initializer::PHAREDict const& dict)
{
return std::make_unique<ElectromagUserFuncInitializer<Electromag_t, GridLayout>>(dict);
// else
// return std::make_unique<ElectromagInitializer<Electromag_t>>();
}
};

} // namespace PHARE::core

#endif // _PHARE_CORE_DATA_ELECTROMAG_ELECTROMAG_INITIALIZER_HPP_
4 changes: 2 additions & 2 deletions src/core/data/vecfield/vecfield_initializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace core


template<typename VecField, typename GridLayout>
void initialize(VecField& v, GridLayout const& layout)
void initialize(VecField& v, GridLayout const& layout) const
{
static_assert(GridLayout::dimension == VecField::dimension,
"dimension mismatch between vecfield and gridlayout");
Expand All @@ -39,7 +39,7 @@ namespace core
private:
template<typename Field, typename GridLayout>
void initializeComponent_(Field& field, GridLayout const& layout,
initializer::InitFunction<dimension> const& init)
initializer::InitFunction<dimension> const& init) const
{
auto const indices = layout.ghostStartToEndIndices(field, /*includeEnd=*/true);
auto const coords = layout.template indexesToCoordVectors</*WithField=*/true>(
Expand Down
95 changes: 95 additions & 0 deletions tests/simulator/test_samrai_restarts_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import copy

import time
import h5py
import datetime
import unittest
import numpy as np
from pathlib import Path
from datetime import timedelta

from ddt import ddt, data, unpack

from pyphare.cpp import cpp_lib

cpp = cpp_lib()

import pyphare.pharein as ph
from pyphare.pharesee.run import Run
from pyphare.simulator.simulator import Simulator

from tests.simulator import SimulatorTest
from tests.diagnostic import dump_all_diags
from pyphare.pharesee.hierarchy.patchdata import ParticleData
from pyphare.pharesee.hierarchy.fromh5 import get_all_available_quantities_from_h5

# ./build/tests/simulator/phare_outputs/restarts/test/test_restarts_1/1/1/1/00000.00400/restore.000000/nodes.0000001/proc.0000000


def setup_model(ppc=100):
model = ph.MaxwellianFluidModel(protons={"nbr_part_per_cell": ppc})
ph.ElectronModel(closure="isothermal", Te=0.12)
return model


timestep = 0.001
out = "phare_outputs/parse_restarts"
simArgs = dict(
time_step_nbr=2,
time_step=timestep,
cells=200,
dl=0.3,
restart_options=dict(dir=out, mode="overwrite"),
)


def traverse_h5_for_groups_recursive(h5content: "H5Content", group, path=""):
if "level_0000" in path:
for key in group.attrs:
h5content.attr[f"{path}/{key}"] = group.attrs[key]
if isinstance(group, h5py._hl.group.Group):
for key in group:
kpath = f"{path}/{key}"
traverse_h5_for_groups_recursive(h5content, group[key], path=kpath)
else:
if "level_0000" not in path:
return
h5content.data[path] = group


class H5Content:
def __init__(self, path):
self.file = h5py.File(path, "r")
self.data = {}
self.attr = {}
traverse_h5_for_groups_recursive(self, self.file)


@ddt
class RestartsParserTest(SimulatorTest):
def __init__(self, *args, **kwargs):
super(RestartsParserTest, self).__init__(*args, **kwargs)
self.simulator = None

def tearDown(self):
super(RestartsParserTest, self).tearDown()
if self.simulator is not None:
self.simulator.reset()
self.simulator = None
ph.global_vars.sim = None

def test_restart_parser(self):
h5_filepath = "phare_outputs/restarts/test/test_restarts_1/1/1/1/00000.00400/restore.000000/nodes.0000001/proc.0000000"

h5 = H5Content(h5_filepath)
print(
h5.file[
"/PHARE_hierarchy/level_0000/level_0000-patch_0000000-block_0000000/EMAvg_B_x##default/d_ghost_box"
][:]
)
for k in h5.data:
print(k)


if __name__ == "__main__":
unittest.main()

0 comments on commit b7147a9

Please sign in to comment.