MATC: if-elseif or select?

Numerical methods and mathematical models of Elmer
Post Reply
mzenker
Posts: 1999
Joined: 07 Dec 2009, 11:49
Location: Germany

MATC: if-elseif or select?

Post by mzenker »

Hi,

just one short question: Is there any such thing as a select, if-elseif or nested if-else control structure in MATC?
The manual says that there is only if-else, without mentioning a nesting possibility.

Thanks,

Matthias
NJank
Posts: 99
Joined: 05 Dec 2009, 00:05
Location: Baltimore, MD, USA

Re: MATC: if-elseif or select?

Post by NJank »

I believe there's only if-else, but you can nest them. I've used the following as an enthalpy definition, it seems to work as expected. (note most of those except tx are MATC variables defined eariler in the sif file, tx is the temperature variable).

Code: Select all

  Enthalpy = Variable Temperature
    MATC "if (tx<=T1) (cpvs*tx); else if (tx>=T2) ((tx-Tm)*cpvl+Tm*cpvs+Lh); else (P1*tx+P0);"
Also, in case anyone wonders, as an enthalpy formula this uses a simple linear equation for the final else state, which would be much more efficiently set just using a lookup table.

I'm not sure how line splitting works for MATC within a sif file, that would make it more legible for complex structures. someone else may want to comment on that.
mzenker
Posts: 1999
Joined: 07 Dec 2009, 11:49
Location: Germany

Re: MATC: if-elseif or select?

Post by mzenker »

Thank you!
kate.hruby
Posts: 4
Joined: 09 Feb 2018, 23:27
Antispam: Yes

Re: MATC: if-elseif or select?

Post by kate.hruby »

Hi,
I realize I'm resurfacing a pretty old thread, but this has been a huge help for me, so maybe there is still someone around who can answer this!

Within MATC if statements, is there a way to set greater than and less than bounds in the same expression?

Say my tx is a Variable Coordinate from 0 to 10, and I want a value applied from 2 to 8 (and a different value from 0-2 and 8-10).
I've tried: Real MATC "if (2 < tx < 8) 0.01; else 1.0"
And: Real MATC "if ((tx+2) < 8) 0.01; else 1.0"

But nothing seems to work. Is there something I can do with &, or some other creative way to go about it?

Thank you!
Kate
kevinarden
Posts: 2237
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: MATC: if-elseif or select?

Post by kevinarden »

check out MATC general flow control structures

in the MATC manual

http://www.nic.funet.fi/pub/sci/physics/elmer/doc/
kevinarden
Posts: 2237
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: MATC: if-elseif or select?

Post by kevinarden »

"Say my tx is a Variable Coordinate from 0 to 10, and I want a value applied from 2 to 8 (and a different value from 0-2 and 8-10).
I've tried: Real MATC "if (2 < tx < 8) 0.01; else 1.0"
And: Real MATC "if ((tx+2) < 8) 0.01; else 1.0"

In general you would have to nest several if statements that overwrite the values

If tx < 2 then tx = .01
if tx >= 2 then tx = 1.0
if tx < 8 then tx = .01
if tx >= 8 then tx = 1.0

basically you over write the variable with each if statement

Kevin
NJank
Posts: 99
Joined: 05 Dec 2009, 00:05
Location: Baltimore, MD, USA

Re: MATC: if-elseif or select?

Post by NJank »

What's the LUA equivalent construct?
kate.hruby
Posts: 4
Joined: 09 Feb 2018, 23:27
Antispam: Yes

Re: MATC: if-elseif or select?

Post by kate.hruby »

Rad, thanks!

The code that works is:

Real MATC "if (tx < 2) 1.0; if (tx > 8) 1.0; else 0.01"
Post Reply