xmlOutput.F90 30.3 KB
Newer Older
1 2 3 4 5 6
!--------------------------------------------------------------------------------
! Copyright (c) 2016 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

7 8 9 10 11 12 13 14 15 16 17 18 19 20
MODULE m_xmlOutput

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!
!!!   XML output service routines
!!!
!!!   This module provides several subroutines that simplify the
!!!   generation of the out.xml file.
!!!                                         GM'16
!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   IMPLICIT NONE

21
   PRIVATE
22 23 24 25 26
   INTEGER, SAVE :: currentElementIndex
   INTEGER, SAVE :: maxNumElements
   INTEGER, SAVE :: xmlOutputUnit
   CHARACTER(LEN= 40), ALLOCATABLE :: elementList(:)

Gregor Michalicek's avatar
Gregor Michalicek committed
27 28 29
   PUBLIC startXMLOutput, endXMLOutput
   PUBLIC writeXMLElementFormPoly, writeXMLElementPoly
   PUBLIC writeXMLElementForm, writeXMLElement
Daniel Wortmann's avatar
Daniel Wortmann committed
30 31
   PUBLIC writeXMLElementMatrixPoly, writeXMLElementMatrixFormPoly
   PUBLIC writeXMLElementMatrixForm
32 33 34
   PUBLIC openXMLElementFormPoly, openXMLElementPoly
   PUBLIC openXMLElementForm, openXMLElement
   PUBLIC openXMLElementNoAttributes, closeXMLElement
35
   PUBLIC getXMLOutputUnitNumber, isCurrentXMLElement
36

37 38
   CONTAINS

39 40 41 42 43 44
   FUNCTION getXMLOutputUnitNumber()
      IMPLICIT NONE
      INTEGER getXMLOutputUnitNumber
      getXMLOutputUnitNumber = xmlOutputUnit
   END FUNCTION getXMLOutputUnitNumber

45
   SUBROUTINE startXMLOutput()
46 47

      USE m_juDFT_args
48
      USE m_constants
49
      USE m_utility
50
      USE m_compile_descr
51
!$    use omp_lib
52
      
53
      IMPLICIT NONE
54

55 56
#ifdef CPP_MPI
      include "mpif.h"
57
      INTEGER           :: err, isize
58
#endif
59
      INTEGER           :: numFlags
60
      INTEGER           :: nOMPThreads
61 62
      CHARACTER(LEN=8)  :: date
      CHARACTER(LEN=10) :: time
63
      CHARACTER(LEN=10) :: zone
64
      CHARACTER(LEN=10) :: dateString
65 66 67 68
      CHARACTER(LEN=10) :: timeString
      CHARACTER(LEN=6)  :: precisionString
      CHARACTER(LEN=9)  :: flags(11)
      CHARACTER(LEN=20) :: structureSpecifiers(11)
Daniel Wortmann's avatar
Daniel Wortmann committed
69
      CHARACTER(LEN=50) :: gitdesc,githash,gitbranch,compile_date,compile_user,compile_host
70
      CHARACTER(LEN=50) :: compile_flags,link_flags
71
      CHARACTER(LEN=20) :: attributes(7)
72
      
73 74 75 76 77
      maxNumElements = 10
      ALLOCATE(elementList(maxNumElements))
      elementList = ''
      currentElementIndex = 0
      xmlOutputUnit = 53
78 79 80
      CALL DATE_AND_TIME(date,time,zone)
      WRITE(dateString,'(a4,a1,a2,a1,a2)') date(1:4),'/',date(5:6),'/',date(7:8)
      WRITE(timeString,'(a2,a1,a2,a1,a2)') time(1:2),':',time(3:4),':',time(5:6)
81
      IF (juDFT_was_argument("-info")) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
82 83 84 85
         OPEN (xmlOutputUnit,status='scratch')
      ELSE
         OPEN (xmlOutputUnit,file='out.xml',form='formatted',status='unknown')
      ENDIF
86 87
      WRITE (xmlOutputUnit,'(a)') '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
      WRITE (xmlOutputUnit,'(a)') '<fleurOutput fleurOutputVersion="0.27">'
88
      CALL openXMLElement('programVersion',(/'version'/),(/version_const/))
89 90
      CALL get_compile_desc(gitdesc,githash,gitbranch,compile_date,compile_user,compile_host,compile_flags,link_flags)
      CALL writeXMLElement('compilationInfo',(/'date','user','host','flag','link'/),(/compile_date,compile_user,compile_host,compile_flags,link_flags/))
