コード例 #1
0
ファイル: simulation.py プロジェクト: fangohr/nmag-test
def write_to_file(algorithm,nfdeg,nmin,eta,eps_aca,eps,p,kmax,timing,demagf,time_full,demag_full):

    algorithm=int(algorithm)
    outputfile = os.getcwd()+'/'+outputfilename[algorithm]
    (fullbem_data,memory_data) = read_simulation_data()
    
    if os.path.exists(outputfile)==False:
        simu_output = open(outputfilename[algorithm],"w")
        
        headings = []  #the headings over all the columns are created
        for entry in parameters[algorithm]:
            headings.append(headingparameters[entry])
        headings.extend(headingresults)
        formatstring=str()
        for entry in headings:
            formatstring=formatstring+"%s "
        formatstring=formatstring+"\n"
        
        headings=tuple(headings)
        simu_output.write(formatstring % headings)

        simu_output.write("number of surface nodes: %9d,\t full bem storage %12.2f (MB)\n" %\
                                  (fullbem_data[0],1.0*fullbem_data[0]**2*\
                                   fullbem_data[1]/1000000.0))

    else:
        simu_output = open(outputfilename[algorithm],"a")



    rate = 100000000.0*memory_data[0]/(fullbem_data[1]*fullbem_data[0]**2)

    demag_theo = demag.demagfactor_prism("dimensions.txt") #theoretical value of demag factor
    

    formatstring = str() #here the columns are created
    for entry in parameters[algorithm]:
        formatstring = formatstring + format_parameters[entry]
    for entry in format_results:
        formatstring = formatstring + entry
    formatstring = formatstring + "\n"

    variables = []
    for entry in parameters[algorithm]:
        variables.append(vars()[entry])

    
    assert demag_theo!=0.0,"demag_theo should not be zero since it appears in the denominator."
    assert demag_full!=0.0,"demag_full should not be zero since it appears in the denominator."

    variables.extend([100.0*(demagf-demag_theo)/demag_theo,\
                      100.0*(demagf-demag_full)/demag_full, memory_data[0], memory_data[1],\
                      memory_data[2],rate,timing,time_full-timing])

    variables = tuple(variables)
    simu_output.write(formatstring % variables)
                                  
    simu_output.close()
