Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
Added supercell energy background builder
Browse files Browse the repository at this point in the history
Added an energy background builder for the supercell that can be used to inject energy backgrounds into build msl files
  • Loading branch information
seb-eis committed Nov 20, 2020
1 parent 291048d commit b414b39
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ static void AddStaticEnvBackgroundStateEnergies(SCONTEXT_PARAMETER, EnvironmentS
{
let vector = environment->LatticeVector;
let particleId = environment->EnvironmentDefinition->PositionParticleIds[j];
let cellEntry = cellBackground->Begin == NULL? 0.0 : array_Get(*cellBackground, vector.D, particleId);
let cellEntry = cellBackground->Begin == NULL ? 0.0 : array_Get(*cellBackground, vector.D, particleId);
let latticeEntry = latticeBackground->Begin == NULL ? 0.0 : array_Get(*latticeBackground, vecCoorSet4(vector), particleId);
span_Get(environment->EnergyStates, particleId) += cellEntry + latticeEntry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public interface IUnitCellVectorEncoder
/// <returns></returns>
bool TryDecode(in Vector4I encoded, out Fractional3D decoded);

/// <summary>
/// Decodes a 4D encoded vector into absolute fractional vector starting at the unit cell origin without performing bound checks
/// </summary>
/// <param name="encoded"></param>
/// <param name="decoded"></param>
void DecodeUnchecked(in Vector4I encoded, out Fractional3D decoded);

/// <summary>
/// Tries to decode multiple 4D vectors into a list of fractional vectors (Returns false if not possible)
/// </summary>
Expand All @@ -126,6 +133,13 @@ public interface IUnitCellVectorEncoder
/// <returns></returns>
bool TryDecode(in Vector4I encoded, out Cartesian3D decoded);

/// <summary>
/// Decodes a 4D encoded vector into absolute cartesian vector starting at the unit cell origin without performing bound checks
/// </summary>
/// <param name="encoded"></param>
/// <param name="decoded"></param>
void DecodeUnchecked(in Vector4I encoded, out Cartesian3D decoded);

/// <summary>
/// Tries to decoded 4D encoded vector into a spherical vector from unit cell origin (Returns false if not possible)
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public bool TryEncodeAsRelative(in Fractional3D origin, in Fractional3D vector,
/// <inheritdoc />
public bool TryDecode(in Vector4I encoded, out Fractional3D decoded) => TryDecodeFractional(encoded.Coordinates, out decoded);

/// <inheritdoc />
public void DecodeUnchecked(in Vector4I encoded, out Fractional3D decoded)
{
var offset = PositionList[encoded.P];
decoded = new Fractional3D(encoded.A + offset.A, encoded.B + offset.B, encoded.C + offset.C);
}

/// <inheritdoc />
public bool TryDecode(IEnumerable<Vector4I> encoded, out List<Fractional3D> decoded)
{
Expand Down Expand Up @@ -115,6 +122,13 @@ public bool TryDecode(in Vector4I encoded, out Cartesian3D decoded)
return true;
}

/// <inheritdoc />
public void DecodeUnchecked(in Vector4I encoded, out Cartesian3D decoded)
{
DecodeUnchecked(encoded, out Fractional3D fractional3D);
decoded = Transformer.ToCartesian(fractional3D);
}

/// <inheritdoc />
public bool TryDecode(in Vector4I encoded, out Spherical3D decoded)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
/// </summary>
public class EnergyBackgroundEntity : InteropArray<double>
{
/// <summary>
/// The empty energy background property
/// </summary>
public static readonly EnergyBackgroundEntity Empty = new EnergyBackgroundEntity(new double[0, 0, 0, 0, 0]);

/// <inheritdoc />
public EnergyBackgroundEntity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Mocassin.Framework.Events;
using Mocassin.Framework.Extensions;
using Mocassin.Model.Structures;
using Mocassin.Model.Translator.Database.Entities.Other.Meta;
using Mocassin.Model.Translator.Jobs;
using Mocassin.Model.Translator.ModelContext;
Expand Down Expand Up @@ -189,6 +190,9 @@ protected SimulationJobModel GetJobModel(ISimulationModel simulationModel, JobCo
/// <returns></returns>
protected JobMetaDataEntity GetJobMetaDataEntity(JobConfiguration jobConfiguration, IJobCollection jobCollection)
{
var positionCountPerCell = ProjectModelContext.ModelProject
.Manager<IStructureManager>().DataAccess
.Query(x => x.GetLinearizedExtendedPositionCount());
var entity = new JobMetaDataEntity
{
CollectionName = jobConfiguration.CollectionName,
Expand All @@ -200,7 +204,7 @@ protected JobMetaDataEntity GetJobMetaDataEntity(JobConfiguration jobConfigurati
Mcsp = jobConfiguration.TargetMcsp,
TimeLimit = jobConfiguration.TimeLimit,
DopingInfo = jobConfiguration.LatticeConfiguration.GetDopingString(),
LatticeInfo = jobConfiguration.LatticeConfiguration.GetSizeString()
LatticeInfo = jobConfiguration.LatticeConfiguration.GetSizeString(positionCountPerCell)
};

AddKineticMetaData(entity, jobConfiguration);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using Mocassin.Mathematics.ValueTypes;
using Mocassin.Model.Particles;
using Mocassin.Model.Structures;
using Mocassin.Model.Translator.ModelContext;

namespace Mocassin.Model.Translator.Database.Postbuild
{
/// <summary>
/// Builder that creates <see cref="EnergyBackgroundEntity" /> for <see cref="SimulationJobModel" /> instances based on
/// energy provider functions
/// </summary>
public class EnergyBackgroundBuilder
{
/// <summary>
/// The lattice size in A direction
/// </summary>
public int SizeA { get; }

/// <summary>
/// The lattice size in B direction
/// </summary>
public int SizeB { get; }

/// <summary>
/// The lattice size in C direction
/// </summary>
public int SizeC { get; }

/// <summary>
/// Creates a new <see cref="EnergyBackgroundBuilder"/> for a specific supercell size
/// </summary>
/// <param name="sizeA"></param>
/// <param name="sizeB"></param>
/// <param name="sizeC"></param>
public EnergyBackgroundBuilder(int sizeA, int sizeB, int sizeC)
{
SizeA = sizeA;
SizeB = sizeB;
SizeC = sizeC;
}

/// <summary>
/// Builds an <see cref="EnergyBackgroundEntity"/> using the provided <see cref="IProjectModelContext"/> and energy provider for <see cref="Cartesian3D"/> vectors
/// </summary>
/// <param name="modelContext"></param>
/// <param name="energyFunc"></param>
/// <returns></returns>
public EnergyBackgroundEntity Build(IProjectModelContext modelContext, Func<IParticle, Cartesian3D, double> energyFunc)
{
var transformedEnergyFunc = TransformEnergyFunction(modelContext, energyFunc);
return Build(modelContext, transformedEnergyFunc);
}

/// <summary>
/// Builds an <see cref="EnergyBackgroundEntity"/> using the provided <see cref="IProjectModelContext"/> and energy provider for <see cref="Fractional3D"/> vectors
/// </summary>
/// <param name="modelContext"></param>
/// <param name="energyFunc"></param>
/// <returns></returns>
public EnergyBackgroundEntity Build(IProjectModelContext modelContext, Func<IParticle, Fractional3D, double> energyFunc)
{
var transformedEnergyFunc = TransformEnergyFunction(modelContext, energyFunc);
return Build(modelContext, transformedEnergyFunc);
}

/// <summary>
/// Builds an <see cref="EnergyBackgroundEntity"/> using the provided <see cref="IProjectModelContext"/> and energy provider for <see cref="Vector4I"/> vectors
/// </summary>
/// <param name="modelContext"></param>
/// <param name="energyFunc"></param>
/// <returns></returns>
public EnergyBackgroundEntity Build(IProjectModelContext modelContext, Func<IParticle, Vector4I, double> energyFunc)
{
var particles = modelContext.ModelProject.DataTracker.MapObjects<IParticle>();
var positionCount = modelContext.ModelProject
.Manager<IStructureManager>().DataAccess
.Query(x => x.GetLinearizedExtendedPositionCount());
var rawResult = CreateRawArray(positionCount, particles.Length);

for (var a = 0; a < SizeA; a++)
{
for (var b = 0; b < SizeB; b++)
{
for (var c = 0; c < SizeC; c++)
{
for (var p = 0; p < positionCount; p++)
{
for (var particleId = 1; particleId < particles.Length; particleId++)
{
var particle = particles[particleId];
var vector4 = new Vector4I(a, b, c, p);
rawResult[a, b, c, p, particleId] = energyFunc.Invoke(particle, vector4);
}
}
}
}
}

return new EnergyBackgroundEntity(rawResult);
}

/// <summary>
/// Provides a new zero initialized 5D <see cref="double"/> array of correct size
/// </summary>
/// <param name="positionCount"></param>
/// <param name="particleCount"></param>
/// <returns></returns>
private double[,,,,] CreateRawArray(int positionCount, int particleCount) => new double[SizeA, SizeB, SizeC, positionCount, particleCount];

/// <summary>
/// Transforms the provided energy provider function for <see cref="Cartesian3D"/> to use <see cref="Vector4I"/> data
/// </summary>
/// <param name="modelContext"></param>
/// <param name="energyFunc"></param>
/// <returns></returns>
private static Func<IParticle, Vector4I, double> TransformEnergyFunction(IProjectModelContext modelContext, Func<IParticle, Cartesian3D, double> energyFunc)
{
var vectorEncoder = modelContext.ModelProject
.Manager<IStructureManager>().DataAccess
.Query(x => x.GetVectorEncoder());

double ProvideEnergy(IParticle particle, Vector4I vector4)
{
vectorEncoder.DecodeUnchecked(vector4, out Cartesian3D cartesian3D);
return energyFunc.Invoke(particle, cartesian3D);
}

return ProvideEnergy;
}

/// <summary>
/// Transforms the provided energy provider function for <see cref="Fractional3D"/> to use <see cref="Vector4I"/> data
/// </summary>
/// <param name="modelContext"></param>
/// <param name="energyFunc"></param>
/// <returns></returns>
private static Func<IParticle, Vector4I, double> TransformEnergyFunction(IProjectModelContext modelContext, Func<IParticle, Fractional3D, double> energyFunc)
{
var vectorEncoder = modelContext.ModelProject
.Manager<IStructureManager>().DataAccess
.Query(x => x.GetVectorEncoder());

double ProvideEnergy(IParticle particle, Vector4I vector4)
{
vectorEncoder.DecodeUnchecked(vector4, out Fractional3D fractional3D);
return energyFunc.Invoke(particle, fractional3D);
}

return ProvideEnergy;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public void CopyTo(LatticeConfiguration latticeConfiguration)
/// <returns></returns>
public string GetSizeString() => $"{SizeA},{SizeB},{SizeC}";

/// <summary>
/// Get set size as a default formatted <see cref="string" /> including the position count
/// </summary>
/// <param name="sizeP"></param>
/// <returns></returns>
public string GetSizeString(int sizeP) => $"{SizeA},{SizeB},{SizeC},{sizeP}";

/// <summary>
/// Get the set dopings as a default formatted <see cref="string" />
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ public interface IProjectModelContextBuilder
/// </summary>
/// <returns></returns>
Task<IProjectModelContext> BuildContextAsync();

/// <summary>
/// Builds a new project model context from the current model project
/// </summary>
/// <returns></returns>
IProjectModelContext Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public async Task<IProjectModelContext> BuildContextAsync()
return projectModelContext;
}

/// <inheritdoc />
public IProjectModelContext Build() => BuildContextAsync().Result;

/// <summary>
/// Creates all context components independently and awaits their completion
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Mocassin.Model.Translator.ModelContext;
using Mocassin.Tools.Evaluation.Queries;
using Mocassin.UI.Data.Base;
using Mocassin.UI.Data.Helper;
using Mocassin.UI.Data.Main;

namespace Mocassin.Tools.Evaluation.Context
Expand Down Expand Up @@ -148,21 +149,6 @@ public void EnsureModelContextCreated(JobContext jobContext)
GetSimulationModel(jobContext.JobModel);
}

/// <summary>
/// Restores the <see cref="IProjectModelContext" /> from a passed project xml <see cref="string" />
/// </summary>
/// <param name="projectXml"></param>
/// <returns></returns>
public IProjectModelContext RestoreProjectModelContext(string projectXml)
{
var dbBuildTemplate = ProjectDataObject.CreateFromXml<SimulationDbBuildTemplate>(projectXml);
var modelProject = ModelProjectProvider.Invoke();
modelProject.InputPipeline.PushToProject(dbBuildTemplate.ProjectModelData.GetInputSequence());
dbBuildTemplate.ProjectCustomizationTemplate.PushToModel(modelProject);
var builder = new ProjectModelContextBuilder(modelProject);
return builder.BuildContextAsync().Result;
}

/// <summary>
/// Takes an <see cref="IQueryable{T}" /> of <see cref="SimulationJobPackageModel" /> and builds the sequence of
/// <see cref="IProjectModelContext" /> instances
Expand All @@ -173,7 +159,7 @@ public IQueryable<IProjectModelContext> RestoreProjectModelContext(IQueryable<Si
{
return jobPackageModels
.Include(x => x.ProjectXml)
.Select(x => RestoreProjectModelContext(x.ProjectXml));
.Select(x => MslHelper.RestoreModelContext(x));
}

/// <summary>
Expand All @@ -188,7 +174,7 @@ public IProjectModelContext GetProjectModelContext(int contextId)
if (ProjectContextCache.TryGetValue(contextId, out var context)) return context;

var packageModel = LoadJobPackageModel(contextId);
var context2 = RestoreProjectModelContext(packageModel.ProjectXml);
var context2 = MslHelper.RestoreModelContext(packageModel);
lock (lockObject)
{
ProjectContextCache[contextId] = context2;
Expand Down Expand Up @@ -243,8 +229,8 @@ public ISimulationModel GetSimulationModel(SimulationJobModel jobModel)
var modelContext = GetProjectModelContext(jobModel);
if (SimulationModelCache.TryGetValue(jobModel.SimulationPackageId, out var result)) return result;

var buildGraph = ProjectDataObject.CreateFromXml<SimulationDbBuildTemplate>(jobModel.SimulationJobPackageModel.ProjectXml);
var simulation = buildGraph.ProjectJobSetTemplate
var buildTemplate = ProjectDataObject.CreateFromXml<SimulationDbBuildTemplate>(jobModel.SimulationJobPackageModel.ProjectXml);
var simulation = buildTemplate.ProjectJobSetTemplate
.ToInternals(modelContext.ModelProject)
.First(x => x.CollectionId == jobModel.JobMetaData.CollectionIndex)
.GetSimulation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class MetaDataHelper
public static int GetNumberOfUnitCells(IJobMetaData metaData)
{
var split = metaData.LatticeInfo.Split(',');
if (split.Length != 3) throw new InvalidOperationException("The lattice info in the database is corrupt.");
if (split.Length < 3) throw new InvalidOperationException("The lattice info in the database is corrupt.");
return split.Sum(int.Parse);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override LatticeMetaData GetValue(JobContext jobContext)
protected Vector4I GetLatticeSizeInfo(JobContext jobContext, IUnitCellVectorEncoder vectorEncoder)
{
var split = jobContext.JobModel.JobMetaData.LatticeInfo.Split(',');
if (split.Length != 3) throw new InvalidOperationException("Invalid format of lattice size string.");
if (split.Length < 3) throw new InvalidOperationException("Invalid format of lattice size string.");
return new Vector4I(int.Parse(split[0]), int.Parse(split[1]), int.Parse(split[2]), vectorEncoder.PositionCount);
}
}
Expand Down
Loading

0 comments on commit b414b39

Please sign in to comment.