Skip to content

Commit

Permalink
Implements mpi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lmdiazangulo committed Jan 29, 2025
1 parent c5ed581 commit a903b90
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 70 deletions.
36 changes: 25 additions & 11 deletions src_pyWrapper/pyWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Probe():
+ BULK_CURRENT_PROBE_TAGS \
+ POINT_PROBE_TAGS \
+ FAR_FIELD_TAG \
+ MOVIE_TAGS
+ MOVIE_TAGS

def __init__(self, probe_filename):
if isinstance(probe_filename, os.PathLike):
self.filename = probe_filename.as_posix()
Expand Down Expand Up @@ -92,7 +93,7 @@ def __init__(self, probe_filename):
self.field, self.direction = Probe._getFieldAndDirection(tag)
self.cell_init, self.cell_end = \
Probe._positionStrToTwoCells(position_str)

if self.domainType == 'time':
self.data = self.data.rename(columns={
't': 'time',
Expand Down Expand Up @@ -179,17 +180,32 @@ def _getFieldAndDirection(tag: str):


class FDTD():
def __init__(self, input_filename, path_to_exe=None, flags=[], run_in_folder=None):
def __init__(self, input_filename, path_to_exe=None,
flags=None, run_in_folder=None, mpi_command=None):

self._setFilename(input_filename)

if path_to_exe is None:
self.path_to_exe = os.path.join(
os.getcwd(), DEFAULT_SEMBA_FDTD_PATH)
semba_exe = \
os.path.join(os.getcwd(), DEFAULT_SEMBA_FDTD_PATH)
else:
semba_exe = path_to_exe
assert os.path.isfile(semba_exe)

if mpi_command is None:
mpi_command_parts = []
else:
self.path_to_exe = path_to_exe
assert os.path.isfile(self.path_to_exe)
mpi_command_parts = mpi_command.split()

if flags is None:
flags = []
elif isinstance(flags, str):
flags = flags.split()

case_name = self.getCaseName() + ".json"
self.run_command = \
mpi_command_parts + [semba_exe] + ["-i", case_name] + flags

self.flags = flags
self._hasRun = False

if run_in_folder != None:
Expand Down Expand Up @@ -263,9 +279,7 @@ def run(self):
json.dump(self._input, open(self._filename, 'w'))

os.chdir(self.getFolder())
case_name = self.getCaseName() + ".json"
self.output = subprocess.run(
[self.path_to_exe, "-i", case_name]+self.flags)
self.output = subprocess.run(self.run_command)

self._hasRun = True
assert self.hasFinishedSuccessfully()
Expand Down
148 changes: 93 additions & 55 deletions test/pyWrapper/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def test_towel_hanger_case_creates_output_probes(tmp_path):
assert countLinesInFile(probe_mid[0]) == 3
assert countLinesInFile(probe_end[0]) == 3

@no_mpi_skip
def test_airplane_case_with_mpi(tmp_path):
fn = CASES_FOLDER + 'airplane/airplane.fdtd.json'
solver = FDTD(fn,
path_to_exe=SEMBA_EXE,
run_in_folder=tmp_path,
flags=['-mapvtk'],
mpi_command='mpirun -np 2')
solver.run()

vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)


def test_sphere_case_with_far_field_probe_launches(tmp_path):
fn = CASES_FOLDER + 'sphere/sphere.fdtd.json'
Expand Down Expand Up @@ -66,29 +79,34 @@ def test_tagnumbers_3_surfaces(tmp_path):
solver['general']['numberOfSteps'] = 1

solver.run()

vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)

face_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'tagnumber')
face_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='tagnumber')
assert face_tag_dict[64] == 4
assert face_tag_dict[128] == 4
assert face_tag_dict[192] == 4

line_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'tagnumber')
line_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='tagnumber')
assert line_tag_dict[64] == 8
assert line_tag_dict[128] == 4
assert line_tag_dict[192] == 4

face_media_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'mediatype')
assert face_media_dict[0] == 4 #PEC surface
assert face_media_dict[304] == 4 #SGBC surface
assert face_media_dict[305] == 4 #SGBC surface

line_media_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'mediatype')
assert line_media_dict[0.5] == 8 #PEC line
assert line_media_dict[3.5] == 8 #SGBC line