Daniel Wortmann's avatar
Daniel Wortmann committed
91
      CALL writeXMLElement('gitInfo',(/'version       ','branch        ','lastCommitHash'/),(/gitdesc,gitbranch,githash/))
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      CALL getComputerArchitectures(flags, numFlags)
      IF (numFlags.EQ.0) THEN
         numFlags = 1
         flags(numFlags) = 'GEN'
      END IF
      CALL writeXMLElementNoAttributes('targetComputerArchitectures',flags(1:numFlags))
      IF (numFlags.GT.1) THEN 
         STOP "ERROR: Define only one system architecture! (called by xmlOutput)"
      END IF
      CALL getPrecision(precisionString)
      CALL writeXMLElement('precision',(/'type'/),(/precisionString/))
      CALL getTargetStructureProperties(structureSpecifiers, numFlags)
      CALL writeXMLElementNoAttributes('targetStructureClass',structureSpecifiers(1:numFlags))
      CALL getAdditionalCompilationFlags(flags, numFlags)
      IF (numFlags.GE.1) THEN
         CALL writeXMLElementNoAttributes('additionalCompilerFlags',flags(1:numFlags))
      END IF
      CALL closeXMLElement('programVersion')
110 111 112 113 114 115 116 117 118 119

      CALL openXMLElementNoAttributes('parallelSetup')
      nOMPThreads = -1
      !$ nOMPThreads=omp_get_max_threads()
      IF(nOMPThreads.NE.-1) THEN
         WRITE(attributes(1),'(i0)') nOMPThreads
         CALL writeXMLElementFormPoly('openMP',(/'ompThreads'/),&
                                      attributes(:1),reshape((/10,8/),(/1,2/)))
      END IF

120 121
#ifdef CPP_MPI
      CALL MPI_COMM_SIZE(MPI_COMM_WORLD,isize,err)
122 123 124
      WRITE(attributes(1),'(i0)') isize
      CALL writeXMLElementFormPoly('mpi',(/'mpiProcesses'/),&
                                   attributes(:1),reshape((/13,8/),(/1,2/)))
125
#endif
126 127
      CALL closeXMLElement('parallelSetup')

128
      CALL writeXMLElement('startDateAndTime',(/'date','time','zone'/),(/dateString,timeString,zone/))
129 130 131
   END SUBROUTINE startXMLOutput

   SUBROUTINE endXMLOutput()
132

133
      IMPLICIT NONE
134 135 136

      CHARACTER(LEN=8)  :: date
      CHARACTER(LEN=10) :: time
137
      CHARACTER(LEN=10)  :: zone
138
      CHARACTER(LEN=10) :: dateString
139
      CHARACTER(LEN=10)  :: timeString
140

141 142 143 144
      DO WHILE (currentElementIndex.NE.0)
         CALL closeXMLElement(elementList(currentElementIndex))
      END DO
      DEALLOCATE(elementList)
145 146 147 148
      CALL DATE_AND_TIME(date,time,zone)
      WRITE(dateString,'(a4,a1,a2,a1,a2)') date(1:4),'/',date(5:6),'/',date(7:8)
      WRITE(timeString,'(a2,a1,a2,a1,a2)') time(1:2),':',time(3:4),':',time(5:6)
      CALL writeXMLElement('endDateAndTime',(/'date','time','zone'/),(/dateString,timeString,zone/))
149 150 151 152
      WRITE (xmlOutputUnit,'(a)') '</fleurOutput>'
      CLOSE(xmlOutputUnit)
   END SUBROUTINE endXMLOutput

Gregor Michalicek's avatar
Gregor Michalicek committed
153
   SUBROUTINE writeXMLElementFormPoly(elementName,attributeNames,attributeValues,lengths,contentList)
154 155 156

      IMPLICIT NONE

Gregor Michalicek's avatar
Gregor Michalicek committed
157 158 159 160 161
      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CLASS(*),         INTENT(IN)           :: attributeValues(:)
      INTEGER,          INTENT(IN)           :: lengths(:,:)
      CLASS(*),         INTENT(IN), OPTIONAL :: contentList(:)
162 163 164 165 166 167

      CHARACTER(LEN= 30), ALLOCATABLE :: charAttributeValues(:)
      CHARACTER(LEN= 30), ALLOCATABLE :: charContentList(:)
      INTEGER                         :: i

      ALLOCATE(charAttributeValues(SIZE(attributeValues)))
Gregor Michalicek's avatar
Gregor Michalicek committed
168 169
      IF (PRESENT(contentList)) THEN
         ALLOCATE(charContentList(SIZE(contentList)))
170 171
      ELSE
         ALLOCATE(charContentList(1))
Gregor Michalicek's avatar
Gregor Michalicek committed
172
      END IF
173 174 175 176 177 178

      charAttributeValues = ''
      charContentList = ''

      DO i = 1, SIZE(attributeValues)
         SELECT TYPE (attributeValues)
179
           TYPE IS(INTEGER)
180
               WRITE(charAttributeValues(i),'(i0)') attributeValues(i)
