erdc/proteus

PointGauges variables with underscore text parsing

ejtovar opened this issue · 5 comments

If a model has variables such as ['h', 'h_u', 'h_v'], the following piece of code will not work:


heightPointGauges = PointGauges(gauges=((('h_u'), ((7.56, 16.05,  0),
                                                 (7.56, 14.55, 0),
                                                 (7.56, 13.05, 0),
                                                 (7.56, 11.55, 0),
                                                 (9.36, 13.80, 0),
                                                 (10.36, 13.80, 0),
                                                 (12.96, 11.22, 0),
                                                 (15.56, 13.80, 0))),),
                                activeTime=(0., opts.final_time),
                                fileName='island_wave_gauges.csv')

but


heightPointGauges = PointGauges(gauges=((('h'), ((7.56, 16.05,  0),
                                                 (7.56, 14.55, 0),
                                                 (7.56, 13.05, 0),
                                                 (7.56, 11.55, 0),
                                                 (9.36, 13.80, 0),
                                                 (10.36, 13.80, 0),
                                                 (12.96, 11.22, 0),
                                                 (15.56, 13.80, 0))),),
                                activeTime=(0., opts.final_time),
                                fileName='island_wave_gauges.csv')

will work. The loop line seen here https://github.com/erdc/proteus/blob/master/proteus/Gauges.py#L67 separates the h_u variable into three separate characters. A hack involves surrounding the single variable with [] as follows: (['h_u']). However, surrounding multiple single variables like (['h'],['h_u']) gives an error.

@ejtovar what about ('h_u',) and ('h', 'h_u'), or ['h_u'] and ['h', 'h_u']?
I think your problem here is that putting parentheses around a string without a comma does not make it a tuple (('h_u') is equivalent to just 'h_u'), so your code worked fine only if your unique string had a single character. Parentheses are also superfluous for (['h_u']), this works because it is a list

Yes, you are correct!

Perhaps we should add a conditional to where the fields are parsed to check if the argument is a tuple and if not, make it a tuple. It seems bizarre for the user to know this.

Perhaps we should add a conditional to where the fields are parsed to check if the argument is a tuple and if not, make it a tuple. It seems bizarre for the user to know this.

@zhang-alvin There are many places in the code where we are supposed to pass a list of variables even if there is only one variable for some cases in practice.
An extra conditional would not hurt if that makes things easier, but making non-tuple arguments become tuples could cause issues (what if the user passes a list of strings?). Checking if it is a string (field variables are strings here), and if it is, making it a list or a tuple would work better though.

Anyway, I think the confusion came from the use of tuples within tuples when calling PointGauges here, but those can be replaced by lists instead for clarity. Also, the documentation does mention that it must be a "set of fields to be monitored":
https://proteustoolkit.org/api/proteus.Gauges.html (there is even an example with a single field variable ('p',))
If it is confusing, it might be worth updating the documentation, but don't forget to "read (and update) the docs"™ ;)