From 7900f00fc0019fd5062b13e7102aa8bbc22e6303 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 11 Aug 2023 15:30:54 -0700 Subject: [PATCH 1/3] Add amrex::Print() Like Function Add `amr.Print(...)` that behaves like Python's `print()`, but only prints on the AMReX IOProcessor. --- src/amrex/space1d/__init__.py | 6 ++++++ src/amrex/space2d/__init__.py | 6 ++++++ src/amrex/space3d/__init__.py | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/amrex/space1d/__init__.py b/src/amrex/space1d/__init__.py index 6fac7643..0185e14f 100644 --- a/src/amrex/space1d/__init__.py +++ b/src/amrex/space1d/__init__.py @@ -32,3 +32,9 @@ # def d_decl(x, y, z): return (x,) + + +def Print(*args, **kwargs): + """Wrap amrex::Print() - only the IO processor writes""" + if ParallelDescriptor.IOProcessor(): + print(*args, **kwargs) diff --git a/src/amrex/space2d/__init__.py b/src/amrex/space2d/__init__.py index c30ed8c5..f21e915b 100644 --- a/src/amrex/space2d/__init__.py +++ b/src/amrex/space2d/__init__.py @@ -32,3 +32,9 @@ # def d_decl(x, y, z): return (x, y) + + +def Print(*args, **kwargs): + """Wrap amrex::Print() - only the IO processor writes""" + if ParallelDescriptor.IOProcessor(): + print(*args, **kwargs) diff --git a/src/amrex/space3d/__init__.py b/src/amrex/space3d/__init__.py index d8163ea8..eabf0fff 100644 --- a/src/amrex/space3d/__init__.py +++ b/src/amrex/space3d/__init__.py @@ -32,3 +32,9 @@ # def d_decl(x, y, z): return (x, y, z) + + +def Print(*args, **kwargs): + """Wrap amrex::Print() - only the IO processor writes""" + if ParallelDescriptor.IOProcessor(): + print(*args, **kwargs) From 103ae7f3b132f9352c6b1a7210ecdb5cbca1be14 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Tue, 15 Aug 2023 14:35:28 -0700 Subject: [PATCH 2/3] Test `amr.Print(...)` --- tests/test_utility.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_utility.py b/tests/test_utility.py index 646b8e13..033ce976 100644 --- a/tests/test_utility.py +++ b/tests/test_utility.py @@ -6,3 +6,8 @@ def test_concatenate(): pltname = amr.concatenate("plt", 1000, 5) assert pltname == "plt01000" + + +def test_print(): + print("hello from everyone") + amr.Print("byeee from IO processor") From e947318af22b4a1a99a600abb127b7d429d519be Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Tue, 15 Aug 2023 14:56:03 -0700 Subject: [PATCH 3/3] amr.Print(): Fallback to All if not Init and warn --- src/amrex/space1d/__init__.py | 5 ++++- src/amrex/space2d/__init__.py | 5 ++++- src/amrex/space3d/__init__.py | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/amrex/space1d/__init__.py b/src/amrex/space1d/__init__.py index 0185e14f..6e042339 100644 --- a/src/amrex/space1d/__init__.py +++ b/src/amrex/space1d/__init__.py @@ -36,5 +36,8 @@ def d_decl(x, y, z): def Print(*args, **kwargs): """Wrap amrex::Print() - only the IO processor writes""" - if ParallelDescriptor.IOProcessor(): + if not initialized(): + print("warning: Print all - AMReX not initialized") + print(*args, **kwargs) + elif ParallelDescriptor.IOProcessor(): print(*args, **kwargs) diff --git a/src/amrex/space2d/__init__.py b/src/amrex/space2d/__init__.py index f21e915b..235189dd 100644 --- a/src/amrex/space2d/__init__.py +++ b/src/amrex/space2d/__init__.py @@ -36,5 +36,8 @@ def d_decl(x, y, z): def Print(*args, **kwargs): """Wrap amrex::Print() - only the IO processor writes""" - if ParallelDescriptor.IOProcessor(): + if not initialized(): + print("warning: Print all - AMReX not initialized") + print(*args, **kwargs) + elif ParallelDescriptor.IOProcessor(): print(*args, **kwargs) diff --git a/src/amrex/space3d/__init__.py b/src/amrex/space3d/__init__.py index eabf0fff..50e7897c 100644 --- a/src/amrex/space3d/__init__.py +++ b/src/amrex/space3d/__init__.py @@ -36,5 +36,8 @@ def d_decl(x, y, z): def Print(*args, **kwargs): """Wrap amrex::Print() - only the IO processor writes""" - if ParallelDescriptor.IOProcessor(): + if not initialized(): + print("warning: Print all - AMReX not initialized") + print(*args, **kwargs) + elif ParallelDescriptor.IOProcessor(): print(*args, **kwargs)