-
Notifications
You must be signed in to change notification settings - Fork 1
Fixed bug when using plasma_density_from_file #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| # 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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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
kyrsjo
left a comment
There was a problem hiding this 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...)
|
Could you please fix (i.e. delete it) comment here: |
| self.plasma_profile.ns = ns | ||
|
|
||
| return | ||
| except FileNotFoundError: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 NoneThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
|
@finnerudo what is the status of this branch? |
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.