Skip to content

Conversation

@finnerudo
Copy link
Collaborator

@finnerudo finnerudo commented Oct 7, 2025

When using plasma_density_from_file to make plasma ramp in HiPACE++, abel would crash because there was an assert self.plasma_density_from_file is None that is executed everytime a HiPACE++ stage is ran. I changed it to if there is plasma_density_from file it sets the plasma profile ss and ns which is needed for tracking the plasma density and just returns.

@finnerudo finnerudo added the bug Something isn't working label Oct 7, 2025
# check that there is not already a plasma density profile set
assert self.plasma_density_from_file is None
# If there is already a density file open and make the plasma profile
if self.plasma_density_from_file:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check that self.plasma_density_from_file is type str.
Also catch non-existent file, e.g. raise ValueError with meaningful error message

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe here explicitly check that it is None, otherwise e.g. False (a bool) or 0 (an int) would have the same meaning as None here but maybe not everywhere else.

# If there is already a density file open and make the plasma profile
if self.plasma_density_from_file:
ss, ns = [], []
with open(self.plasma_density_from_file, 'r') as f:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Catch errors when reading / parsing file

Copy link
Collaborator

@kyrsjo kyrsjo left a comment

Choose a reason for hiding this comment

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

Error handling, delete wrong comment "Save to file" (first one...)

@kyrsjo
Copy link
Collaborator

kyrsjo commented Nov 11, 2025

self.plasma_profile.ns = ns

return
except FileNotFoundError:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good job, but this block only handles the case where the file doesn’t exist. If another problem occurs, e.g. permission denied, or the path is a directory, you will get an unhandled exception (like PermissionError) and the program will crash, unless you catch it too.


return
except FileNotFoundError:
raise FileNotFoundError("File can not be located using given path") from None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Typo in the error message. Should be "File cannot be located using given path.". Or perhaps it is better to instead write "Could not open specified plasma profile file.", which is more specific regarding what file could not be opened.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, something like f"Could not open specified plasma profile file '{self.plasma_density_from_file}'." would probably be ideal.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please also add a comment in the code about from None

assert self.plasma_density_from_file is None
# If there is already a density file open and make the plasma profile
if self.plasma_density_from_file:
if isinstance(self.plasma_density_from_file, str):
Copy link
Collaborator

@ben-c-2013 ben-c-2013 Nov 13, 2025

Choose a reason for hiding this comment

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

I suggest something like:

try:
    data = np.loadtxt(self.plasma_density_from_file, comments='#')
    ss, ns = data[:, 0], data[:, 1]
    self.plasma_profile.ss = ss
    self.plasma_profile.ns = ns
    return
except OSError:
    raise FileNotFoundError("Plasma profile file cannot be located or opened using given path.") from None
except ValueError:
    raise ValueError("Plasma profile file must contain two numeric values (SI units) in the order <longitudinal position> <plasma density> per line.") from None

Copy link
Collaborator

@ben-c-2013 ben-c-2013 Nov 13, 2025

Choose a reason for hiding this comment

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

This avoids unnecessary conversions, loop and is far more compact. Should also be able to handle commented lines and blank lines, as well as handle more errors.

But Please TEST it for me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(also please use f-strings to include the bad file name etc.)

@kyrsjo
Copy link
Collaborator

kyrsjo commented Dec 18, 2025

@finnerudo what is the status of this branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants