Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

lazy evaluation in expressions

Please login with a confirmed email address before reporting spam

Is there way to use lazy evaluation in Comsol?

For example, I would like to define a sigmoid function which consists of a position x and a transition length dx. In c, I would define this:

double sigmoid(double x, double dx) {
return ((x > 16 * dx) ? 1 : (x < -16 * dx) ? 0 : (x == 0) ? 0.5 : 1 / (1 - exp(-x / dx));
}

If this is called with dx = 0, no problem. Similarly if it's called with x = -1000 and dx = 1e-6, no problem.

5 Replies Last Post Sep 16, 2016, 3:08 p.m. EDT
Jeff Hiller COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 30, 2016, 2:37 p.m. EDT
You can use an expression involving booleans in COMSOL to achieve this, or, if you prefer/need smooth transitions instead of sharp jumps you can use smoothed step functions instead of booleans, etc. See the Reference Manual chapter on expressions, functions and operators (Page 221 and following if my memory serves me right).
Best,
Jeff
You can use an expression involving booleans in COMSOL to achieve this, or, if you prefer/need smooth transitions instead of sharp jumps you can use smoothed step functions instead of booleans, etc. See the Reference Manual chapter on expressions, functions and operators (Page 221 and following if my memory serves me right). Best, Jeff

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 30, 2016, 3:04 p.m. EDT
Thanks! I used a ramp function instead. The point is to avoid discontinuities in force versus velocity, as I'm using a term dependent on the sign of the x-component of velocity to implement an inelastic collision with a given coefficient of restitution: if the contact force when recoiling is some fraction of the contact force when colliding then the energy loss will be proportional to the fractional difference. But the exact shape doesn't matter, and continuity beyond D1 shouldn't matter. In any case this works and there's no worry about singularities.

For example, the following evaluates with lazy evaluation but not without:

(1 == 2) && (1 / 0)

But I don't think the following will evaluate:

(1 == 2) * (1 / 0)

In any case, I'm not sure the booleans yield lazy evaluation (don't evaluate expression unless it's needed) as is done with the ?: operator in most programming languages. But I don't need it now.

Thanks! I used a ramp function instead. The point is to avoid discontinuities in force versus velocity, as I'm using a term dependent on the sign of the x-component of velocity to implement an inelastic collision with a given coefficient of restitution: if the contact force when recoiling is some fraction of the contact force when colliding then the energy loss will be proportional to the fractional difference. But the exact shape doesn't matter, and continuity beyond D1 shouldn't matter. In any case this works and there's no worry about singularities. For example, the following evaluates with lazy evaluation but not without: (1 == 2) && (1 / 0) But I don't think the following will evaluate: (1 == 2) * (1 / 0) In any case, I'm not sure the booleans yield lazy evaluation (don't evaluate expression unless it's needed) as is done with the ?: operator in most programming languages. But I don't need it now.

Jeff Hiller COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 30, 2016, 4:37 p.m. EDT
Hi Daniel,
I was more thinking of using the if(,,) operator, which I think should give you the lazy evaluation you are after. Sorry about the vague/confusing reference to booleans.
Jeff
Hi Daniel, I was more thinking of using the if(,,) operator, which I think should give you the lazy evaluation you are after. Sorry about the vague/confusing reference to booleans. Jeff

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Jun 30, 2016, 8:38 p.m. EDT
Thanks! BTW the velocity dependence on contact force is absolutely killing convergence so I either need to smooth is further or try another way to attain inelastic collisions.
Thanks! BTW the velocity dependence on contact force is absolutely killing convergence so I either need to smooth is further or try another way to attain inelastic collisions.

Please login with a confirmed email address before reporting spam

Posted: 8 years ago Sep 16, 2016, 3:08 p.m. EDT
Very delayed follow-up: I changed my contact equation to something simpler:

Fcontact = u(x−x0) (x0 − x) [1 − η(1 − fvx )] Kcontact,

where η is an inelasticity coefficient, u(x−x0) is a heavy side function which was implemented using the "if" function in Comsol, fvx is a sigmoid function which goes from 0 for negative vx to 1 for positive vx, vx being the velocity in the x direction.

This works very well, and the "if" function appears to work as expected. The convergence issues are much-reduced: previously I was using a power-law dependence on x−x0, which was too nonlinear.
Very delayed follow-up: I changed my contact equation to something simpler: Fcontact = u(x−x0) (x0 − x) [1 − η(1 − fvx )] Kcontact, where η is an inelasticity coefficient, u(x−x0) is a heavy side function which was implemented using the "if" function in Comsol, fvx is a sigmoid function which goes from 0 for negative vx to 1 for positive vx, vx being the velocity in the x direction. This works very well, and the "if" function appears to work as expected. The convergence issues are much-reduced: previously I was using a power-law dependence on x−x0, which was too nonlinear.

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.