#-------------------------------------------------------------------------------
# Build the controller.
#-------------------------------------------------------------------------------
control = SpheralController(integrator,
                            WT,
                            statsStep=statsStep,
                            restartStep=restartStep,
                            redistributeStep=redistributeStep,
                            restartBaseName=restartBaseName,
                            initializeMassDensity=False)
output("control")

#-------------------------------------------------------------------------------
# Monitor the evolution of the mass averaged strain.
#-------------------------------------------------------------------------------
strainHistory = AverageStrain(damageModel, dataDir + "/strainhistory.txt")
control.appendPeriodicWork(strainHistory.sample, strainFrequency)


#-------------------------------------------------------------------------------
# Select the nodes for the VISAR sampling.
#-------------------------------------------------------------------------------
def xymagnitude(vec):
    return sqrt((vec.x)**2 + (vec.y)**2)


class SelectVISARNodes:
    def __init__(self, z0, dz, dr, label):
        self.z0 = z0
        self.dz = dz
        self.dr = dr
    damageModelTarget = DamageModelConstructor(
        target,
        kWeibull=kWeibull,
        mWeibull=mWeibull,
        kernel=WT,
        seed=randomSeed,
        volume=0.0,  # forces internal computation.
        volumeStretchFactor=1.0,
        strainAlgorithm=strainType,
        effectiveDamageAlgorithm=damageType,
        useDamageGradient=useDamageGradient,
        flawAlgorithm=effectiveFlawAlgorithm,
        criticalDamageThreshold=criticalDamageThreshold)

    # Build some history objects to follow the time evolution of stuff.
    strainHistory = AverageStrain(damageModelTarget,
                                  os.path.join(dataDir, "strainhistory.txt"))

# Construct a time integrator.
integrator = CheapSynchronousRK2Integrator(db)
integrator.appendPhysicsPackage(hydro)
if useDamage:
    integrator.appendPhysicsPackage(damageModelTarget)
integrator.lastDt = dt
integrator.verbose = verbosedt
if dtMin:
    integrator.dtMin = dtMin
if dtMax:
    integrator.dtMax = dtMax
integrator.dtGrowth = dtGrowth
integrator.rigorousBoundaries = rigorousBoundaries
if useDamage:
    damageModelTarget = DamageModelConstructor(target,
                                               kWeibull = kWeibull,
                                               mWeibull = mWeibull,
                                               kernel = WT,
                                               seed = randomSeed,
                                               volume = 0.0,  # forces internal computation.
                                               volumeStretchFactor = 1.0,
                                               strainAlgorithm = strainType,
                                               effectiveDamageAlgorithm = damageType,
                                               useDamageGradient = useDamageGradient,
                                               flawAlgorithm = effectiveFlawAlgorithm,
                                               criticalDamageThreshold = criticalDamageThreshold)

# Build some history objects to follow the time evolution of stuff.
    strainHistory = AverageStrain(damageModelTarget,
                                  os.path.join(dataDir, "strainhistory.txt"))

# Construct a time integrator.
integrator = CheapSynchronousRK2Integrator(db)
integrator.appendPhysicsPackage(hydro)
if useDamage:
    integrator.appendPhysicsPackage(damageModelTarget)
integrator.lastDt = dt
integrator.verbose = verbosedt
if dtMin:
    integrator.dtMin = dtMin
if dtMax:
    integrator.dtMax = dtMax
integrator.dtGrowth = dtGrowth
integrator.rigorousBoundaries = rigorousBoundaries