A First Vfe Program

As an example of a simple VfeTools application the following program computes the stiffness matrix for a 2D plane stress 3 node triangular element. The material model is assumed isotropic, linear elastic, with Young’s modulus and Poisson’s ratio of 1.0 and 0.3, respectively. The resulting stiffness matrix is printed to standard output.

C

A First Program - C Version

#include <stdio.h>
#include "sam/base/base.h"
#include "sam/vfe/vfe.h"

static Vdouble x[3][3] = {{0., 0., 0.}, {1., 0., 0.}, {0., 1., 0.}};

/*----------------------------------------------------------------------
                      Generate Stiffness Matrix for 3 Node Triangle
----------------------------------------------------------------------*/
int
main()
{
    Vint i, j;
    Vdouble prop[2];
    Vdouble kl[21];
    vfe_Solid2D* solid2d;
    vfe_MatlFun* matlfun;
    vfe_LinMat* linmat;

    /* Create material object and set properties */
    linmat = vfe_LinMatBegin();
    vfe_LinMatDef(linmat, SYS_MAT_ISOTROPIC);
    prop[0] = 1.0;
    prop[1] = .3;
    vfe_LinMatSetElasProp(linmat, prop);

    /* Create MatlFun object and load material functions */
    matlfun = vfe_MatlFunBegin();
    vfe_LinMatMatlFun(linmat, matlfun);

    /* Create 2D solid element formulation */
    solid2d = vfe_Solid2DBegin();
    vfe_Solid2DSetTopology(solid2d, SYS_SHAPETRI, 2, 0);

    /* Register material functions */
    vfe_Solid2DSetObject(solid2d, VFE_MATLFUN, matlfun);

    /* Form and print stiffness matrix */
    vfe_Solid2DStiff(solid2d, x, kl);
    printf("Lower triangle of stiffness matrix\n");
    for (i = 0; i < 6; i++) {
        for (j = 0; j <= i; j++) {
            printf("%12.4e ", kl[i * (i + 1) / 2 + j]);
        }
        printf("\n");
    }
    /* Delete objects */
    vfe_Solid2DEnd(solid2d);
    vfe_MatlFunEnd(matlfun);
    vfe_LinMatEnd(linmat);
    return 0;
}
C++

A First Program - C++ Version

The following program is a listing of the C++ version of the same “A First Program” listed above which used C language bindings.

#include <stdio.h>
#include "sam/base/base.h"
#include "sam/vfe/vfe.h"

static Vdouble x[3][3] = {{0., 0., 0.}, {1., 0., 0.}, {0., 1., 0.}};

/*----------------------------------------------------------------------
                      Generate Stiffness Matrix for 3 Node Triangle
----------------------------------------------------------------------*/
int
main()
{
    Vint i, j;
    Vdouble prop[2];
    Vdouble kl[21];
    vfe_Solid2D* solid2d;
    vfe_MatlFun* matlfun;
    vfe_LinMat* linmat;

    /* Create material object and set properties */
    linmat = new vfe_LinMat();
    linmat->Def(SYS_MAT_ISOTROPIC);
    prop[0] = 1.0;
    prop[1] = .3;
    linmat->SetElasProp(prop);

    /* Create MatlFun object and load material functions */
    matlfun = new vfe_MatlFun();
    linmat->MatlFun(matlfun);

    /* Create 2D solid element formulation */
    solid2d = new vfe_Solid2D();
    solid2d->SetTopology(SYS_SHAPETRI, 2, 0);

    /* Register material functions */
    solid2d->SetObject(VFE_MATLFUN, matlfun);

    /* Form and print stiffness matrix */
    solid2d->Stiff(x, kl);
    printf("Lower triangle of stiffness matrix\n");
    for (i = 0; i < 6; ++i) {
        for (j = 0; j <= i; ++j) {
            printf("%12.4e ", kl[i * (i + 1) / 2 + j]);
        }
        printf("\n");
    }
    /* Delete objects */
    delete solid2d;
    delete matlfun;
    delete linmat;
    return 0;
}
FORTRAN

A First Program - FORTRAN Version

The following program is a listing of the FORTRAN version of the same “A First Program” listed above which used C language bindings. Although the FORTRAN language does not distinguish between upper and lower case characters, the example retains the conventions employed in the C version for clarity. Note that the prefix vfe_ is replaced by vfef_.

C-----------------------------------------------------------------------
C                   Generate Stiffness Matrix for 3 Node Triangle
C-----------------------------------------------------------------------
      program main
      include 'base/fortran/base.inc'
      include 'vfe/fortran/vfe.inc'
      integer i,j
      double precision prop(2)
      double precision kl(21)
      double precision solid2d
      double precision matlfun
      double precision linmat
      double precision x(3,3)
      data x / 0.,0.,0., 1.,0.,0., 0.,1.,0. /

