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

Checking of Madx commands #9

Closed
coldfix opened this issue Dec 1, 2014 · 4 comments
Closed

Checking of Madx commands #9

coldfix opened this issue Dec 1, 2014 · 4 comments

Comments

@coldfix
Copy link
Member

coldfix commented Dec 1, 2014

Currently, commands are checked only rudimentary. By defining a list of allowed parameters (+types) it should be easy to check any Madx command before execution.

EDIT: This information is already available in mad_dict.c, i.e. in the const_command_def variable, or just using the defined_commands...

So scratch the following:

I have something like this in mind:

_commands = {
    'twiss': {
        'sequence': str,
        'range': Range,
        ...
    },
    'match': {
        ...
    }
}

def mad_command(*args, **kwargs):
    if len(args) == 1:
        command = arg[0]
        param_types = _commands.get(command)
        if command:
            for name, value in kwargs.items():
                try:
                    type = param_types[name]
                except KeyError:
                    raise ValueError("Invalid parameter {!r} for command {}"
                                     .format(name, command))

                try:
                    value = type(value)
                except ValueError:
                    raise ValueError("Invalid type ({}) for parameter {}"
                                     .format(type, name))

    ...
@coldfix coldfix changed the title Checking of .madx() commands Checking of Madx commands Dec 1, 2014
@mina-acc
Copy link

mina-acc commented Feb 4, 2017

Hello
I want to use the flag "error" in cpymad, but I don't know how! I already use it in Madx and there was no problem. This is the error block in my madx file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Select, flag=error, clear = true;
select, flag=error, class=quadrupole;
efcomp, order:=0, radius:=0.013,
dknr={tgauss(2)*1e-4,tgauss(2)*1e-3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
select, flag=error, class=quadrupole;
esave,file=efield.tab0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
I have already checked the installed cpymad on my pc with the twiss example and everything works correctly. I was wondering if you could help me.
Thanks!

@coldfix
Copy link
Member Author

coldfix commented Feb 4, 2017

Hey, welcome to github!

Two beginner tips first:

  • please open new issues to discuss separate topics.
  • you can fence off code blocks as shown here, so your formatting doesn't get lost.

Did you consider to just leave these commands in a .madx file and madx.call() that file from cpymad? This is much easier and recommended if you are only going to change parameters manually. In general, you probably don't need cpymad if you are not trying to write a program which generates commands automatically or adjusts parameters dynamically.

If you really do need python code to input these commands: Most MAD-X commands can be converted to python syntax pretty straight forward, e.g.:

# (assuming your Madx instance is called `madx`:)
madx.command.select(flag="error", clear=True)
# note the trailing underscore in 'class_' is needed because class is a python keyword:
madx.command.select(flag="error", class_="quadrupole")
# Now this command is a bit non-standard and currently cpymad does a bad job getting it right.
# But you can still generate your command string manually and input it to cpymad:
madx.input('efcomp, order:=0, radius:=0.013, dknr={tgauss(2)*1e-4,tgauss(2)*1e-3};')
madx.command.esave(file="efield.tab0")

I agree that many of this should be simplified and made more straight forward, but this is not on the top of my priority list right now;)

@mina-acc
Copy link

mina-acc commented Feb 8, 2017

Hi.
Sorry for discussing this issue here. I thought that I found the related topic for my issue.
Yes. I already used the madx.call() command. The main motivation for me for switching to cpymad was that I need to use different seeds for the errors and calculating twiss function for each error data. I used while loop in MADX to change the seed every time, add errors to magnets and calculating twiss function. The madx problem was that it rewrites the twiss file each time in loop, so I only have access to the twiss data of last turn in loop.
I tried to use cpymad, so I could save the error and twiss data for any individual seed in each turn of while loop.
Thanks for your informative response. That works, except the part 'class_="quadrupole"'. Cpymad didn't recognize the quadrupoles. Instead, I use "pattern" and call all of the quadrupoles by their names.

@coldfix
Copy link
Member Author

coldfix commented Mar 24, 2018

Concerning the original issue: implemented in the branch merged in 24717d2.

@coldfix coldfix closed this as completed Mar 24, 2018
coldfix added a commit that referenced this issue Mar 25, 2018
- command/element etc:
    * retrieve information about commands from MAD-X ``defined_commands`` and
      store in ``Command`` instances.
    * use ``Command`` to improve command string generation and type-checks in
      ``util.mad_command`` (#9)
    * quote filename parameters when composing command string
    * use deferred expressions (``:=``) whenever passing strings to
      non-string parameters (#11)
    * subclass elements, beam from ``Command``
    * support attribute access for table/mappings/commands/elements/beams etc
    * allow case-insensitive access
    * overload index-access in tables to retrieve rows
    * implement ``Element.__delitem__`` by setting value to default
    * return name for global elements too
    * add ``Madx.base_types`` data variable that yields the base elements
    * add ``Element.parent``/``base_type`` attributes
    * more concise string representations
    * strip -Proxy suffix from class names
    * apply user defined row/column selections even when no output file is
      specified

- installation:
    * automatically use ``-lquadmath``
    * add ``--static`` flag for setup script, use ``--shared`` by default
    * no more need to link against PTC shared object separately
    * finally provide some binary wheels for py 3.5 and 3.6 (#32)

- raise cython language_level to 3
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