181
            TYPE IS(REAL)
182
               WRITE(charAttributeValues(i),'(f19.10)') attributeValues(i)
183
            TYPE IS(LOGICAL)
184
               WRITE(charAttributeValues(i),'(l1)') attributeValues(i)
185
#ifndef __PGI
186
            TYPE IS(CHARACTER(LEN=*))
187
               WRITE(charAttributeValues(i),'(a)') TRIM(ADJUSTL(attributeValues(i)))
188 189
            CLASS DEFAULT
               STOP 'Type of attributeValues not allowed'
190
#endif
191 192 193
         END SELECT
      END DO

Gregor Michalicek's avatar
Gregor Michalicek committed
194 195 196 197
      IF (PRESENT(contentList)) THEN
         DO i = 1, SIZE(contentList)
            SELECT TYPE(contentList)
               TYPE IS(INTEGER)
198
                  WRITE(charContentList(i),'(i0)') contentList(i)
Gregor Michalicek's avatar
Gregor Michalicek committed
199
               TYPE IS(REAL)
200
                  WRITE(charContentList(i),'(f19.10)') contentList(i)
Gregor Michalicek's avatar
Gregor Michalicek committed
201
               TYPE IS(LOGICAL)
202
                  WRITE(charContentList(i),'(l1)') contentList(i)
203
#ifndef __PGI
204
               TYPE IS(CHARACTER(LEN=*))
205
                  WRITE(charContentList(i),'(a)') TRIM(ADJUSTL(contentList(i)))
Gregor Michalicek's avatar
Gregor Michalicek committed
206 207
               CLASS DEFAULT
                  STOP 'Type of contentList not allowed'
208
#endif
Gregor Michalicek's avatar
Gregor Michalicek committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
            END SELECT
         END DO
         CALL writeXMLElementForm(elementName,attributeNames,charAttributeValues,lengths,charContentList)
         DEALLOCATE(charContentList,charAttributeValues)
      ELSE
         CALL writeXMLElementForm(elementName,attributeNames,charAttributeValues,lengths)
      END IF

   END SUBROUTINE writeXMLElementFormPoly

   SUBROUTINE writeXMLElementPoly(elementName,attributeNames,attributeValues,contentList)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CLASS(*),         INTENT(IN)           :: attributeValues(:)
      CLASS(*),         INTENT(IN), OPTIONAL :: contentList(:)
227

Gregor Michalicek's avatar
Gregor Michalicek committed
228 229
      INTEGER, ALLOCATABLE :: lengths(:,:)
      INTEGER              :: contentListSize
230

Gregor Michalicek's avatar
Gregor Michalicek committed
231 232 233 234 235 236 237 238 239
      contentListSize = 0
      IF (PRESENT(contentList)) THEN
         contentListSize = SIZE(contentList)
      END IF

      ALLOCATE(lengths(MAX(SIZE(attributeNames),contentListSize),3))
      lengths = 0
      CALL writeXMLElementFormPoly(elementName,attributeNames,attributeValues,lengths,contentList)
      DEALLOCATE(lengths)
240

241 242
   END SUBROUTINE writeXMLElementPoly

Gregor Michalicek's avatar
Gregor Michalicek committed
243
   SUBROUTINE writeXMLElementForm(elementName,attributeNames,attributeValues,lengths,contentList)
244 245 246

      IMPLICIT NONE

Gregor Michalicek's avatar
Gregor Michalicek committed
247 248 249 250 251
      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CHARACTER(LEN=*), INTENT(IN)           :: attributeValues(:)
      INTEGER,          INTENT(IN)           :: lengths(:,:)
      CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: contentList(:)
252 253

      CHARACTER(LEN=200), ALLOCATABLE :: contentLineList(:)
Gregor Michalicek's avatar
Gregor Michalicek committed
254 255
      INTEGER, ALLOCATABLE            :: bigLengths(:,:)
      INTEGER :: i, j, contentLineLength, contentLineListSize
256 257
      CHARACTER(LEN=150)               :: format
      CHARACTER(LEN=1000)              :: outputString
258
      INTEGER                         :: contentListSize, overallListSize, numContentLineChars
Gregor Michalicek's avatar
Gregor Michalicek committed
259
      INTEGER                         :: lengthsShape(2)
260 261 262 263 264 265

      IF(SIZE(attributeNames).NE.SIZE(attributeValues)) THEN
         WRITE(*,*) 'attributeNames', attributeNames
         WRITE(*,*) 'attributeValues', attributeValues
         STOP 'ERROR: SIZE(attributeNames).NE.SIZE(attributeValues)'
      END IF
Gregor Michalicek's avatar
Gregor Michalicek committed
266 267 268 269 270

      lengthsShape = SHAPE(lengths)
      contentListSize = 0
      IF (PRESENT(contentList)) THEN
         contentListSize = SIZE(contentList)
