Skip to content
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
79 changes: 79 additions & 0 deletions doc/conventions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# PHARE Conventions

### Reference document for the code base


# Sections

1. C++
2. Python
3. CMake
4. Tests
5. Etc

<br/>

# 1. C++

## 1.1 General

...


<br/>

# 2. Python

## 2.1 General

...

## 2.2 dependencies and imports

Third party depenencies are stated in the file `requirements.txt` in the project root.
Fewer dependencies is generally better but there should be a cost/benefit assessment for adding new dependencies.

### 2.2.1 Python file import structure.

Generally, we want to avoid importing any dependency at the top of a python script that may rely on binary libraries.

Exceptions to this are things like numpy, which are widely used and tested.

Things to expressly avoid importing at the top of a python script are

- h5py
- mpi4py
- scipy.optimize

The first two are noted as they can, and will pull in system libraries such as libmpi.so and libhdf5.so, which may not be the libraries which were used during PHARE build time, this can cause issues at runtime.

scipy.optimize relies on system libraries which may not be available at runtime.

The gist here is to only import these libraries at function scope when you actually need them, so that python files can be imported
or scanned for tests and not cause issues during these operations, until the functions are used at least.

<br/>

# 3. CMake

## 3.1 General

...


<br/>

# 4. Tests

## 4.1 General

...

<br/>

# 5. Etc

## 5.1 General

...

21 changes: 3 additions & 18 deletions pyphare/pyphare/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,10 @@
#


import json

# continue to use override if set
_cpp_lib_override = None


def cpp_lib(override=None):
import importlib

global _cpp_lib_override
if override is not None:
_cpp_lib_override = override
if _cpp_lib_override is not None:
return importlib.import_module(_cpp_lib_override)

if not __debug__:
return importlib.import_module("pybindlibs.cpp")
try:
return importlib.import_module("pybindlibs.cpp_dbg")
except ImportError:
return importlib.import_module("pybindlibs.cpp")
return importlib.import_module("pybindlibs.cpp")


def cpp_etc_lib():
Expand All @@ -37,6 +20,8 @@ def build_config():


def build_config_as_json():
import json

return json.dumps(build_config())


Expand Down
9 changes: 8 additions & 1 deletion pyphare/pyphare/pharesee/hierarchy/fromh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from ...core.gridlayout import GridLayout
from .hierarchy_utils import field_qties
import h5py

from pathlib import Path
from pyphare.core.phare_utilities import listify

Expand Down Expand Up @@ -129,6 +129,8 @@ def h5_filename_from(diagInfo):


def get_times_from_h5(filepath, as_float=True):
import h5py # see doc/conventions.md section 2.1.1

f = h5py.File(filepath, "r")
if as_float:
times = np.array(sorted([float(s) for s in list(f[h5_time_grp_key].keys())]))
Expand Down Expand Up @@ -216,6 +218,7 @@ def add_time_from_h5(hier, filepath, time, **kwargs):
# add times to 'hier'
# we may have a different selection box for that time as for already existing times
# but we need to keep them, per time
import h5py # see doc/conventions.md section 2.1.1

h5f = h5py.File(filepath, "r")
selection_box = kwargs.get("selection_box", None)
Expand All @@ -239,6 +242,8 @@ def add_data_from_h5(hier, filepath, time):
if not hier.has_time(time):
raise ValueError("time does not exist in hierarchy")

import h5py # see doc/conventions.md section 2.1.1

h5f = h5py.File(filepath, "r")

# force using the hierarchy selection box at that time if existing
Expand All @@ -260,6 +265,8 @@ def new_from_h5(filepath, times, **kwargs):
# loads all datasets from the filepath h5 file as patchdatas
# we authorize user to pass only one selection box for all times
# but in this case they're all the same
import h5py # see doc/conventions.md section 2.1.1

