Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Add user level documentation for Expressions #11990

Merged
merged 104 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
8ea46bc
add build script nav menu custom build
sunethwarna Jan 24, 2024
2408cad
add expression util method docs
sunethwarna Jan 24, 2024
fd1d09f
add io docs
sunethwarna Jan 24, 2024
47e4072
ad general docs
sunethwarna Jan 24, 2024
51d281a
add menu infos
sunethwarna Jan 24, 2024
5a520df
update docs
sunethwarna Jan 25, 2024
65f1ba4
update IOs
sunethwarna Jan 27, 2024
1150805
add domain size exp io
sunethwarna Jan 27, 2024
b3fd523
update domain size exp io docs
sunethwarna Jan 27, 2024
02ae667
add expression arithmetics docs
sunethwarna Jan 27, 2024
57e86d3
add exp notes docs
sunethwarna Jan 27, 2024
f736e5c
add exp interface docs
sunethwarna Jan 27, 2024
fabd2a3
add abs docs
sunethwarna Jan 27, 2024
4e651c3
add Pow docs
sunethwarna Jan 27, 2024
1405537
add scale docs
sunethwarna Jan 27, 2024
1dce9d5
minor
sunethwarna Jan 27, 2024
b770f85
minor
sunethwarna Jan 27, 2024
e6873e4
add entity max docs
sunethwarna Jan 27, 2024
41b4dc0
add entity min docs
sunethwarna Jan 27, 2024
55df3d3
minor
sunethwarna Jan 27, 2024
2017d1a
fix for sets
sunethwarna Jan 27, 2024
5fd5dfa
minor
sunethwarna Jan 27, 2024
23e65bc
add scalar norm docs
sunethwarna Jan 27, 2024
617aed2
add collapse docs
sunethwarna Jan 27, 2024
b77b6cf
add comb docs
sunethwarna Jan 27, 2024
f9cb77d
add reshape docs
sunethwarna Jan 27, 2024
fc24f09
add slice docs
sunethwarna Jan 27, 2024
6de5f1a
Merge remote-tracking branch 'origin/master' into docs/expression_doc…
sunethwarna Feb 2, 2024
e7bc7f0
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
bb19329
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
025a2ae
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
ca6c0a7
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
8becb4b
Merge branch 'docs/expression_documentation' of https://github.com/Kr…
sunethwarna Feb 2, 2024
945983c
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
e984de2
Update docs/pages/Kratos/Expressions/General/Overview.md
sunethwarna Feb 2, 2024
a1c4ff3
Update docs/pages/Kratos/Expressions/General/Notes.md
sunethwarna Feb 2, 2024
71fa5db
Update docs/pages/Kratos/Expressions/General/Notes.md
sunethwarna Feb 2, 2024
630cbe8
Update docs/pages/Kratos/Expressions/General/Notes.md
sunethwarna Feb 2, 2024
25a767d
Update docs/pages/Kratos/Expressions/General/Expression_Types.md
sunethwarna Feb 2, 2024
0ea045f
Update docs/pages/Kratos/Expressions/General/Expression_Types.md
sunethwarna Feb 2, 2024
a076293
Update docs/pages/Kratos/Expressions/General/Expression_Types.md
sunethwarna Feb 2, 2024
2e43ac9
Update docs/pages/Kratos/Expressions/General/Expression_Types.md
sunethwarna Feb 2, 2024
a8f1bde
Update docs/pages/Kratos/Expressions/General/Expression_Types.md
sunethwarna Feb 2, 2024
33327ff
Update docs/pages/Kratos/Expressions/General/Expression_Arithmetics.md
sunethwarna Feb 2, 2024
50f52cb
Update docs/pages/Kratos/Expressions/General/Expression_Arithmetics.md
sunethwarna Feb 2, 2024
9b7d0b1
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
2e92fd8
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
8ee5530
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
396c2ad
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
dd57579
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
2c00817
Update docs/pages/Kratos/Expressions/General/Working_with_Numpy.md
sunethwarna Feb 2, 2024
90f3a75
Merge branch 'docs/expression_documentation' of https://github.com/Kr…
sunethwarna Feb 2, 2024
49060a5
minor
sunethwarna Feb 2, 2024
33f921f
add expected output
sunethwarna Feb 2, 2024
af0ebd6
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
dcb3acb
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
ccab6cf
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
8e58f61
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
9c465b9
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
a6b1f87
Update docs/pages/Kratos/Expressions/IOs/C_Array_Expression_IO.md
sunethwarna Feb 2, 2024
16c6488
Update docs/pages/Kratos/Expressions/IOs/Domain_Size_Expression_IO.md
sunethwarna Feb 2, 2024
d3b92b5
Update docs/pages/Kratos/Expressions/IOs/Domain_Size_Expression_IO.md
sunethwarna Feb 2, 2024
d7bd0fd
Update docs/pages/Kratos/Expressions/IOs/Integration_Point_Expression…
sunethwarna Feb 2, 2024
5131b41
Update docs/pages/Kratos/Expressions/IOs/Integration_Point_Expression…
sunethwarna Feb 2, 2024
5dd974a
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
4e537cf
Update docs/pages/Kratos/Expressions/IOs/Integration_Point_Expression…
sunethwarna Feb 2, 2024
172b51d
Update docs/pages/Kratos/Expressions/IOs/Literal_Expression_Input.md
sunethwarna Feb 2, 2024
03ac264
Update docs/pages/Kratos/Expressions/IOs/Literal_Expression_Input.md
sunethwarna Feb 2, 2024
0736590
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
5475531
Update docs/pages/Kratos/Expressions/IOs/Literal_Expression_Input.md
sunethwarna Feb 2, 2024
78b8aba
Update docs/pages/Kratos/Expressions/IOs/Literal_Expression_Input.md
sunethwarna Feb 2, 2024
19f5579
Update docs/pages/Kratos/Expressions/IOs/Nodal_Position_Expression_IO.md
sunethwarna Feb 2, 2024
4e60c14
Update docs/pages/Kratos/Expressions/IOs/Nodal_Position_Expression_IO.md
sunethwarna Feb 2, 2024
787c79c
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
0c1c235
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
75ca8d2
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
d51e94b
Update docs/pages/Kratos/Expressions/IOs/Variable_Expression_IO.md
sunethwarna Feb 2, 2024
486655a
Merge branch 'docs/expression_documentation' of https://github.com/Kr…
sunethwarna Feb 2, 2024
edd63e3
revert changes
sunethwarna Feb 2, 2024
f4065d1
add exp arithmetics exp output
sunethwarna Feb 2, 2024
b7de9eb
minor
sunethwarna Feb 2, 2024
9a32385
add expected output
sunethwarna Feb 2, 2024
156476e
Merge remote-tracking branch 'origin/master' into docs/expression_doc…
sunethwarna Feb 5, 2024
c5599c9
fix cpp output order
sunethwarna Feb 5, 2024
8988ac2
replace mathbb with mathbf
sunethwarna Feb 5, 2024
a7d7da1
minor
sunethwarna Feb 6, 2024
2821a6c
update abs
sunethwarna Feb 6, 2024
39f1f16
fix collapse
sunethwarna Feb 6, 2024
4956e08
fix comb
sunethwarna Feb 6, 2024
f9f5f1a
fix entity max
sunethwarna Feb 6, 2024
4dca0e6
fix entity min
sunethwarna Feb 6, 2024
b026471
fix entity sum
sunethwarna Feb 6, 2024
1ee6f76
fix inner product
sunethwarna Feb 6, 2024
e7e61da
fix inner product
sunethwarna Feb 6, 2024
cfc8b27
fix norm inf
sunethwarna Feb 6, 2024
23fe1ca
norm l2 fixing
sunethwarna Feb 6, 2024
578198d
fix p norm
sunethwarna Feb 6, 2024
da51ebb
minor
sunethwarna Feb 6, 2024
e0f0ef2
fix pow
sunethwarna Feb 6, 2024
6a8728c
fix reshape
sunethwarna Feb 6, 2024
996c089
fix scale
sunethwarna Feb 6, 2024
2f7b511
minor
sunethwarna Feb 6, 2024
b7248a5
fix bash with console
sunethwarna Feb 6, 2024
2d43e74
fix slice
sunethwarna Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/pages/Kratos/Documentation_Guide/How_Tos/Testing.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: Documentation testing and updating
keywords:
keywords:
tags: [Testing.md]
sidebar: documentation_guide
summary:
summary:
---


