GRIDMAN
grid managment library
triang.f
Go to the documentation of this file.
1 C> @file apps/triang.f
2 C> Combine plasma grid (fort.30) with triangular grid in the "void" region.
3 C> Replacement for TRIAGEOM.
4 C>
5 C> Based on GRIDMAN library
6 C Author: Vladislav Kotov, v.kotov@fz-juelich.de
7 
8 ! Copyright (c) 2017 Forschungszentrum Juelich GmbH
9 ! Vladislav Kotov
10 !
11 ! This file is part of GRIDMAN.
12 !
13 ! GRIDMAN is free software: you can redistribute it and/or modify
14 ! it under the terms of the GNU General Public License as published by
15 ! the Free Software Foundation, either version 3 of the License, or
16 ! (at your option) any later version.
17 !
18 ! GRIDMAN is distributed in the hope that it will be useful,
19 ! but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ! GNU General Public License for more details.
22 !
23 ! You should have received a copy of the GNU General Public License
24 ! along with GRIDMAN. If not, see <http://www.gnu.org/licenses/>.
25 
26  PROGRAM triang
28  u gridman_unit,gridman_dbg,gridman_check
29  USE gridman_lib,ONLY:gridman_carre_read30,gridman_grid2d_triang,
30  u gridman_tria_read,
31  u gridman_grid2d_merge,gridman_tria_write,
33  u gridman_grid_write,gridman_template_write,
35  IMPLICIT NONE
36  INTRINSIC len_trim,get_command_argument,adjustl,trim
37 
38  INTEGER,PARAMETER :: file_length=1024
39 C INPUT PARAMETERS
40  CHARACTER(LEN=FILE_LENGTH) ::
41  c fort30_in,fort33_in,fort34_in,fort35_in
42  REAL(GRIDMAN_DP) :: tol
43  CHARACTER(LEN=FILE_LENGTH) ::
44  c fort33_out,fort34_out,fort35_out,
45  c vtk_out,grid_out,template_out
46  LOGICAL :: dbgmod,lcheck
47 
48 C LOCAL VARIABLES
49  TYPE(gridman_grid) :: plasma,combined,tria,tria_out
50  INTEGER :: ierr,res,lt
51  CHARACTER(LEN=8) :: ftmp
52 
53  CALL get_command_argument(1,ftmp)
54  IF(len_trim(ftmp).GT.0) THEN
55  IF(adjustl(trim(ftmp)).EQ.'help') THEN
56  CALL print_description
57  stop
58  END IF
59  ELSE
60 C 0. READ INPUT FROM NAMELIST
61  CALL read_input
62  END IF
63 
64 C 1. READ FORT.30
65  WRITE(gridman_unit,*) "TRIANG: read ",trim(fort30_in)
66  CALL gridman_carre_read30(plasma,fort30_in,ierr,.true.)
67  IF(ierr.NE.0) CALL triang_error("failed to read fort.30")
68 
69 C 2. READ INPUT TRIANGULAR GRID
70  WRITE(gridman_unit,*) "TRIANG: read "//trim(fort33_in)
71  WRITE(gridman_unit,*) " "//trim(fort34_in)//" "
72  w //trim(fort35_in)
73  CALL gridman_tria_read(tria,
74  c (/fort33_in,fort34_in,fort35_in/),ierr)
75  IF(ierr.NE.0) CALL triang_error("failed to read triangular grid")
76 
77 C 3. PREPARE INDEXES: cell indexes will be taken from the plasma grid
78  WRITE(gridman_unit,*) "TRIANG: preparing indexes"
79  CALL gridman_index_deallocate(tria%CELLINDEX(1),ierr)
80  IF(ierr.NE.0) CALL triang_error("")
81  DEALLOCATE(tria%CELLINDEX)
82  tria%NCELLINDEX=0
83 
84 C 4. MERGE GRIDS
85  WRITE(gridman_unit,*) "TRIANG: merging grids"
86  CALL gridman_grid2d_merge(combined,tria,plasma,tol,ierr)
87  IF(ierr.NE.0)
88  f CALL triang_error("merging failed. Try to change TOL")
89 
90 C 5. TRIANGULATE
91  WRITE(gridman_unit,*) "TRIANG: triangulate plasma grid"
92  CALL gridman_grid2d_triang(tria_out,combined,ierr)
93  IF(ierr.NE.0) CALL triang_error("triangulation failed")
94  tria_out%DESCRIPTION=trim(tria_out%DESCRIPTION)//
95  / ' - Created by TRIANG '
96 
97 C 6. WRITE DOWN COMBINED GRID
98  WRITE(gridman_unit,*) "TRIANG: writing combined grid into"
99  WRITE(gridman_unit,*) " ",trim(fort33_out)
100  WRITE(gridman_unit,*) " ",trim(fort34_out)
101  WRITE(gridman_unit,*) " ",trim(fort35_out)
102 
103  CALL gridman_tria_write(tria_out,
104  c (/fort33_out,fort34_out,fort35_out/),ierr)
105  IF(ierr.NE.0) CALL triang_error("writing combined grid failed")
106 
107 C 7. CHECK THE CREATED GRID
108  WRITE(gridman_unit,*) "TRIANG: checking the created grid"
109  CALL gridman_tria_read(tria,
110  c (/fort33_out,fort34_out,fort35_out/),ierr)
111  IF(ierr.NE.0)
112  f CALL triang_error("the created grid cannot be read")
113  CALL gridman_grid2d_check(tria,res,ierr)
114  IF(res.NE.0.OR.ierr.NE.0)
115  c CALL triang_error("the created grid does not pass checks")
116 
117 C 8. WRITE IN OTHER FORMATS
118  lt=len_trim(vtk_out)
119  IF(lt.GT.0) THEN
120  WRITE(gridman_unit,*) "TRIANG: writing combined grid"
121  WRITE(gridman_unit,*) " into VTK file ",vtk_out(1:lt)
123  c vtk_out,tria_out%DESCRIPTION,tria_out,
124  c tria_out%NCELLS,0,0,0,0,ierr)
125  IF(ierr.NE.0) CALL triang_error("could not write VTK file")
126  END IF
127  lt=len_trim(template_out)
128  IF(lt.GT.0) THEN
129  WRITE(gridman_unit,*) "TRIANG: writing combined grid"
130  WRITE(gridman_unit,*)
131  w " into DG template file ",template_out(1:lt)
132  CALL gridman_template_write(tria_out,template_out,ierr)
133  IF(ierr.NE.0) CALL triang_error("could not write template file")
134  END IF
135  lt=len_trim(grid_out)
136  IF(lt.GT.0) THEN
137  WRITE(gridman_unit,*) "TRIANG: writing combined grid"
138  WRITE(gridman_unit,*) " into GRIDMAN-grid file ",grid_out(1:lt)
139  CALL gridman_grid_write(tria_out,grid_out,ierr)
140  IF(ierr.NE.0) CALL triang_error("could not write grid file")
141  END IF
142 
143 C DEALLOCATE MEMORY
144  CALL clean_all
145  WRITE(gridman_unit,*) "TRIANG COMPLETED"
146 
147  CONTAINS
148 
149 C
150  SUBROUTINE read_input
151  INTEGER :: io
152  namelist /triang/ fort30_in,fort33_in,fort34_in,fort35_in,
153  n tol,fort33_out,fort34_out,fort35_out,
154  n vtk_out,grid_out,template_out,dbgmod,lcheck
155 
156 C DEFAULTS
157  fort30_in=''
158  fort33_in=''
159  fort34_in=''
160  fort35_in=''
161  fort33_out=''
162  fort34_out=''
163  fort35_out=''
164  vtk_out=''
165  grid_out=''
166  template_out=''
167  tol=1e-5
168  dbgmod=.false.
169  lcheck =.false.
170 
171  rewind(5)
172  READ(5,nml=triang,iostat=io)
173  IF(io.NE.0) CALL triang_error("can not read namelist TRIANG")
174 
175  gridman_dbg=dbgmod
176  gridman_check=lcheck
177 
178  END SUBROUTINE read_input
179 C
180  SUBROUTINE clean_all
181  CALL gridman_grid_deallocate(plasma,ierr)
182  CALL gridman_grid_deallocate(combined,ierr)
183  CALL gridman_grid_deallocate(tria,ierr)
184  CALL gridman_grid_deallocate(tria_out,ierr)
185  END SUBROUTINE clean_all
186 
187 C Print error message and exit
188  SUBROUTINE triang_error(STR)
189  USE gridman
190  IMPLICIT NONE
191  CHARACTER(*),INTENT(IN) :: str
192  INTEGER :: lt
193  INTRINSIC len_trim
194  lt=len_trim(str)
195  IF(lt.GT.0)
196  w WRITE(gridman_unit,*) "ERROR in TRIANG: "//str(1:lt)
197  stop "ERROR in TRIANG - see log output. "
198  END SUBROUTINE triang_error
199 
200 C
201  SUBROUTINE print_description
202  INTRINSIC index,len,trim
203  CHARACTER(LEN=FILE_LENGTH) :: path
204  CHARACTER(LEN=128) :: str
205  INTEGER :: i,io
206  CALL get_command_argument(0,path)
207  i=index(path,'/',.true.)
208  IF(i.GT.len(path)) THEN
209  path=''
210  ELSE
211  path=path(1:i)
212  END IF
213 
214  IF(gridman_dbg) WRITE(gridman_unit,*) " PATH:",trim(path)
215 
216  OPEN(unit=3,file=trim(path)//'triang.parameters.description',
217  o status='OLD',iostat=io)
218  IF(io.NE.0) GOTO 100
219 
220  DO
221  READ(3,'(A)',iostat=io,end=200) str
222  IF(io.NE.0) GOTO 200
223  WRITE(*,*) trim(str)
224  END DO
225  200 RETURN
226 
227  100 WRITE(gridman_unit,*)
228  w "Could not find triang.parameters.description",
229  w trim(path)
230  WRITE(gridman_unit,*)
231  w "Use 'which triang' to invoke via the full path"
232 
233  END SUBROUTINE print_description
234 
235  END PROGRAM triang
236 
integer, save, public gridman_unit
Index of the standard output unit.
Definition: gridman.f:116
subroutine gridman_grid2d_merge(GRID, GRID1, GRID2, TOL, IERR)
Merge two 2D grids by connecting their boundary edges.
Definition: merge.f:39
integer, parameter, public gridman_dp
Kind parameter for real numbers.
Definition: gridman.f:93
Explicit interfaces to GRIDMAN subroutines and functions.
Definition: gridman.f:251
subroutine gridman_grid2d_check(GRID, RES, IERR)
Check correctness of the 2D grid object.
Definition: grid2d.f:37
Data-type which describes a grid as a set of edges, methods in grid.f.
Definition: gridman.f:168
subroutine gridman_vtk_grid2d_write(FNAME, HEADER, GRID, NELEMENTS, NCS, NCV, NPS, NPV, IERR, CELL_SCALAR, CSNAME, CELL_VECTOR, CVNAME, POINT_SCALAR, PSNAME, POINT_VECTOR, PVNAME)
Write 2D grid and data in VTK ASCII legacy format.
Definition: vtk.f:611
subroutine gridman_grid2d_triang(TRIA, GRID, IERR)
Triangulation of 2D grid.
Definition: triang.f:55
subroutine gridman_grid_deallocate(GRID, IERR)
Deallocate grid object.
Definition: grid1.f:184
subroutine print_description
Print manual.
Definition: convgrid.f:690
Definition of data types, global constants and variables.
Definition: gridman.f:83
subroutine gridman_index_deallocate(INDEX, IERR)
Allocate index object.
Definition: index.f:98