I wish I was able to use the more compact and straightforward colon thing (so I don't have to figure out the correct interpolation expression!).
Is there a way to do that in Python?
Might be compact and straightforward to you, but to many it would seem obscure (there's no accounting for taste, is there?). But the problem isn't just esthetic--it's that most people won't understand or remember the colon syntax. Not to say that Python is immune from readability problems--that language has a ton of obscure notations and terminology, just ask any C/C++ or FORTRAN programmer. (ooh, did he say that?)
The only place one should see the colon notation for specifying position-dependent values of range variables is in legacy code. Noone should be writing new code that uses it, because both hoc and Python have better ways to specify variation of range variables along a section. The algorithm is
Code: Select all
for each segment in section foo
set range variable to f(range of this segment)
For your particular case (linear variation along the length of a section), f would return
where val0 and val1 are the assumed values of the range value at the section's 0 and 1 ends, and x is range of the center of the segment (i.e. normalized distance of the segment center from the section's 0 end). After rearrangement, this is
How to implement this in hoc? Given a section called foo,
Code: Select all
foo for (x,0) diam(x) = val0 + (val1 - val0) * x
The Python equivalent is
for seg in foo: seg.diam = val0 + (val1 - val0) * seg.x
where foo is the Python alias for a section that was created by a statement such as
foo = h.Section('foo')
If I wanted to do this in many sections, I'd probably factor out the linear function into an actual function.