selection_box = kwargs.get("selection_box", [None] * len(times))
if none_iterable(selection_box) and all_iterables(times):
selection_box = [selection_box] * len(times)
Expand Down
12 changes: 0 additions & 12 deletions src/python3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ set_target_properties(cpp
set_property(TARGET cpp PROPERTY INTERPROCEDURAL_OPTIMIZATION ${PHARE_INTERPROCEDURAL_OPTIMIZATION})
set_property(TARGET cpp APPEND_STRING PROPERTY LINK_FLAGS " ${PHARE_LINK_FLAGS}")

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
pybind11_add_module(cpp_dbg cpp_simulator.cpp)
target_link_libraries(cpp_dbg PUBLIC phare_simulator)
target_compile_options(cpp_dbg PRIVATE ${PHARE_WERROR_FLAGS} -DPHARE_HAS_HIGHFIVE=${PHARE_HAS_HIGHFIVE} -DPHARE_DIAG_DOUBLES=1 -DPHARE_CPP_MOD_NAME=cpp_dbg)
set_target_properties(cpp_dbg
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/pybindlibs"
)
set_property(TARGET cpp_dbg PROPERTY INTERPROCEDURAL_OPTIMIZATION ${PHARE_INTERPROCEDURAL_OPTIMIZATION})
set_property(TARGET cpp_dbg APPEND_STRING PROPERTY LINK_FLAGS " ${PHARE_LINK_FLAGS}")
endif (CMAKE_BUILD_TYPE STREQUAL "Debug")


pybind11_add_module(cpp_etc cpp_etc.cpp)
target_compile_options(cpp_etc PRIVATE ${PHARE_WERROR_FLAGS} -DPHARE_HAS_HIGHFIVE=${PHARE_HAS_HIGHFIVE})
Expand Down
2 changes: 0 additions & 2 deletions tests/amr/data/field/refine/test_refine_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from pyphare.core.phare_utilities import refinement_ratio
from pyphare.pharesee.hierarchy.patchdata import FieldData

# in this module, we assume the refinement ratio is 2
refinement_ratio = 2

# below is a drawing representing a 1D coarse FieldData, its ghost box and the
# associated refined FieldData this module aims at creating.
Expand Down
8 changes: 2 additions & 6 deletions tests/core/data/gridlayout/allocSizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

import os
import sys

print(sys.path)
import gridparams
import utilities
import gridparams

from pyphare.core import gridlayout


Expand Down Expand Up @@ -69,7 +68,6 @@ def main(path="./"):

gl = gridlayout.GridLayout()

directions = gl.directions
quantities = [
"Bx",
"By",
Expand Down Expand Up @@ -97,8 +95,6 @@ def main(path="./"):
dyList = [0.0, 0.1, 0.1]
dzList = [0.0, 0.0, 0.1]

maxNbrDim = 3

baseName = "allocSizes"

# out_1D = open(os.path.join(path, baseName + '_1d.txt'), 'w')
Expand Down
11 changes: 5 additions & 6 deletions tests/core/data/gridlayout/deriv.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
#!/usr/bin/env python
#!coding: utf-8

import numpy as np
import os
import sys
import math
import scipy.misc

import sys
import numpy as np

from pyphare.core import gridlayout
import os
import gridparams
import cellCenteredCoordinates

import utilities
import fieldNodeCoordinates
import cellCenteredCoordinates


class DerivParams(cellCenteredCoordinates.CenteredCoordParams):
Expand Down
11 changes: 4 additions & 7 deletions tests/core/data/gridlayout/fieldNodeCoordinates.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#!/usr/bin/env python
#!coding: utf-8

import numpy as np

# import math

import os
import sys
import utilities
import numpy as np
import cellCenteredCoordinates

from pyphare.core import gridlayout
import os
import gridparams
import cellCenteredCoordinates
import utilities

# TODO : FieldNode coords is general case of cellCenteredCoord
# Which means this has to be fully refactor
Expand Down
10 changes: 3 additions & 7 deletions tests/core/data/gridlayout/gridIndexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
quantities and for all interpolation orders (1,2,3,4), in 1D, 2D and 3D.
"""

import numpy as np

import sys

from pyphare.core import gridlayout
import os
import sys
import utilities
import gridparams

from pyphare.core import gridlayout


class IndexingParams(gridparams.GridParams):
def __init__(self, dim, interpOrder):
Expand Down Expand Up @@ -94,7 +93,6 @@ def main(path="./"):

gl = gridlayout.GridLayout()

directions = gl.directions
quantities = [
"Bx",
"By",
Expand Down Expand Up @@ -122,8 +120,6 @@ def main(path="./"):
dyList = [0.0, 0.1, 0.1]
dzList = [0.0, 0.0, 0.1]

maxNbrDim = 3

baseName = "gridIndexing"

outFilenameBase = os.path.join(path, baseName)
Expand Down
4 changes: 2 additions & 2 deletions tests/core/data/gridlayout/test_linear_combinaisons_yee.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env pyhton
#!coding: utf-8

import numpy as np
import utilities

import os
import sys


# this script writes the following file
# in 1D, in 2D and in 3D :
# {dim} {interpOrder_i} ExToMoment
Expand Down
10 changes: 3 additions & 7 deletions tests/diagnostic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def all_timestamps(sim):


def dump_all_diags(pops=[], flush_every=100, timestamps=None):
import pyphare.pharein as ph, numpy as np
import pyphare.pharein as ph

sim = ph.global_vars.sim

Expand All @@ -35,9 +35,7 @@ def dump_all_diags(pops=[], flush_every=100, timestamps=None):
"pressure_tensor",
]:
ph.FluidDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
flush_every=flush_every,
quantity=quantity, write_timestamps=timestamps, flush_every=flush_every
)

for pop in pops:
Expand All @@ -59,7 +57,5 @@ def dump_all_diags(pops=[], flush_every=100, timestamps=None):

for quantity in ["E", "B"]:
ph.ElectromagDiagnostics(
quantity=quantity,
write_timestamps=timestamps,
flush_every=flush_every,
quantity=quantity, write_timestamps=timestamps, flush_every=flush_every
)
12 changes: 7 additions & 5 deletions tests/diagnostic/job_1d.py.in
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python3

import pyphare.pharein as ph
from pyphare.pharein import ElectronModel
from tests.simulator import basicSimulatorArgs, makeBasicModel

from tests.diagnostic import dump_all_diags
from tests.simulator import basicSimulatorArgs, makeBasicModel

out = "phare_outputs/diags_1d/"
simInput = {"diag_options": {"format": "phareh5", "options": {"dir": out, "mode" : "overwrite"}}}
simInput = {
"diag_options": {"format": "phareh5", "options": {"dir": out, "mode": "overwrite"}}
}

ph.Simulation(**basicSimulatorArgs(dim = 1, interp = 1, **simInput))
ph.Simulation(**basicSimulatorArgs(dim=1, interp=1, **simInput))
model = makeBasicModel()
ElectronModel(closure="isothermal",Te = 0.12)
ph.ElectronModel(closure="isothermal", Te=0.12)
dump_all_diags(model.populations)
12 changes: 7 additions & 5 deletions tests/diagnostic/job_2d.py.in
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#!/usr/bin/env python3

import pyphare.pharein as ph
from pyphare.pharein import ElectronModel
from tests.simulator import basicSimulatorArgs, makeBasicModel

from tests.diagnostic import dump_all_diags
from tests.simulator import basicSimulatorArgs, makeBasicModel

out = "phare_outputs/diags_2d/"
simInput = {"diag_options": {"format": "phareh5", "options": {"dir": out, "mode" : "overwrite"}}}
simInput = {
"diag_options": {"format": "phareh5", "options": {"dir": out, "mode": "overwrite"}}
}

ph.Simulation(**basicSimulatorArgs(dim = 2, interp = 1, **simInput))
ph.Simulation(**basicSimulatorArgs(dim=2, interp=1, **simInput))
model = makeBasicModel()
ElectronModel(closure="isothermal",Te = 0.12)
ph.ElectronModel(closure="isothermal", Te=0.12)
dump_all_diags(model.populations)
Loading
Loading