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

Clean up the function calls to domain_decomposition from main #73

Open
mortele opened this issue Apr 9, 2021 · 6 comments
Open

Clean up the function calls to domain_decomposition from main #73

mortele opened this issue Apr 9, 2021 · 6 comments
Labels
refactor Code rewrite

Comments

@mortele
Copy link
Member

mortele commented Apr 9, 2021

These long function calls to domain_decomposition(...) with many lines of arguments doubled by the if test clause for the molecules being present is getting cumbersome. Look into rewriting this in a more sane way, so we can add conditional arrays to the decomposition without having to add 2ⁿ if test clauses, each with it's own 10+ lines of arguments. See discussion from #72.

@mortele mortele added the refactor Code rewrite label Apr 9, 2021
@xinmeng2020
Copy link
Contributor

xinmeng2020 commented Apr 9, 2021

@mortele Referring your example code #72, put the args in a tuple looks nice.
I just add that we need a * operator to unpack the tuple:

args = tuple(args[:10 + int(charges_flag) + int(mass_flag)])
dd = domain_decomposition(
    *args,  ##args  
    ...
)   
## testing
def f (a, b, c):
    return a*b*c

x = tuple([3, 5 ])
print(   f(*x, 10 )  ) ## 150

@mortele
Copy link
Member Author

mortele commented Apr 10, 2021

Ah, yes, thanks. Editing the example in #72.

@xinmeng2020
Copy link
Contributor

xinmeng2020 commented Apr 10, 2021

I tried to define two args arg_in and arg_recv; but I think using arg_recv in my try here is a silly mistake..
As when the tuple is generated the values are used, not the variables, thus I still have to make the args_recv explicit.

  • Although it is possible to transform the explicit list as a string, and execute it using e.g. exec function in python, which will still keep the arg_recv = dd as single line. I not sure whether that will be too strange (see the code in the next reply).
    !!!!!!!!!!!!!!!!!!!!!!!!!! NOT CORRECT, can ignore !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Affiliated_DD_Args_in_Len = 9 ## can also use the append without defining the length ?
    args_in = [None for _ in range(Affiliated_DD_Args_Len)]
    args_in[:7] = [
         velocities,
         indices,
         bond_forces,
         angle_forces,
         field_forces,
         names, 
         types
    ]
    if charges_flag: ## add charge related 
        args_in[7] = charges 
        args_in[8] = field_q_forces  

    ## args_recv for the (..) = dd
    args_recv = args_in.copy()
    args_recv.insert(0, positions)
    if molecules_flag:
        args_recv.append(bonds)
        args_recv.append(molecules)
    ## convert to tuple
    args_in = tuple(args_in)
    args_recv= tuple(args_recv)
    
    ############### DD 
    if config.domain_decomposition:
        dd = domain_decomposition(
            positions,
            pm,
            *args_in,
            molecules=molecules if molecules_flag else None,
            bonds=bonds if molecules_flag else None,
            verbose=args.verbose,
            comm=comm,
        )
        args_recv = dd  #<------------ WRONG 

@xinmeng2020
Copy link
Contributor

xinmeng2020 commented Apr 10, 2021

Hi @mortele, this is a rewriting of the call about the domain_decomposition() function.

  • this introduces two variables: args_in (tuple of variables) and args_recv (list of strings);
  • the tricks is to convert the variables inside args_recv to a string and execute the string as code;
  • the benefit is that we can handle all the flag conditions once for all;
  • special care needed about the exec function in python, for safety and speed;
  • I tested in a short DPPC run. No error while running
    args_in = [
         velocities,
         indices,
         bond_forces,
         angle_forces,
         field_forces,
         names, 
         types
    ]
    args_recv = [
         'positions',
         'velocities',
         'indices',
         'bond_forces',
         'angle_forces',
         'field_forces',
         'names', 
         'types'
    ]

    if charges_flag: ## add charge related 
        args_in.append(charges) 
        args_in.append(field_q_forces)
        args_recv.append('charges')
        args_recv.append('field_q_forces')

    if molecules_flag:
        args_recv.append('bonds')
        args_recv.append('molecules')
    
    ## convert to tuple
    args_in = tuple(args_in)
    
    ## cmd string to excecut the (...) = dd 
    _str_receive_dd =  ','.join(args_recv)
    _cmd_receive_dd = f"({_str_receive_dd }) = dd"
    
    ############### DD 
    if config.domain_decomposition:
        dd = domain_decomposition(
            positions,
            pm,
            *args_in,
            molecules=molecules if molecules_flag else None,
            bonds=bonds if molecules_flag else None,
            verbose=args.verbose,
            comm=comm,
        )
        exec(_cmd_receive_dd ) 

@mortele
Copy link
Member Author

mortele commented Apr 10, 2021

Nice work! We should meet on Monday to discuss!

@xinmeng2020
Copy link
Contributor

Nice work! We should meet on Monday to discuss!

👌😄 see you on Monday~

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

No branches or pull requests

2 participants