271
      END IF
Gregor Michalicek's avatar
Gregor Michalicek committed
272 273 274 275 276 277 278 279 280
      overallListSize = MAX(SIZE(attributeNames),contentListSize)
      ALLOCATE(bigLengths(overallListSize,2))
      bigLengths = 0
      DO j = 1, 2
         DO i = 1, MIN(overallListSize,lengthsShape(1))
            bigLengths(i,j) = lengths(i,j)
         END DO
      END DO

281 282
      outputString = '<'//TRIM(ADJUSTL(elementName))
      DO i = 1, SIZE(attributeNames)
Gregor Michalicek's avatar
Gregor Michalicek committed
283 284 285 286 287 288 289
         WRITE(format,'(a)') "(a,a,a"
         IF (bigLengths(i,1).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,1)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a,a"
         IF (bigLengths(i,2).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,2)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a)"
         WRITE(outputString,format) TRIM(ADJUSTL(outputString)), ' ', TRIM(ADJUSTL(attributeNames(i))),&
                                    '="', TRIM(ADJUSTL(attributeValues(i))), '"'
290
      END DO
291
      WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"
Gregor Michalicek's avatar
Gregor Michalicek committed
292 293 294 295 296 297 298 299 300
      IF (PRESENT(contentList)) THEN
         contentLineLength = 5 ! At most 5 data elements per line
         contentLineListSize = SIZE(contentList) / contentLineLength
         IF(contentLineListSize*contentLineLength.NE.SIZE(contentList)) THEN
            contentLineListSize = contentLineListSize + 1
         END IF
         ALLOCATE(contentLineList(contentLineListSize))
         CALL fillContentLineList(contentList,contentLineList,contentLineLength)
         IF(SIZE(contentLineList).LE.1) THEN
301
            outputString = TRIM(ADJUSTL(outputString))//'>'//TRIM(ADJUSTL(contentLineList(1)))//'</'//&
302
                 TRIM(ADJUSTL(elementName))//'>'
Gregor Michalicek's avatar
Gregor Michalicek committed
303 304 305
         ELSE
            outputString = TRIM(ADJUSTL(outputString))//'>'
         END IF
306
         WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))
Gregor Michalicek's avatar
Gregor Michalicek committed
307 308
         IF(SIZE(contentLineList).GT.1) THEN
            DO i = 1, SIZE(contentLineList)
309 310 311 312 313 314 315
               IF (i.EQ.SIZE(contentLineList)) THEN
                  numContentLineChars = 20*MOD(SIZE(contentList),contentLineLength)
                  IF(numContentLineChars.EQ.0) numContentLineChars = 20 * contentLineLength
                  WRITE(format,'(a,i0,a,i0,a)') "(a",3*(currentElementIndex+2),",a",numContentLineChars,")"
               ELSE
                  WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+2),",a100)"
               END IF
Gregor Michalicek's avatar
Gregor Michalicek committed
316 317 318 319 320 321 322 323 324 325 326
               WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(contentLineList(i)))
            END DO
            WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"
            outputString = '</'//TRIM(ADJUSTL(elementName))//'>'
            WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))
         END IF
      ELSE
         outputString = TRIM(ADJUSTL(outputString))//'/>'
         WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))
      END IF
      DEALLOCATE (bigLengths)
327

Gregor Michalicek's avatar
Gregor Michalicek committed
328 329
   END SUBROUTINE writeXMLElementForm

Daniel Wortmann's avatar
Daniel Wortmann committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
   SUBROUTINE writeXMLElementMatrixPoly(elementName,attributeNames,attributeValues,matrix)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CLASS(*),         INTENT(IN)           :: attributeValues(:)
      CLASS(*),         INTENT(IN)           :: matrix(:,:)

      INTEGER, ALLOCATABLE :: lengths(:,:)
      INTEGER              :: numDifferentMatrixElementLengths

      numDifferentMatrixElementLengths = 1 ! At the moment nothing else is implemented.
                                           ! In principle one could think about an own
                                           ! length for each matrix element.

      ALLOCATE(lengths(MAX(SIZE(attributeNames),numDifferentMatrixElementLengths),3))
      lengths = 0
      CALL writeXMLElementMatrixFormPoly(elementName,attributeNames,attributeValues,lengths,matrix)
      DEALLOCATE(lengths)

   END SUBROUTINE writeXMLElementMatrixPoly

   SUBROUTINE writeXMLElementMatrixFormPoly(elementName,attributeNames,attributeValues,lengths,matrix)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CLASS(*),         INTENT(IN)           :: attributeValues(:)
      INTEGER,          INTENT(IN)           :: lengths(:,:)
      CLASS(*),         INTENT(IN)           :: matrix(:,:)

      CHARACTER(LEN= 30), ALLOCATABLE :: charAttributeValues(:)
      CHARACTER(LEN= 50), ALLOCATABLE :: charMatrix(:,:)
      INTEGER                         :: i, j

      ALLOCATE(charAttributeValues(SIZE(attributeValues)))
      ALLOCATE(charMatrix(SIZE(matrix,1),SIZE(matrix,2)))

      charAttributeValues = ''
      charMatrix = ''

      DO i = 1, SIZE(attributeValues)
         SELECT TYPE (attributeValues)
           TYPE IS(INTEGER)
               WRITE(charAttributeValues(i),'(i0)') attributeValues(i)
            TYPE IS(REAL)
               WRITE(charAttributeValues(i),'(f19.10)') attributeValues(i)
            TYPE IS(LOGICAL)
               WRITE(charAttributeValues(i),'(l1)') attributeValues(i)
