Skip to content

Commit

Permalink
+ further unit tests for strength changes
Browse files Browse the repository at this point in the history
+ constructor test repaired
  • Loading branch information
chrxh committed Nov 21, 2024
1 parent 21a7cf1 commit 32ff33d
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 53 deletions.
4 changes: 2 additions & 2 deletions source/EngineGpuKernels/RadiationProcessor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ __inline__ __device__ void RadiationProcessor::radiate(SimulationData& data, flo
auto sumActiveRatios = 0.0f;
for (int i = 0; i < numActiveSources; ++i) {
auto index = data.preprocessedSimulationData.activeRadiationSources.getActiveSource(i);
sumActiveRatios += cudaSimulationParameters.radiationSources[index].strengthRatio;
sumActiveRatios += cudaSimulationParameters.radiationSources[index].strength;
}
if (sumActiveRatios > 0) {
auto randomRatioValue = data.numberGen1.random(1.0f);
Expand All @@ -239,7 +239,7 @@ __inline__ __device__ void RadiationProcessor::radiate(SimulationData& data, flo
auto matchSource = false;
for (int i = 0; i < numActiveSources; ++i) {
sourceIndex = data.preprocessedSimulationData.activeRadiationSources.getActiveSource(i);
sumActiveRatios += cudaSimulationParameters.radiationSources[sourceIndex].strengthRatio;
sumActiveRatios += cudaSimulationParameters.radiationSources[sourceIndex].strength;
if (randomRatioValue <= sumActiveRatios) {
matchSource = true;
break;
Expand Down
6 changes: 3 additions & 3 deletions source/EngineInterface/RadiationSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ enum RadiationSourceShapeType_

struct RadiationSource
{
float strengthRatio = 0.0f;
bool strengthRatioPinned = false;
float strength = 0.0f;
bool strengthPinned = false;
float posX = 0;
float posY = 0;
float velX = 0;
Expand All @@ -64,7 +64,7 @@ struct RadiationSource
}
}
return posX == other.posX && posY == other.posY && velX == other.velX && velY == other.velY && useAngle == other.useAngle && angle == other.angle
&& strengthRatio == other.strengthRatio && strengthRatioPinned == other.strengthRatioPinned;
&& strength == other.strength && strengthPinned == other.strengthPinned;
}
bool operator!=(RadiationSource const& other) const { return !operator==(other); }
};
42 changes: 28 additions & 14 deletions source/EngineInterface/SimulationParametersEditService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ auto SimulationParametersEditService::getRadiationStrengths(SimulationParameters

auto baseStrength = 1.0f;
for (int i = 0; i < parameters.numRadiationSources; ++i) {
baseStrength -= parameters.radiationSources[i].strengthRatio;
baseStrength -= parameters.radiationSources[i].strength;
}
if (baseStrength < 0) {
baseStrength = 0;
}

result.values.emplace_back(baseStrength);
for (int i = 0; i < parameters.numRadiationSources; ++i) {
result.values.emplace_back(parameters.radiationSources[i].strengthRatio);
if (parameters.radiationSources[i].strengthRatioPinned) {
result.values.emplace_back(parameters.radiationSources[i].strength);
if (parameters.radiationSources[i].strengthPinned) {
result.pinned.insert(i + 1);
}
}
Expand All @@ -28,30 +28,41 @@ auto SimulationParametersEditService::getRadiationStrengths(SimulationParameters
return result;
}

void SimulationParametersEditService::applyRadiationStrengthValues(SimulationParameters& parameters, RadiationStrengths const& strengths)
void SimulationParametersEditService::applyRadiationStrengths(SimulationParameters& parameters, RadiationStrengths const& strengths)
{
CHECK(parameters.numRadiationSources + 1 == strengths.values.size());

parameters.baseStrengthRatioPinned = strengths.pinned.contains(0);
for (int i = 0; i < parameters.numRadiationSources; ++i) {
parameters.radiationSources[i].strengthRatio = strengths.values.at(i + 1);
parameters.radiationSources[i].strength = strengths.values.at(i + 1);
parameters.radiationSources[i].strengthPinned = strengths.pinned.contains(i + 1);
}
}

void SimulationParametersEditService::adaptRadiationStrengths(RadiationStrengths& strengths, RadiationStrengths& origStrengths, int changeIndex) const
{
if (strengths.values.size() == strengths.pinned.size()) {
auto pinnedValues = strengths.pinned;
pinnedValues.insert(changeIndex);

if (strengths.values.size() == pinnedValues.size()) {
strengths = origStrengths;
return;
}
for (auto const& strength : strengths.values) {
if (strength < 0) {
strengths = origStrengths;
return;
}
}

auto sum = 0.0f;
for (auto const& ratio : strengths.values) {
sum += ratio;
for (auto const& strength : strengths.values) {
sum += strength;
}
auto diff = sum - 1;
auto sumWithoutFixed = 0.0f;
for (int i = 0; i < strengths.values.size(); ++i) {
if (!strengths.pinned.contains(i)) {
if (!pinnedValues.contains(i)) {
sumWithoutFixed += strengths.values.at(i);
}
}
Expand All @@ -64,14 +75,14 @@ void SimulationParametersEditService::adaptRadiationStrengths(RadiationStrengths
auto reduction = 1.0f - diff / sumWithoutFixed;

for (int i = 0; i < strengths.values.size(); ++i) {
if (!strengths.pinned.contains(i)) {
if (!pinnedValues.contains(i)) {
strengths.values.at(i) *= reduction;
}
}
} else {
for (int i = 0; i < strengths.values.size(); ++i) {
if (!strengths.pinned.contains(i)) {
strengths.values.at(i) = -diff / toFloat(strengths.values.size() - strengths.pinned.size());
if (!pinnedValues.contains(i)) {
strengths.values.at(i) = -diff / toFloat(strengths.values.size() - pinnedValues.size());
}
}
}
Expand Down Expand Up @@ -104,11 +115,11 @@ auto SimulationParametersEditService::calcRadiationStrengthsForAddingSpot(Radiat
auto SimulationParametersEditService::calcRadiationStrengthsForDeletingSpot(
RadiationStrengths const& strengths, int deleteIndex) const -> RadiationStrengths
{
auto numRemainingUnpinned = 0;
auto existsUnpinned = false;
auto sumRemainingUnpinnedStrengths = 0.0f;
for (int i = 0; i < strengths.values.size(); ++i) {
if (!strengths.pinned.contains(i) && i != deleteIndex) {
++numRemainingUnpinned;
existsUnpinned = true;
sumRemainingUnpinnedStrengths += strengths.values.at(i);
}
}
Expand All @@ -129,6 +140,9 @@ auto SimulationParametersEditService::calcRadiationStrengthsForDeletingSpot(
}
}
}
if (!existsUnpinned) {
result.pinned.erase(0);
}

return result;
}
2 changes: 1 addition & 1 deletion source/EngineInterface/SimulationParametersEditService.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SimulationParametersEditService

public:
RadiationStrengths getRadiationStrengths(SimulationParameters const& parameters) const;
void applyRadiationStrengthValues(SimulationParameters& parameters, RadiationStrengths const& strengths);
void applyRadiationStrengths(SimulationParameters& parameters, RadiationStrengths const& strengths);

void adaptRadiationStrengths(RadiationStrengths& strengths, RadiationStrengths& origStrengths, int changeIndex) const;
RadiationStrengths calcRadiationStrengthsForAddingSpot(RadiationStrengths const& strengths) const;
Expand Down
9 changes: 7 additions & 2 deletions source/EngineTests/ConstructorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,12 @@ TEST_F(ConstructorTests, constructFirstCell_noSeparation)
auto genome = GenomeDescriptionService::get().convertDescriptionToBytes(
GenomeDescription()
.setHeader(GenomeHeaderDescription().setSeparateConstruction(false).setStiffness(0.35f))
.setCells({CellGenomeDescription().setColor(2).setExecutionOrderNumber(4).setInputExecutionOrderNumber(5).setOutputBlocked(true)}));
.setCells({CellGenomeDescription()
.setCellFunction(ConstructorGenomeDescription().setMakeSelfCopy())
.setColor(2)
.setExecutionOrderNumber(4)
.setInputExecutionOrderNumber(5)
.setOutputBlocked(true)}));

DataDescription data;
data.addCell(
Expand Down Expand Up @@ -627,7 +632,7 @@ TEST_F(ConstructorTests, constructFirstCell_noSeparation)
EXPECT_EQ(4, actualConstructedCell.executionOrderNumber);
EXPECT_EQ(5, actualConstructedCell.inputExecutionOrderNumber);
EXPECT_TRUE(actualConstructedCell.outputBlocked);
EXPECT_EQ(CellFunction_None, actualConstructedCell.getCellFunctionType());
EXPECT_EQ(CellFunction_Constructor, actualConstructedCell.getCellFunctionType());
EXPECT_EQ(123, actualConstructedCell.activationTime);
EXPECT_TRUE(approxCompare(0.35f, actualConstructedCell.stiffness, 0.01f));
EXPECT_TRUE(approxCompare(_parameters.cellNormalEnergy[0], actualConstructedCell.energy));
Expand Down
Loading

0 comments on commit 32ff33d

Please sign in to comment.