face_media_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='mediatype')
assert face_media_dict[0] == 4 # PEC surface
assert face_media_dict[304] == 4 # SGBC surface
assert face_media_dict[305] == 4 # SGBC surface

line_media_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='mediatype')
assert line_media_dict[0.5] == 8 # PEC line
assert line_media_dict[3.5] == 8 # SGBC line


def test_tagnumbers_1_volume(tmp_path):
fn = CASES_FOLDER + 'tagNumber_mediaType/pec_volume.fdtd.json'
solver = FDTD(input_filename=fn, path_to_exe=SEMBA_EXE,
Expand All @@ -100,18 +118,23 @@ def test_tagnumbers_1_volume(tmp_path):
vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)

face_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'tagnumber')
face_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='tagnumber')
assert face_tag_dict[64] == 36

line_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'tagnumber')

line_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='tagnumber')
assert len(line_tag_dict) == 0

face_media_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'mediatype')
assert face_media_dict[0] == 36 #PEC surface

line_media_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'mediatype')

face_media_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='mediatype')
assert face_media_dict[0] == 36 # PEC surface

line_media_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='mediatype')
assert len(line_media_dict) == 0


def test_tagnumbers_2_volumes(tmp_path):
fn = CASES_FOLDER + 'tagNumber_mediaType/pec_volumes.fdtd.json'
solver = FDTD(input_filename=fn, path_to_exe=SEMBA_EXE,
Expand All @@ -123,19 +146,24 @@ def test_tagnumbers_2_volumes(tmp_path):
vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)

face_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'tagnumber')
face_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='tagnumber')
assert face_tag_dict[64] == 36
assert face_tag_dict[128] == 36

line_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'tagnumber')

line_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='tagnumber')
assert len(line_tag_dict) == 0

face_media_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'mediatype')
assert face_media_dict[0] == 72 #PEC surface

line_media_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'mediatype')

face_media_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='mediatype')
assert face_media_dict[0] == 72 # PEC surface

line_media_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='mediatype')
assert len(line_media_dict) == 0


def test_tagnumbers_1_line(tmp_path):
fn = CASES_FOLDER + 'tagNumber_mediaType/pec_line.fdtd.json'
solver = FDTD(input_filename=fn, path_to_exe=SEMBA_EXE,
Expand All @@ -147,18 +175,23 @@ def test_tagnumbers_1_line(tmp_path):
vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)

face_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'tagnumber')
face_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='tagnumber')
assert len(face_tag_dict) == 0

line_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'tagnumber')

line_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='tagnumber')
assert line_tag_dict[64] == 2

face_media_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'mediatype')

face_media_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='mediatype')
assert len(face_media_dict) == 0

line_media_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'mediatype')
assert line_media_dict[0.5] == 2 #PEC line


line_media_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='mediatype')
assert line_media_dict[0.5] == 2 # PEC line


def test_tagnumbers_volume_and_surfaces(tmp_path):
fn = CASES_FOLDER + 'tagNumber_mediaType/volume_and_surfaces.fdtd.json'
solver = FDTD(input_filename=fn, path_to_exe=SEMBA_EXE,
Expand All @@ -170,26 +203,31 @@ def test_tagnumbers_volume_and_surfaces(tmp_path):
vtkmapfile = solver.getVTKMap()
assert os.path.isfile(vtkmapfile)

face_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'tagnumber')
face_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='tagnumber')
assert face_tag_dict[64] == 6
assert face_tag_dict[128] == 1
assert face_tag_dict[192] == 1

line_tag_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'tagnumber')

line_tag_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='tagnumber')
assert line_tag_dict[64] == 1
assert line_tag_dict[128] == 4
assert line_tag_dict[192] == 3

face_media_dict = createPropertyDictionary(vtkmapfile, celltype = 9, property = 'mediatype')
assert face_media_dict[-1] == 1 #PEC surface
assert face_media_dict[0] == 6 #PEC surface
assert face_media_dict[305] == 1 #SGBC surface

line_media_dict = createPropertyDictionary(vtkmapfile, celltype = 3, property = 'mediatype')
assert line_media_dict[-0.5] == 4 #PMC line
assert line_media_dict[0.5] == 1 #PEC line
assert line_media_dict[3.5] == 3 #SGBC line


