{"id":1966,"date":"2021-01-04T09:09:47","date_gmt":"2021-01-04T07:09:47","guid":{"rendered":"https:\/\/www.albertopassalacqua.com\/?p=1966"},"modified":"2021-01-05T20:43:12","modified_gmt":"2021-01-05T18:43:12","slug":"dimensional-units-in-openfoam","status":"publish","type":"post","link":"https:\/\/www.albertopassalacqua.com\/?p=1966","title":{"rendered":"P. 1. Dimensional units in OpenFOAM"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p style=\"text-align: justify;\">One of the first OpenFOAM<sup>\u00ae<\/sup> features users encounter when they start learning how to set simulations up is that the code verifies the dimensional consistency of the variables involved in operations.<\/p>\n<p>While this verification can be disabled, and is performed only when using dimensioned data types, it is a very convenient feature and helps preventing incorrect operations when programming new features.<\/p>\n<p style=\"text-align: justify;\">Probably the simplest example of dimensioned type is the <code>dimensionedScalar<\/code>. The origin of the name is straightforward:<\/p>\n<ul>\n<li style=\"text-align: justify;\">a <code>scalar<\/code> type in OpenFOAM is either a C++ <code>float<\/code> or a <code>double<\/code>, depending on the options with which the OpenFOAM source code has been compiled.<\/li>\n<li style=\"text-align: justify;\">dimensioned indicates a <code>dimensionSet<\/code> is associated to the scalar<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">A <code>dimensionSet<\/code> is what defines the dimensions of the data type it is associated to, by specifying the exponents of the fundamental units of the unit system of choice.<\/p>\n<\/div><\/div>\n\n\n\n<p style=\"text-align: justify;\">Such fundamental units are:<\/p>\n<ol>\n<li>Mass<\/li>\n<li>Length<\/li>\n<li>Time<\/li>\n<li>Temperature<\/li>\n<li>Amount of substance\u00a0<\/li>\n<li>Electric current\u00a0<\/li>\n<li>Luminous intensity<\/li>\n<\/ol>\n<p style=\"text-align: justify;\">Consequently, a dimensionedScalar used to store a velocity magnitude measured in m s<sup>-1<\/sup> is represented as:<\/p>\n<p><code><span class=\"cmtt-12\">[0<\/span><span class=\"cmtt-12\"> 1 <\/span><span class=\"cmtt-12\">-1<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00]<\/span><\/code><\/p>\n<p>Note that square brackets are used to represent a <code>dimensionSet<\/code> in input files. Consequently, a density ? will be specified as:<\/p>\n<p><code>rho\u00a0 \u00a0 rho [1 -3 0 0 0 0 0] 1000;<\/code><\/p>\n<p>and a kinematic viscosity as:<\/p>\n<p><code><span class=\"cmtt-12\">nu\u00a0 \u00a0 <\/span><span class=\"cmtt-12\">nu<\/span><span class=\"cmtt-12\">\u00a0<\/span><span class=\"cmtt-12\">\u00a0[0<\/span><span class=\"cmtt-12\">\u00a02<\/span><span class=\"cmtt-12\">\u00a0-1<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00<\/span><span class=\"cmtt-12\">\u00a00]<\/span><span class=\"cmtt-12\">\u00a0<\/span><span class=\"cmtt-12\"> 0.1;<\/span><\/code><\/p>\n<p style=\"text-align: justify;\">as it can be seen in many of the tutorials for incompressible flows.<\/p>\n<p style=\"text-align: justify;\">Using dimension sets adds some caveats when implementing new features. First, in the code a dimensionSet object is constructed simply specifying the tuple of exponents:<\/p>\n<p><code>dimensionSet(<span class=\"cmtt-12\">0, <\/span><span class=\"cmtt-12\">2, <\/span><span class=\"cmtt-12\">-1,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0)<\/span><\/code><\/p>\n<p style=\"text-align: justify;\">This syntax becomes particularly useful when defining dimensioned constants, which will be created as follow:<\/p>\n<p><code>dimensionedScalar nu0(\"nu0\", dimensionSet(<span class=\"cmtt-12\">0, <\/span><span class=\"cmtt-12\">2, <\/span><span class=\"cmtt-12\">-1,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0), 0.1);<\/span><\/code><\/p>\n<p>Similarly, dimensionSet can be used to construct vectors, tensors and fields in the code.\u00a0<\/p>\n<p style=\"text-align: justify;\">Finally, one aspect to be considered concerns working with fields. When operating with elements of a<code> volScalarField<\/code>, <code>volVectorField<\/code> and <code>volTensorField<\/code> directly by accessing the list element, verification of dimensional units is not performed. Consequently, if in the code the programmer works on a <code>volScalarField<\/code> as follows:<\/p>\n<p><code>a[i] = ...;<\/code><\/p>\n<p style=\"text-align: justify;\">the assignment will only involve the value of the element of index i, and no dimensional verification will be performed. If a dimensioned type appears on the right-hand side of the assignment operator, the <code>.value()<\/code> method will need to be invoked:<\/p>\n<p><code>dimensionedScalar nu0(\"nu0\", dimensionSet(<span class=\"cmtt-12\">0, <\/span><span class=\"cmtt-12\">2, <\/span><span class=\"cmtt-12\">-1,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0,<\/span><span class=\"cmtt-12\"> 0), 0.1);<\/span><\/code><\/p>\n<p><code>a[i] = nu0.value()*...;<\/code><\/p>\n<p style=\"text-align: justify;\">To conclude, the <code>dimensionSet<\/code> of a dimensioned type <code>a<\/code> can be retrieved using the <code>.dimensions()<\/code> method:<\/p>\n<p><code>dimensionSet myDimensionSet(a.dimensions());<\/code><\/p>\n<p style=\"text-align: justify;\">For more technical details about the dimensionSet object, see the official documentation <a href=\"https:\/\/www.openfoam.com\/documentation\/guides\/latest\/api\/classFoam_1_1dimensionSet.html\">here<\/a>.<\/p>\n\n\n\n<p style=\"text-align: justify;\"><em>This offering is not approved or endorsed by OpenCFD Limited, the producer of the OpenFOAM software and owner of the OPENFOAM\u00ae and OpenCFD\u00ae trade marks. Alberto Passalacqua is not associated to OpenCFD Ltd.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the first OpenFOAM features users encounter when they start learning how to set simulations up is that the code verifies the dimensional consistency of the variables involved in operations. Some details about this feature will be discussed in this post.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,125,132,6],"tags":[],"class_list":["post-1966","post","type-post","status-publish","format-standard","hentry","category-cfd","category-education","category-free-software","category-openfoam"],"_links":{"self":[{"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/posts\/1966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1966"}],"version-history":[{"count":11,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/posts\/1966\/revisions"}],"predecessor-version":[{"id":1986,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=\/wp\/v2\/posts\/1966\/revisions\/1986"}],"wp:attachment":[{"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.albertopassalacqua.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}