Neko  0.9.0
A portable framework for high-order spectral element flow simulations
derivative.f90
Go to the documentation of this file.
1 ! Copyright (c) 2024, The Neko Authors
2 ! All rights reserved.
3 !
4 ! Redistribution and use in source and binary forms, with or without
5 ! modification, are permitted provided that the following conditions
6 ! are met:
7 !
8 ! * Redistributions of source code must retain the above copyright
9 ! notice, this list of conditions and the following disclaimer.
10 !
11 ! * Redistributions in binary form must reproduce the above
12 ! copyright notice, this list of conditions and the following
13 ! disclaimer in the documentation and/or other materials provided
14 ! with the distribution.
15 !
16 ! * Neither the name of the authors nor the names of its
17 ! contributors may be used to endorse or promote products derived
18 ! from this software without specific prior written permission.
19 !
20 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 ! POSSIBILITY OF SUCH DAMAGE.
32 !
33 !
35 
36 module derivative
37  use num_types, only : rp, dp, sp
38  use json_module, only : json_file
41  use field, only : field_t
42  use operators, only : dudxyz
43  use case, only : case_t
46  use field_writer, only : field_writer_t
47  use utils, only : neko_error
48  implicit none
49  private
50 
53  type, public, extends(simulation_component_t) :: derivative_t
55  type(field_t), pointer :: u
57  type(field_t), pointer :: du
59  real(kind=rp), pointer :: dr(:,:,:,:)
61  real(kind=rp), pointer :: ds(:,:,:,:)
63  real(kind=rp), pointer :: dt(:,:,:,:)
65  type(field_writer_t) :: writer
66 
67  contains
69  procedure, pass(this) :: init => derivative_init_from_json
71  procedure, pass(this) :: init_from_attributes => &
74  procedure, pass(this) :: free => derivative_free
76  procedure, pass(this) :: compute_ => derivative_compute
77  end type derivative_t
78 
79 contains
80 
82  subroutine derivative_init_from_json(this, json, case)
83  class(derivative_t), intent(inout) :: this
84  type(json_file), intent(inout) :: json
85  class(case_t), intent(inout), target :: case
86  character(len=:), allocatable :: fieldname
87  character(len=:), allocatable :: direction
88  character(len=20) :: fields(1)
89 
90  ! Add fields keyword to the json so that the field_writer picks it up.
91  ! Will also add fields to the registry.
92  call json_get(json, "field", fieldname)
93  call json_get(json, "direction", direction)
94 
95  fields(1) = "d" // trim(fieldname) // "_d" // direction
96  call json%add("fields", fields)
97 
98  call this%init_base(json, case)
99  call this%writer%init(json, case)
100 
101  call derivative_init_from_attributes(this, fieldname, direction)
102  end subroutine derivative_init_from_json
103 
105  subroutine derivative_init_from_attributes(this, fieldname, direction)
106  class(derivative_t), intent(inout) :: this
107  character(len=*) :: fieldname
108  character(len=*) :: direction
109 
110  this%u => neko_field_registry%get_field_by_name(trim(fieldname))
111 
112  this%du => neko_field_registry%get_field_by_name(&
113  "d" // fieldname // "_d" // direction)
114 
115  if (direction .eq. "x") then
116  this%dr => this%case%fluid%c_Xh%drdx
117  this%ds => this%case%fluid%c_Xh%dsdx
118  this%dt => this%case%fluid%c_Xh%dtdx
119  else if (direction .eq. "y") then
120  this%dr => this%case%fluid%c_Xh%drdy
121  this%ds => this%case%fluid%c_Xh%dsdy
122  this%dt => this%case%fluid%c_Xh%dtdy
123  else if (direction .eq. "z") then
124  this%dr => this%case%fluid%c_Xh%drdz
125  this%ds => this%case%fluid%c_Xh%dsdz
126  this%dt => this%case%fluid%c_Xh%dtdz
127  else
128  call neko_error("The direction of the derivative must be x, y or z")
129  end if
130  end subroutine derivative_init_from_attributes
131 
133  subroutine derivative_free(this)
134  class(derivative_t), intent(inout) :: this
135  call this%free_base()
136  call this%writer%free()
137  nullify(this%du)
138  nullify(this%u)
139  nullify(this%dr)
140  nullify(this%ds)
141  nullify(this%dt)
142  end subroutine derivative_free
143 
147  subroutine derivative_compute(this, t, tstep)
148  class(derivative_t), intent(inout) :: this
149  real(kind=rp), intent(in) :: t
150  integer, intent(in) :: tstep
151 
152  call dudxyz(this%du%x, this%u%x, this%dr, this%dr, this%dr,&
153  this%case%fluid%c_Xh)
154  end subroutine derivative_compute
155 
156 end module derivative
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Definition: json_utils.f90:54
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:45
Defines a simulation case.
Definition: case.f90:34
Implements the derivative_t type.
Definition: derivative.f90:36
subroutine derivative_init_from_attributes(this, fieldname, direction)
Actual constructor.
Definition: derivative.f90:106
subroutine derivative_free(this)
Destructor.
Definition: derivative.f90:134
subroutine derivative_compute(this, t, tstep)
Compute the derivative field.
Definition: derivative.f90:148
subroutine derivative_init_from_json(this, json, case)
Constructor from json.
Definition: derivative.f90:83
Defines a registry for storing solution fields.
type(field_registry_t), target, public neko_field_registry
Global field registry.
Implements the field_writer_t type.
Defines a field.
Definition: field.f90:34
Implements fld_file_output_t.
Utilities for retrieving parameters from the case files.
Definition: json_utils.f90:34
integer, parameter, public dp
Definition: num_types.f90:9
integer, parameter, public sp
Definition: num_types.f90:8
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Operators.
Definition: operators.f90:34
subroutine, public dudxyz(du, u, dr, ds, dt, coef)
Compute derivative of a scalar field along a single direction.
Definition: operators.f90:76
Simulation components are objects that encapsulate functionality that can be fit to a particular comp...
Utilities.
Definition: utils.f90:35
A simulation component that computes a derivative of a field. Wraps the duxyz operator.
Definition: derivative.f90:53
A simulation component that writes a 3d field to a file.
A simple output saving a list of fields to a .fld file.
Base abstract class for simulation components.