Expand Down
69 changes: 69 additions & 0 deletions docs/pages/Kratos/Expressions/General/Expression_Arithmetics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Expression Arithmetics
keywords:
tags: [expression, arithmetics]
sidebar: kratos_expressions
summary:
---

## Introduction

The ```Expressions``` of type ```NodalExpression```, ```ConditionExpression```, ```ElementExpression``` and their underlying ```Expression``` can be used in arithmetic formulations.

Following operations are supported:
* Addition ```+``` or ```+=```
* Subtraction ```-``` or ```-=```
* Multiplication ```*``` or ```*=```
* Division ```/``` or ```/=```
* Power ```**``` or ```**=```

These operations are carried out only when their results are required ([lazy evaluation](https://en.wikipedia.org/wiki/Lazy_evaluation)).

## Example usage
Here is an example for using ```NodalExpression``` (other expression types can be used in a similar fashion).
```python
import KratosMultiphysics as Kratos
model = Kratos.Model()
model_part = model.CreateModelPart("test")
node_1 = model_part.CreateNewNode(1, 0, 0, 0)
node_2 = model_part.CreateNewNode(2, 1, 0, 0)
node_3 = model_part.CreateNewNode(3, 1, 1, 0)

# setting VELOCITY of each node
node_1.SetValue(Kratos.VELOCITY, Kratos.Array3([1,2,3]))
node_2.SetValue(Kratos.VELOCITY, Kratos.Array3([4,5,6]))
node_3.SetValue(Kratos.VELOCITY, Kratos.Array3([7,8,9]))

# create nodal expression
nodal_expression = Kratos.Expression.NodalExpression(model_part)

# read non-historical nodal VELOCITY to expression
Kratos.Expression.VariableExpressionIO.Read(nodal_expression, Kratos.VELOCITY, False)

# now we can do arithmetics.
nodal_expression += 1 # Adds 1 to all the components of VELOCITY in all the entities
nodal_expression *= 2 # Multiplies by 2, the all the components of VELOCITY in all the entities
nodal_expression /= 3 # Divides by 2, the all the components of VELOCITY in all the entities
nodal_expression -= 0.5 # Subtract by 2, the all the components of VELOCITY in all the entities

# writing the result to the ACCELERATION variable
Kratos.Expression.VariableExpressionIO.Write(nodal_expression, Kratos.ACCELERATION, False)

for node in model_part.Nodes:
print(node.GetValue(Kratos.ACCELERATION))
```
Expected output:
```console
| / |
' / __| _` | __| _ \ __|
. \ | ( | | ( |\__ \
_|\_\_| \__,_|\__|\___/ ____/
Multi-Physics 9.4."3"-docs/expression_documentation-156476ea1c-Release-x86_64
Compiled for GNU/Linux and Python3.11 with GCC-13.2
Compiled with threading and MPI support.
Maximum number of threads: 30.
Running without MPI.
[3](0.833333,1.5,2.16667)
[3](2.83333,3.5,4.16667)
[3](4.83333,5.5,6.16667)
```
11 changes: 11 additions & 0 deletions docs/pages/Kratos/Expressions/General/Expression_Interface.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Expression Interface
keywords:
tags: [Expression_Interface.md]
sidebar: kratos_expressions
summary:
---

## Introduction

TODO: Link this page to the doxygen page once it is properly online.
87 changes: 87 additions & 0 deletions docs/pages/Kratos/Expressions/General/Expression_Types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
title: Expression Types
keywords:
tags: [Expression_Types.md]
sidebar: kratos_expressions
summary:
---

## Introduction

There are four types of expressions.
* `Expression`
* `NodalExpression`
* `ConditionExpression`
* `ElementExpression`

## `Expression`

`Expression` is the lowest level class that everything else is built on top of. It models an array of numeric values (scalars, vectors, matrices, etc.), supports arithmetic operations, but is not related to any container in Kratos. To create an `Expression` or write an existing one, you can use one of the derived classes of `ExpressionIO`.

TODO: add an example (@matekelemen) - requires exposing the `Input` and `Output` nested classes of `VariableExpressionIO` to python.


sunethwarna marked this conversation as resolved.
Show resolved Hide resolved
## Nodal Expression

Nodal expressions are used to store data related to nodal quantities. They may be historical or non-historical. Following code snippet illustrtes creating a nodal expression
and reading ```VELOCITY``` from non-historical nodal container.
```python
import KratosMultiphysics as Kratos
model = Kratos.Model()
model_part = model.CreateModelPart("test")
node_1 = model_part.CreateNewNode(1, 0.0, 0.0, 0.0)
node_2 = model_part.CreateNewNode(2, 0.0, 1.0, 0.0)
node_1.SetValue(Kratos.VELOCITY, Kratos.Array3([1,2,3]))
node_2.SetValue(Kratos.VELOCITY, Kratos.Array3([3,4,5]))

# now create the expression:
nodal_expression = Kratos.Expression.NodalExpression(model_part)

# now read the VELOCITY from the non-historical container
Kratos.Expression.VariableExpressionIO.Read(nodal_expression, Kratos.VELOCITY, False)
```

## Condition Expression

Condition expressions are used to store data related to condition quantities. Following code snippet illustrtes creating a condition expression
and reading ```VELOCITY``` from the conditions' container.
```python
import KratosMultiphysics as Kratos
model = Kratos.Model()
model_part = model.CreateModelPart("test")
node_1 = model_part.CreateNewNode(1, 0.0, 0.0, 0.0)
node_2 = model_part.CreateNewNode(2, 0.0, 1.0, 0.0)

prop = model_part.CreateNewProperties(1)
cond_1 = model_part.CreateNewCondition("LineCondition2D2N", 1, [1, 2], prop)
cond_1.SetValue(Kratos.VELOCITY, Kratos.Array3([1,2,3]))

# now create the expression:
condition_expression = Kratos.Expression.ConditionExpression(model_part)

# now read the VELOCITY from the condition data value container
Kratos.Expression.VariableExpressionIO.Read(condition_expression, Kratos.VELOCITY)
```

## Element Expression

Element expressions are used to store data related to element quantities. Following code snippet illustrtes creating a element expression
and reading ```VELOCITY``` from the elements' container.
```python
import KratosMultiphysics as Kratos
model = Kratos.Model()
model_part = model.CreateModelPart("test")
node_1 = model_part.CreateNewNode(1, 0.0, 0.0, 0.0)
node_2 = model_part.CreateNewNode(2, 0.0, 1.0, 0.0)
node_3 = model_part.CreateNewNode(3, 0.0, 1.0, 0.0)

prop = model_part.CreateNewProperties(1)
elem_1 = model_part.CreateNewElement("Element2D3N", 1, [1, 2, 3], prop)
elem_1.SetValue(Kratos.VELOCITY, Kratos.Array3([1,2,3]))

# now create the expression:
element_expression = Kratos.Expression.ElementExpression(model_part)

# now read the VELOCITY from the condition data value container
Kratos.Expression.VariableExpressionIO.Read(element_expression, Kratos.VELOCITY)
```
15 changes: 15 additions & 0 deletions docs/pages/Kratos/Expressions/General/Notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Notes
keywords:
tags: [notes, warnings, expression]
sidebar: kratos_expressions
summary:
---

## Introduction

The use of ```Expression```, ```NodalExpression```, ```ConditionExpression``` or ```ElementExpression``` should be done carefully for the following reasons.

1. If it is possible to implement the arithmetics or opertations you do in ```C++``` level, then it is encouraged.
2. Use arithmetic operators (`+=`, `-=`, `*=`, `/=`, `**=`, `+`, `-`, `*`, `/`) carefully. Operators keep their operands in memory, which means that if you chain operations for long enough, at some point you will run out of RAM. In such cases, `Kratos.Expression.Utils.Collpase` to forcefully evaluate an expression and discard the hierarchy of operands it stored. An example use case would be updating the same expression with new data in every time step of an analysis.
3. Expressions have significant memory and performance overhead due to dynamic polymorphism. If it becomes a bottleneck, move your calculations to `C++`.
50 changes: 50 additions & 0 deletions docs/pages/Kratos/Expressions/General/Overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: Overview
keywords:
tags: [overview.md]
sidebar: kratos_expressions
summary:
---
## Introduction

An expression represents a mechanism where a given mathematical expression is evaluated when actual value of that expression in required. One can compare these
expressions to lazy expressions (expression templates) in [Eigen](https://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html) or in [Boost](https://www.boost.org/doc/libs/1_82_0/doc/html/boost_yap/manual.html). The main difference being, Kratos expressions are **dynamic** lazy expressions (evaluated on demand, not automatically) whose type is evaluated at runtime, whereas Eigen and Boost has **static** lazy expressions whose type is known at compile-time. Few of the main advantages of these lazy expressions are:
* May reduce memory footprint.
* May reduce computational cost.

KratosMultiphysics stores its data in different containers depending on what kind of geometry that data is associated with (node, element, condition, etc.) and what that data represents. Each container requires a ```Kratos::Variable``` to access its underlying data. This requires defining the variable beforehand, and then using it to store and retrieve its value. Expressions allow users/developers to access and manipulate the data within these containers regardless of what `Kratos::Variable` they belong to. All expressions support **shared memory** and **distributed memory** parallelism. Though implemented in C++, expressions are meant to be used mainly in python.

## Supported data containers

The following data containers are supported:
* NodalExpression - data related to nodes (with historical or non-historical variables).
* ConditionExpression - data related to conditions.
* ElementExpression - data related to elements.

## Supported data types

Expressions are compatible with most main variable types in Kratos:

* ```int```
* ```double```
* ```array_1d<double, 3>```
* ```array_1d<double, 4>```
* ```array_1d<double, 6>```
* ```array_1d<double, 9>```
* ```Vector```
* ```Matrix```

## Use cases

Expressions are useful in following scenarios:
1. Transferring data between Kratos and other libraries - third-party libraries may be developed in other languages like Fortran as well.
2. Transferring data between Kratos and numpy/scipy.
3. Storing and fetching Kratos data in HDF5.
4. Non performance-critical intermediate calculations in python.
5. Transerring data back and forth within Kratos without variables such as in OptimizationApplication.
6. Quick and convenient prototyping of research code in python while using **shared memory** and **distributed memory** paralellism.





Loading
Loading