#ifndef __PGI
            TYPE IS(CHARACTER(LEN=*))
               WRITE(charAttributeValues(i),'(a)') TRIM(ADJUSTL(attributeValues(i)))
            CLASS DEFAULT
               STOP 'Type of attributeValues not allowed'
#endif
         END SELECT
      END DO

      DO j = 1, SIZE(matrix,2)
         DO i = 1, SIZE(matrix,1)
            SELECT TYPE(matrix)
               TYPE IS(INTEGER)
                  WRITE(charMatrix(i,j),'(i0)') matrix(i,j)
               TYPE IS(REAL)
                  WRITE(charMatrix(i,j),'(f19.10)') matrix(i,j)
               TYPE IS(COMPLEX)
                  WRITE(charMatrix(i,j),'(a,SP,f8.5,a,f8.5,a)') '(',REAL(matrix(i,j)),',', AIMAG(matrix(i,j)),')'
               TYPE IS(LOGICAL)
                  WRITE(charMatrix(i,j),'(l1)') matrix(i,j)
#ifndef __PGI
               TYPE IS(CHARACTER(LEN=*))
                  WRITE(charMatrix(i,j),'(a)') TRIM(ADJUSTL(matrix(i,j)))
               CLASS DEFAULT
                  STOP 'Type of matrix not allowed'
#endif
            END SELECT
         END DO
      END DO

      CALL writeXMLElementMatrixForm(elementName,attributeNames,charAttributeValues,lengths,charMatrix)
      DEALLOCATE(charMatrix,charAttributeValues)
   END SUBROUTINE writeXMLElementMatrixFormPoly

   SUBROUTINE writeXMLElementMatrixForm(elementName,attributeNames,attributeValues,lengths,matrix)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN) :: elementName
      CHARACTER(LEN=*), INTENT(IN) :: attributeNames(:)
      CHARACTER(LEN=*), INTENT(IN) :: attributeValues(:)
      INTEGER,          INTENT(IN) :: lengths(:,:)
      CHARACTER(LEN=*), INTENT(IN) :: matrix(:,:)

      CHARACTER(LEN=400), ALLOCATABLE :: matrixRows(:)
      INTEGER, ALLOCATABLE            :: bigLengths(:,:)
      INTEGER                         :: i, j
      CHARACTER(LEN=70)               :: format
      CHARACTER(LEN=400)              :: outputString
      INTEGER                         :: matrixRowLength, numMatrixRows
      INTEGER                         :: overallListSize
      INTEGER                         :: lengthsShape(2)

      IF(SIZE(attributeNames).NE.SIZE(attributeValues)) THEN
         WRITE(*,*) 'attributeNames', attributeNames
         WRITE(*,*) 'attributeValues', attributeValues
         STOP 'ERROR in writeXMLElementMatrixForm: SIZE(attributeNames).NE.SIZE(attributeValues)'
      END IF

      lengthsShape = SHAPE(lengths)

      overallListSize = MAX(SIZE(attributeNames),1)
      ALLOCATE(bigLengths(overallListSize,2))
      bigLengths = 0
      DO j = 1, 2
         DO i = 1, MIN(overallListSize,lengthsShape(1))
            bigLengths(i,j) = lengths(i,j)
         END DO
      END DO

      outputString = '<'//TRIM(ADJUSTL(elementName))
      DO i = 1, SIZE(attributeNames)
         WRITE(format,'(a)') "(a,a,a"
         IF (bigLengths(i,1).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,1)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a,a"
         IF (bigLengths(i,2).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,2)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a)"
         WRITE(outputString,format) TRIM(ADJUSTL(outputString)), ' ', TRIM(ADJUSTL(attributeNames(i))),&
                                    '="', TRIM(ADJUSTL(attributeValues(i))), '"'
      END DO
      WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"

      matrixRowLength = SIZE(matrix,1)
      numMatrixRows = SIZE(matrix,2)

      ALLOCATE(matrixRows(numMatrixRows))

      matrixRows = ''
      DO i = 1, numMatrixRows
         DO j = 1, matrixRowLength
            WRITE(matrixRows(i),'(a,a1,a)') TRIM(ADJUSTL(matrixRows(i))),' ',TRIM(ADJUSTL(matrix(j,i)))
         END DO
      END DO

      outputString = TRIM(ADJUSTL(outputString))//'>'
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))

      DO i = 1, numMatrixRows
         WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+2),",a)"
         WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(matrixRows(i)))
      END DO

      WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"
      outputString = '</'//TRIM(ADJUSTL(elementName))//'>'
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))

      DEALLOCATE (bigLengths)

   END SUBROUTINE writeXMLElementMatrixForm

