13.6.1. Macro “sensitivityBrutForceMethodFlowrate.py

13.6.1.1. Objective

The objective of this macro is to perform a sensitivity analysis with brute force method on a set of eight parameters used in the flowrate model described in Presentation of the problem. Sensitivity indexes are computed dividing conditional variance by the standard deviation of the output variable.

Warning

This macro is purely illustrative. It is not meant to be used for proper results with a real code / function as it needs a large number of computation to only get the first order index. Its main appeal is to be nicely illustrative: it shows plainly the definition of the conditional expectation and also its variance used to defined the first order sobol indices.

13.6.1.2. Macro Uranie

"""
Example of Sobol estimation with a brut-force approach
"""
from URANIE import DataServer, Launcher, Sampler
import ROOT

def draw_bar_with_tuple(val, name, stitle):
    """Draw the results estimated with brut-force approach."""
    h_div = ROOT.TH1F("hDivdrawBarWithTuple", stitle, 3, 0, 3)
    h_div.SetCanExtend(ROOT.TH1.kXaxis)  # .SetBit(ROOT.TH1.kCanRebin)
    h_div.SetStats(0)

    if h_div != 0:
        h_div.SetBarWidth(0.45)
        h_div.SetBarOffset(0.1)
        h_div.SetMarkerColor(2)
        h_div.SetMarkerSize(2)
        h_div.SetFillColor(49)
        h_div.SetTitle(stitle)
        for ite, value in enumerate(val):
            h_div.Fill(name[ite], value)
        h_div.LabelsDeflate()
        h_div.LabelsOption(">u")
        h_div.SetMinimum(0.0)
        h_div.SetMaximum(1.0)
        ROOT.gStyle.SetPaintTextFormat("5.2f")
        h_div.Draw("bar2, text45")
    return h_div

nCond = 50
nbins = 10
# Create a DataServer.TDataServer
tds = DataServer.TDataServer()

print(" ******************************************************")
print(" ** sensitivityBrutForceMethodFlowrate nbins[%i] nCond[%i]" %
      (nbins, nCond))
print(" **")

# Create a DataServer.TDataServer
tds = DataServer.TDataServer()
# Add the eight attributes of the study with uniform law
tds.addAttribute(DataServer.TUniformDistribution("rw", 0.05, 0.15))
tds.addAttribute(DataServer.TUniformDistribution("r", 100.0, 50000.0))
tds.addAttribute(DataServer.TUniformDistribution("tu", 63070.0, 115600.0))
tds.addAttribute(DataServer.TUniformDistribution("tl", 63.1, 116.0))
tds.addAttribute(DataServer.TUniformDistribution("hu", 990.0, 1110.0))
tds.addAttribute(DataServer.TUniformDistribution("hl", 700.0, 820.0))
tds.addAttribute(DataServer.TUniformDistribution("l", 1120.0, 1680.0))
tds.addAttribute(DataServer.TUniformDistribution("kw", 9855.0, 12045.0))

nvar = tds.getNAttributes()
print(" ** nX["+str(nvar)+"]")
nS = nbins*nvar*nCond
print(" ** nS["+str(nS)+"]")


# sam = Sampler.TSampling(tds, "lhs", nS)
sam = Sampler.TQMC(tds, "halton", nS)
sam.generateSample()

# Load the function
ROOT.gROOT.LoadMacro("UserFunctions.C")

# Create a TLauncherFunction from a TDataServer and an analytical function
# Rename the outpout attribute "ymod"
tlf = Launcher.TLauncherFunction(tds, "flowrateModel",
                                 "rw:r:tu:tl:hu:hl:l:kw", "ymod")
# Evaluate the function on all the design of experiments
tlf.setDrawProgressBar(False)
tlf.run()

Canvas = ROOT.TCanvas("c1", "Graph for the Macro modeler", 5, 64, 1270, 667)
pad = ROOT.TPad("pad", "pad", 0, 0.03, 1, 1)
pad.Draw()
pad.Divide(2, 2)
pad.cd(1)
tds.computeStatistic("ymod")
tds.draw("ymod")
dstdy = tds.getAttribute("ymod").getStd()
svary = dstdy * dstdy

print(" ** ymod: std["+str(round(dstdy, 4))+"] vary["+str(round(svary, 1))+"]")

tds.getAttribute("ymod").setOutput()


ROOT.gStyle.SetOptStat(1)

valSobolCrt = []
sName = []

