solve.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from .coefficients import coefficients
  2. from .adaptation import hdpg1d
  3. from .postprocess import utils
  4. import sys
  5. def queryYesNo(question, default="yes"):
  6. valid = {"yes": True, "y": True, "ye": True,
  7. "no": False, "n": False}
  8. if default is None:
  9. prompt = " [y/n] "
  10. elif default == "yes":
  11. prompt = " [Y/n] "
  12. elif default == "no":
  13. prompt = " [y/N] "
  14. else:
  15. raise ValueError("invalid default answer: '%s'" % default)
  16. while True:
  17. sys.stdout.write(question + prompt)
  18. choice = input().lower()
  19. if default is not None and choice == '':
  20. return valid[default]
  21. elif choice in valid:
  22. return valid[choice]
  23. else:
  24. sys.stdout.write("Please respond with 'yes' or 'no' "
  25. "(or 'y' or 'n').\n")
  26. def getCoefficients():
  27. question = 'Do you want to use the default parameters?'
  28. isDefault = queryYesNo(question, "yes")
  29. if (isDefault):
  30. Coeff = coefficients(1e-6, 0, 0, 2, 2, 1e-6, 1e-6)
  31. else:
  32. Coeff = coefficients.fromInput()
  33. return Coeff
  34. def menu():
  35. menu = {}
  36. menu['1.'] = "Solve with HDG."
  37. menu['2.'] = "Solve with HDPG."
  38. menu['3.'] = "Exit."
  39. for key, value in sorted(menu.items()):
  40. print(key, value)
  41. def hdgSolve():
  42. hdgCoeff = getCoefficients()
  43. print("Solving...")
  44. hdgSolution = hdpg1d(hdgCoeff)
  45. # solve the problem adaptively and plot convergence history
  46. hdgSolution.adaptive()
  47. print("Problem solved. Please check the convergence plot.")
  48. utils(hdgSolution).convHistory()
  49. def runInteractive():
  50. menu()
  51. selection = input("Please Select: ")
  52. if selection == '1':
  53. hdgSolve()
  54. elif selection == '2':
  55. print("In development...")
  56. elif selection == '3':
  57. print("Bye.")
  58. else:
  59. print("Unknown Option Selected!")