Skip to content

Commit

Permalink
Merge pull request #718 from galacticusorg/featModelReportState
Browse files Browse the repository at this point in the history
Add options to report the base parameter file name and sampling state from the `posteriorSampleLikelihoodBaseParameters` class
  • Loading branch information
abensonca authored Oct 12, 2024
2 parents e51f60c + 657777d commit 429b2f2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 38 deletions.
86 changes: 55 additions & 31 deletions source/models.likelihoods.base_parameters.F90
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@
Implementation of a posterior sampling likelihood class which allows arbitrary modification of a base parameter object.
!!}
private
type(varying_string ) :: baseParametersFileName
type(inputParameters), pointer :: parametersModel => null()
type(parameterList ), dimension(:), allocatable :: modelParametersActive_ , modelParametersInactive_
type (varying_string ) :: baseParametersFileName
type (inputParameters), pointer :: parametersModel => null()
type (parameterList ), dimension(:), allocatable :: modelParametersActive_ , modelParametersInactive_
logical :: reportFileName = .false., reportState =.false.
contains
!![
<methods>
Expand Down Expand Up @@ -159,38 +160,53 @@ subroutine baseParametersInitialize(self,modelParametersActive_,modelParametersI
end if
return
end subroutine baseParametersInitialize

subroutine baseParametersUpdate(self,simulationState,modelParametersActive_,modelParametersInactive_,stateVector)
use :: Display , only : displayIndent , displayMessage , displayUnindent , displayVerbositySet, &
& verbosityLevelStandard
use :: Error , only : Error_Report , errorStatusSuccess
use :: Model_Parameters , only : modelParameterDerived
use :: ISO_Varying_String, only : operator(//)
use :: Kind_Numbers , only : kind_int8
use :: String_Handling , only : String_Count_Words , String_Join , String_Split_Words, operator(//)
implicit none
class (posteriorSampleLikelihoodBaseParameters), intent(inout) :: self
class (posteriorSampleStateClass ), intent(inout) :: simulationState
type (modelParameterList ), intent(in ), dimension(:) :: modelParametersActive_, modelParametersInactive_
double precision , intent(in ), dimension(:) :: stateVector
type (varying_string ), allocatable , dimension(:) :: parameterNames
integer :: i , j , &
& parameterCount
logical :: firstIteration , dependenciesResolved , &
& dependenciesUpdated
character (len=24 ) :: valueText
double precision :: valueDerived
type (varying_string ) :: parameterText
! Declarations of GNU libmatheval procedures used.

subroutine baseParametersUpdate(self,simulationState,modelParametersActive_,modelParametersInactive_,stateVector,report)
use :: Display , only : displayIndent , displayMessage , displayUnindent , displayVerbositySet, &
& verbosityLevelStandard, displayVerbosity , enumerationVerbosityLevelType
use :: Error , only : Error_Report , errorStatusSuccess
use :: Model_Parameters , only : modelParameterDerived
use :: ISO_Varying_String, only : operator(//)
use :: Kind_Numbers , only : kind_int8
use :: String_Handling , only : String_Count_Words , String_Join , String_Split_Words, operator(//)
implicit none
class (posteriorSampleLikelihoodBaseParameters), intent(inout) :: self
class (posteriorSampleStateClass ), intent(inout) :: simulationState
type (modelParameterList ), intent(in ), dimension(:) :: modelParametersActive_, modelParametersInactive_
double precision , intent(in ), dimension(:) :: stateVector
logical , intent(in ), optional :: report
type (varying_string ), allocatable , dimension(:) :: parameterNames
integer :: i , j , &
& parameterCount
logical :: firstIteration , dependenciesResolved , &
& dependenciesUpdated
character (len=24 ) :: valueText
double precision :: valueDerived
type (varying_string ) :: parameterText
type (enumerationVerbosityLevelType ) :: verbosityLevel
! Declarations of GNU libmatheval procedures used.
#ifdef MATHEVALAVAIL
integer (kind_int8 ) :: evaluator
integer (kind_int8 ) :: evaluator
#endif
integer (kind_int8 ), external :: Evaluator_Create_
double precision , external :: Evaluator_Evaluate_
external :: Evaluator_Destroy_
integer (kind_int8 ), external :: Evaluator_Create_
double precision , external :: Evaluator_Evaluate_
external :: Evaluator_Destroy_
!![
<optionalArgument name="report" defaultsTo=".false."/>
!!]

! Set verbosity for our reports.
if (report_ .and. (self%reportFileName .or. self%reportState)) then
verbosityLevel=displayVerbosity()
call displayVerbositySet(verbosityLevelStandard)
end if
! Report name of parameter file being evaluated if requested.
if (report_ .and. self%reportFileName) call displayMessage("Evaluating likelihood for file `"//char(self%baseParametersFileName)//"`")
! Update parameter values.
if (report_ .and. self%reportState) call displayIndent("State:")
do i=1,size(modelParametersActive_)
if (report_ .and. self%reportState) &
& call displayMessage(char(modelParametersActive_(i)%modelParameter_%name())//" = "//char(self%modelParametersActive_(i)%parameter_%get()))
if (self%modelParametersActive_(i)%indexElement == 0) then
! Simply overwrite the parameter.
call self%modelParametersActive_(i)%parameter_%set(modelParametersActive_(i)%modelParameter_%unmap(stateVector(i)))
Expand All @@ -213,7 +229,7 @@ subroutine baseParametersUpdate(self,simulationState,modelParametersActive_,mode
parameterNames(self%modelParametersActive_(i)%indexElement)=trim(valueText)
call self%modelParametersActive_(i)%parameter_%set(String_Join(parameterNames," "))
deallocate(parameterNames)
endif
end if
end do
! Resolve dependencies in derived parameters.
if (size(modelParametersInactive_) > 0) then
Expand Down Expand Up @@ -316,7 +332,15 @@ subroutine baseParametersUpdate(self,simulationState,modelParametersActive_,mode
end if
firstIteration=.false.
end do
if (report_ .and. self%reportState) then
do i=1,size(modelParametersInactive_)
call displayMessage(char(modelParametersInactive_(i)%modelParameter_%name())//" = "//char(self%modelParametersInactive_(i)%parameter_%get()))
end do
end if
end if
if (report_ .and. self%reportState) call displayUnindent("")
call self%parametersModel%reset()
! Restore verbosity level.
if (report_ .and. (self%reportFileName .or. self%reportState)) call displayVerbositySet(verbosityLevel)
return
end subroutine baseParametersUpdate
28 changes: 21 additions & 7 deletions source/models.likelihoods.galaxy_population.F90
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function galaxyPopulationConstructorParameters(parameters) result(self)
type (inputParameters ), intent(inout) :: parameters
type (varying_string) :: baseParametersFileName, failedParametersFileName
logical :: randomize , collaborativeMPI , &
& outputAnalyses
& outputAnalyses , reportFileName , &
& reportState
type (varying_string) :: evolveForestsVerbosity
type (inputParameters ), pointer :: parametersModel

Expand Down Expand Up @@ -97,6 +98,18 @@ function galaxyPopulationConstructorParameters(parameters) result(self)
<defaultValue>.true.</defaultValue>
<source>parameters</source>
</inputParameter>
<inputParameter>
<name>reportFileName</name>
<description>If true, report the base parameter file name being evaluated.</description>
<defaultValue>.false.</defaultValue>
<source>parameters</source>
</inputParameter>
<inputParameter>
<name>reportState</name>
<description>If true, report the state being evaluated.</description>
<defaultValue>.false.</defaultValue>
<source>parameters</source>
</inputParameter>
<inputParameter>
<name>evolveForestsVerbosity</name>
<description>The verbosity level to use while performing evolve forests tasks.</description>
Expand All @@ -112,27 +125,28 @@ function galaxyPopulationConstructorParameters(parameters) result(self)
!!]
allocate(parametersModel)
parametersModel=inputParameters (baseParametersFileName,noOutput=.true.)
self =posteriorSampleLikelihoodGalaxyPopulation(parametersModel,randomize,outputAnalyses,collaborativeMPI,enumerationVerbosityLevelEncode(evolveForestsVerbosity,includesPrefix=.false.),failedParametersFileName)
self =posteriorSampleLikelihoodGalaxyPopulation(parametersModel,baseParametersFileName,randomize,outputAnalyses,collaborativeMPI,reportFileName,reportState,enumerationVerbosityLevelEncode(evolveForestsVerbosity,includesPrefix=.false.),failedParametersFileName)
!![
<inputParametersValidate source="parameters"/>
!!]
nullify(parametersModel)
return
end function galaxyPopulationConstructorParameters

function galaxyPopulationConstructorInternal(parametersModel,randomize,outputAnalyses,collaborativeMPI,evolveForestsVerbosity,failedParametersFileName) result(self)
function galaxyPopulationConstructorInternal(parametersModel,baseParametersFileName,randomize,outputAnalyses,collaborativeMPI,reportFileName,reportState,evolveForestsVerbosity,failedParametersFileName) result(self)
!!{
Constructor for ``galaxyPopulation'' posterior sampling likelihood class.
!!}
implicit none
type (posteriorSampleLikelihoodGalaxyPopulation) :: self
type (inputParameters ), intent(inout), target :: parametersModel
logical , intent(in ) :: randomize , collaborativeMPI, &
& outputAnalyses
& outputAnalyses , reportFileName , &
& reportState
type (enumerationVerbosityLevelType ), intent(in ) :: evolveForestsVerbosity
type (varying_string ), intent(in ) :: failedParametersFileName
type (varying_string ), intent(in ) :: failedParametersFileName, baseParametersFileName
!![
<constructorAssign variables="*parametersModel, randomize, outputAnalyses, collaborativeMPI, evolveForestsVerbosity, failedParametersFileName"/>
<constructorAssign variables="*parametersModel, baseParametersFileName, randomize, outputAnalyses, collaborativeMPI, reportFileName, reportState, evolveForestsVerbosity, failedParametersFileName"/>
!!]

return
Expand Down Expand Up @@ -234,7 +248,7 @@ double precision function galaxyPopulationEvaluate(self,simulationState,modelPar
end if
end if
! Update parameter values.
call self%update(simulationState,modelParametersActive_,modelParametersInactive_,stateVector(:,iRank))
call self%update(simulationState,modelParametersActive_,modelParametersInactive_,stateVector(:,iRank),report=isActive)
! Build the task and outputter objects.
call Tasks_Evolve_Forest_Construct_(self%parametersModel,self%task_)
!![
Expand Down

0 comments on commit 429b2f2

Please sign in to comment.