Skip to content

Commit

Permalink
Add initial benchmarks
Browse files Browse the repository at this point in the history
Add FixedMath reference to demos
  • Loading branch information
Bartl committed Feb 13, 2018
1 parent 0cfa6dd commit aba9e16
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 6 deletions.
6 changes: 6 additions & 0 deletions BEPUbenchmark/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
68 changes: 68 additions & 0 deletions BEPUbenchmark/BEPUbenchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{97B92266-B7EE-468A-BA8B-EDCC6EC27C9B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>BEPUbenchmark</RootNamespace>
<AssemblyName>BEPUbenchmark</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Benchmark.cs" />
<Compile Include="Benchmarks\PyramidBenchmark.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BEPUphysics\BEPUphysics.csproj">
<Project>{c0d52c9f-14b8-4008-8ddc-109c27561a5c}</Project>
<Name>BEPUphysics</Name>
</ProjectReference>
<ProjectReference Include="..\BEPUutilities\BEPUutilities.csproj">
<Project>{34853dea-43a6-4f2f-a379-d1ee04d256d2}</Project>
<Name>BEPUutilities</Name>
</ProjectReference>
<ProjectReference Include="..\FixedMath.Net\src\FixedMath.NET.csproj">
<Project>{d46c606e-5826-465f-9983-251e3d3b2f1c}</Project>
<Name>FixedMath.NET</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
42 changes: 42 additions & 0 deletions BEPUbenchmark/Benchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using BEPUphysics;
using BEPUutilities;
using FixMath.NET;
using System;

namespace BEPUbenchmark
{
abstract class Benchmark
{
protected Space space;

protected abstract void InitializeSpace();
protected void Step()
{
}

public double Run()
{
space = new Space();
space.ForceUpdater.Gravity = new Vector3(0, (Fix64)(-9.81m), 0);
space.TimeStepSettings.TimeStepDuration = 1 / 60m;

InitializeSpace();

long startTime = DateTime.Now.Ticks;
for (int i = 0; i < 1000; i++)
{
Step();
space.Update();
}

long runTime = (DateTime.Now.Ticks - startTime);

return (double)runTime / TimeSpan.TicksPerSecond;
}

public string GetName()
{
return this.GetType().Name;
}
}
}
153 changes: 153 additions & 0 deletions BEPUbenchmark/Benchmarks/BurlapBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using BEPUphysics.CollisionRuleManagement;
using BEPUphysics.Constraints.TwoEntity.Joints;
using BEPUphysics.Entities;
using BEPUphysics.Entities.Prefabs;
using BEPUphysics.NarrowPhaseSystems;
using BEPUutilities;
using FixMath.NET;
using System.Linq;

namespace BEPUbenchmark.Benchmarks
{
class BurlapBenchmark : Benchmark
{
protected override void InitializeSpace()
{
//Joints can also act like springs by modifying their springSettings.
//Though using a bunch of DistanceJoint objects can be slower than just applying direct spring forces,
//it is significantly more stable and allows rigid structures.
//The extra stability can make it useful for cloth-like simulations.
Entity latticePiece;
BallSocketJoint joint;

NarrowPhaseHelper.Factories.BoxBox.Count = 4000;
NarrowPhaseHelper.Factories.BoxSphere.Count = 1000;

int numColumns = 40;
int numRows = 40;
Fix64 xSpacing = 1.0m;
Fix64 zSpacing = 1.0m;
var lattice = new Entity[numRows, numColumns];
for (int i = 0; i < numRows; i++)
for (int j = 0; j < numColumns; j++)
{
latticePiece = new Box(
new Vector3(
xSpacing * i - (numRows - 1) * xSpacing / 2,
15.58m,
2 + zSpacing * j - (numColumns - 1) * zSpacing / 2),
xSpacing, .2m, zSpacing, 10);

lattice[i, j] = latticePiece;

space.Add(latticePiece);
}
//The joints composing the cloth can have their max iterations set independently from the solver iterations.
//More iterations (up to the solver's own max) will increase the quality at the cost of speed.
int clothIterations = 3;
//So while the above clamps joint iterations, setting the solver's iteration limit can lower the
//rest of the solving load (collisions).
space.Solver.IterationLimit = 10;

Fix64 damping = 20000, stiffness = 20000;
Fix64 starchDamping = 5000, starchStiffness = 500;

//Loop through the grid and set up the joints.
for (int i = 0; i < numRows; i++)
for (int j = 0; j < numColumns; j++)
{
if (i == 0 && j + 1 < numColumns)
{
//Add in column connections for left edge.
joint = new BallSocketJoint(lattice[0, j], lattice[0, j + 1], lattice[0, j].Position + new Vector3(-xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);
}
if (i == numRows - 1 && j + 1 < numColumns)
{
//Add in column connections for right edge.
joint = new BallSocketJoint(lattice[numRows - 1, j], lattice[numRows - 1, j + 1], lattice[numRows - 1, j].Position + new Vector3(xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);
}
if (i + 1 < numRows && j == 0)
{
//Add in row connections for top edge.
joint = new BallSocketJoint(lattice[i, 0], lattice[i + 1, 0], lattice[i, 0].Position + new Vector3(xSpacing / 2, 0, -zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);
}
if (i + 1 < numRows && j == numColumns - 1)
{
//Add in row connections for bottom edge.
joint = new BallSocketJoint(lattice[i, numColumns - 1], lattice[i + 1, numColumns - 1], lattice[i, numColumns - 1].Position + new Vector3(xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);

}


if (i + 1 < numRows && j + 1 < numColumns)
{
//Add in interior connections.
joint = new BallSocketJoint(lattice[i, j], lattice[i + 1, j], lattice[i, j].Position + new Vector3(xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);

joint = new BallSocketJoint(lattice[i, j], lattice[i, j + 1], lattice[i, j].Position + new Vector3(xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);

joint = new BallSocketJoint(lattice[i, j], lattice[i + 1, j + 1], lattice[i, j].Position + new Vector3(xSpacing / 2, 0, zSpacing / 2));
joint.SpringSettings.Damping = damping; joint.SpringSettings.Stiffness = stiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);
}

if (i + 2 < numRows && j + 2 < numColumns)
{
//Add in skipping 'starch' connections.
joint = new BallSocketJoint(lattice[i, j], lattice[i + 2, j], lattice[i, j].Position + new Vector3(xSpacing, 0, zSpacing));
joint.SpringSettings.Damping = starchDamping; joint.SpringSettings.Stiffness = starchStiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);

joint = new BallSocketJoint(lattice[i, j], lattice[i, j + 2], lattice[i, j].Position + new Vector3(xSpacing, 0, zSpacing));
joint.SpringSettings.Damping = starchDamping; joint.SpringSettings.Stiffness = starchStiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);

joint = new BallSocketJoint(lattice[i, j], lattice[i + 2, j + 2], lattice[i, j].Position + new Vector3(xSpacing, 0, zSpacing));
joint.SpringSettings.Damping = starchDamping; joint.SpringSettings.Stiffness = starchStiffness;
joint.SolverSettings.MaximumIterationCount = clothIterations;
space.Add(joint);
}

//Add in collision rules.
if (j - 1 >= 0)
{
if (i - 1 >= 0) CollisionRules.AddRule(lattice[i, j], lattice[i - 1, j - 1], CollisionRule.NoBroadPhase);
CollisionRules.AddRule(lattice[i, j], lattice[i, j - 1], CollisionRule.NoBroadPhase);
if (i + 1 < numRows) CollisionRules.AddRule(lattice[i, j], lattice[i + 1, j - 1], CollisionRule.NoBroadPhase);
}

if (i + 1 < numRows) CollisionRules.AddRule(lattice[i, j], lattice[i + 1, j], CollisionRule.NoBroadPhase);
}




//Add some ground.
var sphere = new Sphere(new Vector3(7, 0, 0), 10);
sphere.Material.KineticFriction = .2m;
space.Add(sphere);
space.Add(new Box(new Vector3(0, -20.5m, 0), 100, 10, 100));
}
}
}
51 changes: 51 additions & 0 deletions BEPUbenchmark/Benchmarks/PyramidBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using BEPUphysics.Entities.Prefabs;
using BEPUutilities;
using FixMath.NET;

namespace BEPUbenchmark.Benchmarks
{
class PyramidBenchmark : Benchmark
{
protected override void InitializeSpace()
{
Fix64 boxSize = 2;
int boxCount = 20;
Fix64 platformLength = MathHelper.Min(50, boxCount * boxSize + 10);
space.Add(new Box(new Vector3(0, -.5m, 0), boxCount * boxSize + 20, 1,
platformLength));

for (int i = 0; i < boxCount; i++)
{
for (int j = 0; j < boxCount - i; j++)
{
space.Add(new Box(
new Vector3(
-boxCount * boxSize / 2 + boxSize / 2 * i + j * (boxSize),
(boxSize / 2) + i * boxSize,
0),
boxSize, boxSize, boxSize, 20));
}
}
//Down here are the 'destructors' used to blow up the pyramid.

Sphere pow = new Sphere(new Vector3(-25, 5, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
pow = new Sphere(new Vector3(-15, 10, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
pow = new Sphere(new Vector3(-5, 15, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
pow = new Sphere(new Vector3(5, 15, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
pow = new Sphere(new Vector3(15, 10, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
pow = new Sphere(new Vector3(25, 5, 70), 2, 40);
pow.LinearVelocity = new Vector3(0, 10, -100);
space.Add(pow);
}
}
}
29 changes: 29 additions & 0 deletions BEPUbenchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using BEPUbenchmark.Benchmarks;
using System;

namespace BEPUbenchmark
{
class Program
{
static Benchmark[] benchmarks = { new PyramidBenchmark() };

static void Main(string[] args)
{
Console.WriteLine("Running benchmarks...\n");

double runtime = 0;
foreach (Benchmark b in benchmarks)
{
Console.Write(b.GetName()+"... ");
double time = b.Run();

Console.WriteLine(Math.Round(time, 2) + "s");
runtime += time;
}

Console.WriteLine("\nCumulative runtime: "+Math.Round(runtime, 2));
Console.WriteLine("\nPress enter exit");
Console.Read();
}
}
}
36 changes: 36 additions & 0 deletions BEPUbenchmark/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("BEPUbenchmark")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("BEPUbenchmark")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("97b92266-b7ee-468a-ba8b-edcc6ec27c9b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit aba9e16

Please sign in to comment.