Path: blob/devel/ElmerWorkflows/Gid2Elmer/elmer.gid/findparents.f90
3204 views
PROGRAM FindParents1!----------------------------------------------------------------------------2! Determines parents for boundary elements in Elmer mesh files.3!4! Written by: Mikko Lyly 18 May 20045! Modified by: Mark Smith 7 March 20146!----------------------------------------------------------------------------7IMPLICIT NONE89TYPE HashEntry_t10INTEGER :: Element11TYPE(HashEntry_t), POINTER :: Next12END TYPE HashEntry_t1314TYPE HashTable_t15TYPE(HashEntry_t), POINTER :: Head16END TYPE HashTable_t1718TYPE(HashTable_t), ALLOCATABLE :: HashTable(:)19TYPE(HashEntry_t), POINTER :: HashPtr, HashPtr12021LOGICAL :: Found22INTEGER :: i, j, a(30), Istat23INTEGER :: Candidate, Parent(2)24INTEGER :: NumberOfParents, Element, CheckCount, NumberOfNodes, Node25INTEGER :: Nnodes, Nbulk, Nboundary, BoundaryCode, ElementDim, BoundaryDim26INTEGER, ALLOCATABLE :: ElementCode(:)2728!---------------------------------------------------------------------------2930! Read header:31! ------------32OPEN(10, FILE='mesh.header')33READ(10,*) Nnodes, Nbulk, Nboundary34CLOSE(10)3536! Prepare the element hash table for nodes (inverse connectivity):37! ----------------------------------------------------------------38OPEN(10, FILE='mesh.elements')3940ISTAT = 041ALLOCATE( HashTable( Nnodes ), ElementCode( Nbulk ), STAT = Istat )42IF( Istat /= 0 ) THEN43PRINT *,'Memory allocation error. Aborting.'44STOP45END IF4647DO i = 1,Nnodes48NULLIFY( HashTable( i ) % Head )49ENDDO5051DO i = 1, Nbulk52READ(10,*) A(1:3), A(4:3+MOD(A(3),100))5354! A(1) = element number55! A(2) = tag56! A(3) = code57! A(4:) = node numbers5859ElementCode(i) = A(3)60NumberOfNodes = MOD(A(3),100)6162DO j = 1,NumberOfNodes63Node = A(3+j)6465HashPtr => HashTable( Node ) % Head66Found = .FALSE.6768DO WHILE( ASSOCIATED( HashPtr ) )69IF( HashPtr % Element == A(1) ) THEN70Found = .TRUE.71EXIT72END IF73HashPtr => HashPtr % Next74END DO7576IF( .NOT.Found ) THEN77ALLOCATE( HashPtr )78HashPtr % Element = i79HashPtr % Next => HashTable( Node ) % Head80HashTable( Node ) % Head => HashPtr81END IF8283END DO84END DO85CLOSE(10)868788! Make the mesh.boundary -file with parents:89! ------------------------------------------90OPEN(10, FILE='mesh.boundary')91OPEN(11, FILE='mesh.boundary.corrected' )9293DO i = 1, Nboundary94READ(10,*) A(1:5), A(6:5+MOD(A(5),100))9596! A(1) = boundaryelement number97! A(2) = tag98! A(3) = left parent (assumed unknown)99! A(4) = right parent (assumed unknown)100! A(5) = code101! A(6:) = node numbers102103BoundaryCode = A(5)104NumberOfNodes = MOD(A(5),100)105106SELECT CASE( INT(BoundaryCode/100) )107CASE( 1 )108BoundaryDim = 0109CASE( 2 )110BoundaryDim = 1111CASE( 3, 4 )112BoundaryDim = 2113CASE( 5, 6, 7, 8 )114BoundaryDim = 3115CASE DEFAULT116PRINT *,'Cannot detect dimension for boundary element',A(1)117BoundaryDim = 0118END SELECT119120NumberOfParents = 0121HashPtr1 => HashTable( A(6) ) % Head122Parent = 0123124DO WHILE( ASSOCIATED( HashPtr1 ) )125126Candidate = HashPtr1 % Element127CheckCount = 0128129Do j = 1,NumberOfNodes130Node = A(5+j)131HashPtr => HashTable( Node ) % Head132DO WHILE( ASSOCIATED( HashPtr ) )133IF( HashPtr % Element == Candidate ) &134CheckCount = CheckCount+1135HashPtr => HashPtr % Next136END DO137END DO138139IF( CheckCount == NumberOfNodes ) THEN140NumberOfParents = NumberOfParents+1141142IF( NumberOfParents > 2 ) THEN143PRINT *,'Confused: Found more than 2 parents'144PRINT *,'for boundary element =',A(1)145END IF146147SELECT CASE( INT(ElementCode(Candidate)/100) )148CASE(1)149ElementDim = 0150CASE(2)151ElementDim = 1152CASE( 3, 4 )153ElementDim = 2154CASE( 5, 6, 7, 8 )155ElementDim = 3156CASE DEFAULT157PRINT *,'Cannot detect dimension for element',Candidate158ElementDim = 0159END SELECT160161IF( ElementDim /= BoundaryDim+1 ) THEN162PRINT *,'Confused: Dimension of the boundary element',A(1)163PRINT *,'is incompatible with possible parent',Candidate164END IF165166IF( NumberOfParents <= 2 ) Parent( NumberOfParents ) = Candidate167168END IF169170HashPtr1 => HashPtr1 % Next171END DO172173WRITE(11,'(100I8)') A(1:2), Parent(1:2), A(5), A(6:5+MOD(A(5),100))174175END DO176177! Close, deallocate, and destroy hash tables:178! -------------------------------------------179CLOSE(10)180CLOSE(11)181182DEALLOCATE( ElementCode )183184DO i = 1,Nnodes185HashPtr => HashTable(i) % Head186DO WHILE( ASSOCIATED( HashPtr ) )187HashPtr1 => HashPtr % Next188DEALLOCATE( HashPtr )189HashPtr => HashPtr1190END DO191END DO192193! Done:194! -----195196END PROGRAM FindParents197198199