Author Topic: Global parameters defination  (Read 946 times)

Leal

  • Newbie
  • *
  • Posts: 2
Global parameters defination
« on: January 07, 2017, 04:54:12 AM »
Hello Dr. Stukowski and everyone,

   Thanks for your quick response to my last posted question. It is very useful.
    Here I also have anther question about methods to define and access global parameters. The example is given below:
      for i in range(20):
        .........
         L  = i*5
         M  = (i+1)*5
         Select=SelectExpressionModifier(expression='Position.Y*Position.Y+Position.Z*Position.Z>L*L&& Position.Y*Position.Y+Position.Z*Position.Z<M*M')
         node.modifiers.append(Select)
        .........
    By using the code, I want to bin the sample by a sequence of cylindrical rings(the radius are calculated during the loops). While, the self-defined L and M seems to be local variables and can not be accepted or updated in the modification pipeline.  My current compromised solution is to assign the variable, M,N, to some unused particle properties, such as the particle identifier:
          node.source.particle_properties.particle_identifier.marray[:] = L (or M)
          node.source.particle_properties.particle_identifier.changed()
    So far as I know, some global parameters, such as N(number of particles),Frame, Timestep are acceptable in the pipeline. So is there any way to define global variables rather than per-atom variables that can simplify the code?
   
Best Regards
Leal
« Last Edit: January 07, 2017, 05:25:36 AM by Leal »

Alexander Stukowski

  • Administrator
  • Hero Member
  • *****
  • Posts: 638
Re: Global parameters defination
« Reply #1 on: January 07, 2017, 07:35:28 PM »
Dear Leal,

Yes, it is possible to define global values that are accessible by modifiers in the data pipeline and which can be referenced in expressions of the SelectExpressionModifier. They can be defined by adding entries to the attributes dictionary of the node.source object.

However, this doesn't seem to be the right strategy for the problem at hand, because the global attributes are static across the entire pipeline. If I understand you correctly, you want to create several SelectExpressionModifiers, each with different values for L and M. In this case I suggest you simply replace the variables L and M in the selection expression with their numeric values, i.e.

Code: [Select]
for i in ...
    L  = i*5
    M  = (i+1)*5
    Select=SelectExpressionModifier(expression='Position.Y*Position.Y+Position.Z*Position.Z>{L}*{L} && Position.Y*Position.Y+Position.Z*Position.Z<{M}*{M}'.format(L=L, M=M))
    node.modifiers.append(Select)

Another alternative is to use a PythonScriptModifier instead of a SelectExpressionModifier. Unlike the latter, the user-defined Python modifier function has direct access to variables defined in the Python script.

Let me know if you have further questions. The best solution to this scripting problem probably depends on what exactly you want to do with these ring-shaped particle selections.
« Last Edit: January 07, 2017, 11:23:46 PM by Alexander Stukowski »