Skip to content

Commit

Permalink
Newton_slsqp_solver added
Browse files Browse the repository at this point in the history
  • Loading branch information
hamish2014 committed Sep 20, 2018
1 parent 572bbdb commit 3977622
Show file tree
Hide file tree
Showing 9 changed files with 775 additions and 45 deletions.
Binary file modified Gui/Resources/resources.rcc
Binary file not shown.
147 changes: 109 additions & 38 deletions Gui/Resources/ui/assembly2_prefs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,109 @@
<rect>
<x>0</x>
<y>0</y>
<width>563</width>
<height>512</height>
<width>532</width>
<height>510</height>
</rect>
</property>
<property name="windowTitle">
<string>Assembly2 Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
<widget class="QGroupBox" name="verticalGroupBox">
<property name="title">
<string>Solver</string>
</property>
</spacer>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>6</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Algorithm</string>
</property>
</widget>
</item>
<item>
<widget class="Gui::PrefComboBox" name="gui::prefcombox_1">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="editable">
<bool>false</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>solver_to_use</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Mod/Assembly2</cstring>
</property>
<item>
<property name="text">
<string>dof_reduction_solver</string>
</property>
</item>
<item>
<property name="text">
<string>newton_solver_slsqp</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="Gui::PrefCheckBox" name="gui::prefcheckbox_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>Use cache (experimental)</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>useCache</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Mod/Assembly2</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="groupBox">
Expand All @@ -36,7 +119,7 @@
</sizepolicy>
</property>
<property name="title">
<string>Solver</string>
<string>General</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
Expand Down Expand Up @@ -166,34 +249,22 @@
</property>
</widget>
</item>
<item>
<widget class="Gui::PrefCheckBox" name="gui::prefcheckbox_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>Use solver cache (experimental)</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
<property name="prefEntry" stdset="0">
<cstring>useCache</cstring>
</property>
<property name="prefPath" stdset="0">
<cstring>Mod/Assembly2</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
Expand Down
15 changes: 11 additions & 4 deletions assembly2/solvers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import common
from common import constraintsObjectsAllExist
from dof_reduction_solver import solveConstraints as solveConstraints_dof_reduction_solver
from newton_solver import solveConstraints as solveConstraints_newton_solver

_default = "_assembly2_preference_"

Expand All @@ -19,15 +20,21 @@ def solveConstraints(
):
if solver_name == _default or use_cache == _default:
preferences = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Assembly2")
if solver_name == _default:
solver_name = preferences.GetString('solver_to_use', 'dof_reduction_solver')
if use_cache == _default:
use_cache = preferences.GetBool('useCache', False)
if solver_name == _default:
#solver_name = preferences.GetString('solver_to_use', 'dof_reduction_solver')
solver_name = {
0:'dof_reduction_solver',
1:'newton_solver_slsqp'
}[ preferences.GetInt('solver_to_use',0) ]
if use_cache == _default:
use_cache = preferences.GetBool('useCache', False)
if not constraintsObjectsAllExist(doc):
return
updateOldStyleConstraintProperties(doc)
if solver_name == 'dof_reduction_solver':
return solveConstraints_dof_reduction_solver( doc, showFailureErrorDialog, printErrors, use_cache )
elif solver_name == 'newton_solver_slsqp':
return solveConstraints_newton_solver( doc, showFailureErrorDialog, printErrors, use_cache )
else:
raise NotImplementedError( '%s solver interface not added yet' % solver_name )

Expand Down
1 change: 1 addition & 0 deletions assembly2/solvers/dof_reduction_solver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''
from assembly2.lib3D import *
from assembly2.solvers.common import *
from assembly2.core import QtGui
import time, numpy
from numpy import pi, inf
from numpy.linalg import norm
Expand Down
11 changes: 8 additions & 3 deletions assembly2/solvers/dof_reduction_solver/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ def tearDownClass(cls):
debugPrint(0,' total running time: %3.2f s' % (time.time() - stats.t_start) )
debugPrint(0,'------------------------------------------')

def check_solution( self, constraintSystem, solution ):
a = constraintSystem.variableManager.X

def get_solver_X( self, solver_result ):
return solver_result.variableManager.X

def check_solution( self, solver_result, solution ):
a = self.get_solver_X( solver_result )
b = solution if type( solution ) != str else [ float(v) for v in solution[1:-1].split() ]
self.assertTrue(
len(a) == len(b) and numpy.allclose( a, b ),
Expand All @@ -55,7 +59,7 @@ def _test_file( self, testFile_basename, solution = None ):
# continue
doc = FreeCAD.open(testFile)
t_start_solver = time.time()
constraintSystem = solveConstraints( doc, solver_name = 'dof_reduction_solver', use_cache = self.use_cache )
constraintSystem = solveConstraints( doc, solver_name = 'dof_reduction_solver', use_cache = self.use_cache, showFailureErrorDialog=False )
if solution:
self.check_solution( constraintSystem, solution )
stats.t_solver += time.time() - t_start_solver
Expand All @@ -73,6 +77,7 @@ def _test_file( self, testFile_basename, solution = None ):
stats.t_cache += time.time() - t_start_cache
constraintSystem.update()
stats.n_solved += 1
FreeCAD.closeDocument( doc.Name )
debugPrint(1,'\n\n\n')

def testAssembly_01_2_cubes( self ):
Expand Down
Loading

0 comments on commit 3977622

Please sign in to comment.