c = ROOT.TCanvas()
c.Divide(2)
c.cd(1)
for ivar in range(nvar):

    print(" *****************************")
    print(" *** "+str(tds.getAttribute(ivar).GetName()))

    svar = tds.getAttribute(ivar).GetName()

    if ivar == 0:
        pad.cd(2)
    else:
        c.cd(1)

    tds.drawProfile("ymod:"+svar, "", "nclass="+str(nbins))
    hprofs = ROOT.gPad.GetPrimitive("Profile ymod:%s (Bin = %i )" %
                                    (svar, nbins+2))

    ntd = ROOT.TNtupleD("dd", "sjsjs", "i:x:m")
    ntd.SetMarkerColor(ROOT.kBlue)
    ntd.SetMarkerStyle(8)
    nnbins = hprofs.GetNbinsX()
    for i in range(1, nnbins+1):
        ntd.Fill(i-1, hprofs.GetBinCenter(i), hprofs.GetBinContent(i))

    tds.draw("ymod:"+svar)
    ntd.Draw("m:x", "", "same")

    if ivar == 0:
        pad.cd(3)
    else:
        c.cd(2)

    ntd.Draw("m")
    htemp = ROOT.gPad.GetPrimitive("htemp")

    dvarcond = htemp.GetRMS()

    # Tempory ROOT.TTree for histogram
    valSobolCrt.append(dvarcond*dvarcond / svary)
    sName.append(svar)

    print(" *** S1[ %s] Cond. Var.[%4.6g] -- [%1.6g]" %
          (svar, dvarcond*dvarcond, valSobolCrt[-1]))

    c.Modified()
    c.Update()
    c.SaveAs("SAFlowRateVersus"+svar+".png")

pad.cd(4)
hDiv = draw_bar_with_tuple(valSobolCrt, sName,
                           "Sensitivity Indexes: ymod  [Brute-Force Method]")

Each parameter is related to the TDataServer as a TAttribute and obeys an uniform law on specific interval:

tds = DataServer.TDataServer()
tds.addAttribute(DataServer.TUniformDistribution("rw", 0.05, 0.15))
tds.addAttribute(DataServer.TUniformDistribution("r", 100.0, 50000.0))
tds.addAttribute(DataServer.TUniformDistribution("tu", 63070.0, 115600.0))
tds.addAttribute(DataServer.TUniformDistribution("tl", 63.1, 116.0))
tds.addAttribute(DataServer.TUniformDistribution("hu", 990.0, 1110.0))
tds.addAttribute(DataServer.TUniformDistribution("hl", 700.0, 820.0))
tds.addAttribute(DataServer.TUniformDistribution("l", 1120.0, 1680.0))
tds.addAttribute(DataServer.TUniformDistribution("kw", 9855.0, 12045.0))

A design-of-experiments is built with a “Halton” method (\(n_S\) = 4000):

sam = Sampler.TQMC(tds, "halton", nS)
sam.generateSample()

The function flowrateModel is loaded from the macro UserFunctions.C (the file can be found in ${URANIESYS}/share/uranie/macros):

ROOT.gROOT.LoadMacro("UserFunctions.C")

The flowrateModel model is applied on previous variables:

tlf = Launcher.TLauncherFunction(tds, "flowrateModel",
                                 "rw:r:tu:tl:hu:hl:l:kw", "ymod")
tlf.run()

Characteristic values for the output attribute are computed:

tds.computeStatistic("ymod")

Sensitivity indexes are computed in the for loop. Average value of output variable is computed on nbins+2=12 points for each input variable:

c.cd(1)
...
nnbins = hprofs.GetNbinsX()
for i in range(1,nnbins+1): ntd.Fill(i-1, hprofs.GetBinCenter(i), hprofs.GetBinContent(i))

The RMS value is obtained from the graphic of ymod versus the considered output variable and the sensitivity index is computed dividing the conditional variance value by the standard deviation of the output variable ymod.

    c.cd(2)

ntd.Draw("m")
htemp = ROOT.gPad.GetPrimitive("htemp")

dvarcond = htemp.GetRMS()
valSobolCrt.append(dvarcond*dvarcond / svary)

13.6.1.3. Graph

../../_images/sensitivityBrutForceMethodFlowrate.png

Figure 13.18 Graph of the macro “sensitivityBrutForceMethodFlowrate.py”

13.6.1.4. Console

--- Uranie v4.11/0 --- Developed with ROOT (6.36.06)
                      Copyright (C) 2013-2026 CEA/DES 
                      Contact: support-uranie@cea.fr 
                      Date: Thu Feb 12, 2026

Info in <TCanvas::Print>: png file SAFlowRateVersusrw.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersusr.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersustu.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersustl.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersushu.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersushl.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersusl.png has been created
Info in <TCanvas::Print>: png file SAFlowRateVersuskw.png has been created
 ******************************************************
 ** sensitivityBrutForceMethodFlowrate nbins[10] nCond[50]
 **
 ** nX[8]
 ** nS[4000]
 ** ymod: std[45.6059] vary[2079.9]
 *****************************
 *** rw
 *** S1[ rw] Cond. Var.[1763.28] -- [0.847774]
 *****************************
 *** r
 *** S1[ r] Cond. Var.[0.0624988] -- [3.00489e-05]
 *****************************
 *** tu
 *** S1[ tu] Cond. Var.[0.0886501] -- [4.26223e-05]
 *****************************
 *** tl
 *** S1[ tl] Cond. Var.[0.0909604] -- [4.37331e-05]
 *****************************
 *** hu
 *** S1[ hu] Cond. Var.[88.368] -- [0.0424867]
 *****************************
 *** hl
 *** S1[ hl] Cond. Var.[88.2039] -- [0.0424078]
 *****************************
 *** l
 *** S1[ l] Cond. Var.[84.9543] -- [0.0408454]
 *****************************
 *** kw
 *** S1[ kw] Cond. Var.[20.8848] -- [0.0100413]