Gregor Michalicek's avatar
Gregor Michalicek committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
   SUBROUTINE writeXMLElement(elementName,attributeNames,attributeValues,contentList)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CHARACTER(LEN=*), INTENT(IN)           :: attributeValues(:)
      CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: contentList(:)

      INTEGER, ALLOCATABLE :: lengths(:,:)
      INTEGER              :: contentListSize

      contentListSize = 0
      IF (PRESENT(contentList)) THEN
         contentListSize = SIZE(contentList)
506
      END IF
Gregor Michalicek's avatar
Gregor Michalicek committed
507 508 509 510 511

      ALLOCATE(lengths(MAX(SIZE(attributeNames),contentListSize),2))
      lengths = 0
      CALL writeXMLElementForm(elementName,attributeNames,attributeValues,lengths,contentList)
      DEALLOCATE(lengths)
512

513 514
   END SUBROUTINE writeXMLElement

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
   SUBROUTINE writeXMLElementNoAttributes(elementName,contentList)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: contentList(:)

      CHARACTER(LEN=200), ALLOCATABLE :: contentLineList(:)
      INTEGER :: i, j, contentLineLength, contentLineListSize
      CHARACTER(LEN=70)               :: format
      CHARACTER(LEN=200)              :: outputString
      INTEGER                         :: contentListSize, overallListSize, numContentLineChars

      outputString = '<'//TRIM(ADJUSTL(elementName))

      WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"

      contentLineLength = 5 ! At most 5 data elements per line
      contentLineListSize = SIZE(contentList) / contentLineLength
      IF(contentLineListSize*contentLineLength.NE.SIZE(contentList)) THEN
         contentLineListSize = contentLineListSize + 1
      END IF
      ALLOCATE(contentLineList(contentLineListSize))
      CALL fillContentLineList(contentList,contentLineList,contentLineLength)
539 540 541
      IF(contentLineListSize.EQ.0) THEN
         outputString = TRIM(ADJUSTL(outputString))//'> </'//TRIM(ADJUSTL(elementName))//'>'
      ELSE IF(contentLineListSize.EQ.1) THEN
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
         outputString = TRIM(ADJUSTL(outputString))//'>'//TRIM(ADJUSTL(contentLineList(1)))//'</'//&
              TRIM(ADJUSTL(elementName))//'>'
      ELSE
         outputString = TRIM(ADJUSTL(outputString))//'>'
      END IF
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))
      IF(contentLineListSize.GT.1) THEN
         DO i = 1, SIZE(contentLineList)
            IF (i.EQ.SIZE(contentLineList)) THEN
               numContentLineChars = 20*MOD(SIZE(contentList),contentLineLength)
               IF(numContentLineChars.EQ.0) numContentLineChars = 20 * contentLineLength
               WRITE(format,'(a,i0,a,i0,a)') "(a",3*(currentElementIndex+2),",a",numContentLineChars,")"
            ELSE
               WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+2),",a100)"
            END IF
            WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(contentLineList(i)))
         END DO
         WRITE(format,'(a,i0,a)') "(a",3*(currentElementIndex+1),",a)"
         outputString = '</'//TRIM(ADJUSTL(elementName))//'>'
         WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(outputString))
      END IF

   END SUBROUTINE writeXMLElementNoAttributes

566 567 568 569
   SUBROUTINE fillContentLineList(contentList,contentLineList,contentLineLength)

      IMPLICIT NONE

570
      CHARACTER(LEN= *), INTENT(IN)    :: contentList(:)
571 572 573 574 575 576 577 578 579 580
      CHARACTER(LEN=200), INTENT(INOUT) :: contentLineList(:)
      INTEGER,            INTENT(IN)    :: contentLineLength
      INTEGER :: i, j

      contentLineList = ''
      DO i = 1, SIZE(contentLineList)
         DO j = 1, contentLineLength
            IF((i-1)*contentLineLength+j.GT.SIZE(contentList)) THEN
               RETURN
            END IF
