Neko  0.8.1
A portable framework for high-order spectral element flow simulations
scalar_source_term.f90
Go to the documentation of this file.
1 
2 ! Copyright (c) 2024, The Neko Authors
3 ! All rights reserved.
4 !
5 ! Redistribution and use in source and binary forms, with or without
6 ! modification, are permitted provided that the following conditions
7 ! are met:
8 !
9 ! * Redistributions of source code must retain the above copyright
10 ! notice, this list of conditions and the following disclaimer.
11 !
12 ! * Redistributions in binary form must reproduce the above
13 ! copyright notice, this list of conditions and the following
14 ! disclaimer in the documentation and/or other materials provided
15 ! with the distribution.
16 !
17 ! * Neither the name of the authors nor the names of its
18 ! contributors may be used to endorse or promote products derived
19 ! from this software without specific prior written permission.
20 !
21 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 ! POSSIBILITY OF SUCH DAMAGE.
33 !
36  use neko_config, only : neko_bcknd_device
37  use num_types, only : rp
41  use field, only : field_t
42  use field_list, only : field_list_t
43  use json_utils, only : json_get
44  use json_module, only : json_file, json_core, json_value
45  use coefs, only : coef_t
46  use user_intf, only : user_t
47  use utils, only : neko_warning
48  implicit none
49  private
50 
55  type, public :: scalar_source_term_t
57  class(source_term_wrapper_t), allocatable :: source_terms(:)
59  type(field_t), pointer :: f => null()
60  contains
62  procedure, pass(this) :: init => scalar_source_term_init
64  procedure, pass(this) :: free => scalar_source_term_free
66  procedure, pass(this) :: compute => scalar_source_term_compute
68  procedure, nopass, private :: init_user_source
69 
70  end type scalar_source_term_t
71 
72 contains
73 
75  subroutine scalar_source_term_init(this, json, f, coef, user)
76  class(scalar_source_term_t), intent(inout) :: this
77  type(json_file), intent(inout) :: json
78  type(field_t), pointer, intent(in) :: f
79  type(coef_t), intent(inout) :: coef
80  type(user_t), intent(in) :: user
81 
82  type(field_list_t) :: rhs_fields
83  ! Json low-level manipulator.
84  type(json_core) :: core
85  ! Pointer to the source_terms JSON object and the individual sources.
86  type(json_value), pointer :: source_object, source_pointer
87  ! Buffer for serializing the json.
88  character(len=:), allocatable :: buffer
89  ! A single source term as its own json_file.
90  type(json_file) :: source_subdict
91  ! Source type
92  character(len=:), allocatable :: type
93  ! Dummy source strenth values
94  real(kind=rp) :: values(3)
95  logical :: found
96  integer :: n_sources, i
97 
98  call this%free()
99 
100  this%f => f
101 
102 
103  if (json%valid_path('case.scalar.source_terms')) then
104  ! We package the fields for the source term to operate on in a field list.
105  allocate(rhs_fields%fields(1))
106  rhs_fields%fields(1)%f => f
107 
108  call json%get_core(core)
109  call json%get('case.scalar.source_terms', source_object, found)
110 
111  n_sources = core%count(source_object)
112  allocate(this%source_terms(n_sources))
113 
114 
115  do i=1, n_sources
116  ! Create a new json containing just the subdict for this source.
117  call core%get_child(source_object, i, source_pointer, found)
118  call core%print_to_string(source_pointer, buffer)
119  call source_subdict%load_from_string(buffer)
120  call json_get(source_subdict, "type", type)
121 
122  ! The user source is treated separately
123  if ((trim(type) .eq. "user_vector") .or. &
124  (trim(type) .eq. "user_pointwise")) then
125  if (source_subdict%valid_path("start_time") .or. &
126  source_subdict%valid_path("end_time")) then
127  call neko_warning("The start_time and end_time parameters have&
128  & no effect on the scalar user source term")
129  end if
130 
131  call init_user_source(this%source_terms(i)%source_term, &
132  rhs_fields, coef, type, user)
133  else
134 
135  call source_term_factory(this%source_terms(i)%source_term, &
136  source_subdict, rhs_fields, coef)
137  end if
138  end do
139  end if
140 
141  end subroutine scalar_source_term_init
142 
150  subroutine init_user_source(source_term, rhs_fields, coef, type, user)
151  class(source_term_t), allocatable, intent(inout) :: source_term
152  type(field_list_t) :: rhs_fields
153  type(coef_t), intent(inout) :: coef
154  character(len=*) :: type
155  type(user_t), intent(in) :: user
156 
158 
159  select type (source_term)
160  type is (scalar_user_source_term_t)
161  call source_term%init_from_components(rhs_fields, coef, type, &
162  user%scalar_user_f_vector, &
163  user%scalar_user_f)
164  end select
165  end subroutine init_user_source
166 
168  subroutine scalar_source_term_free(this)
169  class(scalar_source_term_t), intent(inout) :: this
170  integer :: i
171 
172  nullify(this%f)
173 
174  if (allocated(this%source_terms)) then
175  do i=1, size(this%source_terms)
176  call this%source_terms(i)%free()
177  end do
178  deallocate(this%source_terms)
179  end if
180 
181  end subroutine scalar_source_term_free
182 
186  subroutine scalar_source_term_compute(this, t, tstep)
187  class(scalar_source_term_t), intent(inout) :: this
188  real(kind=rp), intent(in) :: t
189  integer, intent(in) :: tstep
190  integer :: i, n
191 
192  this%f = 0.0_rp
193 
194  ! Add contribution from all source terms.
195  if (allocated(this%source_terms)) then
196  do i=1, size(this%source_terms)
197  call this%source_terms(i)%source_term%compute(t, tstep)
198  end do
199  end if
200 
201  end subroutine scalar_source_term_compute
202 end module scalar_source_term
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:44
Coefficients.
Definition: coef.f90:34
Defines a field.
Definition: field.f90:34
Utilities for retrieving parameters from the case files.
Definition: json_utils.f90:34
Build configurations.
Definition: neko_config.f90:34
integer, parameter neko_bcknd_device
Definition: neko_config.f90:44
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Implements the scalar_source_term_t type.
subroutine init_user_source(source_term, rhs_fields, coef, type, user)
Initialize the user source term.
subroutine scalar_source_term_init(this, json, f, coef, user)
Constructor.
subroutine scalar_source_term_free(this)
Destructor.
subroutine scalar_source_term_compute(this, t, tstep)
Add all the source term to the passed right-hand side fields.
Implements the scalar_user_source_term_t type.
Defines a factory subroutine for source terms.
subroutine, public source_term_factory(source_term, json, fields, coef)
Source term factory. Both constructs and initializes the object.
Implements the source_term_t type and a wrapper source_term_wrapper_t.
Definition: source_term.f90:34
Interfaces for user interaction with NEKO.
Definition: user_intf.f90:34
Utilities.
Definition: utils.f90:35
subroutine neko_warning(warning_msg)
Definition: utils.f90:191
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition: coef.f90:54
field_list_t, To be able to group fields together
Definition: field_list.f90:7
Wrapper contaning and executing the scalar source terms.
A source-term for the scalar, with procedure pointers pointing to the actual implementation in the us...
Base abstract type for source terms.
Definition: source_term.f90:44
A helper type that is needed to have an array of polymorphic objects.
Definition: source_term.f90:70