Skip to content

Commit

Permalink
Extract and store SNOPT version in history file (#214)
Browse files Browse the repository at this point in the history
* added sntitle to extract SNOPT version information

* added optVersion to docs

* added test for SNOPT version extraction

* moved sntitle call to if block
  • Loading branch information
ewu63 authored Mar 14, 2021
1 parent 4a16b2d commit 9865d31
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/api/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ In this case, the history file would have the following layout::
│ ├── endTime
│ ├── optTime
│ ├── optimizer
│ └── version
│ ├── version
│ └── optVersion
├── optProb
├── varInfo
│ └── xvars
Expand Down
4 changes: 4 additions & 0 deletions pyoptsparse/pyOpt_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
options={},
checkDefaultOptions=True,
caseSensitiveOptions=True,
version=None,
):
"""
This is the base optimizer class that all optimizers inherit from.
Expand Down Expand Up @@ -60,6 +61,8 @@ def __init__(
self.callCounter = 0
self.sens = None
self.optProb = None
self.version = version

# Default options:
self.appendLinearConstraints = False
self.jacType = "dense"
Expand Down Expand Up @@ -798,6 +801,7 @@ def _setMetadata(self):
self.metadata = {
"version": __version__,
"optimizer": self.name,
"optVersion": self.version,
"optName": self.optProb.name,
"nprocs": MPI.COMM_WORLD.size,
"optOptions": options,
Expand Down
15 changes: 15 additions & 0 deletions pyoptsparse/pySNOPT/pySNOPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import time
import datetime
import re

# =============================================================================
# External Python modules
Expand Down Expand Up @@ -149,6 +150,19 @@ def __init__(self, raiseError=True, options={}):
if snopt is None:
if raiseError:
raise Error("There was an error importing the compiled snopt module")
else:
version = None
else:
# extract SNOPT version
version_str = snopt.sntitle().decode("utf-8")
# The version_str is going to look like
# S N O P T 7.7.5 (Oct 2020)
# we search between "S N O P T" and "("
res = re.search("S N O P T(.*)\(", version_str)
if res is not None:
version = res.group(1).strip()
else:
version = None

super().__init__(
name,
Expand All @@ -157,6 +171,7 @@ def __init__(self, raiseError=True, options={}):
informs=self.informs,
options=options,
checkDefaultOptions=False,
version=version,
)

# SNOPT need Jacobians in csc format
Expand Down
3 changes: 3 additions & 0 deletions pyoptsparse/pySNOPT/source/f2py/snopt.pyf
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ python module snopt ! in
double precision dimension(lenrw) :: rw
integer optional,check(len(rw)>=lenrw),depend(rw) :: lenrw=len(rw)
end subroutine sngetr
subroutine sntitle(title) ! in :snopt:sn02lib.f
character*30, intent(out) :: title
end subroutine sntitle
subroutine snoptc(start,m,n,ne,nname,nncon,nnobj,nnjac,iobj,objadd,prob,userfg,jcol,indj,locj,bl,bu,names,hs,x,pi,rc,inform,mincw,miniw,minrw,ns,ninf,sinf,obj,cu,lencu,iu,leniu,ru,lenru,cw,lencw,iw,leniw,rw,lenrw) ! in :snopt:snoptc.f
use snoptc__user__routines
character*(*) intent(inout) :: start
Expand Down
14 changes: 13 additions & 1 deletion test/test_hs015.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,21 @@ def check_hist_file(self, optimizer, tol):
# Metadata checks
metadata = hist.getMetadata()
self.assertEqual(metadata["optimizer"], optimizer)
metadata_def_keys = ["optName", "optOptions", "nprocs", "startTime", "endTime", "optTime", "version"]
metadata_def_keys = [
"optName",
"optOptions",
"nprocs",
"startTime",
"endTime",
"optTime",
"version",
"optVersion",
]
for key in metadata_def_keys:
self.assertIn(key, metadata)
# we test that SNOPT version is stored correctly
if optimizer == "SNOPT" and key == "optVersion":
self.assertNotEqual(metadata[key], None)
hist.getOptProb()

# Info checks
Expand Down

0 comments on commit 9865d31

Please sign in to comment.