Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
adjoint_output.f90
Go to the documentation of this file.
1! Copyright (c) 2020-2023, 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!
35 use num_types, only: rp
37 use scalar_scheme, only: scalar_scheme_t
38 use field_list, only: field_list_t
39 use neko_config, only: neko_bcknd_device
40 use device, only: device_memcpy, device_to_host
41 use output, only: output_t
42 implicit none
43 private
44
46 type, public, extends(output_t) :: adjoint_output_t
47 type(field_list_t) :: adjoint
48 contains
49 procedure, pass(this) :: sample => adjoint_output_sample
50 end type adjoint_output_t
51
52 interface adjoint_output_t
53 module procedure adjoint_output_init
54 end interface adjoint_output_t
55
56contains
57
58 function adjoint_output_init(precision, adjoint, scalar, name, path) &
59 result(this)
60 integer, intent(inout) :: precision
61 class(adjoint_fluid_scheme_t), intent(in), target :: adjoint
62 ! TODO
63 ! replace with adjoint scalar
64 class(scalar_scheme_t), intent(in), optional, target :: scalar
65 character(len=*), intent(in), optional :: name
66 character(len=*), intent(in), optional :: path
67 type(adjoint_output_t) :: this
68 character(len=1024) :: fname
69
70 if (present(name) .and. present(path)) then
71 fname = trim(path) // trim(name) // '.fld'
72 else if (present(name)) then
73 fname = trim(name) // '.fld'
74 else if (present(path)) then
75 fname = trim(path) // 'adjoint.fld'
76 else
77 fname = 'adjoint.fld'
78 end if
79
80 call this%init_base(fname, precision)
81
82 if (present(scalar)) then
83 call this%adjoint%init(5)
84 else
85 call this%adjoint%init(4)
86 end if
87
88 call this%adjoint%assign(1, adjoint%p_adj)
89 call this%adjoint%assign(2, adjoint%u_adj)
90 call this%adjoint%assign(3, adjoint%v_adj)
91 call this%adjoint%assign(4, adjoint%w_adj)
92
93 if (present(scalar)) then
94 call this%adjoint%assign(5, scalar%s)
95 end if
96
97 end function adjoint_output_init
98
100 subroutine adjoint_output_sample(this, t)
101 class(adjoint_output_t), intent(inout) :: this
102 real(kind=rp), intent(in) :: t
103 integer :: i
104
105 if (neko_bcknd_device .eq. 1) then
106
107 associate(fields => this%adjoint%items)
108 do i = 1, size(fields)
109 call device_memcpy(fields(i)%ptr%x, fields(i)%ptr%x_d, &
110 fields(i)%ptr%dof%size(), device_to_host, &
111 sync = (i .eq. size(fields))) ! Sync on the last field
112 end do
113 end associate
114
115 end if
116
117 call this%file_%write(this%adjoint, t)
118
119 end subroutine adjoint_output_sample
120
121end module adjoint_output
Defines an output for a adjoint.
subroutine adjoint_output_sample(this, t)
Sample a adjoint solution at time t.
type(adjoint_output_t) function adjoint_output_init(precision, adjoint, scalar, name, path)