solve.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.from_input()
  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. hdgSolution = hdpg1d(hdgCoeff)
  44. # solve the problem adaptively and plot convergence history
  45. hdgSolution.adaptive()
  46. utils(hdgSolution).convHistory()
  47. def runInteractive():
  48. menu()
  49. selection = input("Please Select: ")
  50. if selection == '1':
  51. hdgSolve()
  52. elif selection == '2':
  53. print("In development...")
  54. elif selection == '3':
  55. print("Bye.")
  56. else:
  57. print("Unknown Option Selected!")