Gen. David Petraeus’ fall from grace should not overshadow his achievements both as military strategist and commander.

Petraeus first caught the attention of those following the Middle Eastern crises in 2003, when he commanded a division within the US expeditionary force that liberated Iraq.

The force accomplished its mission of toppling Saddam Hussein in just three weeks. Then started the tougher task of helping Iraqis rebuild their nation on new foundations — a mission that quickly ran into trouble as various armed groups backed by rival regional powers tried to sabotage nation-building efforts through low-intensity warfare.

Appointed commander in Mosul, one of the most dangerous parts of post-Saddam Iraq, Petraeus realized that America’s superior war machine, irresistible in wholesale classical warfare, would not be effective in this different type of conflict. Already a student of the Vietnam War, Petraeus now turned his attention to the conflicts like the one his command now faced — the Algerian wars of 1954-62 and 1993-98.

I was surprised to hear Petraeus, at a late-night meeting in London, discuss some events of the second Algerian war in detail that I had not imagined any American commander to have mastered.

Petraeus developed his military doctrine further when, in 2007, President George W Bush ordered him to lead the “surge” in Iraq. Bush’s resolute leadership was the key reason for the success of the strategy — but credit is also due to Petraeus’ ability to learn and adapt.

In subsequent conversations, I witnessed the maturing of a gifted general capable of rethinking classical tenets of military doctrine.

Petraeus realized that fighting is just a part of war and, at times, not even the most decisive one. In Iraq, and to a lesser extent in Afghanistan where he also served (briefly) as commander, he set out to achieve his objectives with a mixture of hard and soft power.

He learned Arabic, studied the history of Iraqi tribes and the genealogy of the sheiks — and spent endless sessions in negotiations where his diplomatic talents often made the difference.

Some detractors claim that he was little more than a public-relations man, manipulating the media and hoodwinking the politicians in Washington. The truth is that Petraeus understood the facts of life in a modern democracy. From his study of the Vietnam experience, he knew the role that hostile media and political elites, often more focused on the next election than the nation’s long-term interests, could play in undermining any war effort.

Before leaving to lead the surge in Iraq, he was targeted for character assassination by those who’d opposed the toppling of Saddam Hussein. A full-page anti-Petraeus ad in The New York Times labeled him “General Betray-US.”

A year later, however, success on the ground plus openness to the media and careful communication with Washington politicians had redefined his image as a new American hero.

Success in Washington was vital for success in Iraq. The image of a United States divided against itself to the point of trying to politically lynch its own president and its own military commander on the ground had been a key factor in encouraging bitter-enders and Jihadists to go on fighting.

That image also prevented many Iraqis from taking sides. I frequently met Iraqi tribal chiefs, politicians and religious leaders who wondered how they could side with the Americans when many Americans refused to be on their own side.

Petraeus’ patient work with the US media and Congress helped change that image. He succeeded in redefining the United States in Iraqi minds as a strong and united power headed for victory. That, in turn, created a stampede among the Iraqis — away from the jihadists and toward the winning side.

A contentious people at the best of times, Iraqis seldom agree on views of men who shape their history. Petraeus was an exception: Iraqis from all sects, ethnic groups and social backgrounds (including many who were not happy with the American invasion) have a positive view of him.

His acceptance of President Obama’s offer of CIA directorship came as a surprise. It was obvious that this was designed to remove the general from the limelight and kill all thought of a political destiny for him. Now Petraeus’s own misbehavior has ensured that outcome.

Petraeus’ demise as a result of a tawdry tale of marital infidelity shows that, like most great men, he was not flawless.