Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/plugins/python/geogate_phases_python.F90
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module geogate_phases_python
use ESMF, only: ESMF_VM, ESMF_VMGet, ESMF_VMBarrier, ESMF_Mesh, ESMF_MeshGet
use ESMF, only: ESMF_MAXSTR, ESMF_GEOMTYPE_GRID, ESMF_GEOMTYPE_MESH
use ESMF, only: ESMF_UtilStringLowerCase, ESMF_FieldBundleIsCreated
use ESMF, only: ESMF_TraceRegionEnter, ESMF_TraceRegionExit

use NUOPC, only: NUOPC_CompAttributeGet, NUOPC_GetAttribute
use NUOPC_Model, only: NUOPC_ModelGet
Expand Down Expand Up @@ -74,7 +75,7 @@ subroutine geogate_phases_python_run(gcomp, rc)
type(ESMF_Clock) :: clock
type(ESMF_VM) :: vm
integer, save :: timeStep = 0
logical :: impOnExpMesh
logical, save :: remapImportFields = .false.
character(ESMF_MAXSTR) :: namespace
character(ESMF_MAXSTR) :: cvalue, message, timeStr
character(len=*), parameter :: subname = trim(modName)//':(geogate_phases_python_run) '
Expand All @@ -83,6 +84,9 @@ subroutine geogate_phases_python_run(gcomp, rc)
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)

! Enter trace region
call ESMF_TraceRegionEnter('geogate_phases_python_run')

