Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GSAGH-550, GSAGH-560 : Analysis case is not consistent with the Load case in Gsa_GH #737

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions GsaGH/Components/4_Analysis/AnalyseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected override void BeforeSolveInstance() {

// add report output to old components
if (Params.Output.Count < 5) {
Params.RegisterOutputParam(new Param_String());
Params.Output[1].Name = "Errors";
Params.Output[1].NickName = "E";
Params.Output[1].Description = "Analysis Task Errors";
Expand Down Expand Up @@ -222,21 +223,15 @@ protected override void SolveInternal(IGH_DataAccess da) {
if (_analysis) {
IReadOnlyDictionary<int, AnalysisTask> gsaTasks = model.ApiModel.AnalysisTasks();
if (gsaTasks.Count < 1) {
var task = new GsaAnalysisTask {
Id = model.ApiModel.AddAnalysisTask(),
};
task.CreateDefaultCases(model);
int taskId = TaskHelper.CreateDefaultStaticAnalysisTask(model);
var task = new GsaAnalysisTask(taskId, model.ApiModel);
if (task.Cases == null || task.Cases.Count == 0) {
this.AddRuntimeWarning(
" Model contains no loads and has not been analysed, but has been assembled.");
} else {
this.AddRuntimeRemark(
" Model contained no Analysis Tasks. Default Task has been created containing " +
"all cases found in model");
foreach (GsaAnalysisCase ca in task.Cases) {
model.ApiModel.AddAnalysisCaseToTask(task.Id, ca.Name, ca.Definition);
}

gsaTasks = model.ApiModel.AnalysisTasks();
}
}
Expand Down
7 changes: 2 additions & 5 deletions GsaGH/Components/4_Analysis/CreateAnalysisTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,9 @@ private bool GetAnalysisCases(IGH_DataAccess da, string name, out List<GsaAnalys
this.AddRuntimeRemark("Default Task has been created; it will by default contain all cases found in model");
}
} else {
cases = new List<GsaAnalysisCase>();
var footfallAnalysisCase = new GsaAnalysisCase {
Name = name,
Definition = "Footfall",
cases = new List<GsaAnalysisCase> {
new GsaAnalysisCase(name, "Footfall")
};
cases.Add(footfallAnalysisCase);
}

return true;
Expand Down
19 changes: 1 addition & 18 deletions GsaGH/Helpers/Assembly/ModelAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal partial class ModelAssembly {
private GsaIntDictionary<Axis> _axes;
private GsaGuidIntListDictionary<GSAElement> _elements;
private GsaIntDictionary<GridLine> _gridLines;
private GsaModel _gsaModel;
private GsaGuidDictionary<EntityList> _lists;
private ConcurrentDictionary<int, ConcurrentBag<int>> memberElementRelationship;
private GsaGuidDictionary<Member> _members;
Expand Down Expand Up @@ -251,23 +250,8 @@ private void CheckIfModelIsEmpty() {
private void ConvertAndAssembleAnalysisTasks(List<GsaAnalysisTask> analysisTasks) {
// Set Analysis Tasks in model
if (analysisTasks != null) {
ReadOnlyDictionary<int, AnalysisTask> existingTasks = _model.AnalysisTasks();
foreach (GsaAnalysisTask task in analysisTasks) {
if (!existingTasks.Keys.Contains(task.Id)) {
task.Id = _model.AddAnalysisTask(task.ApiTask);
}

if (task.Cases == null || task.Cases.Count == 0) {
task.CreateDefaultCases(_gsaModel);
}

if (task.Cases == null) {
continue;
}

foreach (GsaAnalysisCase ca in task.Cases) {
_model.AddAnalysisCaseToTask(task.Id, ca.Name, ca.Definition);
}
TaskHelper.ImportAnalysisTask(task, _model);
}
}
}
Expand Down Expand Up @@ -610,7 +594,6 @@ private void ReportWarningFromAddingGridSurfacesOrList(string mes, string troubl
private void SetupModel(GsaModel model, LengthUnit unit) {
model ??= new GsaModel();
_model = model.ApiModel;
_gsaModel = model;
_unit = unit;
_model.UiUnits().LengthLarge = UnitMapping.GetApiUnit(_unit);
UiUnits units = _model.UiUnits();
Expand Down
47 changes: 47 additions & 0 deletions GsaGH/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

using GsaAPI;

using GsaGH.Parameters;

namespace GsaGH.Helpers {
internal static partial class TaskHelper {
public static int CreateDefaultStaticAnalysisTask(GsaModel model) {
int taskId = model.ApiModel.AddAnalysisTask();
model.ApiModel.CreateDefaultAnalysisCasesForTheTask(taskId);
return taskId;
}

public static void AddAnalysisTask(GsaAnalysisTask task, GsaModel model) {
task.Id = model.ApiModel.AddAnalysisTask(task.ApiTask);
model.ApiModel.CreateDefaultAnalysisCasesForTheTask(task.Id);
}

public static void ImportAnalysisTask(GsaAnalysisTask task, ref GsaModel model) {
ReadOnlyDictionary<int, AnalysisTask> existingTasks = model.ApiModel.AnalysisTasks();
if (task != null && !existingTasks.Keys.Contains(task.Id)) {
int highestTask = existingTasks.Count;
var analysisCases = new Dictionary<int, AnalysisCase>();
foreach (GsaAnalysisCase analysisCase in task.Cases) {
analysisCases.Add(analysisCase.Id, new AnalysisCase(analysisCase.Name, analysisCase.Definition));
}
if (task.ApiTask != null) {
if (analysisCases.Count == 0) {
AddAnalysisTask(task, model);
} else {
model.ApiModel.ImportAnalysisTask(task.ApiTask, new ReadOnlyDictionary<int, AnalysisCase>(analysisCases));
task.Id = highestTask + 1;
}
}
}
}

public static void ImportAnalysisTask(GsaAnalysisTask task, Model model) {
var gsaModel = new GsaModel(model);
ImportAnalysisTask(task, ref gsaModel);
}
}
}
29 changes: 5 additions & 24 deletions GsaGH/Parameters/0_Model/GsaModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,40 +153,21 @@ internal void SetUserDefaultUnits() {

internal Tuple<List<GsaAnalysisTaskGoo>, List<GsaAnalysisCaseGoo>> GetAnalysisTasksAndCombinations() {
ReadOnlyDictionary<int, AnalysisTask> tasks = ApiModel.AnalysisTasks();
ReadOnlyDictionary<int, LoadCase> loadCases = ApiModel.LoadCases();
ReadOnlyDictionary<int, AnalysisCase> analysisCases = ApiModel.AnalysisCases();

var tasksList = new List<GsaAnalysisTaskGoo>();
var caseList = new List<GsaAnalysisCaseGoo>();
var caseIDs = new List<int>();


foreach (KeyValuePair<int, AnalysisTask> item in tasks) {
var task = new GsaAnalysisTask(item.Key, item.Value, ApiModel);
var task = new GsaAnalysisTask(item.Key, ApiModel);
tasksList.Add(new GsaAnalysisTaskGoo(task));
caseIDs.AddRange(task.Cases.Select(acase => acase.Id));
}

caseIDs.AddRange(GetLoadCases());

foreach (int caseId in caseIDs) {
string caseName = ApiModel.AnalysisCaseName(caseId);
if (caseName == string.Empty) {
if (loadCases.ContainsKey(caseId)) {
caseName = loadCases[caseId].Name;
}
if (caseName == string.Empty) {
caseName = "Case " + caseId;
}
}

string caseDescription = ApiModel.AnalysisCaseDescription(caseId);
if (caseDescription == string.Empty) {
caseDescription = "L" + caseId;
}

foreach (KeyValuePair<int, AnalysisCase> item in analysisCases) {
caseList.Add(
new GsaAnalysisCaseGoo(new GsaAnalysisCase(caseId, caseName, caseDescription)));
new GsaAnalysisCaseGoo(new GsaAnalysisCase(item.Key, item.Value.Name, item.Value.Description)));
}

return new Tuple<List<GsaAnalysisTaskGoo>, List<GsaAnalysisCaseGoo>>(tasksList, caseList);
}

Expand Down
26 changes: 14 additions & 12 deletions GsaGH/Parameters/4_Analysis/GsaAnalysisCase.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
using GsaGH.Helpers;
using System.Xml.Linq;

using GsaAPI;

using GsaGH.Helpers;

namespace GsaGH.Parameters {
/// <summary>
/// Analysis Case definition, for instance `L1` for LoadCase 1 or `L1 + L2` for combining multiple load cases in one Analysis case.
/// <para>Refer to <see href="https://docs.oasys-software.com/structural/gsa/references/analysiscases.html">Analysis cases</see> to read more.</para>
/// </summary>
public class GsaAnalysisCase {
public string Definition { get; set; }
public string Name { get; set; }
private AnalysisCase ApiCase { get; set; }
psarras marked this conversation as resolved.
Show resolved Hide resolved
internal int Id { get; set; } = 0;

public GsaAnalysisCase() { }

public string Definition => ApiCase.Description;
public string Name => ApiCase.Name;
private GsaAnalysisCase() {
ApiCase = new AnalysisCase(string.Empty, string.Empty);
}
public GsaAnalysisCase(string name, string description) {
Name = name;
Definition = description;
ApiCase = new AnalysisCase(name, description);
}

internal GsaAnalysisCase(int id, string name, string description = "") {
internal GsaAnalysisCase(int id, string name, string description) {
Id = id;
Name = name;
Definition = description;
ApiCase = new AnalysisCase(name, description);
}

public GsaAnalysisCase Duplicate() {
Expand All @@ -37,7 +40,6 @@ public override string ToString() {
if (Definition != null) {
s += " " + Definition;
}

return string.Join(" ", id, s).TrimSpaces();
}
}
Expand Down
29 changes: 16 additions & 13 deletions GsaGH/Parameters/4_Analysis/GsaAnalysisTask.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

using GsaAPI;

using GsaGH.Helpers;

using Rhino.Commands;

namespace GsaGH.Parameters {
/// <summary>
/// <para>An analysis task is a package of work for the solver. Thus we can have a static analysis task, a modal analysis task, etc. Each analysis task has one or more analysis case(s). The distinction is that the cases corresponds to result sets and define items such as loading (in the static case) while the task describes what the solver has to do. </para>
Expand All @@ -20,26 +23,26 @@ public GsaAnalysisTask() {
Id = 0;
}

internal GsaAnalysisTask(int id, AnalysisTask task, Model model) {
internal GsaAnalysisTask(int id, Model model) {
Id = id;
foreach (int caseId in task.Cases) {
string caseName = model.AnalysisCaseName(caseId);
string caseDescription = model.AnalysisCaseDescription(caseId);
Cases.Add(new GsaAnalysisCase(caseId, caseName, caseDescription));
}
ApiTask = model.AnalysisTasks()[Id];
CreateCases(model);
}

internal GsaAnalysisTask(AnalysisTask task, Model model) {
ApiTask = task;
CreateCases(model);
}
private void CreateCases(Model model) {
ReadOnlyDictionary<int, AnalysisCase> analysisCases = model.AnalysisCases();
foreach (int caseId in ApiTask.Cases.Where(x => analysisCases.ContainsKey(x))) {
AnalysisCase analysisCase = model.AnalysisCases()[caseId];
Cases.Add(new GsaAnalysisCase(caseId, analysisCase.Name, analysisCase.Description));
}
}

public override string ToString() {
return (Id > 0 ? $"ID:{Id} " : string.Empty) + $"'{ApiTask.Name}' {ApiTask.Type}".Replace("_", " ")
.TrimSpaces();
}

internal void CreateDefaultCases(GsaModel gsaModel) {
Tuple<List<GsaAnalysisTaskGoo>, List<GsaAnalysisCaseGoo>> tuple
= gsaModel.GetAnalysisTasksAndCombinations();
Cases = tuple.Item2.Select(x => x.Value).ToList();
}
}
}
34 changes: 34 additions & 0 deletions GsaGHTests/1_BaseParameters/0_Model/GsaModelTest.cs
psarras marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using GsaGH.Helpers.Assembly;
using GsaGH.Helpers.GsaApi.EnumMappings;
using GsaGH.Helpers.Import;
using GsaGH.Parameters;

using GsaGHTests.Helper;
Expand Down Expand Up @@ -130,5 +131,38 @@ public void TestModelLengthUnit() {

Assert.Equal(LengthUnit.Foot, UnitMapping.GetUnit(m.ApiModel.UiUnits().LengthLarge));
}

[Fact]
public void ShouldNotHaveAnyTasks() {
GsaAnalysis gsaAnalysis = null;
var assembly = new ModelAssembly(null, null, null, null, null, null, gsaAnalysis, LengthUnit.Meter, Length.Zero,
false, null);
var model = new GsaModel(assembly.GetModel());
List<GsaAnalysisTaskGoo> importedTasks = model.GetAnalysisTasksAndCombinations().Item1;
Assert.Empty(importedTasks);
Assert.Empty(importedTasks);
}

SandeepArup marked this conversation as resolved.
Show resolved Hide resolved
[Fact]
public void AnalysisTaskAndCasesCanBeImportedFromSeedModel() {
//seed model to read existing analysis task
var seedModel = new GsaModel();
seedModel.ApiModel.Open(GsaFile.SteelDesignComplex);
List<GsaAnalysisTaskGoo> seedTasks = seedModel.GetAnalysisTasksAndCombinations().Item1;
var analysis = new GsaAnalysis();
foreach (GsaAnalysisTaskGoo task in seedTasks) {
analysis.AnalysisTasks.Add(task.Value);
}

//import into new model
var assembly = new ModelAssembly(null, null, null, null, null, null, analysis,
LengthUnit.Meter, Length.Zero, false, null);
var model = new GsaModel(assembly.GetModel());

List<GsaAnalysisTaskGoo> importedTasks = model.GetAnalysisTasksAndCombinations().Item1;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use equality operator on every type so we avoid long loops of asserts

Suggested change
Assert.Equal(importedTasks, seedTasks);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used existing helper class Assert.True(Duplicates.AreEqual(seedTasks, importedTasks));

Assert.True(Duplicates.AreEqual(seedTasks, importedTasks));

}
}
}
13 changes: 0 additions & 13 deletions GsaGHTests/1_BaseParameters/4_Analysis/GsaAnalysisCaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,9 @@ public void DuplicateTest() {

Duplicates.AreEqual(original, duplicate);

duplicate.Id = 0;
duplicate.Name = "";
duplicate.Definition = "";

Assert.Equal(1, original.Id);
Assert.Equal("name", original.Name);
Assert.Equal("description", original.Definition);
}

[Fact]
public void EmptyConstructorTest() {
var analysisCase = new GsaAnalysisCase();

Assert.Equal(0, analysisCase.Id);
Assert.Null(analysisCase.Name);
Assert.Null(analysisCase.Definition);
}
}
}
Loading
Loading