581 582
            WRITE(contentLineList(i),'(a,a20)') TRIM(ADJUSTL(contentLineList(i))),&
                        TRIM(ADJUSTL(contentList((i-1)*contentLineLength+j)))
583 584 585 586 587 588 589 590
         END DO
      END DO
   END SUBROUTINE fillContentLineList

   SUBROUTINE openXMLElementPoly(elementName,attributeNames,attributeValues)

      IMPLICIT NONE

591 592 593
      CHARACTER(LEN=*)             :: elementName
      CHARACTER(LEN=*), INTENT(IN) :: attributeNames(:)
      CLASS(*),         INTENT(IN) :: attributeValues(:)
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
      INTEGER, ALLOCATABLE :: lengths(:,:)

      ALLOCATE(lengths(SIZE(attributeNames),2))
      lengths = 0
      CALL openXMLElementFormPoly(elementName,attributeNames,attributeValues,lengths)
      DEALLOCATE(lengths)

   END SUBROUTINE openXMLElementPoly

   SUBROUTINE openXMLElementFormPoly(elementName,attributeNames,attributeValues,lengths)

      IMPLICIT NONE

      CHARACTER(LEN=*)             :: elementName
      CHARACTER(LEN=*), INTENT(IN) :: attributeNames(:)
      CLASS(*),         INTENT(IN) :: attributeValues(:)
      INTEGER,          INTENT(IN) :: lengths(:,:)

613 614 615 616 617 618 619 620 621 622
      CHARACTER(LEN= 30), ALLOCATABLE :: charAttributeValues(:)
      INTEGER                         :: i

      ALLOCATE(charAttributeValues(SIZE(attributeValues)))

      charAttributeValues = ''

      DO i = 1, SIZE(attributeValues)
         SELECT TYPE (attributeValues)
            TYPE IS(INTEGER)
623
               WRITE(charAttributeValues(i),'(i0)') attributeValues(i)
624
            TYPE IS(REAL)
625
               WRITE(charAttributeValues(i),'(f19.10)') attributeValues(i)
626
            TYPE IS(LOGICAL)
627
               WRITE(charAttributeValues(i),'(l1)') attributeValues(i)
628
#ifndef __PGI
629
            TYPE IS(CHARACTER(LEN=*))
630
               WRITE(charAttributeValues(i),'(a)') TRIM(ADJUSTL(attributeValues(i)))
631 632
            CLASS DEFAULT
               STOP 'Type of attributeValues not allowed'
633
#endif
634 635 636
         END SELECT
      END DO

637
      CALL openXMLElementForm(elementName,attributeNames,charAttributeValues,lengths)
638 639
      DEALLOCATE(charAttributeValues)

640
   END SUBROUTINE openXMLElementFormPoly
641

642 643 644 645 646 647 648 649 650 651 652
   SUBROUTINE openXMLElementNoAttributes(elementName)

      IMPLICIT NONE

      CHARACTER(LEN=*)             :: elementName

      INTEGER :: i
      CHARACTER(LEN=70)  :: format
      CHARACTER(LEN=200) :: openingString

      IF(currentElementIndex.EQ.maxNumElements) THEN
653
         WRITE(*,*) 'elementName ', TRIM(ADJUSTL(elementName))
654 655 656 657 658 659 660
         STOP 'ERROR: xml hierarchy too deep!'
      END IF
      currentElementIndex = currentElementIndex + 1
      elementList(currentElementIndex) = TRIM(ADJUSTL(elementName))
      openingString = '<'//TRIM(ADJUSTL(elementName))//'>'
      WRITE(format,'(a,i0,a)') "(a",3*currentElementIndex,",a)"
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(openingString))
661

662 663
   END SUBROUTINE openXMLElementNoAttributes

664 665 666 667
   SUBROUTINE openXMLElement(elementName,attributeNames,attributeValues)

      IMPLICIT NONE

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
      CHARACTER(LEN=*), INTENT(IN)           :: elementName
      CHARACTER(LEN=*), INTENT(IN)           :: attributeNames(:)
      CHARACTER(LEN=*), INTENT(IN)           :: attributeValues(:)

      INTEGER, ALLOCATABLE :: lengths(:,:)

      ALLOCATE(lengths(SIZE(attributeNames),2))
      lengths = 0
      CALL openXMLElementForm(elementName,attributeNames,attributeValues,lengths)
      DEALLOCATE(lengths)

   END SUBROUTINE

   SUBROUTINE openXMLElementForm(elementName,attributeNames,attributeValues,lengths)

      IMPLICIT NONE

685 686 687
      CHARACTER(LEN=*)             :: elementName
      CHARACTER(LEN=*), INTENT(IN) :: attributeNames(:)
      CHARACTER(LEN=*), INTENT(IN) :: attributeValues(:)
