PageRenderTime 33ms CodeModel.GetById 9ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ClassicalField/Diffusion/Analytic1DDiffusion/src/Analytic1DDiffusionExample.f90

http://github.com/xyan075/examples
FORTRAN Modern | 326 lines | 158 code | 42 blank | 126 comment | 0 complexity | ceb7fbe67449d11cad19eb66871c26dc MD5 | raw file
  1!> \file
  2!> \author Chris Bradley
  3!> \brief This is an example program to solve a 1D diffusion equation and compare it to the analytic solution using OpenCMISS calls.
  4!>
  5!> \section LICENSE
  6!>
  7!> Version: MPL 1.1/GPL 2.0/LGPL 2.1
  8!>
  9!> The contents of this file are subject to the Mozilla Public License
 10!> Version 1.1 (the "License"); you may not use this file except in
 11!> compliance with the License. You may obtain a copy of the License at
 12!> http://www.mozilla.org/MPL/
 13!>
 14!> Software distributed under the License is distributed on an "AS IS"
 15!> basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 16!> License for the specific language governing rights and limitations
 17!> under the License.
 18!>
 19!> The Original Code is OpenCMISS
 20!>
 21!> The Initial Developer of the Original Code is University of Auckland,
 22!> Auckland, New Zealand and University of Oxford, Oxford, United
 23!> Kingdom. Portions created by the University of Auckland and University
 24!> of Oxford are Copyright (C) 2007 by the University of Auckland and
 25!> the University of Oxford. All Rights Reserved.
 26!>
 27!> Contributor(s):
 28!>
 29!> Alternatively, the contents of this file may be used under the terms of
 30!> either the GNU General Public License Version 2 or later (the "GPL"), or
 31!> the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 32!> in which case the provisions of the GPL or the LGPL are applicable instead
 33!> of those above. If you wish to allow use of your version of this file only
 34!> under the terms of either the GPL or the LGPL, and not to allow others to
 35!> use your version of this file under the terms of the MPL, indicate your
 36!> decision by deleting the provisions above and replace them with the notice
 37!> and other provisions required by the GPL or the LGPL. If you do not delete
 38!> the provisions above, a recipient may use your version of this file under
 39!> the terms of any one of the MPL, the GPL or the LGPL.
 40!>
 41
 42!> \example ClassicalField/Diffusion/Analytic1DDiffusion/src/Analytic1DDiffusionExample.f90
 43!! Example program to solve a 1D diffusion equation using OpenCMISS calls.
 44!!
 45!! \htmlinclude ClassicalField/Diffusion/Analytic1DDiffusion/history.html
 46!<
 47
 48!> Main program
 49PROGRAM ANALYTIC1DDIFFUSIONEXAMPLE
 50
 51  USE OPENCMISS
 52
 53#ifdef WIN32
 54  USE IFQWIN
 55#endif
 56
 57  IMPLICIT NONE
 58
 59  !Test program parameters
 60
 61  REAL(CMISSDP), PARAMETER ::    PI=3.141592653589793238462643383279502884197_CMISSDP
 62
 63  INTEGER(CMISSIntg), PARAMETER :: NUMBER_GLOBAL_X_ELEMENTS=6
 64  REAL(CMISSDP), PARAMETER :: LENGTH=3.0_CMISSDP
 65  REAL(CMISSDP), PARAMETER :: END_TIME=0.1_CMISSDP
 66  REAL(CMISSDP), PARAMETER :: TIME_STEP=0.01_CMISSDP
 67  REAL(CMISSDP), PARAMETER :: A=1.0_CMISSDP
 68  REAL(CMISSDP), PARAMETER :: B=PI/2.0_CMISSDP
 69  REAL(CMISSDP), PARAMETER :: C=0.0_CMISSDP
 70  REAL(CMISSDP), PARAMETER :: K=1.0_CMISSDP
 71  
 72  INTEGER(CMISSIntg), PARAMETER :: CoordinateSystemUserNumber=1
 73  INTEGER(CMISSIntg), PARAMETER :: RegionUserNumber=2
 74  INTEGER(CMISSIntg), PARAMETER :: BasisUserNumber=3
 75  INTEGER(CMISSIntg), PARAMETER :: GeneratedMeshUserNumber=4
 76  INTEGER(CMISSIntg), PARAMETER :: MeshUserNumber=5
 77  INTEGER(CMISSIntg), PARAMETER :: DecompositionUserNumber=6
 78  INTEGER(CMISSIntg), PARAMETER :: GeometricFieldUserNumber=7
 79  INTEGER(CMISSIntg), PARAMETER :: DependentFieldUserNumber=8
 80  INTEGER(CMISSIntg), PARAMETER :: MaterialsFieldUserNumber=9
 81  INTEGER(CMISSIntg), PARAMETER :: EquationsSetUserNumber=10
 82  INTEGER(CMISSIntg), PARAMETER :: EquationsSetFieldUserNumber=11
 83  INTEGER(CMISSIntg), PARAMETER :: ProblemUserNumber=12
 84  INTEGER(CMISSIntg), PARAMETER :: AnalyticFieldUserNumber=13
 85  !Program types
 86  
 87  !Program variables
 88  
 89  !CMISS variables
 90
 91  TYPE(CMISSBasisType) :: Basis
 92  TYPE(CMISSCoordinateSystemType) :: CoordinateSystem,WorldCoordinateSystem
 93  TYPE(CMISSDecompositionType) :: Decomposition
 94  TYPE(CMISSEquationsType) :: Equations
 95  TYPE(CMISSEquationsSetType) :: EquationsSet
 96  TYPE(CMISSFieldType) :: GeometricField,DependentField,EquationsSetField,MaterialsField,AnalyticField
 97  TYPE(CMISSFieldsType) :: Fields
 98  TYPE(CMISSGeneratedMeshType) :: GeneratedMesh  
 99  TYPE(CMISSMeshType) :: Mesh
