Expressions
Expressions in Quanscient Allsolve
Section titled “Expressions in Quanscient Allsolve”Quanscient Allsolve uses the power of expressions all around the application. You can define and use them for most inputs, wherever you see an icon. An expression can consist of numbers, matrices, simple arithmetics and predefined variables and functions among other things.
Some functions and variables are predefined as expressions for all projects but a project can have any amount of custom shared expressions.
Expression language
Section titled “Expression language”The expression language of Allsolve is a subset of Octave. In expressions, everything is a matrix of doubles. Even scalars are considered 1 x 1 matrices.
The supported features are listed below.
Numbers
Section titled “Numbers”Double precision number in any format, such as 41
, 8.91
, -0.91233
, +4.655e-23
.
Constants
Section titled “Constants”Basic mathematical constants are predefined: pi
, mu0
and epsilon0
. Constants can be used in place of numbers, like 2 * pow(pi,2)
.
Expression API
Section titled “Expression API”Basic math constants
Section titled “Basic math constants”The constant pi
Vacuum magnetic permeability
epsilon0
Section titled “epsilon0”Vacuum electric permittivity
Quanscient variables
Section titled “Quanscient variables”Time in a transient simulation
The x-coordinate of the geometry
The y-coordinate of the geometry
The z-coordinate of the geometry
Basic math functions
Section titled “Basic math functions”The sine function
- input: expression
The cosine function
- input: expression
The tangent function
- input: expression
The inverse sine function
- input: expression
The inverse cosine function
- input: expression
The inverse tangent function
- input: expression
The absolute value function
- input: expression
The square root function
- input: expression
The log function
- input: expression
The log10 function
- input: expression
The exponent function
- input: expression
The power function
-
base: expression
-
exponent: expression
Return an expression equal to base
to the power of exponent
The modulo function
-
input: expression
-
modval: float
- divisor value
Returns an expression equal to the remainder resulting from the division of input
by modval
Quanscient functions
Section titled “Quanscient functions”The dx function
- input: expression
Returns the x space derivative expression.
The dy function
- input: expression
Returns the y space derivative expression.
The dz function
- input: expression
Returns the z space derivative expression.
The dt function
- input: expression
Returns the first-order time derivative expression.
The dtdt function
- input: expression
Returns the second-order time derivative expression.
dtdtdt
Section titled “dtdtdt”The dtdtdt function
- input: expression
Returns the third-order time derivative expression.
dtdtdtdt
Section titled “dtdtdtdt”The dtdtdtdt function
- input: expression
Returns the fourth-order time derivative expression.
The max function
-
a: expression
-
b: expression
Returns the maximum value of given values. Can be used with 2 expressions, fields or parameters.
The min function
-
a: expression
-
b: expression
Returns the minimum value of given values. Can be used with 2 expressions, fields or parameters.
maxvalue
Section titled “maxvalue”The expression max function
-
region: Region
-
expression: expression
-
refinement: int
Returns the maximum value of a scalar expression on a region. For example maxvalue(reg.surface, compx(u), 5)
. Vector expressions needs to be converted to scalar with eg. norm
or compx
functions. The accuracy of the value of an expression can be improved by providing a higher refinement
value.
minvalue
Section titled “minvalue”The expression min function
-
region: Region
-
expression: expression
-
refinement: int
Returns the minimum value of a scalar expression on a region. For example minvalue(reg.surface, compx(u), 5)
. Vector expressions need to be converted to scalar with eg. norm
or compx
functions. The accuracy of the value of an expression can be improved by providing a higher refinement
value.
Probe the value of an expression on a point region
-
region: Region - A point region
-
expression: expression - Expression to evaluate at point.
interpolate
Section titled “interpolate”Interpolate the value of an expression on a region in given coordinates.
-
region: Region
-
expression: expression
-
coords: [float, float, float]
Interpolate the value of an expression on a region in given coordinates [x, y, z]
.
The expression must be scalar. Vector expressions needs to be converted to scalar with eg. norm
or compx
functions.
ifpositive
Section titled “ifpositive”Return separate values based on compared value.
-
condExpr: expression
-
trueExpr: expression
-
falseExpr: expression
Returns a conditional expression. The expression value is trueExpr
for all evaluation points where condExpr
is larger or equal to zero. Otherwise, its value is falseExpr
.
Example
At points, where x + y >= 0, value is 1, otherwise -1.
ifpositive(x+y, 1, -1)
The comp function
-
component: int
-
input: expression
Get the n-th component of a column vector expression or the n-th row of a matrix expression.
The compx function
- input: expression
Get the x component of a column vector or the first row of a matrix expression. Equivalent to comp(0, input)
The compy function
- input: expression
Get the y component of a column vector or the second row of a matrix expression. Equivalent to comp(1, input)
The compz function
- input: expression
Get the z component of a column vector or the third row of a matrix expression. Equivalent to comp(2, input)
transpose
Section titled “transpose”The transpose function
- input: expression
Returns an expression that is the transpose of the input vector or matrix expression
inverse
Section titled “inverse”The matrix inverse function
Returns the inverse of a square matrix.
determinant
Section titled “determinant”The matrix determinant function
- input: expression
Returns the determinant of a square matrix.
The gradient function
- input: expression
For a scalar input expression, this is mathematically treated as the gradient of a scalar () and the output is a column vector with one entry per space derivative. For a vector input expression, this is mathematically treated as the gradient of a vector () and the output has one row per component of the input and one column per space derivative.
The divergence function
- input: expression
Computes the divergence of a vector expression. The returned expression is a scalar.
The curl function
- input: expression
Computes the curl of a vector expression. The returned expression is a vector.
The norm function
- input: expression
gives the norm of an expression input.
crossproduct
Section titled “crossproduct”The cross product function
-
a: expression
-
b: expression
Computes the cross-product of two vector expressions. The returned expression is a vector.
doubledotproduct
Section titled “doubledotproduct”The double dot product function
-
a: expression
-
b: expression
Computes the double-dot product of two matrix expressions. The returned expression is a scalar.
Example
doubledotproduct([1, 2; 3, 4], [11, 12; 13, 14])
is resolved to 130
cn(n) is a shortcut for cos(2 * pi * f * n * t)
- n: float - fundamental frequency multiplier
sn(n) is a shortcut for sin(2 * pi * f * n * t)
- n: float - fundamental frequency multiplier
getharmonic
Section titled “getharmonic”Returns the harmnum harmonic of the expression
-
harmnum: int
-
input: expression
harm(n, expression) returns the nth harmonic of the expression
-
harmnum: int
-
input: expression
makeharmonic
Section titled “makeharmonic”makeharmonic creates a harmonic expression
-
harmonicNumbers: List[int]
-
expr: expression
Create harmonic expression from given harmonic indices and an expression.
The on function
-
physreg: Region
-
expr: expression
This function allows to use fields, unknown dof fields or general expressions across physical regions with possibly non-matching meshes by evaluating the expression argument using a (x, y, z) coordinate interpolation.
Integrate to get the total current (flux) through a surface: integrate(reg.integration_surf, on(reg.integration_vol, transpose(j)) * normal(reg.integration_vol), 5)
normal
Section titled “normal”Returns the normal of a region
- physreg: Region - region with project dimension
Given a region of project dimension, returns the normal pointing out of the region. For example in a 3D project, the region must be a volume. For 2D, a surface.
Integrate to get the total current (flux) through a surface: integrate(reg.integration_surf, on(reg.integration_vol, transpose(j)) * normal(reg.integration_vol), 5)
linspace
Section titled “linspace”Produces a row vector of length numValues
with linearly spaced values from start
to end
-
start: float
-
end: float
-
numValues: int
linspace produces a row vector of length numValues
with linearly spaced values from start
to end
. Can be used to create override vectors for sweeps.
linspace(1, 3, 5)
is resolved to [1, 1.5, 2, 2.5, 3]
logspace
Section titled “logspace”Produces a row vector of length numValues
with logarithmically spaced values from 10^(start)
to 10^(end)
, with default base
10
-
start: float
-
end: float
-
numValues: int
-
base: int
logspace produces a row vector of length numValues
with logarithmically spaced values from 10^(start)
to 10^(end)
, with default base
10. If end
is pi, values go from 10^(start)
to pi
instead. Can be used to create override vectors for sweeps.
logspace(1, 3, 5)
is resolved to [10, 31.623, 100, 316.228, 1000]
repelem
Section titled “repelem”Repeats elements of a vector numRepeat
times
-
vec: vector
-
numRepeat: int
Repeats elements of a vector numRepeat
times. Can be used to create override vectors for multivariate sweeps so that numRepeat
varies based on other vector lengths.
repelem([1, 2, 3], 3)
is resolved to [1, 1, 1, 2, 2, 2, 3, 3, 3]
repmat
Section titled “repmat”Repeats matrix rowRepeat
times in row direction and colRepeat
times in column direction
-
mat: matrix
-
rowRepeat: int
-
colRepeat: int
Repeats matrix rowRepeat
times in row direction and colRepeat
times in column direction. Can be used to create override vectors for multivariate sweeps so that rowRepeat
is always 1 and colRepeat
varies based on other vector lengths.
repmat([1, 2, 3], 1, 3)
is resolved to [1, 2, 3, 1, 2, 3, 1, 2, 3]
getcoords
Section titled “getcoords”Get a coordinate of a given point region
- region: Region - A point region
Get the full coordinate vector from a given point region. For example getcoords(reg.point_region)
getcoordscomp
Section titled “getcoordscomp”Get a coordinate component of a given point region
-
region: Region - A point region
-
comp: int - The coordinate component. 0 for x, 2 for z component
Get a specific single axis coordinate from a given point region. For example get y-coordinate: getcoordscomp(reg.point_region, 1)
integrate
Section titled “integrate”Integrates an expression over a region
-
region: Region
-
expr: expression
-
order: int
Integrates an expression over the physical region region
.
Examples
Calculate volume/area/length: integrate(region, 1, 3)
.
Integrate to get the total current (flux) through a surface: integrate(reg.integration_surf, on(reg.integration_vol, transpose(j)) * normal(reg.integration_vol), 5)
wavelet
Section titled “wavelet”Create a wavelet with given frequency and delay
-
frequency: expression
-
delay: expression
Create a ramp signal
-
rampuptime: expression - The time to transition linearly from value 0 to 1
-
holdtime: expression - The time the value stays at 1
-
rampdowntime: expression - The time to transition linearly from value 1 to 0
-
delay: expression - The time at which the rampup starts
It is a signal that starts initially at value 0. Then at 0 seconds + delay
in seconds it transitions from value 0 to 1 in a linear increase. That transition happens in rampuptime
seconds. This is followed by a flat value 1 that is held for holdtime
seconds. This is followed by a linear decrease from value 1 to 0 that happens in rampdowntime
seconds. The value is then forever 0 after that.
vonmises
Section titled “vonmises”The von Mises function
- stress: expression
This returns the von Mises stress expression corresponding to the 3D stress tensor provided as argument. The stress tensor should be provided in Voigt form .
For 2D plane stress problems all related components of the stress tensor are . For plane strain problems do not forget the term .
neumann
Section titled “neumann”The neumann function
-
bndreg: Region
-
calcreg: Region
-
fld: field
Returns the Neumann term on the boundary bndreg
associated with the domain calcreg
and the field fld
.
The term can be integrated over the boundary, e.g. integrate(reg.boundary, neumann(reg.boundary, reg.volume, T), 5)
.
If the field is a vector field, the integration has to be done per component, like integrate(reg.boundary, compx(neumann(reg.boundary, reg.volume, u)), 5)
.
If bndreg
is at an interface region, the calcreg
and fld
together define the correct term to return. For more information, see script API equivalent
lineinterpolate
Section titled “lineinterpolate”Interpolates the expression at a series of points along a line.
-
region: Region
-
expression: expression
-
firstCoords: [float, float, float]
-
lastCoords: [float, float, float]
-
numSamples: int
This method interpolates the expression at a series of points along a line inside a region
. The line for interpolation is defined by a starting and an end point whose [x,y,z] coordinates are provided in the firstCoords
and lastCoords
arguments.
The numSamples
argument determines the number of sample points considered along the line.
If a requested interpolation point along the line cannot be found (because it is outside of region
or because the interpolation algorithm fails to converge, as can happen on curved 3D elements) then an error occurs.
To be able to plot the values of nicely, another output should be created that contains the coordinate points. For example outputs:
interpolatedValues = lineinterpolate(reg.box, [0, 0, 0], [0, 100, 0], 11)
yCoords = linspace(0, 100, 11)
Get the magnitude of the Z = V/I complex impedance.
-
V: expression - Voltage
-
I: expression - Current
For example with a lumped port lump
: absZ(lump.V, lump.I)
Get the real part of the Z = V/I complex impedance.
-
V: expression - Voltage
-
I: expression - Current
For example with a lumped port lump
: realZ(lump.V, lump.I)
Get the imaginary part of the Z = V/I complex impedance.
-
V: expression - Voltage
-
I: expression - Current
For example with a lumped port lump
: imagZ(lump.V, lump.I)
Get the phase of the Z = V/I complex impedance.
-
V: expression - Voltage
-
I: expression - Current
For example with a lumped port lump
: argZ(lump.V, lump.I)