Could not put single input in command "compute reduce"?

Hello everyone! I find an interesting problem when I use the command “compute reduce”.

I just want to compute the mass center’s x coordinate of a group. So I use the code below:

compute      FxArea  FxArea reduce ave x 
variable     FxAreaDis equal c_FxArea[1]
fix          FxAreaCenter  FxArea print 8 "${istep} ${FxAreaDis}" screen no file FxAreaCenterDis.data title "Step FxAreaCenterDis"

I got the error message:

ERROR: Variable FxAreaDis: Mismatched compute in variable formula (src/variable.cpp:1596)

But when I add an input “vx” in the code, the program runs well.

compute      FxArea  FxArea reduce ave x vx
variable     FxAreaDis equal c_FxArea[1]
fix          FxAreaCenter  FxArea print 8 "${istep} ${FxAreaDis}" screen no file FxAreaCenterDis.data title "Step FxAreaCenterDis"

I read the manual about “compute reduce” and I think maybe the question is on this sentence:

If a single input is specified this compute produces a global scalar value. If multiple inputs are specified, this compute produces a global vector of values, the length of which is equal to the number of inputs specified.

However, when I use “dump” command to output “FxAreaDis”, it also reports error. So I got confused.

Could anyone explain the above sentence? Any help will be appreciated! Thanks for reading!

If your script says

compute C1 all reduce ave x

then C1 computes a scalar. Scalars don’t contain more than one value so asking for C1[1] isn’t valid.

If your script says

compute C2 all reduce ave x vx

then C2 computes a vector with two values: C2[1] holds the average of x and C2[2] holds the average of vx.

You haven’t shown us your dump command, so I can’t help you with that for now.

1 Like

Why not simply use the com() variable function?

1 Like

Thanks and you exactly point out the problem!

Just define FxAreaDis equal c_FxArea.

Yeah you are right. This is a more convenient way. Thanks for reminding me!