Short and Long Name VBA Functions

From time to time I write VBA. One of the things I have needed is what is called the short path. If you ever worked at the command line, it looks like what is shown below. “PROGRA~1” is the short name for “Program Files”.

C:\>dir /x *

 Directory of C:\

01/20/2020  04:09 PM    <DIR>          PROGRA~1     Program Files
01/22/2020  02:05 AM    <DIR>          PROGRA~2     Program Files (x86)
11/24/2019  12:07 AM    <DIR>          SOLIDW~1     SOLIDWORKS Data
11/23/2019  10:26 PM    <DIR>                       Users
01/10/2020  07:41 AM    <DIR>                       Windows
               0 File(s)              0 bytes
              10 Dir(s)  347,556,667,392 bytes free

Short names can be useful when processing file names that are hard to parse. For instance, dumping a file list to a Comma Separated Values file. If one of the paths has a comma, that path will get separated into separate values. One way to deal with this is to just put the path in quotes like this “C:\my path\ with a, comma\folder”. The other way to deal with this just store the short path/name. Usually, when I have used the short name, I have needed it in Excel. Therefore, I have a couple of functions written VBA that I use. They are shown below. One of them returns the short name and the other returns the long name.

Option Explicit
 
' Max length of a long path
Private Const MAX_PATH = 32768
 
' Declarations hooking into Windows API
Private Declare Function GetLongPathNameW Lib "kernel32" (ByVal lpszShortPath As Long, ByVal lpszLongPath As Any, ByVal cchBuffer As Long) As Long
Private Declare Function GetShortPathNameW Lib "kernel32" (ByVal lpszLongPath As Long, ByVal lpszShortPath As Any, ByVal cchBuffer As Long) As Long
 
 
Public Function GetLongPathName(strShortPath As String) As String
    Dim strLongPath As String * MAX_PATH
    Dim lLongPathLength As Long
 
    ' Call WIndows API for long path in Unicode
    lLongPathLength = GetLongPathNameW(StrPtr(strShortPath), strLongPath, MAX_PATH)
 
    ' Handle Any errors before returning
    If lLongPathLength = 0 Then
            GetLongPathName = CVErr(xlErrValue)
    Else
        'Convert string and discard extra characters
        GetLongPathName = Left$(StrConv(strLongPath, vbFromUnicode), lLongPathLength)
    End If
