/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. !> \example ClassicalField/Diffusion/Analytic1DDiffusion/src/Analytic1DDiffusionExample.f90
  42. !! Example program to solve a 1D diffusion equation using OpenCMISS calls.
  43. !!
  44. !! \htmlinclude ClassicalField/Diffusion/Analytic1DDiffusion/history.html
  45. !<
  46. !> Main program
  47. PROGRAM ANALYTIC1DDIFFUSIONEXAMPLE
  48. USE OPENCMISS
  49. #ifdef WIN32
  50. USE IFQWIN
  51. #endif
  52. IMPLICIT NONE
  53. !Test program parameters
  54. REAL(CMISSDP), PARAMETER :: PI=3.141592653589793238462643383279502884197_CMISSDP
  55. INTEGER(CMISSIntg), PARAMETER :: NUMBER_GLOBAL_X_ELEMENTS=6
  56. REAL(CMISSDP), PARAMETER :: LENGTH=3.0_CMISSDP
  57. REAL(CMISSDP), PARAMETER :: END_TIME=0.1_CMISSDP
  58. REAL(CMISSDP), PARAMETER :: TIME_STEP=0.01_CMISSDP
  59. REAL(CMISSDP), PARAMETER :: A=1.0_CMISSDP
  60. REAL(CMISSDP), PARAMETER :: B=PI/2.0_CMISSDP
  61. REAL(CMISSDP), PARAMETER :: C=0.0_CMISSDP
  62. REAL(CMISSDP), PARAMETER :: K=1.0_CMISSDP
  63. INTEGER(CMISSIntg), PARAMETER :: CoordinateSystemUserNumber=1
  64. INTEGER(CMISSIntg), PARAMETER :: RegionUserNumber=2
  65. INTEGER(CMISSIntg), PARAMETER :: BasisUserNumber=3
  66. INTEGER(CMISSIntg), PARAMETER :: GeneratedMeshUserNumber=4
  67. INTEGER(CMISSIntg), PARAMETER :: MeshUserNumber=5
  68. INTEGER(CMISSIntg), PARAMETER :: DecompositionUserNumber=6
  69. INTEGER(CMISSIntg), PARAMETER :: GeometricFieldUserNumber=7
  70. INTEGER(CMISSIntg), PARAMETER :: DependentFieldUserNumber=8
  71. INTEGER(CMISSIntg), PARAMETER :: MaterialsFieldUserNumber=9
  72. INTEGER(CMISSIntg), PARAMETER :: EquationsSetUserNumber=10
  73. INTEGER(CMISSIntg), PARAMETER :: EquationsSetFieldUserNumber=11
  74. INTEGER(CMISSIntg), PARAMETER :: ProblemUserNumber=12
  75. INTEGER(CMISSIntg), PARAMETER :: AnalyticFieldUserNumber=13
  76. !Program types
  77. !Program variables
  78. !CMISS variables
  79. TYPE(CMISSBasisType) :: Basis
  80. TYPE(CMISSCoordinateSystemType) :: CoordinateSystem,WorldCoordinateSystem
  81. TYPE(CMISSDecompositionType) :: Decomposition
  82. TYPE(CMISSEquationsType) :: Equations
  83. TYPE(CMISSEquationsSetType) :: EquationsSet
  84. TYPE(CMISSFieldType) :: GeometricField,DependentField,EquationsSetField,MaterialsField,AnalyticField
  85. TYPE(CMISSFieldsType) :: Fields
  86. TYPE(CMISSGeneratedMeshType) :: GeneratedMesh
  87. TYPE(CMISSMeshType) :: Mesh
  88. TYPE(CMISSProblemType) :: Problem
  89. TYPE(CMISSControlLoopType) :: ControlLoop
  90. TYPE(CMISSRegionType) :: Region,WorldRegion
  91. TYPE(CMISSSolverType) :: Solver, LinearSolver
  92. TYPE(CMISSSolverEquationsType) :: SolverEquations
  93. TYPE(CMISSBoundaryConditionsType) :: BoundaryConditions
  94. #ifdef WIN32
  95. !Quickwin type
  96. LOGICAL :: QUICKWIN_STATUS=.FALSE.
  97. TYPE(WINDOWCONFIG) :: QUICKWIN_WINDOW_CONFIG
  98. #endif
  99. !Generic CMISS variables
  100. INTEGER(CMISSIntg) :: EquationsSetIndex
  101. INTEGER(CMISSIntg) :: Err
  102. #ifdef WIN32
  103. !Initialise QuickWin
  104. QUICKWIN_WINDOW_CONFIG%TITLE="General Output" !Window title
  105. QUICKWIN_WINDOW_CONFIG%NUMTEXTROWS=-1 !Max possible number of rows
  106. QUICKWIN_WINDOW_CONFIG%MODE=QWIN$SCROLLDOWN
  107. !Set the window parameters
  108. QUICKWIN_STATUS=SETWINDOWCONFIG(QUICKWIN_WINDOW_CONFIG)
  109. !If attempt fails set with system estimated values
  110. IF(.NOT.QUICKWIN_STATUS) QUICKWIN_STATUS=SETWINDOWCONFIG(QUICKWIN_WINDOW_CONFIG)
  111. #endif
  112. !Intialise OpenCMISS
  113. CALL CMISSInitialise(WorldCoordinateSystem,WorldRegion,Err)
  114. CALL CMISSErrorHandlingModeSet(CMISS_ERRORS_TRAP_ERROR,Err)
  115. CALL CMISSOutputSetOn("Diffusion1DAnalytic",Err)
  116. !Start the creation of a new RC coordinate system
  117. CALL CMISSCoordinateSystem_Initialise(CoordinateSystem,Err)
  118. CALL CMISSCoordinateSystem_CreateStart(CoordinateSystemUserNumber,CoordinateSystem,Err)
  119. !Set the coordinate system to be 1D
  120. CALL CMISSCoordinateSystem_DimensionSet(CoordinateSystem,1,Err)
  121. !Finish the creation of the coordinate system
  122. CALL CMISSCoordinateSystem_CreateFinish(CoordinateSystem,Err)
  123. !Start the creation of the region
  124. CALL CMISSRegion_Initialise(Region,Err)
  125. CALL CMISSRegion_CreateStart(RegionUserNumber,WorldRegion,Region,Err)
  126. !Label the Region
  127. CALL CMISSRegion_LabelSet(Region,"Region",Err)
  128. !Set the regions coordinate system to the 1D RC coordinate system that we have created
  129. CALL CMISSRegion_CoordinateSystemSet(Region,CoordinateSystem,Err)
  130. !Finish the creation of the region
  131. CALL CMISSRegion_CreateFinish(Region,Err)
  132. !Start the creation of a basis (default is trilinear lagrange)
  133. CALL CMISSBasis_Initialise(Basis,Err)
  134. CALL CMISSBasis_CreateStart(BasisUserNumber,Basis,Err)
  135. !Set the basis to be a linear Lagrange basis
  136. CALL CMISSBasis_NumberOfXiSet(Basis,1,Err)
  137. !Finish the creation of the basis
  138. CALL CMISSBasis_CreateFinish(BASIS,Err)
  139. !Start the creation of a generated mesh in the region
  140. CALL CMISSGeneratedMesh_Initialise(GeneratedMesh,Err)
  141. CALL CMISSGeneratedMesh_CreateStart(GeneratedMeshUserNumber,Region,GeneratedMesh,Err)
  142. !Set up a regular mesh
  143. CALL CMISSGeneratedMesh_TypeSet(GeneratedMesh,CMISS_GENERATED_MESH_REGULAR_MESH_TYPE,Err)
  144. !Set the default basis
  145. CALL CMISSGeneratedMesh_BasisSet(GeneratedMesh,Basis,Err)
  146. !Define the mesh on the region
  147. CALL CMISSGeneratedMesh_ExtentSet(GeneratedMesh,[LENGTH],Err)
  148. CALL CMISSGeneratedMesh_NumberOfElementsSet(GeneratedMesh,[NUMBER_GLOBAL_X_ELEMENTS],Err)
  149. !Finish the creation of a generated mesh in the region
  150. CALL CMISSMesh_Initialise(Mesh,Err)
  151. CALL CMISSGeneratedMesh_CreateFinish(GeneratedMesh,MeshUserNumber,Mesh,Err)
  152. !Create a decomposition
  153. CALL CMISSDecomposition_Initialise(Decomposition,Err)
  154. CALL CMISSDecomposition_CreateStart(DecompositionUserNumber,Mesh,Decomposition,Err)
  155. !Set the decomposition to be a general decomposition with the specified number of domains
  156. CALL CMISSDecomposition_TypeSet(Decomposition,CMISS_DECOMPOSITION_CALCULATED_TYPE,Err)
  157. CALL CMISSDecomposition_NumberOfDomainsSet(Decomposition,1,Err)
  158. !Finish the decomposition
  159. CALL CMISSDecomposition_CreateFinish(Decomposition,Err)
  160. !Start to create a default (geometric) field on the region
  161. CALL CMISSField_Initialise(GeometricField,Err)
  162. CALL CMISSField_CreateStart(GeometricFieldUserNumber,Region,GeometricField,Err)
  163. !Set the decomposition to use
  164. CALL CMISSField_MeshDecompositionSet(GeometricField,Decomposition,Err)
  165. !Set the domain to be used by the field components.
  166. CALL CMISSField_ComponentMeshComponentSet(GeometricField,CMISS_FIELD_U_VARIABLE_TYPE,1,1,Err)
  167. !Finish creating the field
  168. CALL CMISSField_CreateFinish(GeometricField,Err)
  169. !Update the geometric field parameters
  170. CALL CMISSGeneratedMesh_GeometricParametersCalculate(GeneratedMesh,GeometricField,Err)
  171. !Create the equations_set
  172. CALL CMISSEquationsSet_Initialise(EquationsSet,Err)
  173. CALL CMISSField_Initialise(EquationsSetField,Err)
  174. CALL CMISSEquationsSet_CreateStart(EquationsSetUserNumber,Region,GeometricField,CMISS_EQUATIONS_SET_CLASSICAL_FIELD_CLASS, &
  175. & CMISS_EQUATIONS_SET_DIFFUSION_EQUATION_TYPE,CMISS_EQUATIONS_SET_NO_SOURCE_DIFFUSION_SUBTYPE,EquationsSetFieldUserNumber, &
  176. & EquationsSetField,EquationsSet,Err)
  177. !Finish creating the equations set
  178. CALL CMISSEquationsSet_CreateFinish(EquationsSet,Err)
  179. !Create the equations set dependent field variables
  180. CALL CMISSField_Initialise(DependentField,Err)
  181. CALL CMISSEquationsSet_DependentCreateStart(EquationsSet,DependentFieldUserNumber,DependentField,Err)
  182. !Finish the equations set dependent field variables
  183. CALL CMISSEquationsSet_DependentCreateFinish(EquationsSet,Err)
  184. !Create the equations set material field variables
  185. CALL CMISSField_Initialise(MaterialsField,Err)
  186. CALL CMISSEquationsSet_MaterialsCreateStart(EquationsSet,MaterialsFieldUserNumber,MaterialsField,Err)
  187. !Finish the equations set dependent field variables
  188. CALL CMISSEquationsSet_MaterialsCreateFinish(EquationsSet,Err)
  189. !Set the conductivity
  190. CALL CMISSField_ComponentValuesInitialise(MaterialsField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,1,K,Err)
  191. !Create the equations set analytic field variables
  192. CALL CMISSField_Initialise(AnalyticField,Err)
  193. CALL CMISSEquationsSet_AnalyticCreateStart(EquationsSet,CMISS_EQUATIONS_SET_DIFFUSION_EQUATION_ONE_DIM_1, &
  194. & AnalyticFieldUserNumber,AnalyticField,Err)
  195. !Finish the equations set analytic field variables
  196. CALL CMISSEquationsSet_AnalyticCreateFinish(EquationsSet,Err)
  197. !Set the analytic field parameters
  198. !Set the multiplicative constant.
  199. CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,1,A,Err)
  200. !Set the phase.
  201. CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,2,B,Err)
  202. !Set the offset.
  203. CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,3,C,Err)
  204. !Set the length to be the length of the mesh
  205. CALL CMISSField_ComponentValuesInitialise(AnalyticField,CMISS_FIELD_U_VARIABLE_TYPE,CMISS_FIELD_VALUES_SET_TYPE,4,LENGTH,Err)
  206. !Create the equations set equations
  207. CALL CMISSEquations_Initialise(Equations,Err)
  208. CALL CMISSEquationsSet_EquationsCreateStart(EquationsSet,Equations,Err)
  209. !Set the equations matrices sparsity type
  210. CALL CMISSEquations_SparsityTypeSet(Equations,CMISS_EQUATIONS_SPARSE_MATRICES,Err)
  211. !Set the equations set output
  212. !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_NO_OUTPUT,Err)
  213. !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_TIMING_OUTPUT,Err)
  214. !CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_MATRIX_OUTPUT,Err)
  215. CALL CMISSEquations_OutputTypeSet(Equations,CMISS_EQUATIONS_ELEMENT_MATRIX_OUTPUT,Err)
  216. !Finish the equations set equations
  217. CALL CMISSEquationsSet_EquationsCreateFinish(EquationsSet,Err)
  218. !Create the problem
  219. CALL CMISSProblem_Initialise(Problem,Err)
  220. CALL CMISSProblem_CreateStart(ProblemUserNumber,Problem,Err)
  221. !Set the problem to be a No Source Diffusion problem
  222. CALL CMISSProblem_SpecificationSet(Problem,CMISS_PROBLEM_CLASSICAL_FIELD_CLASS,CMISS_PROBLEM_DIFFUSION_EQUATION_TYPE, &
  223. & CMISS_PROBLEM_LINEAR_SOURCE_DIFFUSION_SUBTYPE,Err)
  224. !Finish the creation of a problem.
  225. CALL CMISSProblem_CreateFinish(Problem,Err)
  226. !Create the problem control
  227. CALL CMISSProblem_ControlLoopCreateStart(Problem,Err)
  228. CALL CMISSControlLoop_Initialise(ControlLoop,Err)
  229. !Get the control loop
  230. CALL CMISSProblem_ControlLoopGet(Problem,CMISS_CONTROL_LOOP_NODE,ControlLoop,Err)
  231. !Set the times
  232. CALL CMISSControlLoop_TimesSet(ControlLoop,0.0_CMISSDP,END_TIME,TIME_STEP,Err)
  233. !Finish creating the problem control loop
  234. CALL CMISSProblem_ControlLoopCreateFinish(Problem,Err)
  235. !Start the creation of the problem solvers
  236. CALL CMISSSolver_Initialise(Solver,Err)
  237. CALL CMISSSolver_Initialise(LinearSolver,Err)
  238. CALL CMISSProblem_SolversCreateStart(Problem,Err)
  239. CALL CMISSProblem_SolverGet(Problem,CMISS_CONTROL_LOOP_NODE,1,Solver,Err)
  240. !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_NO_OUTPUT,Err)
  241. !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_PROGRESS_OUTPUT,Err)
  242. !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_TIMING_OUTPUT,Err)
  243. !CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_SOLVER_OUTPUT,Err)
  244. CALL CMISSSolver_OutputTypeSet(Solver,CMISS_SOLVER_MATRIX_OUTPUT,Err)
  245. CALL CMISSSolver_DynamicLinearSolverGet(Solver,LinearSolver,Err)
  246. CALL CMISSSolver_OutputTypeSet(LinearSolver,CMISS_SOLVER_PROGRESS_OUTPUT,Err)
  247. !Finish the creation of the problem solver
  248. CALL CMISSProblem_SolversCreateFinish(Problem,Err)
  249. !Create the problem solver equations
  250. CALL CMISSSolver_Initialise(Solver,Err)
  251. CALL CMISSSolverEquations_Initialise(SolverEquations,Err)
  252. CALL CMISSProblem_SolverEquationsCreateStart(Problem,Err)
  253. !Get the solve equations
  254. CALL CMISSProblem_SolverGet(Problem,CMISS_CONTROL_LOOP_NODE,1,Solver,Err)
  255. CALL CMISSSolver_SolverEquationsGet(Solver,SolverEquations,Err)
  256. !Set the solver equations sparsity
  257. CALL CMISSSolverEquations_SparsityTypeSet(SolverEquations,CMISS_SOLVER_SPARSE_MATRICES,Err)
  258. !CALL CMISSSolverEquations_SparsityTypeSet(SolverEquations,CMISS_SOLVER_FULL_MATRICES,Err)
  259. !Add in the equations set
  260. CALL CMISSSolverEquations_EquationsSetAdd(SolverEquations,EquationsSet,EquationsSetIndex,Err)
  261. !Finish the creation of the problem solver equations
  262. CALL CMISSProblem_SolverEquationsCreateFinish(Problem,Err)
  263. !Create the equations set boundary conditions
  264. CALL CMISSBoundaryConditions_Initialise(BoundaryConditions,Err)
  265. CALL CMISSSolverEquations_BoundaryConditionsCreateStart(SolverEquations,BoundaryConditions,Err)
  266. CALL CMISSSolverEquations_BoundaryConditionsAnalytic(SolverEquations,Err)
  267. CALL CMISSSolverEquations_BoundaryConditionsCreateFinish(SolverEquations,Err)
  268. !Solve the problem
  269. CALL CMISSProblem_Solve(Problem,Err)
  270. !Output Analytic analysis
  271. !CALL CMISSEquationsSet_AnalyticTimeSet(EquationsSet,END_TIME,Err)
  272. !CALL CMISSEquationsSet_AnalyticEvaluate(EquationsSet,Err)
  273. CALL CMISSAnalyticAnalysisOutput(DependentField,"Diffusion1DAnalytic",Err)
  274. !Output fields
  275. CALL CMISSFields_Initialise(Fields,Err)
  276. CALL CMISSFields_Create(Region,Fields,Err)
  277. CALL CMISSFields_NodesExport(Fields,"Diffusion1DAnalytic","FORTRAN",Err)
  278. CALL CMISSFields_ElementsExport(Fields,"Diffusion1DAnalytic","FORTRAN",Err)
  279. CALL CMISSFields_Finalise(Fields,Err)
  280. !Finalise and quit
  281. CALL CMISSFinalise(Err)
  282. WRITE(*,'(A)') "Program successfully completed."
  283. STOP
  284. END PROGRAM ANALYTIC1DDIFFUSIONEXAMPLE