100  TYPE(CMISSProblemType) :: Problem
101  TYPE(CMISSControlLoopType) :: ControlLoop
102  TYPE(CMISSRegionType) :: Region,WorldRegion
103  TYPE(CMISSSolverType) :: Solver, LinearSolver
104  TYPE(CMISSSolverEquationsType) :: SolverEquations
105  TYPE(CMISSBoundaryConditionsType) :: BoundaryConditions
106
107#ifdef WIN32
108  !Quickwin type
109  LOGICAL :: QUICKWIN_STATUS=.FALSE.
110  TYPE(WINDOWCONFIG) :: QUICKWIN_WINDOW_CONFIG
111#endif
112  
113  !Generic CMISS variables
114  
115  INTEGER(CMISSIntg) :: EquationsSetIndex
116  INTEGER(CMISSIntg) :: Err
117  
118#ifdef WIN32
119  !Initialise QuickWin
120  QUICKWIN_WINDOW_CONFIG%TITLE="General Output" !Window title
121  QUICKWIN_WINDOW_CONFIG%NUMTEXTROWS=-1 !Max possible number of rows
122  QUICKWIN_WINDOW_CONFIG%MODE=QWIN$SCROLLDOWN
123  !Set the window parameters
124  QUICKWIN_STATUS=SETWINDOWCONFIG(QUICKWIN_WINDOW_CONFIG)
125  !If attempt fails set with system estimated values
126  IF(.NOT.QUICKWIN_STATUS) QUICKWIN_STATUS=SETWINDOWCONFIG(QUICKWIN_WINDOW_CONFIG)
127#endif
128
129  !Intialise OpenCMISS
130  CALL CMISSInitialise(WorldCoordinateSystem,WorldRegion,Err)
131
132  CALL CMISSErrorHandlingModeSet(CMISS_ERRORS_TRAP_ERROR,Err)
133
134  CALL CMISSOutputSetOn("Diffusion1DAnalytic",Err)
135  
136  !Start the creation of a new RC coordinate system
137  CALL CMISSCoordinateSystem_Initialise(CoordinateSystem,Err)
138  CALL CMISSCoordinateSystem_CreateStart(CoordinateSystemUserNumber,CoordinateSystem,Err)
139  !Set the coordinate system to be 1D
140  CALL CMISSCoordinateSystem_DimensionSet(CoordinateSystem,1,Err)
141  !Finish the creation of the coordinate system
142  CALL CMISSCoordinateSystem_CreateFinish(CoordinateSystem,Err)
143  
144  !Start the creation of the region
145  CALL CMISSRegion_Initialise(Region,Err)
146  CALL CMISSRegion_CreateStart(RegionUserNumber,WorldRegion,Region,Err)
147  !Label the Region
148  CALL CMISSRegion_LabelSet(Region,"Region",Err)
149  !Set the regions coordinate system to the 1D RC coordinate system that we have created
150  CALL CMISSRegion_CoordinateSystemSet(Region,CoordinateSystem,Err)
151  !Finish the creation of the region
152  CALL CMISSRegion_CreateFinish(Region,Err)
153  
154  !Start the creation of a basis (default is trilinear lagrange)
155  CALL CMISSBasis_Initialise(Basis,Err)
156  CALL CMISSBasis_CreateStart(BasisUserNumber,Basis,Err)
157  !Set the basis to be a linear Lagrange basis
158  CALL CMISSBasis_NumberOfXiSet(Basis,1,Err)
159  !Finish the creation of the basis
160  CALL CMISSBasis_CreateFinish(BASIS,Err)
161  
162  !Start the creation of a generated mesh in the region
163  CALL CMISSGeneratedMesh_Initialise(GeneratedMesh,Err)
164  CALL CMISSGeneratedMesh_CreateStart(GeneratedMeshUserNumber,Region,GeneratedMesh,Err)
165  !Set up a regular mesh
166  CALL CMISSGeneratedMesh_TypeSet(GeneratedMesh,CMISS_GENERATED_MESH_REGULAR_MESH_TYPE,Err)
167  !Set the default basis
168  CALL CMISSGeneratedMesh_BasisSet(GeneratedMesh,Basis,Err)   
169  !Define the mesh on the region
170  CALL CMISSGeneratedMesh_ExtentSet(GeneratedMesh,[LENGTH],Err)
171  CALL CMISSGeneratedMesh_NumberOfElementsSet(GeneratedMesh,[NUMBER_GLOBAL_X_ELEMENTS],Err)
172  !Finish the creation of a generated mesh in the region
173  CALL CMISSMesh_Initialise(Mesh,Err)
174  CALL CMISSGeneratedMesh_CreateFinish(GeneratedMesh,MeshUserNumber,Mesh,Err)
175  
176  !Create a decomposition
177  CALL CMISSDecomposition_Initialise(Decomposition,Err)
178  CALL CMISSDecomposition_CreateStart(DecompositionUserNumber,Mesh,Decomposition,Err)
179  !Set the decomposition to be a general decomposition with the specified number of domains
180  CALL CMISSDecomposition_TypeSet(Decomposition,CMISS_DECOMPOSITION_CALCULATED_TYPE,Err)
181  CALL CMISSDecomposition_NumberOfDomainsSet(Decomposition,1,Err)
182  !Finish the decomposition
183  CALL CMISSDecomposition_CreateFinish(Decomposition,Err)
184  
185  !Start to create a default (geometric) field on the region
186  CALL CMISSField_Initialise(GeometricField,Err)
187  CALL CMISSField_CreateStart(GeometricFieldUserNumber,Region,GeometricField,Err)
188  !Set the decomposition to use
189  CALL CMISSField_MeshDecompositionSet(GeometricField,Decomposition,Err)
190  !Set the domain to be used by the field components.
191  CALL CMISSField_ComponentMeshComponentSet(GeometricField,CMISS_FIELD_U_VARIABLE_TYPE,1,1,Err)
192  !Finish creating the field
193  CALL CMISSField_CreateFinish(GeometricField,Err)
194       
195  !Update the geometric field parameters
196  CALL CMISSGeneratedMesh_GeometricParametersCalculate(GeneratedMesh,GeometricField,Err)
197  
198  !Create the equations_set
199  CALL CMISSEquationsSet_Initialise(EquationsSet,Err)
200  CALL CMISSField_Initialise(EquationsSetField,Err)
201  CALL CMISSEquationsSet_CreateStart(EquationsSetUserNumber,Region,GeometricField,CMISS_EQUATIONS_SET_CLASSICAL_FIELD_CLASS, &
202    & CMISS_EQUATIONS_SET_DIFFUSION_EQUATION_TYPE,CMISS_EQUATIONS_SET_NO_SOURCE_DIFFUSION_SUBTYPE,EquationsSetFieldUserNumber, &
203    & EquationsSetField,EquationsSet,Err)
204  !Finish creating the equations set
205  CALL CMISSEquationsSet_CreateFinish(EquationsSet,Err)
206
207  !Create the equations set dependent field variables
208  CALL CMISSField_Initialise(DependentField,Err)
209  CALL CMISSEquationsSet_DependentCreateStart(EquationsSet,DependentFieldUserNumber,DependentField,Err)
210  !Finish the equations set dependent field variables
211  CALL CMISSEquationsSet_DependentCreateFinish(EquationsSet,Err)
212
213  !Create the equations set material field variables
214  CALL CMISSField_Initialise(MaterialsField,Err)
215  CALL CMISSEquationsSet_MaterialsCreateStart(EquationsSet,MaterialsFieldUserNumber,MaterialsField,Err)
216  !Finish the equations set dependent field variables
217  CALL CMISSEquationsSet_MaterialsCreateFinish(EquationsSet,Err)
218  !Set the conductivity
219  CALL CMISSField_ComponentValuesInitialise(MaterialsField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,1,K,Err)
220 
221  !Create the equations set analytic field variables
222  CALL CMISSField_Initialise(AnalyticField,Err)
223  CALL CMISSEquationsSet_AnalyticCreateStart(EquationsSet,CMISS_EQUATIONS_SET_DIFFUSION_EQUATION_ONE_DIM_1, & 
224    & AnalyticFieldUserNumber,AnalyticField,Err)
225  !Finish the equations set analytic field variables
226  CALL CMISSEquationsSet_AnalyticCreateFinish(EquationsSet,Err)
227  !Set the analytic field parameters
228  !Set the multiplicative constant. 
229  CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,1,A,Err)
230  !Set the phase. 
231  CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,2,B,Err)
232  !Set the offset. 
233  CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,3,C,Err)
234  !Set the length to be the length of the mesh
235  CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,4,LENGTH,Err)
236  
237  !Create the equations set equations
238  CALL CMISSEquations_Initialise(Equations,Err)
239  CALL CMISSEquationsSet_EquationsCreateStart(EquationsSet,Equations,Err)
240  !Set the equations matrices sparsity type
241  CALL CMISSEquations_SparsityTypeSet(Equations,CMISS_EQUATIONS_SPARSE_MATRICES,Err)
242  !Set the equations set output
243  !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_NO_OUTPUT,Err)
244  !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_TIMING_OUTPUT,Err)
245  !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_MATRIX_OUTPUT,Err)
246  CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_ELEMENT_MATRIX_OUTPUT,Err)
247  !Finish the equations set equations
248  CALL CMISSEquationsSet_EquationsCreateFinish(EquationsSet,Err)
249
250  !Create the problem
251  CALL CMISSProblem_Initialise(Problem,Err)
252  CALL CMISSProblem_CreateStart(ProblemUserNumber,Problem,Err)
253  !Set the problem to be a No Source Diffusion problem
254  CALL CMISSProblem_SpecificationSet(Problem,CMISS_PROBLEM_CLASSICAL_FIELD_CLASS,CMISS_PROBLEM_DIFFUSION_EQUATION_TYPE, &
255    & CMISS_PROBLEM_LINEAR_SOURCE_DIFFUSION_SUBTYPE,Err)
256  !Finish the creation of a problem.
257  CALL CMISSProblem_CreateFinish(Problem,Err)
258
259  !Create the problem control
260  CALL CMISSProblem_ControlLoopCreateStart(Problem,Err)
261  CALL CMISSControlLoop_Initialise(ControlLoop,Err)
262  !Get the control loop
263  CALL CMISSProblem_ControlLoopGet(Problem,CMISS_CONTROL_LOOP_NODE,ControlLoop,Err)
264  !Set the times
265  CALL CMISSControlLoop_TimesSet(ControlLoop,0.0_CMISSDP,END_TIME,TIME_STEP,Err)
266  !Finish creating the problem control loop
267  CALL CMISSProblem_ControlLoopCreateFinish(Problem,Err)
268
269  !Start the creation of the problem solvers
270  CALL CMISSSolver_Initialise(Solver,Err)
271  CALL CMISSSolver_Initialise(LinearSolver,Err)
272  CALL CMISSProblem_SolversCreateStart(Problem,Err)
273  CALL CMISSProblem_SolverGet(Problem,CMISS_CONTROL_LOOP_NODE,1,Solver,Err)
274  !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_NO_OUTPUT,Err)
275  !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_PROGRESS_OUTPUT,Err)
276  !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_TIMING_OUTPUT,Err)
277  !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_SOLVER_OUTPUT,Err)
278  CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_MATRIX_OUTPUT,Err)
279  CALL CMISSSolver_DynamicLinearSolverGet(Solver,LinearSolver,Err)
280  CALL CMISSSolver_OutputTypeSet(LinearSolver,CMISS_SOLVER_PROGRESS_OUTPUT,Err)
281  !Finish the creation of the problem solver
282  CALL CMISSProblem_SolversCreateFinish(Problem,Err)
283
284  !Create the problem solver equations
285  CALL CMISSSolver_Initialise(Solver,Err)
286  CALL CMISSSolverEquations_Initialise(SolverEquations,Err)
287  CALL CMISSProblem_SolverEquationsCreateStart(Problem,Err)
288  !Get the solve equations
289  CALL CMISSProblem_SolverGet(Problem,CMISS_CONTROL_LOOP_NODE,1,Solver,Err)
290  CALL CMISSSolver_SolverEquationsGet(Solver,SolverEquations,Err)
291  !Set the solver equations sparsity
292  CALL CMISSSolverEquations_SparsityTypeSet(SolverEquations,CMISS_SOLVER_SPARSE_MATRICES,Err)
293  !CALL CMISSSolverEquations_SparsityTypeSet(SolverEquations,CMISS_SOLVER_FULL_MATRICES,Err)  
294  !Add in the equations set
295  CALL CMISSSolverEquations_EquationsSetAdd(SolverEquations,EquationsSet,EquationsSetIndex,Err)
296  !Finish the creation of the problem solver equations
297  CALL CMISSProblem_SolverEquationsCreateFinish(Problem,Err)
298
299  !Create the equations set boundary conditions
300  CALL CMISSBoundaryConditions_Initialise(BoundaryConditions,Err)
301  CALL CMISSSolverEquations_BoundaryConditionsCreateStart(SolverEquations,BoundaryConditions,Err)
302  CALL CMISSSolverEquations_BoundaryConditionsAnalytic(SolverEquations,Err)
303  CALL CMISSSolverEquations_BoundaryConditionsCreateFinish(SolverEquations,Err)
304
305  !Solve the problem
306  CALL CMISSProblem_Solve(Problem,Err)
307
308  !Output Analytic analysis
309  !CALL CMISSEquationsSet_AnalyticTimeSet(EquationsSet,END_TIME,Err)
310  !CALL CMISSEquationsSet_AnalyticEvaluate(EquationsSet,Err)
311  CALL CMISSAnalyticAnalysisOutput(DependentField,"Diffusion1DAnalytic",Err)
312
313  !Output fields
314  CALL CMISSFields_Initialise(Fields,Err)
315  CALL CMISSFields_Create(Region,Fields,Err)
316  CALL CMISSFields_NodesExport(Fields,"Diffusion1DAnalytic","FORTRAN",Err)
317  CALL CMISSFields_ElementsExport(Fields,"Diffusion1DAnalytic","FORTRAN",Err)
318  CALL CMISSFields_Finalise(Fields,Err)
319
320  !Finalise and quit
321  CALL CMISSFinalise(Err)
322  WRITE(*,'(A)') "Program successfully completed."
323
324  STOP
325  
326END PROGRAM ANALYTIC1DDIFFUSIONEXAMPLE