Help with if condition

Hi all,

I am very new with lua and my question could be stupid, but I don’t know how to solve my problem.

Basically I use lua language to givo special commands to another code for ion beam transport.

I have created a code to produce particles with an exponential energy distribution and it works fine.

What I want to do now is to add a cut-off to avoid that particle with energy aboove a certain value will be produced but I am not able to do that.

The code I use is:

&nbsp; -- Generate Exponential distributed random variable with parameter lambda. local rand = simion.rand local ln = math.log function exponential\_rand() &nbsp; return -ln(rand())/0.44 end ------------------------ &nbsp; -- if&nbsp; exponential\_rand()\*1e6 \< 8.9\*1e6 then &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function my\_dist() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return exponential\_rand()\*1e6&nbsp; --\< 8.9\*1e6 -- end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end ----------------------- local t = {coordinates=0} for i=1,2000 do &nbsp; local ke = my\_dist() &nbsp; t[#t+1] = standard\_beam { &nbsp;&nbsp;&nbsp; n = 10, &nbsp;&nbsp;&nbsp; tob = 0, &nbsp;&nbsp;&nbsp; mass = 1.00727647, &nbsp;&nbsp;&nbsp; charge = 1, &nbsp;&nbsp;&nbsp; ke = ke, &nbsp;&nbsp;&nbsp; cwf = 1, &nbsp;&nbsp;&nbsp; color = 0, &nbsp;&nbsp;&nbsp; direction = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cone\_direction\_distribution { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; axis = vector(0, 0, 1), &nbsp; &nbsp;&nbsp; half\_angle = - 6.188e-9\*(ke\*1e-6)^6 +2.155e-6\*(ke\*1e-6)^5 -0.0002876\*(ke\*1e-6)^4 + 0.01799\*(ke\*1e-6)^3 -0.497\*(ke\*1e-6)^2 + 3.215\*(ke\*1e-6)+ 69.44, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fill = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }, &nbsp;&nbsp;&nbsp; position = gaussian3d\_distribution { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mean = vector(0, 0, 0), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stdev = vector(0.06, 0.06, 0) &nbsp;&nbsp;&nbsp; } &nbsp; } end particles(t) &nbsp;

I try to add the if condition in the second block of the code, but it has no effect, can you help me with that?

Thak you in advance

Hey schillacif,

don’t know if you solved the problem already, but I think the solution is rather trivial.

I assume the function simion.rand returns a random number?

If yes, the problem is, that you are not returning the random number that you are checking for.

Additionally the function “my_dist” is set globally, which isn’t great idea in most cases.

Might try something like this:

local rand = simion.rand local ln = math.log local multiplicator = 1e6 local cutOff = 8.9 local function exponential\_rand() return -ln(rand())/0.44 end local function my\_dist() local randomNumber = exponential\_rand() if randomNumber \<= cutOff then return randomNumber\*multiplicator else return cutOff\*multiplicator end end local t = {coordinates=0} for i=1,2000 do local ke = my\_dist() t[#t+1] = standard\_beam { n = 10, tob = 0, mass = 1.00727647, charge = 1, ke = ke, cwf = 1, color = 0, direction = cone\_direction\_distribution { axis = vector(0, 0, 1), half\_angle = - 6.188e-9\*(ke\*1e-6)^6 +2.155e-6\*(ke\*1e-6)^5 -0.0002876\*(ke\*1e-6)^4 + 0.01799\*(ke\*1e-6)^3 -0.497\*(ke\*1e-6)^2 + 3.215\*(ke\*1e-6)+ 69.44, fill = true }, position = gaussian3d\_distribution { mean = vector(0, 0, 0), stdev = vector(0.06, 0.06, 0) } } end particles(t)

Hey schillacif,

don’t know if you solved the problem already, but I think the solution is rather trivial.

I assume the function simion.rand returns a random number?

If yes, the problem is, that you are not returning the random number that you are checking for.

Additionally the function “my_dist” is set globally, which isn’t great idea in most cases.

Might try something like this:

local rand = simion.rand local ln = math.log local multiplicator = 1e6 local cutOff = 8.9 local function exponential\_rand() return -ln(rand())/0.44 end local function my\_dist() local randomNumber = exponential\_rand() if randomNumber \<= cutOff then return randomNumber\*multiplicator else return cutOff\*multiplicator end end local t = {coordinates=0} for i=1,2000 do local ke = my\_dist() t[#t+1] = standard\_beam { n = 10, tob = 0, mass = 1.00727647, charge = 1, ke = ke, cwf = 1, color = 0, direction = cone\_direction\_distribution { axis = vector(0, 0, 1), half\_angle = - 6.188e-9\*(ke\*1e-6)^6 +2.155e-6\*(ke\*1e-6)^5 -0.0002876\*(ke\*1e-6)^4 + 0.01799\*(ke\*1e-6)^3 -0.497\*(ke\*1e-6)^2 + 3.215\*(ke\*1e-6)+ 69.44, fill = true }, position = gaussian3d\_distribution { mean = vector(0, 0, 0), stdev = vector(0.06, 0.06, 0) } } end particles(t)