Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ perf.*
PHARE_REPORT.zip
.cache
.gdbinit
.phlop
.phlop
1 change: 1 addition & 0 deletions tests/simulator/refinement/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ if(HighFive)
endif(testMPI)

phare_python3_exec(9 simple_2d_refinement test_2d_2_core.py ${CMAKE_CURRENT_BINARY_DIR})
phare_python3_exec(9 test_2d_overlaps test_2d_overlaps.py ${CMAKE_CURRENT_BINARY_DIR})

endif()
188 changes: 188 additions & 0 deletions tests/simulator/refinement/test_2d_overlaps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#!/usr/bin/env python3

import numpy as np
import matplotlib as mpl
from pathlib import Path

import pyphare.pharein as ph
from pyphare.cpp import cpp_lib
from pyphare.simulator.simulator import Simulator, startMPI

from tests.simulator import SimulatorTest
from tests.simulator.test_advance import AdvanceTestBase
from tests.diagnostic import dump_all_diags

mpl.use("Agg")

cpp = cpp_lib()


cells = (40, 80)
time_step = 0.005
final_time = 1
timestamps = np.arange(0, final_time + time_step, time_step)
diag_dir = "phare_outputs/test/refinenment/2d/overlaps"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in directory path.

The directory name contains a typo: "refinenment" should be "refinement".

-diag_dir = "phare_outputs/test/refinenment/2d/overlaps"
+diag_dir = "phare_outputs/test/refinement/2d/overlaps"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
diag_dir = "phare_outputs/test/refinenment/2d/overlaps"
diag_dir = "phare_outputs/test/refinement/2d/overlaps"
🤖 Prompt for AI Agents
In tests/simulator/refinement/test_2d_overlaps.py at line 24, fix the typo in
the directory path by changing "refinenment" to "refinement" in the diag_dir
string assignment.

test = AdvanceTestBase(rethrow=True)


def config():
L = 0.5

sim = ph.Simulation(
time_step=time_step,
final_time=final_time,
largest_patch_size=10,
cells=cells,
dl=(0.40, 0.40),
refinement="tagging",
max_nbr_levels=2,
hyper_resistivity=0.002,
resistivity=0.001,
diag_options={
"format": "phareh5",
"options": {"dir": diag_dir, "mode": "overwrite"},
},
strict=False,
)

def density(x, y):
Ly = sim.simulation_domain()[1]
return (
0.4
+ 1.0 / np.cosh((y - Ly * 0.3) / L) ** 2
+ 1.0 / np.cosh((y - Ly * 0.7) / L) ** 2
)

def S(y, y0, l):
return 0.5 * (1.0 + np.tanh((y - y0) / l))

def by(x, y):
Lx = sim.simulation_domain()[0]
Ly = sim.simulation_domain()[1]
sigma = 1.0
dB = 0.1

x0 = x - 0.5 * Lx
y1 = y - 0.3 * Ly
y2 = y - 0.7 * Ly

dBy1 = 2 * dB * x0 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
dBy2 = -2 * dB * x0 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)

return 0.0 # dBy1 + dBy2
Comment on lines +69 to +72
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove unused variables or implement the intended functionality.

The variables dBy1 and dBy2 are calculated but never used, and the function returns 0.0. If these perturbations are intended for future use, consider removing them for now or documenting the intent.

     def by(x, y):
-        Lx = sim.simulation_domain()[0]
-        Ly = sim.simulation_domain()[1]
-        sigma = 1.0
-        dB = 0.1
-
-        x0 = x - 0.5 * Lx
-        y1 = y - 0.3 * Ly
-        y2 = y - 0.7 * Ly
-
-        dBy1 = 2 * dB * x0 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
-        dBy2 = -2 * dB * x0 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)
-
-        return 0.0  # dBy1 + dBy2
+        return 0.0
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dBy1 = 2 * dB * x0 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
dBy2 = -2 * dB * x0 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)
return 0.0 # dBy1 + dBy2
def by(x, y):
return 0.0
🧰 Tools
🪛 Ruff (0.11.9)

69-69: Local variable dBy1 is assigned to but never used

Remove assignment to unused variable dBy1

(F841)


70-70: Local variable dBy2 is assigned to but never used

Remove assignment to unused variable dBy2

(F841)

🪛 Flake8 (7.2.0)

[error] 69-69: local variable 'dBy1' is assigned to but never used

(F841)


[error] 70-70: local variable 'dBy2' is assigned to but never used

(F841)

🤖 Prompt for AI Agents
In tests/simulator/refinement/test_2d_overlaps.py around lines 69 to 72, the
variables dBy1 and dBy2 are computed but not used, and the function returns 0.0.
To fix this, either remove the calculations of dBy1 and dBy2 if they are not
needed, or update the return statement to return the sum of dBy1 and dBy2 if
that is the intended functionality. Additionally, add a comment explaining the
purpose if you keep the variables for future use.


def bx(x, y):
Lx = sim.simulation_domain()[0]
Ly = sim.simulation_domain()[1]
sigma = 1.0
dB = 0.1

x0 = x - 0.5 * Lx
y1 = y - 0.3 * Ly
y2 = y - 0.7 * Ly