End Function
 
 
Public Function GetShortName(ByVal strLongPath As String) As String
    Dim strShortPath As String * MAX_PATH
    Dim lShortPathLength As Long
 
    ' Call Windows API for short path in Unicode
    ' \\?\ must be prepended because this will allow for paths longer than 260 characters
    lShortPathLength = GetShortPathNameW(StrPtr("\\?\" & strLongPath), strShortPath, MAX_PATH)
 
    ' Handle Any errors before returning
    If lShortPathLength = 0 Then
        GetShortName = CVErr(xlErrValue)
    Else
        'Convert string and discard extra characters
        GetShortName = Mid$(StrConv(strShortPath, vbFromUnicode), 5, lShortPathLength - 4)
    End If
End Function

How to Export Your VBA Macros for Storage in a GIT/SVN Repository

This shows a quick workflow I use to handle VBA Macros so that they are diffed properly in a GIT/SVN repository.

Here is the example workbook.

Option Explicit
 Sub SaveCodeModules()
 'This code Exports all VBA modules
 Dim i As Integer
 Dim sName As String
 With ThisWorkbook.VBProject
     For i% = 1 To .VBComponents.Count
             sName = .VBComponents(i).CodeModule.Name
             .VBComponents(i).Export ThisWorkbook.Path & "\" & sName$ & ".vb"
     Next i
 End With
 End Sub

A Comparison Between Differential Equation Solver Suites

Wow! The below article is must read if you solve differential equations on a regular basis. Read it.

A Comparison Between Differential Equation Solver Suites In MATLAB, R, Julia, Python, C, Mathematica, Maple, and Fortran

The quick summary table below is outstanding and directly copied from the article linked above. PDF link for full screen view.

Comparison Of Differential Equation Solver Software

Comsol is Great

Definitely one of my favorite CAE programs is Comsol. I will tell you why. But first, some background. I have used a lot of CAE software, to make a short list:

  • agros2d
  • Comsol/Femlab
  • CalculiX
  • Elmer
  • Z88
  • Autodesk Simulation (formerly Algor)
  • ANSYS
  • Solidworks Simulation (CosmosWorks)
  • NX CAE, Nastran, Flow, etc.
  • Simerics Pumplinx
  • Code_Aster
  • Salome
  • OpenFoam

I am a power user of CAE. I have done a lot and thus seen a lot FEA and CFD. I am also a fan so I read up on the latest techniques such XFEM for crack propagation.

So, in looking at many different platforms, I can say that COMSOL is a very rich and modern platform. Every place you enter information, you can enter an expression. This is important because almost anything can be linked or made dependent on something else. For example, a viscosity that is locally pressure and temperature dependent can be implemented based a 2d lookup table in COMSOL. Another example is solving simple contact problems. Contact is where the loads are displacement dependent. Thus, if you can define the function that defines the force or pressure between two surfaces as a function of position, then you can solve a contact problem without having to use a searching algorithm between surfaces. This is useful for simple problems and may not be so useful in complicated problems where defining the displacement dependent forcing function is difficult.

COMSOL is a much better program than ANSYS even though ANSYS is the industry defacto standard for FEA. COMSOL is easier to use and more modern in the interface and coupling of Multiphysics. The couple between different physics is done via modules or input your own PDE. While entering your own PDE sounds daunting, you don’t have to do that. You can just mix and match the modules using expression to couple material, boundary conditions, mesh, and other phenomenon to get the desire results.

Final comments: The COMSOL User interface is a thing of beauty. The documentation is outstanding.

Here is some screen shots from solving the Flow Past a cylinder example.

Example of a Comsol expression for a time dependent velocity.

 

Velocity field in flow past a cylinder example.

Velocity Field Past a Cylinder

This one of the large expressions used for particle tracking in a fluid. See. It is an expression. A big one.

Simerics Pumplinx Hex Mesh Tutorial

Simerics Pumplinx sometimes doesn’t do a a good job in the help describing certain task such as Hex template meshing.  Here is some graphics that may help.

 

Layout of 4 point Hex Mesh in Simerics Pumplinx.

 

Mesh direction relationships. 1st direction points towards first point.
2nd direction point. etc.

 

Here are the files: Hex Mesh.zip

Six Myths of Polynomial Interpolation and Quadrature

I found a profound paper written by Professor Lloyd N. Trefethen entitled Six Myths of Polynomial Interpolation and Quadrature. This paper is profound because it goes against a lot of the mainstream things that I have learned related to polynomials. After watching Professor Trefethen’s lectures, I found that he is right and knows what he is talking about. Read the paper. It is worth your time.

The paper is here if it is ever removed from the link above.
mythspaper

Transmission Line Method

Transmission Line Method (TLM) or Transmission Line Matrix method is an elegant technique used to solve lossless or lossy wave equations. Here I am gathering together resources on the topic since I have found it is not well studied in the US.

Books

Christopoulos, Christos. “The transmission-line modeling (TLM) method in electromagnetics.” Synthesis Lectures on Computational Electromagnetics 1.1 (2005): 1-132. ISBN: 1598290509

This book is focused on the point of view of electrical engineering.

De Cogan, Donard. Transmission line matrix (TLM) techniques for diffusion applications. Gordon and Breach Science Publishers, 1998. ISBN: 978-9056991296

De Cogan, Donard, William J. O’Connor, and Susan Pulko. Transmission line matrix (TLM) in computational mechanics. CRC Press, 2005. ISBN: 978-0415327176

This is the book I found easiest to read from a Mechanical Engineering perspective. The book starts by looking at the wave equation in 1D. It uses the ideas of boundary conditions to explain the scattering effect that occurs in TLM. It is an easy read for an engineer who has studied the wave equation before.

Papers

Webpages

http://dandadec.com/what-is-tlm/ – This page is Donard De Cogan’s page. Donard’s website has lots of information on TLM such as conferences and papers.

Creating a Custom Header in WordPress

I used an image for header that I wanted to cite the photographer with a link to his website. There isn’t a clear way to do this in WordPress tools. Therefore, I searched and found that I could edit the header.php file for the theme I was using to customize the header.  The header.php file is located at ./wp-content/themes/myThemeName where myThemeName was twentytwelve. i.e.  ./wp-content/themes/twentytwelve/header.php.

I added this piece of code to header.php before the ending </header> tag:

Image by <a href=”http://15belowphoto.com/”>Nick Wooley</a>

Like this:

This piece of code has the effect that the header for my WordPress website has a link to the photographer’s website like below.

The downside to modifying header.php is that it gets overwritten when updates to the theme are made. Ugh! Make sure to document the modifications to header.php. Backing it up is undesirable because security updates may change the header.php code and thus restoring an older version may introduce security issues.

See the results of modifying header.php here:
http://www.teamvardo.com/

 

My Introduction to Computational Fluid Dynamics (CFD)

I have been doing a lot of CFD lately. I have quickly learned that the information one can gain from CFD is far more insightful to the physicals then 0D, lumped parameter equations.

One tool I have been using is Agros2d. It can do 2d axis symmetric and planar PDE problems. Agros2d can handle incompressible steady and unsteady CFD.

As an example, orifice problem has been studied many times. However, it usually isn’t studied from the point of view of an axis symmetric CFD.

Here is the model:Screenshot from 2016-05-07 00:50:44

The inlet boundary condition is a max velocity of 1.5m/s with a parabolic profile (1-(r/R)^2)*1.5m/s. R is the radius of the pipe 0.003m.

The Agros2d model is attached here: Orifice.a2d. You should be able to open the model with Agros2d and hit the “Solve” button in the lower left hand corner.

Below are some the results from the simulation.

Plot01

Velocity field near the beginning and throat.

Plot02

Velocity field just past the throat. Notice the re-circulation.

Velocity field at the outlet. Notice the recirculation

Velocity field at the outlet. Notice the recirculation

.

Velocity along the axis of the pipe (velocity in the z direction).

Velocity along the axis of the pipe (velocity in the z direction).

Velocity color plot at the throat. Notice that there is a dead space between the wall and the stream. The thinniest part of the stream is known as the vena contracta. See the next picture to see the velocity profile across the center of the throat.

Velocity color plot at the throat. Notice that there is a dead space between the wall and the stream. The thinnest part of the stream is known as the vena contracta. See the next picture to see the velocity profile across the center of the throat.

Plot07

This the velocity profile at the center of the throat. It is interesting to see the velocity is in a step shape. The velocity is constant in the stream and near zero otherwise.

Velocity profile at the start of throat where the fluid is accelerating.

Velocity profile at the start of throat where the fluid is accelerating.

Velocity profile at the end of the throat.

Velocity profile at the end of the throat.

Velocity profile at the outlet in the z direction which along the axis of the pipe. Notice the negative velocity part of the profile indicating recirculation.

Velocity profile at the outlet in the z direction which along the axis of the pipe. Notice the negative velocity part of the profile indicating recirculation.

Hopefully you enjoyed this as much as I did making it.