C                  Create material object and set properties
      call vfef_LinMatBegin (linmat)
      call vfef_LinMatDef (linmat,SYS_MAT_ISOTROPIC)
      prop(1) = 1.0
      prop(2) =  .3
      call vfef_LinMatSetElasProp (linmat,prop)

C                  Create MatlFun object and load material functions
      call vfef_MatlFunBegin (matlfun)
      call vfef_LinMatMatlFun (linmat, matlfun)

C                  Create 2D solid element formulation
      call vfef_Solid2DBegin (solid2d)
      call vfef_Solid2DSetTopology (solid2d,SYS_SHAPETRI,2,0)

C                  Register material functions
      call vfef_Solid2DSetObject (solid2d,VFE_MATLFUN,matlfun)

C                  Form and print stiffness matrix
      call vfef_Solid2DStiff (solid2d,x,kl)
      write(6,*) "Lower triangle of stiffness matrix"
      do i = 0,5
         write(6,1000) (kl(i*(i+1)/2+j+1),j=0,i)
      enddo
C                  Delete objects
      call vfef_Solid2DEnd (solid2d)
      call vfef_MatlFunEnd (matlfun)
      call vfef_LinMatEnd  (linmat)
C
1000  format(1x,6e12.4)
      stop
      end
C#

A First Program - C# Version

The following program is a listing of the C# version of the same “A First Program” listed above which used C language bindings. Note that the prefix vfe_ is replaced by vfe..

using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using DevTools;

public class Intro1
{

    public static double[] x = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0 };

    /*----------------------------------------------------------------------
                  Set environment path
    ----------------------------------------------------------------------*/
    public static void SetEnvironmentPath()
    {
        var pathVariableName = "PATH";
        var scope = EnvironmentVariableTarget.Process;
        var oldPathVariableValue = Environment.GetEnvironmentVariable(pathVariableName, scope);
        var newPathVariableValue = oldPathVariableValue + @";${CEE_SAM_EXTERNAL_LIBRARIES_BIN_PATHS}";
        Environment.SetEnvironmentVariable(pathVariableName, newPathVariableValue, scope);
    }

    /*----------------------------------------------------------------------
                          Generate Stiffness Matrix for 3 Node Triangle
    ----------------------------------------------------------------------*/
    public static void Main()
    {
        double[] prop = new double[2];
        double[] kl = new double[21];
        IntPtr solid2d;
        IntPtr matlfun;
        IntPtr linmat;

        SetEnvironmentPath();
        vsy.LicenseValidate(new StringBuilder(HOOPS_LICENSE.KEY));

        /* Create material object and set properties */
        linmat = vfe.LinMatBegin();
        vfe.LinMatDef(linmat, vsy.SYS_MAT_ISOTROPIC);
        prop[0] = 1.0;
        prop[1] = 0.3;
        vfe.LinMatSetElasProp(linmat, prop);

        /* Create MatlFun object and load material functions */
        matlfun = vfe.MatlFunBegin();
        vfe.LinMatMatlFun(linmat, matlfun);

        /* Create 2D solid element formulation */
        solid2d = vfe.Solid2DBegin();
        vfe.Solid2DSetTopology(solid2d, vsy.SYS_SHAPETRI, 2, 0);

        /* Register material functions */
        vfe.Solid2DSetObject(solid2d, vfe.VFE_MATLFUN, matlfun);

        /* Form and print stiffness matrix */
        vfe.Solid2DStiff(solid2d, x, kl);
        Console.Write("Lower triangle of stiffness matrix\n");
        int i;
        for (i = 0; i < 6; i++)
        {
            int j;
            for (j = 0; j <= i; j++)
            {
                Console.Write("{0} ", kl[i * (i + 1) / 2 + j]);
            }
            Console.Write("\n");
        }
        /* Delete objects */
        vfe.Solid2DEnd(solid2d);
        vfe.MatlFunEnd(matlfun);
        vfe.LinMatEnd(linmat);
    }
}

The output of this example program appears below. Only the lower half of the matrix is printed.

Lower triangle of stiffness matrix
7.4176e-01
3.5714e-01   7.4176e-01
-5.4945e-01  -1.6484e-01   5.4945e-01
-1.9231e-01  -1.9231e-01   0.0000e+00   1.9231e-01
-1.9231e-01  -1.9231e-01   0.0000e+00   1.9231e-01   1.9231e-01
-1.6484e-01  -5.4945e-01   1.6484e-01   0.0000e+00   0.0000e+00   5.4945e-01