face_media_dict = createPropertyDictionary(
vtkmapfile, celltype=9, property='mediatype')
assert face_media_dict[-1] == 1 # PEC surface
assert face_media_dict[0] == 6 # PEC surface
assert face_media_dict[305] == 1 # SGBC surface

line_media_dict = createPropertyDictionary(
vtkmapfile, celltype=3, property='mediatype')
assert line_media_dict[-0.5] == 4 # PMC line
assert line_media_dict[0.5] == 1 # PEC line
assert line_media_dict[3.5] == 3 # SGBC line


def test_tagnumbers_count_bug(tmp_path):
fn = CASES_FOLDER + 'tagNumber_mediaType/count_bug.fdtd.json'
solver = FDTD(input_filename=fn, path_to_exe=SEMBA_EXE,
Expand All @@ -201,10 +239,10 @@ def test_tagnumbers_count_bug(tmp_path):
solver["materialAssociations"][1]["materialId"] = 1
solver["materialAssociations"][2]["materialId"] = 3
solver.cleanUp()
solver.run()
solver.run()

solver["materialAssociations"][0]["materialId"] = 3
solver["materialAssociations"][1]["materialId"] = 3
solver["materialAssociations"][2]["materialId"] = 1
solver.cleanUp()
solver.run()
solver.run()
33 changes: 29 additions & 4 deletions test/pyWrapper/test_pyWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def test_read_point_probe_without_planewave():


def test_read_bulk_current_probe():
p = Probe(OUTPUTS_FOLDER + 'twoWires.fdtd_Bulk probe_Jx_15_11_13__15_13_17.dat')

p = Probe(OUTPUTS_FOLDER +
'twoWires.fdtd_Bulk probe_Jx_15_11_13__15_13_17.dat')

assert p.case_name == 'twoWires'
assert p.name == 'Bulk probe'
assert p.type == 'bulkCurrent'
Expand All @@ -88,7 +89,31 @@ def test_fdtd_set_new_folder_to_run(tmp_path):
solver['general']['numberOfSteps'] = 1

solver.run()



def test_fdtd_with_string_args(tmp_path):
input = os.path.join(CASES_FOLDER, 'planewave', 'pw-in-box.fdtd.json')
solver = FDTD(input,
path_to_exe=SEMBA_EXE,
run_in_folder=tmp_path,
flags='-h')
solver['general']['numberOfSteps'] = 1

solver.run()


@no_mpi_skip
def test_fdtd_with_mpi_run(tmp_path):
input = os.path.join(CASES_FOLDER, 'planewave', 'pw-in-box.fdtd.json')
solver = FDTD(input,
path_to_exe=SEMBA_EXE,
run_in_folder=tmp_path,
flags=['-h'],
mpi_command='mpirun -np 2')
solver['general']['numberOfSteps'] = 1

solver.run()


def test_fdtd_clean_up_after_run(tmp_path):
input = CASES_FOLDER + 'planewave/pw-in-box.fdtd.json'
Expand All @@ -97,7 +122,7 @@ def test_fdtd_clean_up_after_run(tmp_path):
solver['general']['numberOfSteps'] = 1

solver.run()

pn = solver.getSolvedProbeFilenames("inbox")
assert os.path.isfile(pn[0])

Expand Down
6 changes: 6 additions & 0 deletions test/pyWrapper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
reason="HDF5 is not available",
)

no_mpi_skip = pytest.mark.skipif(
os.getenv("SEMBA_FDTD_ENABLE_MPI") == "No",
reason="MPI is not available",
)

# Use of absolute path to avoid conflicts when changing directory.
if platform == "linux":
SEMBA_EXE = os.path.join(os.getcwd(), 'build', 'bin', 'semba-fdtd')
elif platform == "win32":
SEMBA_EXE = os.path.join(os.getcwd(), 'build', 'bin', 'semba-fdtd.exe')


TEST_DATA_FOLDER = os.path.join(os.getcwd(), 'testData/')
CASES_FOLDER = os.path.join(TEST_DATA_FOLDER, 'cases/')
MODELS_FOLDER = os.path.join(TEST_DATA_FOLDER, 'models/')
Expand Down

0 comments on commit a903b90

Please sign in to comment.