Skip to content
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

command seems to ignored using input #52

Open
rdemaria opened this issue Aug 26, 2019 · 10 comments
Open

command seems to ignored using input #52

rdemaria opened this issue Aug 26, 2019 · 10 comments

Comments

@rdemaria
Copy link
Collaborator

In the example below, it looks like the "beam;" command is not sent to mad, although it appears in the command log. Splitting the input string in two parts does not show the problem.

from cpymad.madx import Madx
mad=Madx(command_log="log1.madx")

mad.input("""
l1=1; l2=1; l3=1; l4=1;
mb1: sbend, angle:=a1, l:=l1,tilt:=t1;
mb2: sbend, angle:=a2, l:=l2,tilt:=t2;
mb3: sbend, angle:=a3, l:=l3,tilt:=t3;
mb4: sbend, angle:=a4, l:=l4,tilt:=t4;
myseq : line =(mb1,mb2,mb3,mb4);
beam; """)

try:
   mad.use(sequence="myseq")
except RuntimeError:
    print("beam command seems ignored")

mad=Madx(command_log="log2.madx")
mad.input("""
l1=1; l2=1; l3=1; l4=1;
mb1: sbend, angle:=a1, l:=l1,tilt:=t1;
mb2: sbend, angle:=a2, l:=l2,tilt:=t2;
mb3: sbend, angle:=a3, l:=l3,tilt:=t3;
mb4: sbend, angle:=a4, l:=l4,tilt:=t4;
myseq : line =(mb1,mb2,mb3,mb4);
""")
mad.input("beam;")
mad.use(sequence="myseq")
print("works")
@coldfix
Copy link
Member

coldfix commented Aug 26, 2019

Hi, it could be that something is wrong with splitting of commands with the input command. I will investigate and report back.

@coldfix
Copy link
Member

coldfix commented Aug 26, 2019

Hi,

I checked the reason this fails is that the input is sent unsplit to MAD-X pro_input routine which cannot deal properly with multi-command in all cases by itself. This can be addressed in the following ways (both require bit more effort, so I'll have to think about which to use first):

  • patch cpymad to use get_stmt from MAD-X to presplit the input before sending it to pro_input. However: get_stmt operates on files not strings and is therefore not straightforward to use from cpymad. It would require either changing get_stmt API or running get_stmt in a separate thread to prevent deadlocks, and setting an event upon return.
  • include a routine in cpymad that emulates get_stmt function. However, since get_stmt contains some behaviour that looks like a bug to me, I'm not sure whether I would make it behave exactly as in MAD-X

@rdemaria
Copy link
Collaborator Author

Is there a way to write mad input to avoid triggering this bug for the time being?

As far as I know, to get statements I would do in this order:

  1. remove everything in between // and \n
  2. remove everything in between ! and \n
  3. remove everything in between \* and the first *\
  4. remove all whitespaces
  5. make everything lower case besides what is in between " and the second "
  6. split and every ;

@coldfix
Copy link
Member

coldfix commented Aug 26, 2019

On the user side, yes:

  • for complicated input: write the input to a file and use the call command
  • if you have control over the input: manually split up the input into individual constructs (where an IF () {} or WHILE {} counts as one), and invoke input only with single units

@rdemaria
Copy link
Collaborator Author

Do you have a timescale for this fix? I am getting few people reporting being hit by this behaviours as more users are using cpymad.

A very quick fix (although not efficient) could be to reimplement input to always save the string to a temporary file, use "call" and delete the file (the old input could be temporarily hidden with an underscore). This to avoid people experiencing undefined behaviours that is very difficult to trace back. I can prepare a PR if you think it is an option.

@coldfix
Copy link
Member

coldfix commented Sep 16, 2019

Hey, I had already started the implementation but then pursued another project, so I can have a working version by tomorrow I believe. However, I cannot guarantee that it will always do 100% the same as MAD-X does.

@coldfix
Copy link
Member

coldfix commented Sep 18, 2019

Hi,

while adapting the get_stmt function for cpymad, I realized that it is actually not really responsible for commands in a way that MAD-X pro_input() can digest them, and that pro_input() in fact does the real splitting. This means that the reported behaviour was actually a bug in MAD-X that can be reproduced in MAD-X by writing multiple statements on the same line, such as this:

d: drift; myline: line=(d); beam;
use, sequence=my;

gives: +=+=+= fatal: USE - sequence without beam: myline

and

kappa=0; myline: line=(drift);
beam;
use, sequence=myline;

gives: ++++++ warning: unknown sequence skipped: myline

I have created a pull request with corresponding bugfixes for MAD-X, see MethodicalAcceleratorDesign/MAD-X#819.

You mentioned that you got further reports from other users. Do these fall into the same category or do they need to be fixed seperately?

Best, Thomas

@rdemaria
Copy link
Collaborator Author

Thanks a lot!!! I will check with my colleagues!

@rdemaria
Copy link
Collaborator Author

I checked and indeed they were related to a misunderstanding. Thanks again for fixing upstream!

@coldfix
Copy link
Member

coldfix commented Sep 26, 2019

Great. I will leave this open for now as I'm considering to include an adapted _get_stmt into cpymad anyway, to help with current/previous version of MAD-X and improve option, echo behaviour (which is currently effectively disabled by skipping get_stmt).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants