def setup_and_run_cloudy_model(cmdargs, extras, monitor):
    cloudy_cmd = ["time", cmdargs.cloudyexec] 
    # files are simply named after iteration number and density
    filename = "it%.2in%.3ex0%.2f" % (monitor.it, extras.hden, extras.x0)
    print "Calculating " + os.path.join(extras.datapath, filename)
    m = Model(filename, dryrun=False, indir=extras.datapath, outdir=extras.datapath, 
              verbose=True, cloudy_cmd=cloudy_cmd)
    m.write(physical.proplyd(cmdargs.r0, extras.hden, Rmax=cmdargs.Rmax, 
                             W=cmdargs.W, x0=extras.x0, 
                             composition=cmdargs.composition))
    m.write(misc.optimize())
    m.write(misc.stopping())
    if cmdargs.fastcloudy:
        m.write(misc.iterate(iterations=0))
    else:
        m.write(misc.iterate())
    m.write(incident.background())
    m.write(incident.star(extras.logPhiH, cmdargs.Tstar, atmosphere=cmdargs.atmosphere, log_g=4.1)) # th1C
    m.write(save.default())
    if cmdargs.savecont:
        m.write("""\
save every last ionizing continuum 1.0 0 ".icont"
""")
    # Option for small adjustments to Cloudy script via file "cloudy-extras.in" in the input dir
    if cmdargs.read_extra_cloudy_commands_from_file:
        file_with_extra_cloudy_commands = os.path.join(extras.datapath, "cloudy-extras.in")
        try:
            with open(file_with_extra_cloudy_commands) as f:
                m.write(f.read())
        except IOError:
            warnings.warn("Failed to read extra commands from %s" % (file_with_extra_cloudy_commands))

    m.run()
    return m
if __name__ == '__main__':
    if MULTIPROCESS:
        pool = multiprocessing.Pool() # by default makes one process per core

    for Tstar in Tstar_range:
        for radiation in rad_list:
            filename = "proplyd-test-%s%.2i-phi%.2f-n%.2e-A%.3i" % (radiation, Tstar/1000,
                                                                    log_PhiH, hden_Rmax,
                                                                    A)
            print "Calculating " + filename
            m = Model(filename, verbose=True, cloudy_cmd=cloudy_cmd)
            m.write(physical.proplyd(r0, hden_Rmax, Rmax=Rmax, A=A, x0=x0))
            m.write(misc.optimize())
            m.write(misc.stopping())
            m.write(misc.iterate())
            m.write(incident.background())
            m.write(incident.star(log_PhiH, Tstar, atmosphere=radiation, log_g=4.1)) # th1C
            m.write(save.default())
            # Do full calculation
            if MULTIPROCESS:
                pool.apply_async(m)
            else:
                m.run()

    if MULTIPROCESS:
        pool.close()
        pool.join()