688
      INTEGER,          INTENT(IN) :: lengths(:,:)
689 690 691 692

      CHARACTER(LEN=70)  :: format
      CHARACTER(LEN=200) :: openingString

693 694 695 696 697
      INTEGER, ALLOCATABLE            :: bigLengths(:,:)
      INTEGER                         :: i, j
      INTEGER                         :: overallListSize
      INTEGER                         :: lengthsShape(2)

698
      IF(SIZE(attributeNames).NE.SIZE(attributeValues)) THEN
699 700 701
         WRITE(*,*) 'elementName ', TRIM(ADJUSTL(elementName))
         WRITE(*,*) 'attributeNames ', attributeNames
         WRITE(*,*) 'attributeValues ', attributeValues
702 703 704
         STOP 'ERROR: SIZE(attributeNames).NE.SIZE(attributeValues)'
      END IF
      IF(currentElementIndex.EQ.maxNumElements) THEN
705 706 707 708
         WRITE(*,*) 'elementName ', TRIM(ADJUSTL(elementName))
         WRITE(*,*) 'attributeNames ', attributeNames
         WRITE(*,*) 'attributeValues ', attributeValues
         WRITE(*,*) 'elementList ', elementList
709 710
         STOP 'ERROR: xml hierarchy too deep!'
      END IF
711 712 713 714 715 716 717 718 719 720 721

      lengthsShape = SHAPE(lengths)
      overallListSize = SIZE(attributeNames)
      ALLOCATE(bigLengths(overallListSize,2))
      bigLengths = 0
      DO j = 1, 2
         DO i = 1, MIN(overallListSize,lengthsShape(1))
            bigLengths(i,j) = lengths(i,j)
         END DO
      END DO

722 723
      openingString = '<'//TRIM(ADJUSTL(elementName))
      DO i = 1, SIZE(attributeNames)
724 725 726 727 728 729 730
         WRITE(format,'(a)') "(a,a,a"
         IF (bigLengths(i,1).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,1)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a,a"
         IF (bigLengths(i,2).GT.0) WRITE(format,'(a,i0)') TRIM(ADJUSTL(format)),bigLengths(i,2)
         WRITE(format,'(a,a)') TRIM(ADJUSTL(format)),",a)"
         WRITE(openingString,format) TRIM(ADJUSTL(openingString)), ' ', TRIM(ADJUSTL(attributeNames(i))),&
                                    '="', TRIM(ADJUSTL(attributeValues(i))), '"'
731 732
      END DO
      openingString = TRIM(ADJUSTL(openingString))//'>'
733 734 735

      currentElementIndex = currentElementIndex + 1
      elementList(currentElementIndex) = TRIM(ADJUSTL(elementName))
736
      WRITE(format,'(a,i0,a)') "(a",3*currentElementIndex,",a)"
737
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(openingString))
738 739 740
      DEALLOCATE (bigLengths)

   END SUBROUTINE openXMLElementForm
741 742 743 744 745 746 747 748 749 750 751

   SUBROUTINE closeXMLElement(elementName)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN) :: elementName
      INTEGER :: i
      CHARACTER(LEN=70) :: format
      CHARACTER(LEN=70) :: closingString

      IF(TRIM(ADJUSTL(elementList(currentElementIndex))).NE.TRIM(ADJUSTL(elementName))) THEN
752 753
         WRITE(*,*) 'elementList(currentElementIndex): ', TRIM(ADJUSTL(elementList(currentElementIndex)))
         WRITE(*,*) 'elementName ', TRIM(ADJUSTL(elementName))
754 755
         STOP 'ERROR: Closing xml element inconsistency!'
      END IF
756 757
      closingString = '</'//TRIM(ADJUSTL(elementName))//'>'
      WRITE(format,'(a,i0,a)') "(a",3*currentElementIndex,",a)"
758 759
      WRITE(xmlOutputUnit,format) ' ', TRIM(ADJUSTL(closingString))
      currentElementIndex = currentElementIndex - 1
760

761 762
   END SUBROUTINE closeXMLElement

763 764 765 766 767
   LOGICAL FUNCTION isCurrentXMLElement(elementName)

      IMPLICIT NONE

      CHARACTER(LEN=*), INTENT(IN) :: elementName
768
      if (currentElementIndex==0.or.currentElementIndex>SIZE(elementList)) THEN
Daniel Wortmann's avatar
Daniel Wortmann committed
769 770 771
                  isCurrentXMLElement=.false.
                  return
      endif
772 773 774 775
      isCurrentXMLElement = (TRIM(ADJUSTL(elementList(currentElementIndex))).EQ.TRIM(ADJUSTL(elementName)))

   END FUNCTION isCurrentXMLElement

776
END MODULE m_xmlOutput