コード例 #2
0
ファイル: simulation.py プロジェクト: fangohr/nmag-test
def hlib_simulation(
    hlibsimufile,
    hlibsimuname,
    fullsimufile,
    fullsimuname,
    meshfilename="None",
    fulldicttag="Default",
    nfdeg=3,
    nmin=20,
    p=4,
    eta=2.0,
    eps=0.0001,
):
    """This function runs a simulation using Nsim and HLib. The HLib parameters are given
    by the function arguments nfdeg, nmin, p, eta, and eps, the meshfile by the argument
    meshfilename, the nsim script file by hlibsimufile and the simulation name (which is
    specified in the script file) by the hlibsimuname. For comparing the results with those
    obtained by an equivalent simulation using the full BEM, the function fullbem_simulation
    is called. This latter functions stores the time needed to set up the full boundary
    element matrix and the result for the demagnetising factor in the globally defined
    dictionaries dictfulltime and dictfulldemag respectively. The arguments fullsimufile
    and fullsimuname again specifie the corresponding script file and the simulation name.
    The results of the simulation are written (or appended) to a file simulation.dat."""

    print "Entering function hlib_simulation...\n"

    # test of the arguments of the function:
    nfdeg = int(nfdeg)
    assert nfdeg > 0 and nfdeg < 20, "nfdeg does not have a reasonable value!"

    nmin = int(nmin)
    assert nmin > 0 and nmin < 200, "nmin is out of range!"

    p = int(p)
    assert p > -1 and p < 20, "Polynomial order out of range!"

    eta = float(eta)
    assert eta > 0.1 and eta < 10.0, "eta out of  range!"

    eps = float(eps)
    assert eps > 1e-10 and eps < 0.5, "eps out of range"

    hlibsimufile = str(hlibsimufile)
    hlibsimuname = str(hlibsimuname)
    fullsimufile = str(fullsimufile)
    fullsimuname = str(fullsimuname)
    meshfilename = str(meshfilename)
    fulldicttag = str(fulldicttag)

    # if no meshfilename is specified, the routine searches for one in the current working directory
    if meshfilename == "None":
        ls = os.listdir("./")
        for entry in ls:
            if meshfile.match(entry):
                meshfilename = entry
                break

    assert meshfilename != "None", "There is no meshfile in the current working directory."

    if fulldicttag not in dictfulldemag or fulldicttag not in dictfulltime:
        fullbem_simulation(simufile=fullsimufile, simuname=fullsimuname, meshfilename=meshfilename, dicttag=fulldicttag)

    demag_full = dictfulldemag[fulldicttag]
    time_full = dictfulltime[fulldicttag]

    arguments = (
        hlibsimufile
        + " "
        + " "
        + meshfilename
        + " "
        + str(ms)
        + " "
        + str(exchange)
        + " "
        + str(nfdeg)
        + " "
        + str(nmin)
        + " "
        + str(p)
        + " "
        + str(eta)
        + " "
        + str(eps)
        + " --clean"
    )

    # running the simulation and measuring the needed to set up the supermatrix
    timing = run_nsim_with_timing(nsim + " " + arguments)

    # computation of the demagnetisation factor
    demagf = get_demagfactor(hlibsimuname)

    # computation of the theoretical demagnetisation factor, requires a file dimensions text,
    # which has been created during the run of setup.sh
    demagf_theo = demag.demagfactor_prism("dimensions.txt")

    # The file memory_info.dat contains the following entries:
    # 1.line: The number of surface nodes
    # 2.line: The number of bytes needed to store a double precision number
    # 3.line: Total amount of storage for the H-Matrix (in Megabyte)
    # 4.line: Amount of storage for the inadmissible leaves (in Megabyte)
    # 5.line: Amount of storage for the admissible leaves (in Megabyte)

    memory_output = open("memory_info.dat", "r")
    fullbem_data = list()
    memory_data = list()
    i = 0
    for entries in memory_output:
        assert i < 5, "There are too many entries in memory_info.dat."
        if i < 2:
            fullbem_data.append(int(entries))
        else:
            memory_data.append(float(entries))
        i = i + 1

    memory_output.close()

    # rate is the compression rate
    rate = memory_data[0] * 100000000.0 / fullbem_data[0] ** 2 / fullbem_data[1]

    outputfile = os.getcwd() + "/simulation.dat"
    if os.path.exists(outputfile) == False:
        simu_output = open("simulation.dat", "w")
        simu_output.write(
            "%s %s %s %s %s %s %s %s %s %s %s %s %s\n"
            % (
                " nfdeg ",
                " nmin ",
                "  p  ",
                "  eta ",
                "       eps",
                "  dev-demag-th_(%) ",
                "dev-demag-ful_(%)",
                "  smx_(MB)",
                "   ful_(MB)",
                "    rk_(MB)",
                "  rate_(%) ",
                "   time_(sec)",
                "    dt-ful_(sec) ",
            )
        )

        simu_output.write(
            "number of surface nodes: %9d,\t full bem storage %12.2f (MB)\n"
            % (fullbem_data[0], 1.0 * fullbem_data[0] ** 2 * fullbem_data[1] / 1000000.0)
        )

    else:
        simu_output = open("simulation.dat", "a")

    simu_output.write(
        "%4d %7d %5d %7.2f %14.9f %14.7f %16.7f %11.2f %11.2f %11.2f %10.2f %14.5f %15.5f\n"
        % (
            nfdeg,
            nmin,
            p,
            eta,
            eps,
            (demagf - demagf_theo) * 100 / demagf_theo,
            (demagf - demag_full) * 100 / demag_full,
            memory_data[0],
            memory_data[1],
            memory_data[2],
            rate,
            timing,
            time_full - timing,
        )
    )

    simu_output.close()