! Get internal state
nullify(is_local%wrap)
call ESMF_GridCompGetInternalState(gcomp, is_local, rc)
Expand Down Expand Up @@ -124,11 +128,11 @@ subroutine geogate_phases_python_run(gcomp, rc)
call NUOPC_CompAttributeGet(gcomp, name='ImportOnExportMesh', value=cvalue, &
isPresent=isPresent, isSet=isSet, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
impOnExpMesh = .false.
remapImportFields = .false.
if (isPresent .and. isSet) then
if (trim(cvalue) .eq. '.true.' .or. trim(cvalue) .eq. 'true' .or. trim(adjustl(cvalue)) .eq. 'T') impOnExpMesh = .true.
if (trim(cvalue) .eq. '.true.' .or. trim(cvalue) .eq. 'true' .or. trim(adjustl(cvalue)) .eq. 'T') remapImportFields = .true.
end if
write(message, fmt='(A,L)') trim(subname)//' : ImportOnExportMesh = ', impOnExpMesh
write(message, fmt='(A,L)') trim(subname)//' : ImportOnExportMesh = ', remapImportFields
call ESMF_LogWrite(trim(message), ESMF_LOGMSG_INFO)

! Set flag
Expand Down Expand Up @@ -158,12 +162,13 @@ subroutine geogate_phases_python_run(gcomp, rc)
call FB2Node(is_local%wrap%FBImp(n), "import", trim(is_local%wrap%compName(n)), myMeshImp(n), node, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Interpolate import fields to export mesh and add them to node
if (impOnExpMesh) then
if (remapImportFields) then
! Interpolate import fields to export mesh and create new FB
call FB_copy(is_local%wrap%FBImp(n), is_local%wrap%FBImpIntp(n), &
'FBImpOnExp'//trim(is_local%wrap%compName(n)), &
is_local%wrap%meshExp, is_local%wrap%RHImp2Exp(n), rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return

! Load content of FB to Conduit node
call FB2Node(is_local%wrap%FBImpIntp(n), "import_on_export_grid", &
trim(is_local%wrap%compName(n)), myMeshExp, node, rc=rc)
Expand Down Expand Up @@ -223,6 +228,9 @@ subroutine geogate_phases_python_run(gcomp, rc)
! Increase time step
timeStep = timeStep+1

! Exit trace region
call ESMF_TraceRegionExit('geogate_phases_python_run')

call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)

end subroutine geogate_phases_python_run
Expand Down Expand Up @@ -253,6 +261,9 @@ subroutine FB2Node(FBin, parent, compName, myMesh, node, rc)
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called for '//trim(compName), ESMF_LOGMSG_INFO)

! Enter trace region
call ESMF_TraceRegionEnter('FB2Node')

! Add channel
channel = conduit_node_fetch(node, "channels/"//trim(parent)//"/"//trim(compName))

Expand Down Expand Up @@ -339,9 +350,12 @@ subroutine FB2Node(FBin, parent, compName, myMesh, node, rc)
nullify(farrayPtr)
end do

! Clean memeory
! Clean memory
deallocate(fieldNameList)

! Exit trace region
call ESMF_TraceRegionExit('FB2Node')

call ESMF_LogWrite(subname//' done for '//trim(compName), ESMF_LOGMSG_INFO)

end subroutine FB2Node
Expand Down Expand Up @@ -371,6 +385,9 @@ subroutine Node2FB(FBin, compName, node, rc)
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called for '//trim(compName), ESMF_LOGMSG_INFO)

! Enter trace region
call ESMF_TraceRegionEnter('Node2FB')

! Create node with fields
if (conduit_node_has_path(node, "data/fields")) then
fields = conduit_node_fetch(node, "data/fields")
Expand Down Expand Up @@ -419,6 +436,9 @@ subroutine Node2FB(FBin, compName, node, rc)
end do
end if

! Exit trace region
call ESMF_TraceRegionExit('Node2FB')

call ESMF_LogWrite(subname//' done for '//trim(compName), ESMF_LOGMSG_INFO)

end subroutine Node2FB
Expand Down
99 changes: 89 additions & 10 deletions src/plugins/python/geogate_python_interface.F90
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module geogate_python_interface

!-----------------------------------------------------------------------------
! Void phase for Python interaction
! Void phase for Python interaction
!-----------------------------------------------------------------------------

use ESMF, only: ESMF_LogWrite, ESMF_LOGMSG_INFO
use, intrinsic :: iso_c_binding, only : C_PTR, C_CHAR
use ESMF, only: ESMF_TraceRegionEnter, ESMF_TraceRegionExit

use, intrinsic :: iso_c_binding, only : C_PTR, C_CHAR, C_NULL_CHAR

implicit none

Expand All @@ -14,25 +16,29 @@ module geogate_python_interface
!-----------------------------------------------------------------------------

interface
subroutine conduit_fort_to_py(nodeIn, py_script) bind(C, name="conduit_fort_to_py")
subroutine c_conduit_fort_to_py(nodeIn, py_script, node_in_name) bind(C, name="conduit_fort_to_py")
use iso_c_binding
implicit none
type(C_PTR), value, intent(in) :: nodeIn
character(kind=C_CHAR), intent(in) :: py_script(*)
end subroutine conduit_fort_to_py
character(kind=C_CHAR), intent(in) :: node_in_name(*)
end subroutine c_conduit_fort_to_py

function c_conduit_fort_from_py(py_script) result(resOut) bind(C, name="conduit_fort_from_py")
function c_conduit_fort_from_py(py_script, node_out_name) result(resOut) bind(C, name="conduit_fort_from_py")
use iso_c_binding
implicit none
character(kind=C_CHAR), intent(in) :: py_script(*)
character(kind=C_CHAR), intent(in) :: node_out_name(*)
type(C_PTR) :: resOut
end function c_conduit_fort_from_py

function c_conduit_fort_to_py_to_fort(nodeIn, py_script) result(nodeOut) bind(C, name="conduit_fort_to_py_to_fort")
function c_conduit_fort_to_py_to_fort(nodeIn, py_script, node_in_name, node_out_name) result(nodeOut) bind(C, name="conduit_fort_to_py_to_fort")
use iso_c_binding
implicit none
type(C_PTR), value, intent(in) :: nodeIn
character(kind=C_CHAR), intent(in) :: py_script(*)
character(kind=C_CHAR), intent(in) :: node_in_name(*)
character(kind=C_CHAR), intent(in) :: node_out_name(*)
type(C_PTR) :: nodeOut
end function c_conduit_fort_to_py_to_fort
end interface
Expand All @@ -48,12 +54,47 @@ end function c_conduit_fort_to_py_to_fort
contains
!===============================================================================

function conduit_fort_from_py(py_script) result(nodeOut)
subroutine conduit_fort_to_py(nodeIn, py_script, node_in_name)
use iso_c_binding
implicit none

! input/output variables
type(C_PTR), intent(in) :: nodeIn
character(*), intent(in) :: py_script
character(*), intent(in), optional :: node_in_name

! local variables
character(len=*), parameter :: subname = trim(modName)//':(conduit_fort_to_py) '
!---------------------------------------------------------------------------

call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)

! Enter trace region
call ESMF_TraceRegionEnter('conduit_fort_to_py')

! Run Python script
if (present(node_in_name)) then
call c_conduit_fort_to_py(nodeIn, trim(py_script)//C_NULL_CHAR, &
trim(node_in_name)//C_NULL_CHAR)
else
call c_conduit_fort_to_py(nodeIn, trim(py_script)//C_NULL_CHAR, &
'my_node'//C_NULL_CHAR)
end if

! Exit trace region
call ESMF_TraceRegionExit('conduit_fort_to_py')

call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)

end subroutine conduit_fort_to_py

function conduit_fort_from_py(py_script, node_out_name) result(nodeOut)
use iso_c_binding
implicit none

! input/output variables
character(*), intent(in) :: py_script
character(*), intent(in), optional :: node_out_name
type(C_PTR) :: nodeOut

! local variables
Expand All @@ -62,19 +103,34 @@ function conduit_fort_from_py(py_script) result(nodeOut)

call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)

nodeOut = c_conduit_fort_from_py(trim(py_script)//C_NULL_CHAR)
! Enter trace region
call ESMF_TraceRegionEnter('conduit_fort_from_py')

! Run Python script
if (present(node_out_name)) then
nodeOut = c_conduit_fort_from_py(trim(py_script)//C_NULL_CHAR, &
trim(node_out_name)//C_NULL_CHAR)
else
nodeOut = c_conduit_fort_from_py(trim(py_script)//C_NULL_CHAR, &
'my_node_return'//C_NULL_CHAR)
end if

! Exit trace region
call ESMF_TraceRegionExit('conduit_fort_from_py')

call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)

end function conduit_fort_from_py

function conduit_fort_to_py_to_fort(nodeIn, py_script) result(nodeOut)
function conduit_fort_to_py_to_fort(nodeIn, py_script, node_in_name, node_out_name) result(nodeOut)
use iso_c_binding
implicit none

! input/output variables
type(C_PTR), intent(in) :: nodeIn
character(*), intent(in) :: py_script
character(*), intent(in), optional :: node_in_name
character(*), intent(in), optional :: node_out_name
type(C_PTR) :: nodeOut

! local variables
Expand All @@ -83,7 +139,30 @@ function conduit_fort_to_py_to_fort(nodeIn, py_script) result(nodeOut)

call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)

nodeOut = c_conduit_fort_to_py_to_fort(nodeIn, trim(py_script)//C_NULL_CHAR)
! Enter trace region
call ESMF_TraceRegionEnter('conduit_fort_to_py_to_fort')

! Run Python script
if (present(node_in_name) .and. present(node_out_name)) then
nodeOut = c_conduit_fort_to_py_to_fort(nodeIn, trim(py_script)//C_NULL_CHAR, &
trim(node_in_name)//C_NULL_CHAR, &
trim(node_out_name)//C_NULL_CHAR)
else if (present(node_in_name)) then
nodeOut = c_conduit_fort_to_py_to_fort(nodeIn, trim(py_script)//C_NULL_CHAR, &
trim(node_in_name)//C_NULL_CHAR, &
'my_node_return'//C_NULL_CHAR)
else if (present(node_out_name)) then
nodeOut = c_conduit_fort_to_py_to_fort(nodeIn, trim(py_script)//C_NULL_CHAR, &
'my_node'//C_NULL_CHAR, &
trim(node_out_name)//C_NULL_CHAR)
else
nodeOut = c_conduit_fort_to_py_to_fort(nodeIn, trim(py_script)//C_NULL_CHAR, &
'my_node'//C_NULL_CHAR, &
'my_node_return'//C_NULL_CHAR)
end if

! Exit trace region
call ESMF_TraceRegionExit('conduit_fort_to_py_to_fort')

call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)

Expand Down
Loading
Loading