dBx1 = -2 * dB * y1 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
dBx2 = 2 * dB * y2 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)

v1 = -1
v2 = 1.0
return v1 + (v2 - v1) * (S(y, Ly * 0.3, L) - S(y, Ly * 0.7, L)) # + dBx1 + dBx2
Comment on lines +84 to +89
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Remove unused variables or implement the intended functionality.

Similar to the by function, dBx1 and dBx2 are calculated but commented out in the return statement. Consider removing the unused calculations for cleaner code.

     def bx(x, y):
         Lx = sim.simulation_domain()[0]
         Ly = sim.simulation_domain()[1]
-        sigma = 1.0
-        dB = 0.1
-
-        x0 = x - 0.5 * Lx
-        y1 = y - 0.3 * Ly
-        y2 = y - 0.7 * Ly
-
-        dBx1 = -2 * dB * y1 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
-        dBx2 = 2 * dB * y2 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)
 
         v1 = -1
         v2 = 1.0
-        return v1 + (v2 - v1) * (S(y, Ly * 0.3, L) - S(y, Ly * 0.7, L))  # + dBx1 + dBx2
+        return v1 + (v2 - v1) * (S(y, Ly * 0.3, L) - S(y, Ly * 0.7, L))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
dBx1 = -2 * dB * y1 * np.exp(-(x0**2 + y1**2) / (sigma) ** 2)
dBx2 = 2 * dB * y2 * np.exp(-(x0**2 + y2**2) / (sigma) ** 2)
v1 = -1
v2 = 1.0
return v1 + (v2 - v1) * (S(y, Ly * 0.3, L) - S(y, Ly * 0.7, L)) # + dBx1 + dBx2
def bx(x, y):
Lx = sim.simulation_domain()[0]
Ly = sim.simulation_domain()[1]
v1 = -1
v2 = 1.0
return v1 + (v2 - v1) * (S(y, Ly * 0.3, L) - S(y, Ly * 0.7, L))
🧰 Tools
🪛 Ruff (0.11.9)

84-84: Local variable dBx1 is assigned to but never used

Remove assignment to unused variable dBx1

(F841)


85-85: Local variable dBx2 is assigned to but never used

Remove assignment to unused variable dBx2

(F841)

🪛 Flake8 (7.2.0)

[error] 84-84: local variable 'dBx1' is assigned to but never used

(F841)


[error] 85-85: local variable 'dBx2' is assigned to but never used

(F841)

🤖 Prompt for AI Agents
In tests/simulator/refinement/test_2d_overlaps.py around lines 84 to 89, the
variables dBx1 and dBx2 are calculated but not used because their addition in
the return statement is commented out. To clean up the code, either remove the
calculations of dBx1 and dBx2 entirely if they are not needed, or uncomment and
properly integrate them into the return expression if their effect is intended
to be included.


def bz(x, y):
return 0.0

def b2(x, y):
return bx(x, y) ** 2 + by(x, y) ** 2 + bz(x, y) ** 2

def T(x, y):
K = 0.7
temp = 1.0 / density(x, y) * (K - b2(x, y) * 0.5)
assert np.all(temp > 0)
return temp

def vx(x, y):
return 0.0

def vy(x, y):
return 0.0

def vz(x, y):
return 0.0

def vthx(x, y):
return np.sqrt(T(x, y))

def vthy(x, y):
return np.sqrt(T(x, y))

def vthz(x, y):
return np.sqrt(T(x, y))

vvv = {
"vbulkx": vx,
"vbulky": vy,
"vbulkz": vz,
"vthx": vthx,
"vthy": vthy,
"vthz": vthz,
"nbr_part_per_cell": 20,
}

model = ph.MaxwellianFluidModel(
bx=bx,
by=by,
bz=bz,
protons={"charge": 1, "density": density, **vvv, "init": {"seed": 12334}},
)
ph.ElectronModel(closure="isothermal", Te=0.0)

dump_all_diags(model.populations, flush_every=1)

return sim


def get_time(path, time=None, datahier=None):
if time is not None:
time = "{:.10f}".format(time)
from pyphare.pharesee.hierarchy import hierarchy_from

datahier = hierarchy_from(h5_filename=path + "/EM_E.h5", times=time, hier=datahier)
datahier = hierarchy_from(h5_filename=path + "/EM_B.h5", times=time, hier=datahier)

return datahier


def get_hier(path):
return get_time(path)


class HarrisTest(SimulatorTest):
def __init__(self, *args, **kwargs):
super(HarrisTest, self).__init__(*args, **kwargs)
self.simulator = None
self.plot_dir = Path(f"{diag_dir}_plots") / str(cpp.mpi_size())
self.plot_dir.mkdir(parents=True, exist_ok=True)

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

def test_run(self):
sim = Simulator(config(), post_advance=self.post_advance)
sim.initialize()
self.post_advance(0)
sim.run().reset()
return self

def post_advance(self, new_time):
if cpp.mpi_rank() == 0:
L0L1_datahier = get_hier(diag_dir)
test.base_test_overlaped_fields_are_equal(L0L1_datahier, new_time)


if __name__ == "__main__":
startMPI()
HarrisTest().test_run().tearDown()
Loading