# Patran Neutral Mesh File Writer

A few years back, while learning to code in a new in-house Finite element software, I had to display some results in the GUI. And the GUI didn’t allow any GUI display unless a mesh or model was loaded.

With no FE model available, I hacked it by creating a on the fly one 8 noded, single element brick mesh and tricked the software to get my results displayed.

What I did was write a simple patran neutral mesh file. Over the years I have built on that idea and here’s a fortran module that can be used to generate a patran neural mesh file given a set of elements and node coordinates.

```
MODULE PNFW
!
! Patran Neutral Mesh File Writer
! by Sukhbinder Singh
! last update 12 April 2011
!
contains
subroutine getcube(nodes,elements,itype,meshfile,title)
REAL*8 nodes(:,:)
INTEGER elements(:,:),itype
character(len=80),optional :: meshfile,title
character*8 :: fmtstr
!
! to do
!
if (.NOT. PRESENT(meshfile)) then
meshfile="meshfile.mesh"
end if
!
if (.NOT. PRESENT(title)) then
title="Mesh generated by PNFW"
end if
!
! Open the file
open(04,file=	meshfile)
!
! Get the number of nodes and elemenst from input
nnode=size(nodes,2)
nelem=size(elements,2)
!
! Write the initial info
write(04,45)
write(04,*)trim(title)
write(04,65)nnode,nelem
write(04,75)
!
nn=1
!
! Process all nodes
do ii=1,nnode
write(04,15)nn
write(04,25)nodes(1,ii),nodes(2,ii),nodes(3,ii)
write(04,35)
nn=nn+1
end do
!
! Process all elements
fmtstr='(    i8)'
write(fmtstr(2:5),'(i3)')itype
nel=1
do jj=1,nelem
write(04,85)nel,itype
write(04,95)itype
write(04,fmtstr) (elements(k,jj),k=1,itype)
nel=nel+1
end do
!
! close the mesh file
write(04,110)
close(04)
!
write(*,*) trim(meshfile), ' file written!'
!
!  Format statements
!
! For Nodes
15   format(' 1',i7,'       0       2       0       0       0       0       0')
25   format(3(1x,e15.7))
35   format('1G       6       0       0  000000')
!
! Title Card
45   format('25',7x,2('0',7x),'1',5(7x,'0'))
55	  format('a')
!
! Summary Data
65   format('26',7x,2('0',7x),'1',i7,i7,2(7x,'0'),'     -1')
75	  format('Date')
!
! For elements
85	  format(' 2',i7,i7,'       2',5('       0'))
95	  format(i7,'       0       0       0 0.000000000E+00 0.000000000E+00 0.000000000E+00')
!
! Last card
110  format( '99       0       0       0       0       0       0')

end subroutine

end module

! !!!!!!!!!!!!!!!!!!!!!!!!!!!! Example USAGE !!!!!!!!!!!!!!!!!!!!
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

program test
use PNFW
real(kind=8) :: nodes(3,72)
integer      :: elements(8,25)
character*80 :: meshfile,title

open(30,file='example.txt')
do i=1,72
end do

do i=1,25
end do
close(30)

meshfile ='sonu.mesh'
title='Good Mesh cube'

call getcube(nodes,elements,8,meshfile,title)

end program
```